Swadge 2024 2.0.0
APIs to develop games for the Magfest Swadge
Loading...
Searching...
No Matches
p2pConnection.h
Go to the documentation of this file.
1
14
15// clang-format off
16
38
39// clang-format on
40
45
46// clang-format off
47
66
67// clang-format on
68
184
185#ifndef _P2P_CONNECTION_H_
186#define _P2P_CONNECTION_H_
187
188#include <stdint.h>
189#include <stdbool.h>
190
191#include <esp_timer.h>
192#include <esp_now.h>
193
195#define P2P_MAX_DATA_LEN 245
196
204
214
221
222typedef struct _p2pInfo p2pInfo;
223
230typedef void (*p2pConCbFn)(p2pInfo* p2p, connectionEvt_t evt);
231
239typedef void (*p2pMsgRxCbFn)(p2pInfo* p2p, const uint8_t* payload, uint8_t len);
240
250typedef void (*p2pMsgTxCbFn)(p2pInfo* p2p, messageStatus_t status, const uint8_t* data, uint8_t len);
251
260typedef void (*p2pAckSuccessFn)(p2pInfo* p2p, const uint8_t* data, uint8_t len);
261
267typedef void (*p2pAckFailureFn)(p2pInfo* p2p);
268
270#define P2P_START_BYTE 'p'
271
283
287typedef struct
288{
289 uint8_t startByte;
290 uint8_t modeId;
293
297typedef struct
298{
299 uint8_t startByte;
300 uint8_t modeId;
302 uint8_t seqNum;
303 uint8_t macAddr[6];
305
314
318typedef struct _p2pInfo
319{
320 // Messages that every mode uses
321 uint8_t modeId;
322 uint8_t
327 uint8_t dataInAckLen;
328
329 // Callback function pointers
333
335
339 struct
340 {
341 bool isWaitingForAck;
343 uint16_t msgToAckLen;
344 uint32_t timeSentUs;
348
352 struct
353 {
355 uint8_t myMac[6];
356 uint8_t otherMac[6];
357 bool isActive;
358 bool isConnected;
359 bool broadcastReceived;
360 bool rxGameStartMsg;
361 bool rxGameStartAck;
362 bool otherMacReceived;
363 uint8_t mySeqNum;
364 uint8_t lastSeqNum;
366
370 struct
371 {
372 esp_timer_handle_t TxRetry;
373 esp_timer_handle_t TxAllRetries;
374 esp_timer_handle_t Connection;
375 esp_timer_handle_t Reinit;
377} p2pInfo;
378
382typedef struct
383{
384 int8_t rssi;
385 uint8_t mac[6];
386 uint8_t len;
389
390void p2pInitialize(p2pInfo* p2p, uint8_t modeId, p2pConCbFn conCbFn, p2pMsgRxCbFn msgRxCbFn, int8_t connectionRssi);
391void p2pSetAsymmetric(p2pInfo* p2p, uint8_t incomingModeId);
392void p2pRestart(p2pInfo* p2p);
393void p2pDeinit(p2pInfo* p2p);
394
395void p2pStartConnection(p2pInfo* p2p);
396
397void p2pSendMsg(p2pInfo* p2p, const uint8_t* payload, uint16_t len, p2pMsgTxCbFn msgTxCbFn);
398void p2pSendCb(p2pInfo* p2p, const uint8_t* mac_addr, esp_now_send_status_t status);
399void p2pRecvCb(p2pInfo* p2p, const uint8_t* mac_addr, const uint8_t* data, uint8_t len, int8_t rssi);
400void p2pSetDataInAck(p2pInfo* p2p, const uint8_t* ackData, uint8_t ackDataLen);
401void p2pClearDataInAck(p2pInfo* p2p);
402bool p2pIsTxIdle(p2pInfo* p2p);
403
405void p2pSetPlayOrder(p2pInfo* p2p, playOrder_t order);
406
407#endif
uint8_t macAddr[6]
The MAC address destination for this packet.
Definition p2pConnection.h:303
playOrder_t playOrder
Either GOING_FIRST or GOING_SECOND depending on how the handshake went.
void(* p2pMsgTxCbFn)(p2pInfo *p2p, messageStatus_t status, const uint8_t *data, uint8_t len)
This typedef is for the function callback which delivers acknowledge status for transmitted messages ...
Definition p2pConnection.h:250
uint8_t dataInAckLen
The length of any extra data which was appended to the ACK, see p2pSetDataInAck()
Definition p2pConnection.h:327
uint16_t msgToAckLen
The length of the message which is waiting for an ACK.
bool isWaitingForAck
true if waiting for an ACK after transmitting a message
struct _p2pInfo::@120151375375050042220360350053347013150050206263 cnc
Connection state variables.
struct _p2pInfo p2pInfo
All the state variables required for a P2P session with another Swadge.
Definition p2pConnection.h:222
bool p2pIsTxIdle(p2pInfo *p2p)
Return if a transmission is idle, or in progress.
Definition p2pConnection.c:919
playOrder_t
After connecting, one Swadge will be GOING_FIRST and one will be GOING_SECOND.
Definition p2pConnection.h:199
@ GOING_SECOND
This Swadge goes second (player two)
Definition p2pConnection.h:201
@ NOT_SET
Swadges haven't connected yet.
Definition p2pConnection.h:200
@ GOING_FIRST
This swadge goes first (player one)
Definition p2pConnection.h:202
p2pAckSuccessFn SuccessFn
A callback function to be called if the message is ACKed.
esp_timer_handle_t TxRetry
A timer used to retry a transmission multiple times if not acknowledged.
connectionEvt_t
These are the states a Swadge will go through when connecting to another.
Definition p2pConnection.h:207
@ RX_GAME_START_MSG
Another Swadge's start message has been received.
Definition p2pConnection.h:210
@ CON_STARTED
Connection has started.
Definition p2pConnection.h:208
@ CON_ESTABLISHED
Connection has been established.
Definition p2pConnection.h:211
@ RX_GAME_START_ACK
This Swadge's start message has been ACKed.
Definition p2pConnection.h:209
@ CON_LOST
Connection was lost.
Definition p2pConnection.h:212
int8_t connectionRssi
The minimum RSSI required to begin a connection.
Definition p2pConnection.h:334
void p2pSetDataInAck(p2pInfo *p2p, const uint8_t *ackData, uint8_t ackDataLen)
Set data to be automatically used as the payload all future ACKs, until the data is cleared.
Definition p2pConnection.c:629
struct _p2pInfo::@374030303067155177257311056073000364022071317167 tmr
The timers used for connection and ACKing.
uint8_t mac[6]
The MAC address of the sender.
Definition p2pConnection.h:385
uint8_t data[P2P_MAX_DATA_LEN]
The data bytes sent or received.
Definition p2pConnection.h:312
bool isActive
true if the connection process has started
p2pMsgType_t messageType
Message type byte.
Definition p2pConnection.h:291
int8_t rssi
The received signal strength indicator for the packet.
Definition p2pConnection.h:384
p2pConMsg_t conMsg
The connection message to transmit.
Definition p2pConnection.h:324
void(* p2pMsgRxCbFn)(p2pInfo *p2p, const uint8_t *payload, uint8_t len)
This typedef is for the function callback which delivers received p2p packets to the Swadge mode.
Definition p2pConnection.h:239
p2pCommonHeader_t startMsg
The start message to transmit.
Definition p2pConnection.h:325
uint8_t len
The length of the received bytes.
Definition p2pConnection.h:386
struct _p2pInfo::@301102327134161144225013350127366352071333261010 ack
Variables used for acknowledging and retrying messages.
void p2pRestart(p2pInfo *p2p)
Restart by deinitializing then initializing. Persist the msgId and p2p->conCbFn fields.
Definition p2pConnection.c:796
uint32_t timeSentUs
The time the message is waiting for an ACK was transmitted.
void(* p2pAckSuccessFn)(p2pInfo *p2p, const uint8_t *data, uint8_t len)
This typedef is for a function callback called when a message is acknowledged. It make also contain a...
Definition p2pConnection.h:260
uint8_t otherMac[6]
The other Swadge's MAC address.
uint8_t mySeqNum
The current sequence number used for transmissions.
p2pMsgType_t
The five different types of p2p messages.
Definition p2pConnection.h:276
@ P2P_MSG_DATA
A data message.
Definition p2pConnection.h:281
@ P2P_MSG_DATA_ACK
An acknowledge message with extra data.
Definition p2pConnection.h:280
@ P2P_MSG_START
The start message, used during connection.
Definition p2pConnection.h:278
@ P2P_MSG_ACK
An acknowledge message.
Definition p2pConnection.h:279
@ P2P_MSG_CONNECT
The connection broadcast.
Definition p2pConnection.h:277
p2pConCbFn conCbFn
A callback function called during the connection process.
Definition p2pConnection.h:330
p2pDataMsg_t data
The received bytes.
Definition p2pConnection.h:387
bool rxGameStartAck
True if this Swadge's game start message was acknowledged.
bool isConnected
true if connected to another Swadge
void p2pSendCb(p2pInfo *p2p, const uint8_t *mac_addr, esp_now_send_status_t status)
This must be called by whatever function is registered to the Swadge mode's fnEspNowSendCb.
Definition p2pConnection.c:833
void p2pInitialize(p2pInfo *p2p, uint8_t modeId, p2pConCbFn conCbFn, p2pMsgRxCbFn msgRxCbFn, int8_t connectionRssi)
Initialize the p2p connection protocol.
Definition p2pConnection.c:71
void p2pSendMsg(p2pInfo *p2p, const uint8_t *payload, uint16_t len, p2pMsgTxCbFn msgTxCbFn)
Send a message from one Swadge to another. This must not be called before the CON_ESTABLISHED event o...
Definition p2pConnection.c:302
uint8_t incomingModeId
A mode ID to listen for which is different than initialized mode ID. See p2pSetAsymmetric()
Definition p2pConnection.h:323
p2pMsgTxCbFn msgTxCbFn
A callback function called when transmitting a message.
Definition p2pConnection.h:332
p2pDataMsg_t ackMsg
The acknowledge message to transmit.
Definition p2pConnection.h:326
#define P2P_MAX_DATA_LEN
The maximum payload of a p2p packet is 245 bytes.
Definition p2pConnection.h:195
uint8_t myMac[6]
This Swadge's MAC address.
uint8_t seqNum
A sequence number for this packet.
Definition p2pConnection.h:302
p2pAckFailureFn FailureFn
A callback function to be called if the message is not ACKed.
void(* p2pAckFailureFn)(p2pInfo *p2p)
This typedef is for a function callback called when a message is not acknowledged.
Definition p2pConnection.h:267
uint8_t startByte
Start byte, must be P2P_START_BYTE.
Definition p2pConnection.h:289
void(* p2pConCbFn)(p2pInfo *p2p, connectionEvt_t evt)
This typedef is for the function callback which delivers connection statuses to the Swadge mode.
Definition p2pConnection.h:230
uint8_t lastSeqNum
The last sequence number used for transmissions.
void p2pClearDataInAck(p2pInfo *p2p)
Clear any data which was set to be used as the payload in the next ACK.
Definition p2pConnection.c:649
bool rxGameStartMsg
true if the other Swadge's game start message was received
void p2pSetPlayOrder(p2pInfo *p2p, playOrder_t order)
Override whether the Swadge is player 1 or player 2. You probably shouldn't do this,...
Definition p2pConnection.c:907
esp_timer_handle_t Connection
A timer used to cancel a connection if the handshake fails.
bool otherMacReceived
true if the other Swadge's MAC address has been received
messageStatus_t
Message statuses after transmission.
Definition p2pConnection.h:217
@ MSG_FAILED
The message transmission failed.
Definition p2pConnection.h:219
@ MSG_ACKED
The message was acknowledged after transmission.
Definition p2pConnection.h:218
void p2pDeinit(p2pInfo *p2p)
Stop up all timers.
Definition p2pConnection.c:194
esp_timer_handle_t Reinit
A timer used to restart P2P after any complete failures.
p2pCommonHeader_t hdr
The common header bytes for a P2P packet.
Definition p2pConnection.h:311
playOrder_t p2pGetPlayOrder(p2pInfo *p2p)
After the swadge is connected to another, return whether this Swadge is player 1 or player 2....
Definition p2pConnection.c:888
p2pMsgRxCbFn msgRxCbFn
A callback function called when receiving a message.
Definition p2pConnection.h:331
void p2pRecvCb(p2pInfo *p2p, const uint8_t *mac_addr, const uint8_t *data, uint8_t len, int8_t rssi)
This function must be called whenever an ESP NOW packet is received.
Definition p2pConnection.c:439
void p2pStartConnection(p2pInfo *p2p)
Start the connection process by sending broadcasts and notify the mode.
Definition p2pConnection.c:174
void p2pSetAsymmetric(p2pInfo *p2p, uint8_t incomingModeId)
Set the p2p connection protocol to listen for a different mode ID from its own.
Definition p2pConnection.c:164
p2pDataMsg_t msgToAck
A transmitted message which is waiting for an ACK.
esp_timer_handle_t TxAllRetries
A timer used to cancel a transmission if all attempts failed.
bool broadcastReceived
true if a broadcast was received to start the connection handshake
uint8_t modeId
Mode byte, must be unique per-mode.
Definition p2pConnection.h:290
All the state variables required for a P2P session with another Swadge.
Definition p2pConnection.h:319
The byte format for a common header for all P2P packets.
Definition p2pConnection.h:298
The byte format for a connection message for a P2P session.
Definition p2pConnection.h:288
The byte format for a P2P data packet.
Definition p2pConnection.h:310
All the information for a packet to store between the receive callback and the task it's actually pro...
Definition p2pConnection.h:383