Skip to content

Commit f7e0506

Browse files
committed
Adding a layer 2 protocol
1 parent 35d68c3 commit f7e0506

File tree

3 files changed

+72
-36
lines changed

3 files changed

+72
-36
lines changed

MeshBase.cpp

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void MeshBase::Update()
3737
// Periodic sends
3838
if (millis() - last_broadcast_time > PEER_DISCOVERY_TIME)
3939
{
40-
if (!IsReady())
40+
if (!IsReady())
4141
ChooseAddress();
4242
SendPeerDiscovery();
4343
}
@@ -51,11 +51,18 @@ void MeshBase::Update()
5151
uint8_t len = radio.getDynamicPayloadSize();
5252
uint8_t buff[40];
5353
done = radio.read(buff, min(len, sizeof(buff)));
54-
if (pipe_num == 0)
55-
{
56-
HandleMessage(0, buff, len);
57-
} else if (pipe_num == 1) {
58-
HandlePeerDiscovery(buff, len);
54+
55+
const MeshBase::Message* msg = (struct MeshBase::Message*)buff;
56+
uint8_t payload_length = len - sizeof(Message);
57+
const uint8_t* payload = buff + sizeof(Message);
58+
59+
switch(msg->type) {
60+
case type_peer_discovery:
61+
HandlePeerDiscovery(msg, payload, payload_length);
62+
break;
63+
default:
64+
OnMessage(msg, payload, payload_length);
65+
break;
5966
}
6067
} while (!done);
6168
}
@@ -80,22 +87,21 @@ void MeshBase::Update()
8087
}
8188
}
8289

