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.h Source File

RecordWifiConf.h

Go to the documentation of this file.
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 
00038 #ifndef NDEFLIB_RECORDTYPE_RECORDWIFICONF_H_
00039 #define NDEFLIB_RECORDTYPE_RECORDWIFICONF_H_
00040 
00041 #include <RecordMimeType.h>
00042 namespace NDefLib {
00043 
00044 /**
00045  * Specialize the {@link RecordMimeType} to store Wifi configuration information.
00046  */
00047 class RecordWifiConf: public NDefLib::RecordMimeType {
00048 public:
00049 
00050     /**
00051      * Authentication required by the wifi network
00052      */
00053     typedef enum {
00054         AUTH_OPEN = 0x0000, //!< no authentication
00055         AUTH_WPA_PSK = 0x0002,
00056         AUTH_WPA_EAP = 0x0008,
00057         AUTH_WPA2_EAP = 0x0010,
00058         AUTH_WPA2_PSK = 0x0020,
00059     }authType_t;
00060 
00061     /**
00062      * Encryption used by the network
00063      */
00064     typedef enum {
00065         ENC_TYPE_NONE = 0x0001,     //!< no authentication
00066         ENC_TYPE_WEP = 0x0002,      //!< wep is deprecated
00067         ENC_TYPE_TKIP = 0x0004,     //!< deprecated -> only with mixed mode (0x000c)
00068         ENC_TYPE_AES = 0x0008,      //!< includes CCMP and GCMP
00069         ENC_TYPE_AES_TKIP = 0x000c, //!< mixed mode
00070     }encryptionType_t;
00071 
00072     /**
00073      * Create a RecordWifiConf reading the data from the buffer.
00074      * @param header Record header.
00075      * @param buffer Buffer to read the data from.
00076      * @return an object of type RecordVCard or NULL
00077      * @par User is in charge of freeing the pointer returned by this function.
00078      */
00079     static RecordWifiConf* parse(const RecordHeader &header,
00080         const uint8_t* buffer);
00081 
00082 
00083     /**
00084      * Create a record with the wifi configuration, the default parameters create an open network
00085      * @param ssid network name
00086      * @param pass network password
00087      * @param authType authentication used in the network
00088      * @param encType encryption used in the network
00089      */
00090     RecordWifiConf(const std::string &ssid,const std::string &pass="",
00091         authType_t authType=AUTH_OPEN,encryptionType_t encType=ENC_TYPE_NONE);
00092 
00093     /**
00094      * Get the record type.
00095      * @return TYPE_WIFI_CONF
00096      */
00097     virtual RecordType_t get_type() const {
00098         return TYPE_WIFI_CONF;
00099     } //getType
00100 
00101     /**
00102      * @return update the record content and return the number of
00103      * bytes needed to store this record
00104      */
00105     virtual uint16_t get_byte_length () {
00106         update_mime_data();
00107         return RecordMimeType::get_byte_length ();
00108     }
00109 
00110     /**
00111      * Update the content and write it on the buffer.
00112      * @param[out] buffer buffer to write the record content into.
00113      * @return number of bytes written
00114      * @see Record#write
00115      */
00116     virtual uint16_t write(uint8_t *buffer){
00117         update_mime_data();
00118         return RecordMimeType::write(buffer);
00119     }
00120 
00121     /**
00122      * Compare two objects.
00123      * @return true if the records have the same Vcard information
00124      */
00125     bool operator==(const RecordWifiConf &other){
00126         return (mSsid==other.mSsid) &&
00127                (mPasskey==other.mPasskey) &&
00128                (mAuthType==other.mAuthType) &&
00129                (mEncType==other.mEncType);
00130     }
00131 
00132     const std::string& get_network_key() const{
00133         return mPasskey;
00134     }
00135 
00136     void set_network_key(const std::string &newKey){
00137         mPasskey=newKey;
00138         mContentIsChange=true;
00139     }
00140 
00141     const std::string& get_network_ssid() const{
00142         return mSsid;
00143     }
00144 
00145     void set_network_ssid(const std::string &newSsid){
00146         mSsid=newSsid;
00147         mContentIsChange=true;
00148     }
00149 
00150     authType_t get_auth_type() const{
00151         return mAuthType;
00152     }
00153 
00154     void set_auth_type(const authType_t &newAuth){
00155         mAuthType = newAuth;
00156         mContentIsChange=true;
00157     }
00158 
00159     encryptionType_t get_encryption() const{
00160         return mEncType;
00161     }
00162 
00163     void set_encryption_type(const encryptionType_t &newEncript){
00164         mEncType =  newEncript;
00165         mContentIsChange=true;
00166     }
00167 
00168     virtual ~RecordWifiConf(){
00169         if(mMimeData!=NULL)
00170             delete [] mMimeData;
00171     }
00172 
00173 private:
00174 
00175     /**
00176      * function that update the ndef data format
00177      */
00178     void update_mime_data();
00179 
00180     typedef uint16_t fieldType_t; //< type to use for store a field type
00181     typedef uint16_t fieldLenght_t; //<type to use for store a filed length
00182 
00183     /**
00184      * write a data field into the output buffer
00185      * @param dataType field type
00186      * @param data field content
00187      * @param buffer buffer where write the data field
00188      * @return number of write bytes
00189      */
00190     template<typename T>
00191     uint32_t write_data_field(const fieldType_t& dataType,
00192         const T& data, uint8_t *buffer);
00193 
00194     /**
00195      * write a data buffer inside the output buffer
00196      * @param dataType field type
00197      * @param data buffer where read the data
00198      * @param size number of byte to move
00199      * @param out output buffer
00200      * @return number of write bytes
00201      */
00202     uint32_t write_data_field(const fieldType_t& dataType,
00203         const char *data,const fieldLenght_t& size, uint8_t *buffer);
00204 
00205     std::string mSsid;
00206     std::string mPasskey;
00207     authType_t mAuthType;
00208     encryptionType_t mEncType;
00209 
00210     bool mContentIsChange; ///< true if we have to update the string representation of the data
00211     uint8_t *mMimeData; //< buffer where store the record playload
00212     fieldLenght_t mMimeDataLenght;
00213 
00214     static const std::string sWifiConfMimeType;
00215     static const fieldType_t sCredentialField_id;
00216     static const fieldType_t sNetworkIdField_id;
00217     static const uint8_t sDefaultNetworkId;
00218     static const fieldType_t sSsidField_id;
00219     static const fieldType_t sNetworkKeyField_id;
00220     static const fieldType_t sAuthTypeField_id;
00221     static const fieldType_t sEncTypeField_id;
00222 };
00223 }
00224 
00225 #endif /* NDEFLIB_RECORDTYPE_RECORDWIFICONF_H_ */
00226 
00227 
00228 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/