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; }
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; }
// Receives a SNEP request from the client. // Arguments: data will be set to point to the received NDEF message // request[0] will be set the the SNEP request type and request[1] will be set to acceptable length if SNEP request type is Get // Returns the length of the received NDEF message uint32_t SNEP::receiveRequest(uint8_t *&data){ //Act as server, acts as the SNEP server process present on NFC-enabled devices. uint32_t result = _linkLayer->openLinkToClient(true); if(RESULT_OK(result)){ Serial.println(F("SNEP>receiveData: CONNECTED.")); result = _linkLayer->receiveFromClient(data,true); if(RESULT_OK(result)){ if(data[0] != SNEP_SUPPORTED_VERSION){ Serial.print(F("SNEP>receiveData: Recieved an SNEP message of an unsupported version: ")); Serial.println(data[0]); return SNEP_UNSUPPORTED_VERSION; } //_linkLayer->closeLinkToClient(); //Recieve a DISC pdu, Client tears down the connection. // Manage the request that was sent as argument if(data[1] == SNEP_PUT_REQUEST){ data = &data[6]; //Discard SNEP header Serial.println(F("SNEP>receiveData: Returning a SNEP request")); Serial.print(F("SNEP>receiveData: Length: ")); uint32_t length = data[2]; Serial.println(length, HEX); return data[2]; //returing length of NDEF message } else{ // If a request without any NDEF message is retrieved(Reject, Continue or Get), no data can be returned. data = '\0'; return SNEP_UNEXPECTED_REQUEST; } } } return result; }
uint32_t SNEP::rxNDEFPayload(uint8_t *&data) { uint32_t result = _linkLayer->openSNEPServerLink(); if(RESULT_OK(result)) //if connection is error-free { //Serial.println(F("CONNECTED.")); result = _linkLayer->serverLinkRxData(data); if (RESULT_OK(result)) { // TODO: to check header of SNEP packet uint8_t *ptr = data; uint8_t version = *ptr; uint8_t require = *(ptr + 1); uint32_t length = (ptr[2] << 24) + (ptr[3] << 16) + (ptr[4] << 8) + ptr[5]; data = ptr + 6; // Serial.print("NDEF message length: "); // Serial.println(length); return length; } } return result; }
uint32_t SNEP::pushPayload(uint8_t *NDEFMessage, uint32_t length) { SNEP_MESSAGE *snepMessage = (SNEP_MESSAGE *) ALLOCATE_HEADER_SPACE(NDEFMessage, SNEP_MESSAGE_HDR_LEN); snepMessage->version = 0x10; snepMessage->action = 0x02; snepMessage->length = MODIFY_ENDIAN(length); uint32_t result = _linkLayer->openSNEPClientLink(); if(RESULT_OK(result)) //if connection is error-free { result = _linkLayer->clientLinkTxData((uint8_t *)snepMessage, length + SNEP_MESSAGE_HDR_LEN); } return result; }
// Transmits a SNEP request to the server. A link must have been established before this function is called // Arguments: NDEFMessage is the NDEF message to be sent // length is the length of the NDEF message to be sent // request[0] is the the SNEP request type and request[1] is the acceptable length if SNEP request type is Get // Returns the length of the received NDEF message //why *& as argument? uint32_t SNEP::transmitPutRequest(uint8_t *NDEFMessage, uint32_t length){ uint32_t result; //Opening link to server. result = _linkLayer->openLinkToServer(true); //if data-link was succesfully established continue, else abort. if(RESULT_OK(result)){ //If a data-link was established send the request, else abort. SNEP_PDU *snepRequest; snepRequest = (SNEP_PDU *) ALLOCATE_HEADER_SPACE(NDEFMessage, SNEP_PDU_HEADER_LEN); snepRequest -> version = SNEP_SUPPORTED_VERSION; snepRequest -> type = SNEP_PUT_REQUEST; snepRequest -> length = length; //caller must check the result result = _linkLayer -> transmitToServer((uint8_t *)snepRequest, length + SNEP_PDU_HEADER_LEN, true); } return result; }
// Receives a SNEP response from the server. A link must have been established before this function is called // Arguments: data will be set to point to the received NDEF message // responseType is the response type of the received NDEF message // Returns the length of the received NDEF message uint32_t SNEP::receiveResponse(uint8_t *&data){ uint32_t result = _linkLayer->receiveFromClient(data,true); if(RESULT_OK(result)){ if(data[0] != SNEP_SUPPORTED_VERSION){ Serial.print(F("SNEP>receiveData: Recieved an SNEP message of an unsupported version: ")); Serial.println(data[0]); return SNEP_UNSUPPORTED_VERSION; } if(data[1] == SNEP_SUCCESS){ _linkLayer -> closeLinkToServer(); return 0; //in all other cases there is no information present, return 0. }else //In case of Reject or Continue return SNEP_UNEXPECTED_RESPONSE; } return result; //error code from receiveFromClient }
TEST(RESULT, result) { RESULT_OK(); ASSERT_STREQ(get_result(), ERR_OK) << "Should be equal"; ASSERT_STREQ(get_result_code(), SUCCESS) << "Should be equal"; }