-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathNDEFPushProtocol.cpp
More file actions
80 lines (67 loc) · 2.57 KB
/
NDEFPushProtocol.cpp
File metadata and controls
80 lines (67 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "NDEFPushProtocol.h"
NDEFPushProtocol::NDEFPushProtocol(NFCLinkLayer *linkLayer) :
_linkLayer(linkLayer)
{
}
NDEFPushProtocol::~NDEFPushProtocol()
{
}
uint32_t NDEFPushProtocol::rxNDEFPayload(uint8_t *&data)
{
uint32_t result = _linkLayer->openNPPServerLink();
if(RESULT_OK(result)) //if connection is error-free
{
//Serial.println(F("CONNECTED."));
result = _linkLayer->serverLinkRxData(data);
if (RESULT_OK(result))
{
NPP_MESSAGE *nppMessage = (NPP_MESSAGE *)data;
nppMessage->numNDEFEntries = MODIFY_ENDIAN(nppMessage->numNDEFEntries);
nppMessage->NDEFLength = MODIFY_ENDIAN(nppMessage->NDEFLength);
if (nppMessage->version != NPP_SUPPORTED_VERSION)
{
Serial.println(F("Recieved an NPP message of an unsupported version."));
return NPP_UNSUPPORTED_VERSION;
}
else if (nppMessage->numNDEFEntries != 1)
{
Serial.println(F("Invalid number of NDEF entries"));
return NPP_INVALID_NUM_ENTRIES;
}
else if (nppMessage->actionCode != NPP_ACTION_CODE)
{
Serial.println(F("Invalid Action Code"));
return NPP_INVALID_ACTION_CODE;
}
_linkLayer->closeNPPServerLink();
//Serial.println(F("Returning NPP Message"));
//Serial.print(F("Length: "));
//Serial.println(nppMessage->NDEFLength, HEX);
data = nppMessage->NDEFMessage;
return nppMessage->NDEFLength;
}
}
return result;
}
uint32_t NDEFPushProtocol::pushPayload(uint8_t *NDEFMessage, uint32_t length)
{
NPP_MESSAGE *nppMessage = (NPP_MESSAGE *) ALLOCATE_HEADER_SPACE(NDEFMessage, NPP_MESSAGE_HDR_LEN);
nppMessage->version = NPP_SUPPORTED_VERSION;
nppMessage->numNDEFEntries = MODIFY_ENDIAN((uint32_t)0x00000001);
nppMessage->actionCode = NPP_ACTION_CODE;
nppMessage->NDEFLength = MODIFY_ENDIAN(length);
/*uint8_t *buf = (uint8_t *) nppMessage;
Serial.println(F("NPP + NDEF Message"));
for (uint16_t i = 0; i < length + NPP_MESSAGE_HDR_LEN; ++i)
{
Serial.print(F("0x"));
Serial.print(buf[i], HEX);
Serial.print(F(" "));
}*/
uint32_t result = _linkLayer->openNPPClientLink();
if(RESULT_OK(result)) //if connection is error-free
{
result = _linkLayer->clientLinkTxData((uint8_t *)nppMessage, length + NPP_MESSAGE_HDR_LEN);
}
return result;
}