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