Utility library to read and write Ndef messages from/to a Type4 NFC tag
Dependents: NFC M2M_2016_STM32 MyongjiElec_capstone1 IDW01M1_Cloud_IBM ... more
Fork of NDefLib by
NDEF NFC library
This library provides an abstract API to create NDEF formatted messages and records and to read/write them from/to a Type4 NFC Tag.
Implementations
At the moment, the NDEF API is implemented by X_NUCLEO_NFC01A1 and X_NUCLEO_NFC02A1 Dynamic NFC Tag libraries respectively driving the X-NUCLEO-NFC01A1 and X-NUCLEO-NFC02A1 boards.
Diff: RecordType/RecordWifiConf.cpp
- Revision:
- 20:31f727872290
- Parent:
- 19:13d84b136a62
--- a/RecordType/RecordWifiConf.cpp Fri Apr 28 12:13:51 2017 +0000 +++ b/RecordType/RecordWifiConf.cpp Wed Jul 12 12:33:42 2017 +0000 @@ -48,16 +48,16 @@ const RecordWifiConf::fieldType_t RecordWifiConf::sAuthTypeField_id = 0x1003; const RecordWifiConf::fieldType_t RecordWifiConf::sEncTypeField_id = 0x100F; -RecordWifiConf::RecordWifiConf(const std::string &ssid,const std::string &pass, - authType_t authType,encryptionType_t encType): - RecordMimeType(sWifiConfMimeType), - mSsid(ssid), - mPasskey(pass), - mAuthType(authType), - mEncType(encType), - mContentIsChange(true), - mMimeData(NULL), - mMimeDataLenght(0){} +RecordWifiConf::RecordWifiConf(const std::string &ssid,const std::string &pass, authType_t authType,encryptionType_t encType): + RecordMimeType(sWifiConfMimeType), + mSsid(ssid), + mPasskey(pass), + mAuthType(authType), + mEncType(encType), + mContentIsChange(true), + mMimeData(NULL), + mMimeDataLenght(0) { +} /** * move the data into the buffer swapping the byte order @@ -65,7 +65,7 @@ * @param outBuffer buffer where write the data */ template<typename T> -void writeBEFromLE(const T& data,uint8_t *outBuffer){ +void writeBEFromLE(const T& data,uint8_t *outBuffer) { uint8_t *buffer = (uint8_t*)&data; std::reverse_copy(buffer,buffer+sizeof(T),outBuffer); } @@ -89,24 +89,27 @@ offset+=sizeof(dataType);; writeBEFromLE(size,buffer+offset); offset+=sizeof(size); - if(data!=NULL || size!=0) + if (data!=NULL || size!=0) { std::memcpy(buffer+offset,data,size); + } return offset+size; } void RecordWifiConf::update_mime_data(){ - if(!mContentIsChange) + if (!mContentIsChange) { return; + } - mMimeDataLenght=sizeof(sCredentialField_id)+sizeof(fieldLenght_t)+ - sizeof(sNetworkIdField_id)+sizeof(fieldLenght_t)+sizeof(sDefaultNetworkId)+ - sizeof(sSsidField_id)+sizeof(fieldLenght_t)+mSsid.length()+ - sizeof(sNetworkKeyField_id)+sizeof(fieldLenght_t)+mPasskey.length()+ - sizeof(sAuthTypeField_id)+sizeof(fieldLenght_t)+2+ - sizeof(sEncTypeField_id)+sizeof(fieldLenght_t)+2; + mMimeDataLenght = sizeof(sCredentialField_id)+sizeof(fieldLenght_t)+ + sizeof(sNetworkIdField_id)+sizeof(fieldLenght_t)+sizeof(sDefaultNetworkId)+ + sizeof(sSsidField_id)+sizeof(fieldLenght_t)+mSsid.length()+ + sizeof(sNetworkKeyField_id)+sizeof(fieldLenght_t)+mPasskey.length()+ + sizeof(sAuthTypeField_id)+sizeof(fieldLenght_t)+2+ + sizeof(sEncTypeField_id)+sizeof(fieldLenght_t)+2; - if(mMimeData!=NULL) + if (mMimeData!=NULL) { delete [] mMimeData; + } mMimeData = new uint8_t[mMimeDataLenght]; @@ -148,15 +151,12 @@ * @return an object of type RecordVCard or NULL * @par User is in charge of freeing the pointer returned by this function. */ -RecordWifiConf* RecordWifiConf::parse(const RecordHeader &header, - const uint8_t* buffer){ - if (header.get_FNT() != RecordHeader::Mime_media_type - || header.get_type_length() != sWifiConfMimeType.size()) { - return NULL; +RecordWifiConf* RecordWifiConf::parse(const RecordHeader &header, const uint8_t* buffer){ + if (header.get_FNT() != RecordHeader::Mime_media_type || header.get_type_length() != sWifiConfMimeType.size()) { + return NULL; } - if (sWifiConfMimeType.compare(0, sWifiConfMimeType.size(), (const char*) buffer, - sWifiConfMimeType.size()) != 0) { + if (sWifiConfMimeType.compare(0, sWifiConfMimeType.size(), (const char*) buffer, sWifiConfMimeType.size()) != 0) { return NULL; } @@ -171,8 +171,9 @@ readLEFromBE(buffer,&dataLength); buffer+=sizeof(fieldLenght_t); - if(type!=sCredentialField_id) + if(type!=sCredentialField_id) { return NULL; + } std::string ssid; std::string pass; @@ -180,32 +181,35 @@ encryptionType_t encType=ENC_TYPE_NONE; uint16_t enumValue; fieldLenght_t readData=0; - while(readData!=dataLength){ + while (readData!=dataLength) { readLEFromBE(buffer+readData,&type); readData+=sizeof(fieldType_t); fieldLenght_t length; readLEFromBE(buffer+readData,&length); readData+=sizeof(fieldLenght_t); - switch(type){ - case sSsidField_id: + switch(type) { + case sSsidField_id: { ssid.insert(0,(const char*)buffer+readData,length); break; - case sNetworkKeyField_id: + } + case sNetworkKeyField_id: { pass.insert(0,(const char*)buffer+readData,length); break; - case sAuthTypeField_id: + } + case sAuthTypeField_id: { readLEFromBE(buffer+readData,&enumValue); authType = (authType_t)enumValue; break; - case sEncTypeField_id: + } + case sEncTypeField_id: { readLEFromBE(buffer+readData,&enumValue); encType = (encryptionType_t)enumValue; break; + } } readData+=length; - } return new RecordWifiConf(ssid,pass,authType,encType); @@ -213,3 +217,5 @@ } + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/