189#ifndef _P2P_CONNECTION_H_
190#define _P2P_CONNECTION_H_
195#include <esp_timer.h>
198#define P2P_MAX_DATA_LEN 245
273#define P2P_START_BYTE 'p'
278typedef enum __attribute__((packed))
344 bool isWaitingForAck;
346 uint16_t msgToAckLen;
362 bool broadcastReceived;
365 bool otherMacReceived;
375 esp_timer_handle_t TxRetry;
376 esp_timer_handle_t TxAllRetries;
377 esp_timer_handle_t Connection;
378 esp_timer_handle_t Reinit;
400void p2pSendCb(
p2pInfo* p2p,
const uint8_t* mac_addr, esp_now_send_status_t status);
401void p2pRecvCb(
p2pInfo* p2p,
const uint8_t* mac_addr,
const uint8_t* data, uint8_t len, int8_t rssi);
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:253
uint8_t dataInAckLen
The length of any extra data which was appended to the ACK, see p2pSetDataInAck()
Definition p2pConnection.h:330
struct _p2pInfo p2pInfo
All the state variables required for a P2P session with another Swadge.
Definition p2pConnection.h:225
struct _p2pInfo::@24 cnc
Connection state variables.
playOrder_t
After connecting, one Swadge will be GOING_FIRST and one will be GOING_SECOND.
Definition p2pConnection.h:202
@ GOING_SECOND
This Swadge goes second (player two)
Definition p2pConnection.h:204
@ NOT_SET
Swadges haven't connected yet.
Definition p2pConnection.h:203
@ GOING_FIRST
This swadge goes first (player one)
Definition p2pConnection.h:205
connectionEvt_t
These are the states a Swadge will go through when connecting to another.
Definition p2pConnection.h:210
@ RX_GAME_START_MSG
Another Swadge's start message has been received.
Definition p2pConnection.h:213
@ CON_STARTED
Connection has started.
Definition p2pConnection.h:211
@ CON_ESTABLISHED
Connection has been established.
Definition p2pConnection.h:214
@ RX_GAME_START_ACK
This Swadge's start message has been ACKed.
Definition p2pConnection.h:212
@ CON_LOST
Connection was lost.
Definition p2pConnection.h:215
int8_t connectionRssi
The minimum RSSI required to begin a connection.
Definition p2pConnection.h:337
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:622
p2pMsgType_t messageType
Message type byte.
Definition p2pConnection.h:294
int8_t rssi
The received signal strength indicator for the packet.
Definition p2pConnection.h:387
p2pConMsg_t conMsg
The connection message to transmit.
Definition p2pConnection.h:327
struct _p2pInfo::@23 ack
Variables used for acknowledging and retrying messages.
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:242
p2pCommonHeader_t startMsg
The start message to transmit.
Definition p2pConnection.h:328
uint8_t len
The length of the received bytes.
Definition p2pConnection.h:389
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:263
p2pMsgType_t
The five different types of p2p messages.
Definition p2pConnection.h:279
@ P2P_MSG_DATA
A data message.
Definition p2pConnection.h:284
@ P2P_MSG_DATA_ACK
An acknowledge message with extra data.
Definition p2pConnection.h:283
@ P2P_MSG_START
The start message, used during connection.
Definition p2pConnection.h:281
@ P2P_MSG_ACK
An acknowledge message.
Definition p2pConnection.h:282
@ P2P_MSG_CONNECT
The connection broadcast.
Definition p2pConnection.h:280
p2pConCbFn conCbFn
A callback function called during the connection process.
Definition p2pConnection.h:333
p2pDataMsg_t data
The received bytes.
Definition p2pConnection.h:390
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:821
void p2pInitialize(p2pInfo *p2p, uint8_t modeId, p2pConCbFn conCbFn, p2pMsgRxCbFn msgRxCbFn, int8_t connectionRssi)
Initialize the p2p connection protocol.
Definition p2pConnection.c:72
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:295
uint8_t incomingModeId
A mode ID to listen for which is different than initialized mode ID. See p2pSetAsymmetric()
Definition p2pConnection.h:326
p2pMsgTxCbFn msgTxCbFn
A callback function called when transmitting a message.
Definition p2pConnection.h:335
p2pDataMsg_t ackMsg
The acknowledge message to transmit.
Definition p2pConnection.h:329
#define P2P_MAX_DATA_LEN
The maximum payload of a p2p packet is 245 bytes.
Definition p2pConnection.h:198
uint8_t seqNum
A sequence number for this packet.
Definition p2pConnection.h:305
void(* p2pAckFailureFn)(p2pInfo *p2p)
This typedef is for a function callback called when a message is not acknowledged.
Definition p2pConnection.h:270
uint8_t startByte
Start byte, must be P2P_START_BYTE.
Definition p2pConnection.h:292
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:233
void p2pClearDataInAck(p2pInfo *p2p)
Clear any data which was set to be used as the payload in the next ACK.
Definition p2pConnection.c:642
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:895
messageStatus_t
Message statuses after transmission.
Definition p2pConnection.h:220
@ MSG_FAILED
The message transmission failed.
Definition p2pConnection.h:222
@ MSG_ACKED
The message was acknowledged after transmission.
Definition p2pConnection.h:221
void p2pDeinit(p2pInfo *p2p)
Stop up all timers.
Definition p2pConnection.c:195
p2pCommonHeader_t hdr
The common header bytes for a P2P packet.
Definition p2pConnection.h:314
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:876
p2pMsgRxCbFn msgRxCbFn
A callback function called when receiving a message.
Definition p2pConnection.h:334
struct _p2pInfo::@25 tmr
The timers used for connection and ACKing.
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:432
void p2pStartConnection(p2pInfo *p2p)
Start the connection process by sending broadcasts and notify the mode.
Definition p2pConnection.c:175
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:165
uint8_t modeId
Mode byte, must be unique per-mode.
Definition p2pConnection.h:293
All the state variables required for a P2P session with another Swadge.
Definition p2pConnection.h:322
The byte format for a connection message for a P2P session.
Definition p2pConnection.h:291
The byte format for a P2P data packet.
Definition p2pConnection.h:313
All the information for a packet to store between the receive callback and the task it's actually pro...
Definition p2pConnection.h:386