Swadge 2024 2.0.0
APIs to develop games for the Magfest Swadge
Loading...
Searching...
No Matches
p2pConnection.c File Reference

Macros

#define RETRY_TIME_US   3000000
 
#define FAILURE_RESTART_US   8000000
 
#define P2P_LOG(...)
 

Functions

void p2pInitialize (p2pInfo *p2p, uint8_t modeId, p2pConCbFn conCbFn, p2pMsgRxCbFn msgRxCbFn, int8_t connectionRssi)
 Initialize the p2p connection protocol.
 
void p2pSetAsymmetric (p2pInfo *p2p, uint8_t incomingModeId)
 Set the p2p connection protocol to listen for a different mode ID from its own.
 
void p2pStartConnection (p2pInfo *p2p)
 Start the connection process by sending broadcasts and notify the mode.
 
void p2pDeinit (p2pInfo *p2p)
 Stop up all timers.
 
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 occurs. Message addressing, ACKing, and retries all happen automatically.
 
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.
 
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.
 
void p2pClearDataInAck (p2pInfo *p2p)
 Clear any data which was set to be used as the payload in the next ACK.
 
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.
 
playOrder_t p2pGetPlayOrder (p2pInfo *p2p)
 After the swadge is connected to another, return whether this Swadge is player 1 or player 2. This can be used to determine client/server roles.
 
void p2pSetPlayOrder (p2pInfo *p2p, playOrder_t order)
 Override whether the Swadge is player 1 or player 2. You probably shouldn't do this, but you might want to for single player modes.
 

Macro Definition Documentation

◆ RETRY_TIME_US

#define RETRY_TIME_US   3000000

◆ FAILURE_RESTART_US

#define FAILURE_RESTART_US   8000000

◆ P2P_LOG

#define P2P_LOG ( ...)

Function Documentation

◆ p2pInitialize()

void p2pInitialize ( p2pInfo * p2p,
uint8_t modeId,
p2pConCbFn conCbFn,
p2pMsgRxCbFn msgRxCbFn,
int8_t connectionRssi )

Initialize the p2p connection protocol.

Parameters
p2pThe p2pInfo struct with all the state information
modeId8 bit mode ID. Must be unique per-swadge mode.
conCbFnA function pointer which will be called when connection events occur
msgRxCbFnA function pointer which will be called when a packet is received for the swadge mode
connectionRssiThe strength needed to start a connection with another swadge. A positive value means swadges are quite close. I've seen RSSI go as low as -70. 0 is practically touching

◆ p2pSetAsymmetric()

void p2pSetAsymmetric ( p2pInfo * p2p,
uint8_t incomingModeId )

Set the p2p connection protocol to listen for a different mode ID from its own.

Parameters
p2pThe p2pInfo struct with all the state information
incomingModeIdThe unique mode ID used by the other end of this connection

◆ p2pStartConnection()

void p2pStartConnection ( p2pInfo * p2p)

Start the connection process by sending broadcasts and notify the mode.

Parameters
p2pThe p2pInfo struct with all the state information

◆ p2pDeinit()

void p2pDeinit ( p2pInfo * p2p)

Stop up all timers.

Parameters
p2pThe p2pInfo struct with all the state information

◆ p2pSendMsg()

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 occurs. Message addressing, ACKing, and retries all happen automatically.

Parameters
p2pThe p2pInfo struct with all the state information
payloadA byte array to be copied to the payload for this message
lenThe length of the byte array
msgTxCbFnA callback function when this message is ACKed or dropped

◆ p2pRecvCb()

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.

Parameters
p2pThe p2pInfo struct with all the state information
mac_addrThe MAC of the swadge that sent the data
dataThe data
lenThe length of the data
rssiThe rssi of the received data

◆ p2pSetDataInAck()

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.

This data will not be transmitted until the next ACK is sent, whenever that is. Normally an ACK does not contain any payload data, but an application can have a more efficient continuous request and response flow by embedding the response in the ACK.

Parameters
p2pThe p2pInfo struct with all the state information
ackDataThe data to be included in the next ACK, will be copied here
ackDataLenThe length of the data to be included in the next ACK

◆ p2pClearDataInAck()

void p2pClearDataInAck ( p2pInfo * p2p)

Clear any data which was set to be used as the payload in the next ACK.

Parameters
p2pThe p2pInfo struct with all the state information

◆ p2pSendCb()

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.

This is called after an attempted transmission. If it was successful, and the message should be acked, start a retry timer. If it wasn't successful, just try again

Parameters
p2pThe p2pInfo struct with all the state information
mac_addrunused
statusWhether the transmission succeeded or failed

◆ p2pGetPlayOrder()

playOrder_t p2pGetPlayOrder ( p2pInfo * p2p)

After the swadge is connected to another, return whether this Swadge is player 1 or player 2. This can be used to determine client/server roles.

Parameters
p2pThe p2pInfo struct with all the state information
Returns
GOING_SECOND, GOING_FIRST, or NOT_SET

◆ p2pSetPlayOrder()

void p2pSetPlayOrder ( p2pInfo * p2p,
playOrder_t order )

Override whether the Swadge is player 1 or player 2. You probably shouldn't do this, but you might want to for single player modes.

Parameters
p2pThe p2pInfo struct with all the state information
orderThe order to set