83-
void MeshBase::HandlePeerDiscovery(void* buff, uint8_t length)
90+
void MeshBase::HandlePeerDiscovery(const MeshBase::Message* msg, const void* buff, uint8_t length)
8491
{
8592
if (length != sizeof(PeerDiscoveryMessage))
8693
return;
87-
PeerDiscoveryMessage from = *(PeerDiscoveryMessage*)buff;
88-
// Dont know why, but this keeps happening?
89-
/*if (from == 0)
90-
return;*/
94+
const PeerDiscoveryMessage* pd = (struct PeerDiscoveryMessage*)buff;
95+
//if (msg.protocol_version != 1)
96+
// return;
9197

92-
Peer* peer = GetPeer(from.address);
98+
Peer* peer = GetPeer(msg->address_from);
9399
if (peer == NULL)
94100
{
95101
// Found a new peer
96102
Serial.print("New Peer: ");
97-
Serial.println(from.address, DEC);
98-
Peer* p = new Peer(from.address);
103+
Serial.println(msg->address_from, DEC);
104+
Peer* p = new Peer(msg->address_from);
99105
peers.Add(p);
100106
OnNewPeer(p);
101107
} else {
@@ -107,27 +113,37 @@ void MeshBase::HandlePeerDiscovery(void* buff, uint8_t length)
107113
void MeshBase::SendPeerDiscovery()
108114
{
109115
last_broadcast_time = millis();
110-
MeshBase::PeerDiscoveryMessage msg;
111-
msg.version = 1;
112-
msg.address = address;
113-
msg.num_peers = peers.length;
114-
SendBroadcastMessage(PEER_DISCOVERY, &msg, sizeof(MeshBase::PeerDiscoveryMessage));
116+
MeshBase::PeerDiscoveryMessage payload;
117+
payload.protocol_version = 1;
118+
payload.network_capabilities = 0;
119+
payload.application_capabilities = 0;
120+
payload.num_peers = peers.length;
121+
payload.uptime = millis() / 1000;
122+
SendMessage(PEER_DISCOVERY, type_peer_discovery, &payload, sizeof(MeshBase::PeerDiscoveryMessage), true);
115123
}
116124

117-
void MeshBase::SendBroadcastMessage(uint32_t to, const void* data, uint8_t length)
125+
void MeshBase::SendMessage(uint32_t to, uint8_t type, const void* data, uint8_t length, bool is_broadcast)
118126
{
127+
uint8_t buff[32];
128+
Message* msg = (struct Message*)buff;
129+
msg->protocol_version = 1;
130+
msg->ttl = 0;
131+
msg->type = type;
132+
msg->address_from = address;
133+
msg->split_enabled = 0;
134+
msg->split_part = 0;
119135
radio.stopListening();
120-
radio.openWritingPipe(TO_BROADCAST(to));
136+
if (is_broadcast)
137+
radio.openWritingPipe(TO_BROADCAST(to));
138+
else
139+
radio.openWritingPipe(TO_ADDRESS(to));
121140
radio.write(data, length);
122141
radio.startListening();
123142
}
124143

125-
void MeshBase::SendMessage(uint32_t to, const void* data, uint8_t length)
144+
void MeshBase::SendMessage(uint32_t to, uint8_t type, const void* data, uint8_t length)
126145
{
127-
radio.stopListening();
128-
radio.openWritingPipe(TO_ADDRESS(to));
129-
radio.write(data, length);
130-
radio.startListening();
146+
SendMessage(to, type_user, data, length, false);
131147
}
132148

133149
void MeshBase::ChooseAddress()

MeshBase.h

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,30 @@ class MeshBase
1616
Peer(uint32_t address) : address(address), time(0) {}
1717
};
1818

19+
struct Message
20+
{
21+
uint8_t protocol_version;
22+
uint8_t ttl;
23+
uint8_t type;
24+
bool split_enabled : 1;
25+
uint8_t split_part : 7;
26+
uint32_t address_from;
27+
};
28+
29+
// -- Message types --
30+
enum message_type {
31+
type_peer_discovery,
32+
type_peer_list,
33+
type_user,
34+
};
35+
1936
void Begin();
2037
void Update();
21-
void SendMessage(uint32_t address, const void* data, uint8_t length);
38+
void SendMessage(uint32_t address, uint8_t type, const void* data, uint8_t length);
2239
uint32_t GetAddress() const { return address; }
2340
bool IsReady() const { return address != 0; }
2441
protected:
25-
virtual void HandleMessage(uint32_t sender, const void* data, uint8_t length) = 0;
42+
virtual void OnMessage(const MeshBase::Message* meta, const void* data, uint8_t length) = 0;
2643
virtual void OnNewPeer(Peer*) {}
2744
virtual void OnLostPeer(Peer*) {}
2845
private:
@@ -32,8 +49,9 @@ class MeshBase
3249
unsigned long last_peer_check_time;
3350

3451
void SendPeerDiscovery();
35-
void SendBroadcastMessage(uint32_t address, const void* data, uint8_t length);
36-
void HandlePeerDiscovery(void* buff, uint8_t length);
52+
void SendMessage(uint32_t address, uint8_t type, const void* data, uint8_t length, bool is_broadcast);
53+
void HandlePeerDiscovery(const Message* msg, const void* buff, uint8_t length);
54+
void HandleMessage(const Message* msg, const void* data, uint8_t length);
3755
void ChooseAddress();
3856

3957
LinkedList<Peer> peers;
@@ -42,9 +60,11 @@ class MeshBase
4260

4361
struct PeerDiscoveryMessage
4462
{
45-
uint8_t version;
46-
uint32_t address;
47-
uint16_t num_peers;
63+
uint8_t protocol_version;
64+
uint8_t network_capabilities; // What routing/networking can I do for the network
65+
uint8_t application_capabilities; // What type of data do I expose
66+
uint16_t num_peers; // Number of direct peers
67+
uint32_t uptime; // Seconds since boot
4868
};
4969

5070
};

RF_test.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ class App : public MeshBase
77
public:
88
App() : MeshBase(9, 10) {}
99
protected:
10-
virtual void HandleMessage(uint32_t sender, const void* data, uint8_t length)
10+
virtual void OnMessage(const MeshBase::Message* meta, const void* data, uint8_t length)
1111
{
12-
Serial.print(sender, DEC);
12+
Serial.print(meta->address_from, DEC);
1313
Serial.print(" : ");
1414
Serial.println((const char*)data);
1515
}
@@ -20,7 +20,7 @@ protected:
2020
int len = snprintf(buff, 255, "Hello %u from %u", p->address, GetAddress());
2121
Serial.print("Sending message: ");
2222
Serial.println(buff);
23-
SendMessage(p->address, buff, len + 1);
23+
SendMessage(p->address, type_user, buff, len + 1);
2424
}
2525
};
2626

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy