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
15// clang-format off
16
39// clang-format on
40
46// clang-format off
47
67// clang-format on
68
189#ifndef _P2P_CONNECTION_H_
190#define _P2P_CONNECTION_H_
191
192#include <stdint.h>
193#include <stdbool.h>
194
195#include <esp_timer.h>
196
198#define P2P_MAX_DATA_LEN 245
199
207
217
224
225typedef struct _p2pInfo p2pInfo;
226
233typedef void (*p2pConCbFn)(p2pInfo* p2p, connectionEvt_t evt);
234
242typedef void (*p2pMsgRxCbFn)(p2pInfo* p2p, const uint8_t* payload, uint8_t len);
243
253typedef void (*p2pMsgTxCbFn)(p2pInfo* p2p, messageStatus_t status, const uint8_t* data, uint8_t len);
254
263typedef void (*p2pAckSuccessFn)(p2pInfo* p2p, const uint8_t* data, uint8_t len);
264
270typedef void (*p2pAckFailureFn)(p2pInfo* p2p);
271
273#define P2P_START_BYTE 'p'
274
286
290typedef struct
291{
292 uint8_t startByte;
293 uint8_t modeId;
296
300typedef struct
301{
302 uint8_t startByte;
303 uint8_t modeId;
305 uint8_t seqNum;
306 uint8_t macAddr[6];
308
312typedef struct
313{
315 uint8_t data[P2P_MAX_DATA_LEN];
317
321typedef struct _p2pInfo
322{
323 // Messages that every mode uses
324 uint8_t modeId;
325 uint8_t
330 uint8_t dataInAckLen;
331
332 // Callback function pointers
336
338
342 struct
343 {
344 bool isWaitingForAck;
345 p2pDataMsg_t msgToAck;
346 uint16_t msgToAckLen;
347 uint32_t timeSentUs;
348 p2pAckSuccessFn SuccessFn;
349 p2pAckFailureFn FailureFn;
351
355 struct
356 {
357 playOrder_t playOrder;
358 uint8_t myMac[6];
359 uint8_t otherMac[6];
360 bool isActive;
361 bool isConnected;
362 bool broadcastReceived;
363 bool rxGameStartMsg;
364 bool rxGameStartAck;
365 bool otherMacReceived;
366 uint8_t mySeqNum;
367 uint8_t lastSeqNum;
369
373 struct
374 {
375 esp_timer_handle_t TxRetry;
376 esp_timer_handle_t TxAllRetries;
377 esp_timer_handle_t Connection;
378 esp_timer_handle_t Reinit;
380} p2pInfo;
381
385typedef struct
386{
387 int8_t rssi;
388 uint8_t mac[6];
389 uint8_t len;
392
393void p2pInitialize(p2pInfo* p2p, uint8_t modeId, p2pConCbFn conCbFn, p2pMsgRxCbFn msgRxCbFn, int8_t connectionRssi);
394void p2pSetAsymmetric(p2pInfo* p2p, uint8_t incomingModeId);
395void p2pDeinit(p2pInfo* p2p);
396
397void p2pStartConnection(p2pInfo* p2p);
398
399void p2pSendMsg(p2pInfo* p2p, const uint8_t* payload, uint16_t len, p2pMsgTxCbFn msgTxCbFn);
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);
402void p2pSetDataInAck(p2pInfo* p2p, const uint8_t* ackData, uint8_t ackDataLen);
403void p2pClearDataInAck(p2pInfo* p2p);
404
406void p2pSetPlayOrder(p2pInfo* p2p, playOrder_t order);
407
408#endif
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
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
struct _p2pInfo::@22 cnc
Connection state variables.
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
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
struct _p2pInfo::@23 tmr
The timers used for connection and ACKing.
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
struct _p2pInfo::@21 ack
Variables used for acknowledging and retrying messages.
#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
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 common header for all P2P packets.
Definition p2pConnection.h:301
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