ST / NDefLib

Dependents:   NFC M2M_2016_STM32 MyongjiElec_capstone1 IDW01M1_Cloud_IBM ... more

Fork of NDefLib by ST Expansion SW Team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RecordWifiConf.cpp Source File

RecordWifiConf.cpp

00001 /**
00002  ******************************************************************************
00003  * @file    RecordWifiConf.h
00004  * @author  ST / Central Labs
00005  * @version V2.0.0
00006  * @date    28 Apr 2017
00007  * @brief   {@link RecordMimeType} that contains a Wifi configuration data
00008  ******************************************************************************
00009  * @attention
00010  *
00011  * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
00012  *
00013  * Redistribution and use in source and binary forms, with or without modification,
00014  * are permitted provided that the following conditions are met:
00015  *   1. Redistributions of source code must retain the above copyright notice,
00016  *      this list of conditions and the following disclaimer.
00017  *   2. Redistributions in binary form must reproduce the above copyright notice,
00018  *      this list of conditions and the following disclaimer in the documentation
00019  *      and/or other materials provided with the distribution.
00020  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00021  *      may be used to endorse or promote products derived from this software
00022  *      without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00028  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00030  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00032  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  ******************************************************************************
00036  */
00037 #include <algorithm>
00038 
00039 #include <RecordWifiConf.h>
00040 namespace NDefLib {
00041 const std::string RecordWifiConf::sWifiConfMimeType("application/vnd.wfa.wsc");
00042 
00043 const RecordWifiConf::fieldType_t RecordWifiConf::sCredentialField_id = 0x100E;
00044 const RecordWifiConf::fieldType_t RecordWifiConf::sNetworkIdField_id = 0x1026;
00045 const uint8_t  RecordWifiConf::sDefaultNetworkId=0x01;
00046 const RecordWifiConf::fieldType_t RecordWifiConf::sSsidField_id = 0x1045;
00047 const RecordWifiConf::fieldType_t RecordWifiConf::sNetworkKeyField_id = 0x1027;
00048 const RecordWifiConf::fieldType_t RecordWifiConf::sAuthTypeField_id = 0x1003;
00049 const RecordWifiConf::fieldType_t RecordWifiConf::sEncTypeField_id = 0x100F;
00050 
00051 RecordWifiConf::RecordWifiConf(const std::string &ssid,const std::string &pass, authType_t authType,encryptionType_t encType):
00052     RecordMimeType(sWifiConfMimeType),
00053     mSsid(ssid),
00054     mPasskey(pass),
00055     mAuthType(authType),
00056     mEncType(encType),
00057     mContentIsChange(true),
00058     mMimeData(NULL),
00059     mMimeDataLenght(0) {
00060 }
00061 
00062 /**
00063  * move the data into the buffer swapping the byte order
00064  * @param data data to write into the buffer
00065  * @param outBuffer buffer where write the data 
00066 */
00067 template<typename T>
00068 void writeBEFromLE(const T& data,uint8_t *outBuffer) {
00069     uint8_t *buffer = (uint8_t*)&data;
00070     std::reverse_copy(buffer,buffer+sizeof(T),outBuffer);
00071 }
00072 
00073 template<typename T>
00074 uint32_t RecordWifiConf::write_data_field(const fieldType_t& dataType,const T& data,uint8_t  *buffer){
00075     uint32_t offset=0;
00076     writeBEFromLE(dataType,buffer+offset);
00077     offset+=sizeof(dataType);
00078     const fieldLenght_t dataSize = sizeof(T);
00079     writeBEFromLE(dataSize,buffer+offset);
00080     offset+=sizeof(dataSize);
00081     writeBEFromLE(data,buffer+offset);
00082     return offset+dataSize;
00083 }
00084 
00085 uint32_t RecordWifiConf::write_data_field(const fieldType_t& dataType,
00086         const char *data,const fieldLenght_t& size, uint8_t *buffer){
00087     uint32_t offset=0;
00088     writeBEFromLE(dataType,buffer+offset);
00089     offset+=sizeof(dataType);;
00090     writeBEFromLE(size,buffer+offset);
00091     offset+=sizeof(size);
00092     if (data!=NULL || size!=0) {
00093         std::memcpy(buffer+offset,data,size);
00094     }
00095     return offset+size;
00096 }
00097 
00098 void RecordWifiConf::update_mime_data(){
00099     if (!mContentIsChange) {
00100         return;
00101     }
00102 
00103     mMimeDataLenght = sizeof(sCredentialField_id)+sizeof(fieldLenght_t)+
00104         sizeof(sNetworkIdField_id)+sizeof(fieldLenght_t)+sizeof(sDefaultNetworkId)+
00105         sizeof(sSsidField_id)+sizeof(fieldLenght_t)+mSsid.length()+
00106         sizeof(sNetworkKeyField_id)+sizeof(fieldLenght_t)+mPasskey.length()+
00107         sizeof(sAuthTypeField_id)+sizeof(fieldLenght_t)+2+
00108         sizeof(sEncTypeField_id)+sizeof(fieldLenght_t)+2;
00109 
00110     if (mMimeData!=NULL) {
00111         delete [] mMimeData;
00112     }
00113 
00114     mMimeData = new uint8_t[mMimeDataLenght];
00115 
00116     uint32_t writeOffset=0;
00117     writeBEFromLE(sCredentialField_id,mMimeData+writeOffset);
00118     writeOffset+=sizeof(sCredentialField_id);
00119     writeBEFromLE((fieldLenght_t)(mMimeDataLenght-4),mMimeData+writeOffset);
00120     writeOffset+=sizeof(fieldLenght_t);
00121 
00122     writeOffset += write_data_field(sNetworkIdField_id,sDefaultNetworkId,mMimeData+writeOffset);
00123     fieldLenght_t tempLength = (fieldLenght_t)sizeof(std::string::value_type)*mSsid.length();
00124     writeOffset += write_data_field(sSsidField_id,mSsid.data(),tempLength,mMimeData+writeOffset);
00125     writeOffset += write_data_field(sAuthTypeField_id,(int16_t)mAuthType,mMimeData+writeOffset);
00126     writeOffset += write_data_field(sEncTypeField_id,(int16_t)mEncType,mMimeData+writeOffset);
00127     tempLength = (fieldLenght_t)sizeof(std::string::value_type)*mPasskey.length();
00128     writeOffset += write_data_field(sNetworkKeyField_id,mPasskey.data(),tempLength,mMimeData+writeOffset);
00129 
00130     set_mime_data_pointer(mMimeData,mMimeDataLenght);
00131     mContentIsChange=false;
00132 }
00133 
00134 /**
00135  * Read a data from a buffer, where the buffer is in little endian and we are in
00136  * a big endian system
00137  * @param buffer buffer where read the data
00138  * @param out pointer to the variable where store the read data
00139  */
00140 template<typename T>
00141 void readLEFromBE(const uint8_t *const buffer,T* out){
00142     const uint32_t typeSize = sizeof(T);
00143     uint8_t *outBuffer = (uint8_t*)out;
00144     std::reverse_copy(buffer,buffer+typeSize,outBuffer);
00145 }
00146 
00147 /**
00148  * Create a RecordWifiConf reading the data from the buffer.
00149      * @param header Record header.
00150  * @param buffer Buffer to read the data from.
00151  * @return an object of type RecordVCard or NULL
00152  * @par User is in charge of freeing the pointer returned by this function.
00153  */
00154 RecordWifiConf* RecordWifiConf::parse(const RecordHeader &header, const uint8_t* buffer){
00155     if (header.get_FNT() != RecordHeader::Mime_media_type || header.get_type_length() != sWifiConfMimeType.size()) {
00156         return NULL;
00157     }
00158 
00159     if (sWifiConfMimeType.compare(0, sWifiConfMimeType.size(), (const char*) buffer, sWifiConfMimeType.size()) != 0) {
00160         return NULL;
00161     }
00162 
00163     buffer += header.get_type_length();
00164     buffer += header.get_id_length();
00165 
00166     fieldType_t type;
00167     fieldLenght_t dataLength;
00168 
00169     readLEFromBE(buffer,&type);
00170     buffer+=sizeof(fieldType_t);
00171     readLEFromBE(buffer,&dataLength);
00172     buffer+=sizeof(fieldLenght_t);
00173 
00174     if(type!=sCredentialField_id) {
00175         return NULL;
00176     }
00177 
00178     std::string ssid;
00179     std::string pass;
00180     authType_t authType=AUTH_OPEN;
00181     encryptionType_t encType=ENC_TYPE_NONE;
00182     uint16_t enumValue;
00183     fieldLenght_t readData=0;
00184     while (readData!=dataLength) {
00185         readLEFromBE(buffer+readData,&type);
00186         readData+=sizeof(fieldType_t);
00187         fieldLenght_t length;
00188         readLEFromBE(buffer+readData,&length);
00189         readData+=sizeof(fieldLenght_t);
00190 
00191         switch(type) {
00192             case sSsidField_id: {
00193                 ssid.insert(0,(const char*)buffer+readData,length);
00194                 break;
00195             }
00196             case sNetworkKeyField_id: {
00197                 pass.insert(0,(const char*)buffer+readData,length);
00198                 break;
00199             }
00200             case sAuthTypeField_id: {
00201                 readLEFromBE(buffer+readData,&enumValue);
00202                 authType = (authType_t)enumValue;
00203                 break;
00204             }
00205             case sEncTypeField_id: {
00206                 readLEFromBE(buffer+readData,&enumValue);
00207                 encType = (encryptionType_t)enumValue;
00208                 break;
00209             }
00210         }
00211 
00212         readData+=length;
00213     }
00214 
00215     return new RecordWifiConf(ssid,pass,authType,encType);
00216 }//parse
00217 
00218 }
00219 
00220 
00221 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/