v-16

Committer:
DuyLionTran
Date:
Tue Dec 12 15:58:23 2017 +0000
Revision:
0:89e2c8a57572
version 1.6

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DuyLionTran 0:89e2c8a57572 1 /**
DuyLionTran 0:89e2c8a57572 2 ******************************************************************************
DuyLionTran 0:89e2c8a57572 3 * @file RecordWifiConf.h
DuyLionTran 0:89e2c8a57572 4 * @author ST / Central Labs
DuyLionTran 0:89e2c8a57572 5 * @version V2.0.0
DuyLionTran 0:89e2c8a57572 6 * @date 28 Apr 2017
DuyLionTran 0:89e2c8a57572 7 * @brief {@link RecordMimeType} that contains a Wifi configuration data
DuyLionTran 0:89e2c8a57572 8 ******************************************************************************
DuyLionTran 0:89e2c8a57572 9 * @attention
DuyLionTran 0:89e2c8a57572 10 *
DuyLionTran 0:89e2c8a57572 11 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
DuyLionTran 0:89e2c8a57572 12 *
DuyLionTran 0:89e2c8a57572 13 * Redistribution and use in source and binary forms, with or without modification,
DuyLionTran 0:89e2c8a57572 14 * are permitted provided that the following conditions are met:
DuyLionTran 0:89e2c8a57572 15 * 1. Redistributions of source code must retain the above copyright notice,
DuyLionTran 0:89e2c8a57572 16 * this list of conditions and the following disclaimer.
DuyLionTran 0:89e2c8a57572 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
DuyLionTran 0:89e2c8a57572 18 * this list of conditions and the following disclaimer in the documentation
DuyLionTran 0:89e2c8a57572 19 * and/or other materials provided with the distribution.
DuyLionTran 0:89e2c8a57572 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
DuyLionTran 0:89e2c8a57572 21 * may be used to endorse or promote products derived from this software
DuyLionTran 0:89e2c8a57572 22 * without specific prior written permission.
DuyLionTran 0:89e2c8a57572 23 *
DuyLionTran 0:89e2c8a57572 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
DuyLionTran 0:89e2c8a57572 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
DuyLionTran 0:89e2c8a57572 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DuyLionTran 0:89e2c8a57572 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
DuyLionTran 0:89e2c8a57572 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DuyLionTran 0:89e2c8a57572 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
DuyLionTran 0:89e2c8a57572 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
DuyLionTran 0:89e2c8a57572 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
DuyLionTran 0:89e2c8a57572 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
DuyLionTran 0:89e2c8a57572 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
DuyLionTran 0:89e2c8a57572 34 *
DuyLionTran 0:89e2c8a57572 35 ******************************************************************************
DuyLionTran 0:89e2c8a57572 36 */
DuyLionTran 0:89e2c8a57572 37
DuyLionTran 0:89e2c8a57572 38 #ifndef NDEFLIB_RECORDTYPE_RECORDWIFICONF_H_
DuyLionTran 0:89e2c8a57572 39 #define NDEFLIB_RECORDTYPE_RECORDWIFICONF_H_
DuyLionTran 0:89e2c8a57572 40
DuyLionTran 0:89e2c8a57572 41 #include <RecordMimeType.h>
DuyLionTran 0:89e2c8a57572 42 namespace NDefLib {
DuyLionTran 0:89e2c8a57572 43
DuyLionTran 0:89e2c8a57572 44 /**
DuyLionTran 0:89e2c8a57572 45 * Specialize the {@link RecordMimeType} to store Wifi configuration information.
DuyLionTran 0:89e2c8a57572 46 */
DuyLionTran 0:89e2c8a57572 47 class RecordWifiConf: public NDefLib::RecordMimeType {
DuyLionTran 0:89e2c8a57572 48 public:
DuyLionTran 0:89e2c8a57572 49
DuyLionTran 0:89e2c8a57572 50 /**
DuyLionTran 0:89e2c8a57572 51 * Authentication required by the wifi network
DuyLionTran 0:89e2c8a57572 52 */
DuyLionTran 0:89e2c8a57572 53 typedef enum {
DuyLionTran 0:89e2c8a57572 54 AUTH_OPEN = 0x0000, //!< no authentication
DuyLionTran 0:89e2c8a57572 55 AUTH_WPA_PSK = 0x0002,
DuyLionTran 0:89e2c8a57572 56 AUTH_WPA_EAP = 0x0008,
DuyLionTran 0:89e2c8a57572 57 AUTH_WPA2_EAP = 0x0010,
DuyLionTran 0:89e2c8a57572 58 AUTH_WPA2_PSK = 0x0020,
DuyLionTran 0:89e2c8a57572 59 }authType_t;
DuyLionTran 0:89e2c8a57572 60
DuyLionTran 0:89e2c8a57572 61 /**
DuyLionTran 0:89e2c8a57572 62 * Encryption used by the network
DuyLionTran 0:89e2c8a57572 63 */
DuyLionTran 0:89e2c8a57572 64 typedef enum {
DuyLionTran 0:89e2c8a57572 65 ENC_TYPE_NONE = 0x0001, //!< no authentication
DuyLionTran 0:89e2c8a57572 66 ENC_TYPE_WEP = 0x0002, //!< wep is deprecated
DuyLionTran 0:89e2c8a57572 67 ENC_TYPE_TKIP = 0x0004, //!< deprecated -> only with mixed mode (0x000c)
DuyLionTran 0:89e2c8a57572 68 ENC_TYPE_AES = 0x0008, //!< includes CCMP and GCMP
DuyLionTran 0:89e2c8a57572 69 ENC_TYPE_AES_TKIP = 0x000c, //!< mixed mode
DuyLionTran 0:89e2c8a57572 70 }encryptionType_t;
DuyLionTran 0:89e2c8a57572 71
DuyLionTran 0:89e2c8a57572 72 /**
DuyLionTran 0:89e2c8a57572 73 * Create a RecordWifiConf reading the data from the buffer.
DuyLionTran 0:89e2c8a57572 74 * @param header Record header.
DuyLionTran 0:89e2c8a57572 75 * @param buffer Buffer to read the data from.
DuyLionTran 0:89e2c8a57572 76 * @return an object of type RecordVCard or NULL
DuyLionTran 0:89e2c8a57572 77 * @par User is in charge of freeing the pointer returned by this function.
DuyLionTran 0:89e2c8a57572 78 */
DuyLionTran 0:89e2c8a57572 79 static RecordWifiConf* parse(const RecordHeader &header,
DuyLionTran 0:89e2c8a57572 80 const uint8_t* buffer);
DuyLionTran 0:89e2c8a57572 81
DuyLionTran 0:89e2c8a57572 82
DuyLionTran 0:89e2c8a57572 83 /**
DuyLionTran 0:89e2c8a57572 84 * Create a record with the wifi configuration, the default parameters create an open network
DuyLionTran 0:89e2c8a57572 85 * @param ssid network name
DuyLionTran 0:89e2c8a57572 86 * @param pass network password
DuyLionTran 0:89e2c8a57572 87 * @param authType authentication used in the network
DuyLionTran 0:89e2c8a57572 88 * @param encType encryption used in the network
DuyLionTran 0:89e2c8a57572 89 */
DuyLionTran 0:89e2c8a57572 90 RecordWifiConf(const std::string &ssid,const std::string &pass="",
DuyLionTran 0:89e2c8a57572 91 authType_t authType=AUTH_OPEN,encryptionType_t encType=ENC_TYPE_NONE);
DuyLionTran 0:89e2c8a57572 92
DuyLionTran 0:89e2c8a57572 93 /**
DuyLionTran 0:89e2c8a57572 94 * Get the record type.
DuyLionTran 0:89e2c8a57572 95 * @return TYPE_WIFI_CONF
DuyLionTran 0:89e2c8a57572 96 */
DuyLionTran 0:89e2c8a57572 97 virtual RecordType_t get_type() const {
DuyLionTran 0:89e2c8a57572 98 return TYPE_WIFI_CONF;
DuyLionTran 0:89e2c8a57572 99 } //getType
DuyLionTran 0:89e2c8a57572 100
DuyLionTran 0:89e2c8a57572 101 /**
DuyLionTran 0:89e2c8a57572 102 * @return update the record content and return the number of
DuyLionTran 0:89e2c8a57572 103 * bytes needed to store this record
DuyLionTran 0:89e2c8a57572 104 */
DuyLionTran 0:89e2c8a57572 105 virtual uint16_t get_byte_length() {
DuyLionTran 0:89e2c8a57572 106 update_mime_data();
DuyLionTran 0:89e2c8a57572 107 return RecordMimeType::get_byte_length();
DuyLionTran 0:89e2c8a57572 108 }
DuyLionTran 0:89e2c8a57572 109
DuyLionTran 0:89e2c8a57572 110 /**
DuyLionTran 0:89e2c8a57572 111 * Update the content and write it on the buffer.
DuyLionTran 0:89e2c8a57572 112 * @param[out] buffer buffer to write the record content into.
DuyLionTran 0:89e2c8a57572 113 * @return number of bytes written
DuyLionTran 0:89e2c8a57572 114 * @see Record#write
DuyLionTran 0:89e2c8a57572 115 */
DuyLionTran 0:89e2c8a57572 116 virtual uint16_t write(uint8_t *buffer){
DuyLionTran 0:89e2c8a57572 117 update_mime_data();
DuyLionTran 0:89e2c8a57572 118 return RecordMimeType::write(buffer);
DuyLionTran 0:89e2c8a57572 119 }
DuyLionTran 0:89e2c8a57572 120
DuyLionTran 0:89e2c8a57572 121 /**
DuyLionTran 0:89e2c8a57572 122 * Compare two objects.
DuyLionTran 0:89e2c8a57572 123 * @return true if the records have the same Vcard information
DuyLionTran 0:89e2c8a57572 124 */
DuyLionTran 0:89e2c8a57572 125 bool operator==(const RecordWifiConf &other){
DuyLionTran 0:89e2c8a57572 126 return (mSsid==other.mSsid) &&
DuyLionTran 0:89e2c8a57572 127 (mPasskey==other.mPasskey) &&
DuyLionTran 0:89e2c8a57572 128 (mAuthType==other.mAuthType) &&
DuyLionTran 0:89e2c8a57572 129 (mEncType==other.mEncType);
DuyLionTran 0:89e2c8a57572 130 }
DuyLionTran 0:89e2c8a57572 131
DuyLionTran 0:89e2c8a57572 132 const std::string& get_network_key() const{
DuyLionTran 0:89e2c8a57572 133 return mPasskey;
DuyLionTran 0:89e2c8a57572 134 }
DuyLionTran 0:89e2c8a57572 135
DuyLionTran 0:89e2c8a57572 136 void set_network_key(const std::string &newKey){
DuyLionTran 0:89e2c8a57572 137 mPasskey=newKey;
DuyLionTran 0:89e2c8a57572 138 mContentIsChange=true;
DuyLionTran 0:89e2c8a57572 139 }
DuyLionTran 0:89e2c8a57572 140
DuyLionTran 0:89e2c8a57572 141 const std::string& get_network_ssid() const{
DuyLionTran 0:89e2c8a57572 142 return mSsid;
DuyLionTran 0:89e2c8a57572 143 }
DuyLionTran 0:89e2c8a57572 144
DuyLionTran 0:89e2c8a57572 145 void set_network_ssid(const std::string &newSsid){
DuyLionTran 0:89e2c8a57572 146 mSsid=newSsid;
DuyLionTran 0:89e2c8a57572 147 mContentIsChange=true;
DuyLionTran 0:89e2c8a57572 148 }
DuyLionTran 0:89e2c8a57572 149
DuyLionTran 0:89e2c8a57572 150 authType_t get_auth_type() const{
DuyLionTran 0:89e2c8a57572 151 return mAuthType;
DuyLionTran 0:89e2c8a57572 152 }
DuyLionTran 0:89e2c8a57572 153
DuyLionTran 0:89e2c8a57572 154 void set_auth_type(const authType_t &newAuth){
DuyLionTran 0:89e2c8a57572 155 mAuthType = newAuth;
DuyLionTran 0:89e2c8a57572 156 mContentIsChange=true;
DuyLionTran 0:89e2c8a57572 157 }
DuyLionTran 0:89e2c8a57572 158
DuyLionTran 0:89e2c8a57572 159 encryptionType_t get_encryption() const{
DuyLionTran 0:89e2c8a57572 160 return mEncType;
DuyLionTran 0:89e2c8a57572 161 }
DuyLionTran 0:89e2c8a57572 162
DuyLionTran 0:89e2c8a57572 163 void set_encryption_type(const encryptionType_t &newEncript){
DuyLionTran 0:89e2c8a57572 164 mEncType = newEncript;
DuyLionTran 0:89e2c8a57572 165 mContentIsChange=true;
DuyLionTran 0:89e2c8a57572 166 }
DuyLionTran 0:89e2c8a57572 167
DuyLionTran 0:89e2c8a57572 168 virtual ~RecordWifiConf(){
DuyLionTran 0:89e2c8a57572 169 if(mMimeData!=NULL)
DuyLionTran 0:89e2c8a57572 170 delete [] mMimeData;
DuyLionTran 0:89e2c8a57572 171 }
DuyLionTran 0:89e2c8a57572 172
DuyLionTran 0:89e2c8a57572 173 private:
DuyLionTran 0:89e2c8a57572 174
DuyLionTran 0:89e2c8a57572 175 /**
DuyLionTran 0:89e2c8a57572 176 * function that update the ndef data format
DuyLionTran 0:89e2c8a57572 177 */
DuyLionTran 0:89e2c8a57572 178 void update_mime_data();
DuyLionTran 0:89e2c8a57572 179
DuyLionTran 0:89e2c8a57572 180 typedef uint16_t fieldType_t; //< type to use for store a field type
DuyLionTran 0:89e2c8a57572 181 typedef uint16_t fieldLenght_t; //<type to use for store a filed length
DuyLionTran 0:89e2c8a57572 182
DuyLionTran 0:89e2c8a57572 183 /**
DuyLionTran 0:89e2c8a57572 184 * write a data field into the output buffer
DuyLionTran 0:89e2c8a57572 185 * @param dataType field type
DuyLionTran 0:89e2c8a57572 186 * @param data field content
DuyLionTran 0:89e2c8a57572 187 * @param buffer buffer where write the data field
DuyLionTran 0:89e2c8a57572 188 * @return number of write bytes
DuyLionTran 0:89e2c8a57572 189 */
DuyLionTran 0:89e2c8a57572 190 template<typename T>
DuyLionTran 0:89e2c8a57572 191 uint32_t write_data_field(const fieldType_t& dataType,
DuyLionTran 0:89e2c8a57572 192 const T& data, uint8_t *buffer);
DuyLionTran 0:89e2c8a57572 193
DuyLionTran 0:89e2c8a57572 194 /**
DuyLionTran 0:89e2c8a57572 195 * write a data buffer inside the output buffer
DuyLionTran 0:89e2c8a57572 196 * @param dataType field type
DuyLionTran 0:89e2c8a57572 197 * @param data buffer where read the data
DuyLionTran 0:89e2c8a57572 198 * @param size number of byte to move
DuyLionTran 0:89e2c8a57572 199 * @param out output buffer
DuyLionTran 0:89e2c8a57572 200 * @return number of write bytes
DuyLionTran 0:89e2c8a57572 201 */
DuyLionTran 0:89e2c8a57572 202 uint32_t write_data_field(const fieldType_t& dataType,
DuyLionTran 0:89e2c8a57572 203 const char *data,const fieldLenght_t& size, uint8_t *buffer);
DuyLionTran 0:89e2c8a57572 204
DuyLionTran 0:89e2c8a57572 205 std::string mSsid;
DuyLionTran 0:89e2c8a57572 206 std::string mPasskey;
DuyLionTran 0:89e2c8a57572 207 authType_t mAuthType;
DuyLionTran 0:89e2c8a57572 208 encryptionType_t mEncType;
DuyLionTran 0:89e2c8a57572 209
DuyLionTran 0:89e2c8a57572 210 bool mContentIsChange; ///< true if we have to update the string representation of the data
DuyLionTran 0:89e2c8a57572 211 uint8_t *mMimeData; //< buffer where store the record playload
DuyLionTran 0:89e2c8a57572 212 fieldLenght_t mMimeDataLenght;
DuyLionTran 0:89e2c8a57572 213
DuyLionTran 0:89e2c8a57572 214 static const std::string sWifiConfMimeType;
DuyLionTran 0:89e2c8a57572 215 static const fieldType_t sCredentialField_id;
DuyLionTran 0:89e2c8a57572 216 static const fieldType_t sNetworkIdField_id;
DuyLionTran 0:89e2c8a57572 217 static const uint8_t sDefaultNetworkId;
DuyLionTran 0:89e2c8a57572 218 static const fieldType_t sSsidField_id;
DuyLionTran 0:89e2c8a57572 219 static const fieldType_t sNetworkKeyField_id;
DuyLionTran 0:89e2c8a57572 220 static const fieldType_t sAuthTypeField_id;
DuyLionTran 0:89e2c8a57572 221 static const fieldType_t sEncTypeField_id;
DuyLionTran 0:89e2c8a57572 222 };
DuyLionTran 0:89e2c8a57572 223 }
DuyLionTran 0:89e2c8a57572 224
DuyLionTran 0:89e2c8a57572 225 #endif /* NDEFLIB_RECORDTYPE_RECORDWIFICONF_H_ */
DuyLionTran 0:89e2c8a57572 226
DuyLionTran 0:89e2c8a57572 227
DuyLionTran 0:89e2c8a57572 228 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/