Fork for the GitHub
lib_NDEF_Wifi.cpp@0:de13951f30f6, 2019-11-14 (annotated)
- Committer:
- DiegoOstuni
- Date:
- Thu Nov 14 10:34:11 2019 +0000
- Revision:
- 0:de13951f30f6
Add files
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DiegoOstuni | 0:de13951f30f6 | 1 | /** |
DiegoOstuni | 0:de13951f30f6 | 2 | ****************************************************************************** |
DiegoOstuni | 0:de13951f30f6 | 3 | * @file lib_NDEF_Wifi.c |
DiegoOstuni | 0:de13951f30f6 | 4 | * @author Central LAB |
DiegoOstuni | 0:de13951f30f6 | 5 | * @version V1.0.0 |
DiegoOstuni | 0:de13951f30f6 | 6 | * @date 7-October-2015 |
DiegoOstuni | 0:de13951f30f6 | 7 | * @ingroup LibNDEF_Wifi |
DiegoOstuni | 0:de13951f30f6 | 8 | * @brief This file helps to manage a NDEF message representing a Wifi Token. |
DiegoOstuni | 0:de13951f30f6 | 9 | ****************************************************************************** |
DiegoOstuni | 0:de13951f30f6 | 10 | * @attention |
DiegoOstuni | 0:de13951f30f6 | 11 | * |
DiegoOstuni | 0:de13951f30f6 | 12 | * <h2><center>© COPYRIGHT 2016 STMicroelectronics</center></h2> |
DiegoOstuni | 0:de13951f30f6 | 13 | * |
DiegoOstuni | 0:de13951f30f6 | 14 | * Licensed under ST MYLIBERTY SOFTWARE LICENSE AGREEMENT (the "License"); |
DiegoOstuni | 0:de13951f30f6 | 15 | * You may not use this file except in compliance with the License. |
DiegoOstuni | 0:de13951f30f6 | 16 | * You may obtain a copy of the License at: |
DiegoOstuni | 0:de13951f30f6 | 17 | * |
DiegoOstuni | 0:de13951f30f6 | 18 | * http://www.st.com/myliberty |
DiegoOstuni | 0:de13951f30f6 | 19 | * |
DiegoOstuni | 0:de13951f30f6 | 20 | * Unless required by applicable law or agreed to in writing, software |
DiegoOstuni | 0:de13951f30f6 | 21 | * distributed under the License is distributed on an "AS IS" BASIS, |
DiegoOstuni | 0:de13951f30f6 | 22 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, |
DiegoOstuni | 0:de13951f30f6 | 23 | * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, |
DiegoOstuni | 0:de13951f30f6 | 24 | * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. |
DiegoOstuni | 0:de13951f30f6 | 25 | * See the License for the specific language governing permissions and |
DiegoOstuni | 0:de13951f30f6 | 26 | * limitations under the License. |
DiegoOstuni | 0:de13951f30f6 | 27 | * |
DiegoOstuni | 0:de13951f30f6 | 28 | ****************************************************************************** |
DiegoOstuni | 0:de13951f30f6 | 29 | */ |
DiegoOstuni | 0:de13951f30f6 | 30 | |
DiegoOstuni | 0:de13951f30f6 | 31 | /* Includes ------------------------------------------------------------------*/ |
DiegoOstuni | 0:de13951f30f6 | 32 | #include "lib_NDEF_Wifi.h" |
DiegoOstuni | 0:de13951f30f6 | 33 | #define ATTRIBUTE_ID_SSID_LSB 0X10 |
DiegoOstuni | 0:de13951f30f6 | 34 | #define ATTRIBUTE_ID_SSID_MSB 0X45 |
DiegoOstuni | 0:de13951f30f6 | 35 | |
DiegoOstuni | 0:de13951f30f6 | 36 | |
DiegoOstuni | 0:de13951f30f6 | 37 | #define ATTRIBUTE_ID_NETWORK_LSB 0X10 |
DiegoOstuni | 0:de13951f30f6 | 38 | #define ATTRIBUTE_ID_NETWORK_MSB 0X27 |
DiegoOstuni | 0:de13951f30f6 | 39 | |
DiegoOstuni | 0:de13951f30f6 | 40 | |
DiegoOstuni | 0:de13951f30f6 | 41 | /** @addtogroup lib_NDEF_Wifi Wifi OOB library (WPS) |
DiegoOstuni | 0:de13951f30f6 | 42 | * @ingroup libNDEF |
DiegoOstuni | 0:de13951f30f6 | 43 | * @brief This module is used to manage a Wifi Out-Of-Band NDEF message, to start a communication based on Wifi. |
DiegoOstuni | 0:de13951f30f6 | 44 | * @details The Wifi OOB format is described by the Wifi Protected Setup specification. |
DiegoOstuni | 0:de13951f30f6 | 45 | * It consists in a list of data elements formated as type-length-value. |
DiegoOstuni | 0:de13951f30f6 | 46 | * This module allows to build, write & read such data embedded in a NDEF message. |
DiegoOstuni | 0:de13951f30f6 | 47 | * @section Wifi_Library_Usage Wifi NDEF library usage |
DiegoOstuni | 0:de13951f30f6 | 48 | * @subsection Write_Wifi_OOB How to write a Wifi OOB |
DiegoOstuni | 0:de13951f30f6 | 49 | * The Wifi OOB written in a NDEF record by this module has the following structure: |
DiegoOstuni | 0:de13951f30f6 | 50 | * - Version |
DiegoOstuni | 0:de13951f30f6 | 51 | * - Credential |
DiegoOstuni | 0:de13951f30f6 | 52 | - Network index |
DiegoOstuni | 0:de13951f30f6 | 53 | - SSID |
DiegoOstuni | 0:de13951f30f6 | 54 | - Authentication Type |
DiegoOstuni | 0:de13951f30f6 | 55 | - Encryption Type |
DiegoOstuni | 0:de13951f30f6 | 56 | - Network Key |
DiegoOstuni | 0:de13951f30f6 | 57 | - MAC Address |
DiegoOstuni | 0:de13951f30f6 | 58 | - Vendor Extension |
DiegoOstuni | 0:de13951f30f6 | 59 | - Network Key Shareable |
DiegoOstuni | 0:de13951f30f6 | 60 | - Vendor Extension |
DiegoOstuni | 0:de13951f30f6 | 61 | - Version2 |
DiegoOstuni | 0:de13951f30f6 | 62 | |
DiegoOstuni | 0:de13951f30f6 | 63 | To write a Wifi OOB, the user must: |
DiegoOstuni | 0:de13951f30f6 | 64 | 1. Instanciate & initialize a `sWifiTokenInfo` structure, such as: |
DiegoOstuni | 0:de13951f30f6 | 65 | |
DiegoOstuni | 0:de13951f30f6 | 66 | sWifiTokenInfo wps_oob = {.NetworkSSID = "MY_SSID", |
DiegoOstuni | 0:de13951f30f6 | 67 | .AuthenticationType = NDEF_WIFI_AUTHENTICATION_NONE, |
DiegoOstuni | 0:de13951f30f6 | 68 | .EncryptionType = NDEF_WIFI_ENCRYPTION_NONE, |
DiegoOstuni | 0:de13951f30f6 | 69 | .NetworkKey = "01234567"}; |
DiegoOstuni | 0:de13951f30f6 | 70 | @note |
DiegoOstuni | 0:de13951f30f6 | 71 | 1. `AuthenticationType` & `EncryptionType` fields must respectively be values from enums: `Ndef_Wifi_Authentication_t` & `Ndef_Wifi_Encryption_t`. |
DiegoOstuni | 0:de13951f30f6 | 72 | 2. If the `Network key` is set to an empty string, the library sets it to `00000000`.\n |
DiegoOstuni | 0:de13951f30f6 | 73 | Even if 0-length Network Key is supposed to be supported, smartphones dont necessarily accept it. |
DiegoOstuni | 0:de13951f30f6 | 74 | |
DiegoOstuni | 0:de13951f30f6 | 75 | 2. Call the `NDEF_WriteWifiToken` function on a pointer on the previous struct instance: |
DiegoOstuni | 0:de13951f30f6 | 76 | |
DiegoOstuni | 0:de13951f30f6 | 77 | NDEF_WriteWifiToken(&wps_oob); |
DiegoOstuni | 0:de13951f30f6 | 78 | * @subsection Read_Wifi_OOB How to read a Wifi OOB |
DiegoOstuni | 0:de13951f30f6 | 79 | * 1. Read the 1st record of the NDEF message: |
DiegoOstuni | 0:de13951f30f6 | 80 | * |
DiegoOstuni | 0:de13951f30f6 | 81 | * sRecordInfo_t rRecord; |
DiegoOstuni | 0:de13951f30f6 | 82 | * NDEF_ReadNDEF(NDEF_Buffer); |
DiegoOstuni | 0:de13951f30f6 | 83 | * NDEF_IdentifyBuffer(rRecord,NDEF_Buffer); |
DiegoOstuni | 0:de13951f30f6 | 84 | * 2. Decode the Wifi OOB: |
DiegoOstuni | 0:de13951f30f6 | 85 | * |
DiegoOstuni | 0:de13951f30f6 | 86 | * sWifiTokenInfo wps; |
DiegoOstuni | 0:de13951f30f6 | 87 | * NDEF_ReadWifiToken(&record, &wps); |
DiegoOstuni | 0:de13951f30f6 | 88 | * @note |
DiegoOstuni | 0:de13951f30f6 | 89 | * Only the `NetworkSSID` & `NetworkKey` are extracted from the Wifi OOB, other fields of the `sWifiTokenInfo` structure are left un-initialized. |
DiegoOstuni | 0:de13951f30f6 | 90 | * @{ |
DiegoOstuni | 0:de13951f30f6 | 91 | */ |
DiegoOstuni | 0:de13951f30f6 | 92 | |
DiegoOstuni | 0:de13951f30f6 | 93 | /* This buffer contains the data send/received by the TAG */ |
DiegoOstuni | 0:de13951f30f6 | 94 | extern uint8_t NDEF_Buffer [NDEF_MAX_SIZE]; |
DiegoOstuni | 0:de13951f30f6 | 95 | |
DiegoOstuni | 0:de13951f30f6 | 96 | |
DiegoOstuni | 0:de13951f30f6 | 97 | static void NDEF_FillWifiTokenStruct( uint8_t* pPayload, uint32_t PayloadSize, sWifiTokenInfo *pWifiTokenStruct); |
DiegoOstuni | 0:de13951f30f6 | 98 | static void NDEF_Read_WifiToken ( struct sRecordInfo *pRecordStruct, sWifiTokenInfo *pWifiTokenStruct ); |
DiegoOstuni | 0:de13951f30f6 | 99 | |
DiegoOstuni | 0:de13951f30f6 | 100 | /** |
DiegoOstuni | 0:de13951f30f6 | 101 | * @brief This function fills a WifiToken structure with information from the NDEF message. |
DiegoOstuni | 0:de13951f30f6 | 102 | * @param pPayload Pointer on the payload data of the NDEF message. |
DiegoOstuni | 0:de13951f30f6 | 103 | * @param PayloadSize number of data in the payload. |
DiegoOstuni | 0:de13951f30f6 | 104 | * @param pWifiTokenStruct pointer on the structure to fill. |
DiegoOstuni | 0:de13951f30f6 | 105 | * @retval NONE |
DiegoOstuni | 0:de13951f30f6 | 106 | */ |
DiegoOstuni | 0:de13951f30f6 | 107 | static void NDEF_FillWifiTokenStruct( uint8_t* pPayload, uint32_t PayloadSize, sWifiTokenInfo *pWifiTokenStruct) |
DiegoOstuni | 0:de13951f30f6 | 108 | { |
DiegoOstuni | 0:de13951f30f6 | 109 | uint8_t* pLastByteAdd,data1,data2,*temp,*temp_br ; |
DiegoOstuni | 0:de13951f30f6 | 110 | uint16_t SSIDLen,NetWorkKeyLen; |
DiegoOstuni | 0:de13951f30f6 | 111 | uint8_t *dbg,dbg1; |
DiegoOstuni | 0:de13951f30f6 | 112 | |
DiegoOstuni | 0:de13951f30f6 | 113 | |
DiegoOstuni | 0:de13951f30f6 | 114 | pLastByteAdd = (uint8_t*)(pPayload + PayloadSize); |
DiegoOstuni | 0:de13951f30f6 | 115 | pPayload--; |
DiegoOstuni | 0:de13951f30f6 | 116 | |
DiegoOstuni | 0:de13951f30f6 | 117 | while(pPayload++ != pLastByteAdd) |
DiegoOstuni | 0:de13951f30f6 | 118 | { |
DiegoOstuni | 0:de13951f30f6 | 119 | uint8_t attribute = *pPayload; |
DiegoOstuni | 0:de13951f30f6 | 120 | temp_br = pPayload; |
DiegoOstuni | 0:de13951f30f6 | 121 | switch(attribute) |
DiegoOstuni | 0:de13951f30f6 | 122 | { |
DiegoOstuni | 0:de13951f30f6 | 123 | |
DiegoOstuni | 0:de13951f30f6 | 124 | case ATTRIBUTE_ID_SSID_LSB: |
DiegoOstuni | 0:de13951f30f6 | 125 | temp = pPayload; |
DiegoOstuni | 0:de13951f30f6 | 126 | dbg = temp; |
DiegoOstuni | 0:de13951f30f6 | 127 | dbg1 = *++dbg; |
DiegoOstuni | 0:de13951f30f6 | 128 | if(dbg1 == ATTRIBUTE_ID_SSID_MSB ) |
DiegoOstuni | 0:de13951f30f6 | 129 | { |
DiegoOstuni | 0:de13951f30f6 | 130 | data1 = *++dbg; |
DiegoOstuni | 0:de13951f30f6 | 131 | data2 = *++dbg; |
DiegoOstuni | 0:de13951f30f6 | 132 | SSIDLen = data1; |
DiegoOstuni | 0:de13951f30f6 | 133 | SSIDLen = SSIDLen << 8; |
DiegoOstuni | 0:de13951f30f6 | 134 | SSIDLen |= data2; |
DiegoOstuni | 0:de13951f30f6 | 135 | pPayload += 4; |
DiegoOstuni | 0:de13951f30f6 | 136 | memcpy( pWifiTokenStruct->NetworkSSID, pPayload, SSIDLen); |
DiegoOstuni | 0:de13951f30f6 | 137 | /* add end of string charactere */ |
DiegoOstuni | 0:de13951f30f6 | 138 | pWifiTokenStruct->NetworkSSID[SSIDLen] = '\0'; |
DiegoOstuni | 0:de13951f30f6 | 139 | pPayload += SSIDLen - 1; |
DiegoOstuni | 0:de13951f30f6 | 140 | } |
DiegoOstuni | 0:de13951f30f6 | 141 | else if(dbg1 == ATTRIBUTE_ID_NETWORK_MSB ) |
DiegoOstuni | 0:de13951f30f6 | 142 | { |
DiegoOstuni | 0:de13951f30f6 | 143 | data1 = *++dbg; |
DiegoOstuni | 0:de13951f30f6 | 144 | data2 = *++dbg; |
DiegoOstuni | 0:de13951f30f6 | 145 | NetWorkKeyLen = data1; |
DiegoOstuni | 0:de13951f30f6 | 146 | NetWorkKeyLen = NetWorkKeyLen << 8; |
DiegoOstuni | 0:de13951f30f6 | 147 | NetWorkKeyLen |= data2; |
DiegoOstuni | 0:de13951f30f6 | 148 | pPayload += 4; |
DiegoOstuni | 0:de13951f30f6 | 149 | memcpy( pWifiTokenStruct->NetworkKey, pPayload, NetWorkKeyLen); |
DiegoOstuni | 0:de13951f30f6 | 150 | /* add end of string charactere */ |
DiegoOstuni | 0:de13951f30f6 | 151 | pWifiTokenStruct->NetworkKey[NetWorkKeyLen] = '\0'; |
DiegoOstuni | 0:de13951f30f6 | 152 | pPayload += NetWorkKeyLen -1; |
DiegoOstuni | 0:de13951f30f6 | 153 | } |
DiegoOstuni | 0:de13951f30f6 | 154 | else |
DiegoOstuni | 0:de13951f30f6 | 155 | { |
DiegoOstuni | 0:de13951f30f6 | 156 | pPayload = temp_br; |
DiegoOstuni | 0:de13951f30f6 | 157 | } |
DiegoOstuni | 0:de13951f30f6 | 158 | |
DiegoOstuni | 0:de13951f30f6 | 159 | break; |
DiegoOstuni | 0:de13951f30f6 | 160 | |
DiegoOstuni | 0:de13951f30f6 | 161 | default : |
DiegoOstuni | 0:de13951f30f6 | 162 | ; |
DiegoOstuni | 0:de13951f30f6 | 163 | } |
DiegoOstuni | 0:de13951f30f6 | 164 | } |
DiegoOstuni | 0:de13951f30f6 | 165 | |
DiegoOstuni | 0:de13951f30f6 | 166 | } |
DiegoOstuni | 0:de13951f30f6 | 167 | |
DiegoOstuni | 0:de13951f30f6 | 168 | /** |
DiegoOstuni | 0:de13951f30f6 | 169 | * @brief This fonction reads the WifiToken and store data in a structure. |
DiegoOstuni | 0:de13951f30f6 | 170 | * @param pRecordStruct Pointer on the record structure. |
DiegoOstuni | 0:de13951f30f6 | 171 | * @param pWifiTokenStruct Pointer on the structure to fill. |
DiegoOstuni | 0:de13951f30f6 | 172 | * @retval NONE |
DiegoOstuni | 0:de13951f30f6 | 173 | */ |
DiegoOstuni | 0:de13951f30f6 | 174 | static void NDEF_Read_WifiToken ( struct sRecordInfo *pRecordStruct, sWifiTokenInfo *pWifiTokenStruct ) |
DiegoOstuni | 0:de13951f30f6 | 175 | { |
DiegoOstuni | 0:de13951f30f6 | 176 | uint8_t* pPayload; |
DiegoOstuni | 0:de13951f30f6 | 177 | uint32_t PayloadSize; |
DiegoOstuni | 0:de13951f30f6 | 178 | |
DiegoOstuni | 0:de13951f30f6 | 179 | PayloadSize = pRecordStruct->PayloadLength; |
DiegoOstuni | 0:de13951f30f6 | 180 | |
DiegoOstuni | 0:de13951f30f6 | 181 | /* Read record header */ |
DiegoOstuni | 0:de13951f30f6 | 182 | pPayload = (uint8_t*)(pRecordStruct->PayloadBufferAdd); |
DiegoOstuni | 0:de13951f30f6 | 183 | |
DiegoOstuni | 0:de13951f30f6 | 184 | if( pRecordStruct->NDEF_Type == URI_WIFITOKEN_TYPE) |
DiegoOstuni | 0:de13951f30f6 | 185 | NDEF_FillWifiTokenStruct(pPayload , PayloadSize, pWifiTokenStruct); |
DiegoOstuni | 0:de13951f30f6 | 186 | |
DiegoOstuni | 0:de13951f30f6 | 187 | } |
DiegoOstuni | 0:de13951f30f6 | 188 | |
DiegoOstuni | 0:de13951f30f6 | 189 | |
DiegoOstuni | 0:de13951f30f6 | 190 | /** |
DiegoOstuni | 0:de13951f30f6 | 191 | * @brief This fonction reads a NDEF record and retrieves a WifiToken information if any. |
DiegoOstuni | 0:de13951f30f6 | 192 | * @param pRecordStruct Pointer on the record structure. |
DiegoOstuni | 0:de13951f30f6 | 193 | * @param pWifiTokenStruct Pointer on a `sWifiTokenInfo` structure to fill with the WifiToken information. |
DiegoOstuni | 0:de13951f30f6 | 194 | * @retval NDEF_OK WifiToken information from NDEF have been retrieved. |
DiegoOstuni | 0:de13951f30f6 | 195 | * @retval NDEF_ERROR Not able to retrieve the WifiToken information. |
DiegoOstuni | 0:de13951f30f6 | 196 | */ |
DiegoOstuni | 0:de13951f30f6 | 197 | uint16_t NDEF_ReadWifiToken(struct sRecordInfo *pRecordStruct, sWifiTokenInfo *pWifiTokenStruct) |
DiegoOstuni | 0:de13951f30f6 | 198 | { |
DiegoOstuni | 0:de13951f30f6 | 199 | uint16_t status = NDEF_ERROR; |
DiegoOstuni | 0:de13951f30f6 | 200 | |
DiegoOstuni | 0:de13951f30f6 | 201 | if( pRecordStruct->NDEF_Type == URI_WIFITOKEN_TYPE ) |
DiegoOstuni | 0:de13951f30f6 | 202 | { |
DiegoOstuni | 0:de13951f30f6 | 203 | NDEF_Read_WifiToken(pRecordStruct, pWifiTokenStruct ); |
DiegoOstuni | 0:de13951f30f6 | 204 | status = NDEF_OK; |
DiegoOstuni | 0:de13951f30f6 | 205 | } |
DiegoOstuni | 0:de13951f30f6 | 206 | |
DiegoOstuni | 0:de13951f30f6 | 207 | return status; |
DiegoOstuni | 0:de13951f30f6 | 208 | } |
DiegoOstuni | 0:de13951f30f6 | 209 | |
DiegoOstuni | 0:de13951f30f6 | 210 | |
DiegoOstuni | 0:de13951f30f6 | 211 | /** |
DiegoOstuni | 0:de13951f30f6 | 212 | * @brief This fonction writes a NDEF message built with the WifiToken data given in the structure. |
DiegoOstuni | 0:de13951f30f6 | 213 | * @param pWifiTokenStruct Pointer on the structure containing the WifiToken information. |
DiegoOstuni | 0:de13951f30f6 | 214 | * @retval NDEF_OK The NDEF message has been successfully written. |
DiegoOstuni | 0:de13951f30f6 | 215 | * @retval NDEF_ERROR Not able to store the NDEF message inside the tag. |
DiegoOstuni | 0:de13951f30f6 | 216 | */ |
DiegoOstuni | 0:de13951f30f6 | 217 | uint16_t NDEF_WriteWifiToken ( sWifiTokenInfo *pWifiTokenStruct, I2C* mi2cChannel ) |
DiegoOstuni | 0:de13951f30f6 | 218 | { |
DiegoOstuni | 0:de13951f30f6 | 219 | uint16_t status = NDEF_ERROR; |
DiegoOstuni | 0:de13951f30f6 | 220 | uint8_t* pPayload,initStage = 0; |
DiegoOstuni | 0:de13951f30f6 | 221 | uint16_t DataSize; |
DiegoOstuni | 0:de13951f30f6 | 222 | uint32_t PayloadSize,SSIDSize,SSIDKeySize; |
DiegoOstuni | 0:de13951f30f6 | 223 | |
DiegoOstuni | 0:de13951f30f6 | 224 | if(pWifiTokenStruct->NetworkKey[0] == '\0') |
DiegoOstuni | 0:de13951f30f6 | 225 | { |
DiegoOstuni | 0:de13951f30f6 | 226 | /* Empty network key is not supported by Phones */ |
DiegoOstuni | 0:de13951f30f6 | 227 | strcpy(pWifiTokenStruct->NetworkKey, NDEF_WIFI_DEFAULT_NETWORK_KEY); |
DiegoOstuni | 0:de13951f30f6 | 228 | } |
DiegoOstuni | 0:de13951f30f6 | 229 | |
DiegoOstuni | 0:de13951f30f6 | 230 | uint8_t configToken1[] = {0x10,0x4A, /* Attribute ID : Version*/ |
DiegoOstuni | 0:de13951f30f6 | 231 | 0x00,0x01, /* Attribute ID Length*/ |
DiegoOstuni | 0:de13951f30f6 | 232 | 0x10, /* Version 1.0*/ |
DiegoOstuni | 0:de13951f30f6 | 233 | 0x10,0x0E, /* Attribute ID Credential*/ |
DiegoOstuni | 0:de13951f30f6 | 234 | 0x00,0x48, /* Attribute ID Length*/ |
DiegoOstuni | 0:de13951f30f6 | 235 | 0x10,0x26, /* Attribute ID : Network Index*/ |
DiegoOstuni | 0:de13951f30f6 | 236 | 0x00,0x01, /* Attribute Length*/ |
DiegoOstuni | 0:de13951f30f6 | 237 | 0x01, /* Index */ |
DiegoOstuni | 0:de13951f30f6 | 238 | 0x10,0x45, /* Attribute ID :SSID*/ |
DiegoOstuni | 0:de13951f30f6 | 239 | |
DiegoOstuni | 0:de13951f30f6 | 240 | }; |
DiegoOstuni | 0:de13951f30f6 | 241 | |
DiegoOstuni | 0:de13951f30f6 | 242 | /* Fill SSID length + SSID between configToken1 and configToken3*/ |
DiegoOstuni | 0:de13951f30f6 | 243 | |
DiegoOstuni | 0:de13951f30f6 | 244 | uint8_t configToken3[] = {0x10,0x03, /* Attribute ID :Authentication Type*/ |
DiegoOstuni | 0:de13951f30f6 | 245 | 0x00,0x02, /* Attribute Length*/ |
DiegoOstuni | 0:de13951f30f6 | 246 | 0x00,0x01, /* Attribute Type : Open*/ |
DiegoOstuni | 0:de13951f30f6 | 247 | 0x10,0x0F, /* Attribute ID : Encryption Type*/ |
DiegoOstuni | 0:de13951f30f6 | 248 | 0x00,0x02, /* Attribute Length*/ |
DiegoOstuni | 0:de13951f30f6 | 249 | 0x00,0x01, /* Encryption Type : None*/ |
DiegoOstuni | 0:de13951f30f6 | 250 | 0x10,0x27}; /* Attribute ID : Network Key */ |
DiegoOstuni | 0:de13951f30f6 | 251 | |
DiegoOstuni | 0:de13951f30f6 | 252 | |
DiegoOstuni | 0:de13951f30f6 | 253 | /*Fill SSID KEY Length and SSID Key between configToken3 and configToken5*/ |
DiegoOstuni | 0:de13951f30f6 | 254 | |
DiegoOstuni | 0:de13951f30f6 | 255 | uint8_t configToken5[] = {0x10,0x20, /* Attribute ID : MAC Address */ |
DiegoOstuni | 0:de13951f30f6 | 256 | 0x00,0x06, /* Attribute Length*/ |
DiegoOstuni | 0:de13951f30f6 | 257 | 0, /*MAC-ADDRESS*/ |
DiegoOstuni | 0:de13951f30f6 | 258 | 0, /*MAC-ADDRESS*/ |
DiegoOstuni | 0:de13951f30f6 | 259 | 0, /*MAC-ADDRESS*/ |
DiegoOstuni | 0:de13951f30f6 | 260 | 0, /*MAC-ADDRESS*/ |
DiegoOstuni | 0:de13951f30f6 | 261 | 0, /*MAC-ADDRESS*/ |
DiegoOstuni | 0:de13951f30f6 | 262 | 0, /*MAC-ADDRESS*/ |
DiegoOstuni | 0:de13951f30f6 | 263 | 0x10,0x49, /* Attribute ID : Vendor Extension */ |
DiegoOstuni | 0:de13951f30f6 | 264 | 0x00,0x06, /* Attribute Length*/ |
DiegoOstuni | 0:de13951f30f6 | 265 | 0x00,0x37,0x2A, /* Vendor ID:WFA*/ |
DiegoOstuni | 0:de13951f30f6 | 266 | 0x02, /* Subelement ID:Network Key Shareable*/ |
DiegoOstuni | 0:de13951f30f6 | 267 | 0x01, /* Subelement Length*/ |
DiegoOstuni | 0:de13951f30f6 | 268 | 0x01, /*Network Key Shareable : TRUE*/ |
DiegoOstuni | 0:de13951f30f6 | 269 | 0x10,0x49, /* Attribute ID : Vendor Extension */ |
DiegoOstuni | 0:de13951f30f6 | 270 | 0x00,0x06,/* Attribute Length*/ |
DiegoOstuni | 0:de13951f30f6 | 271 | 0x00,0x37,0x2A,/* Vendor ID:WFA*/ |
DiegoOstuni | 0:de13951f30f6 | 272 | 0x00, /* Subelement ID:Version2*/ |
DiegoOstuni | 0:de13951f30f6 | 273 | 0x01, /* Subelement Length:1*/ |
DiegoOstuni | 0:de13951f30f6 | 274 | 0x20 /* Version2*/ |
DiegoOstuni | 0:de13951f30f6 | 275 | }; |
DiegoOstuni | 0:de13951f30f6 | 276 | |
DiegoOstuni | 0:de13951f30f6 | 277 | |
DiegoOstuni | 0:de13951f30f6 | 278 | /* Set size of the tokens */ |
DiegoOstuni | 0:de13951f30f6 | 279 | const uint32_t CONFIG_TOKEN_1 = sizeof(configToken1); |
DiegoOstuni | 0:de13951f30f6 | 280 | const uint32_t CONFIG_TOKEN_3 = sizeof(configToken3); |
DiegoOstuni | 0:de13951f30f6 | 281 | const uint32_t CONFIG_TOKEN_5 = sizeof(configToken5); |
DiegoOstuni | 0:de13951f30f6 | 282 | |
DiegoOstuni | 0:de13951f30f6 | 283 | /* Update Token3 for Autentication & Encryption Types, their default value is coded in token3 */ |
DiegoOstuni | 0:de13951f30f6 | 284 | configToken3[CONFIG_TOKEN_3_AUTHENTICATION_TYPE_INDEX] = pWifiTokenStruct->AuthenticationType & 0xFF; |
DiegoOstuni | 0:de13951f30f6 | 285 | configToken3[CONFIG_TOKEN_3_ENCRYPTION_TYPE_INDEX] = pWifiTokenStruct->EncryptionType & 0xFF; |
DiegoOstuni | 0:de13951f30f6 | 286 | |
DiegoOstuni | 0:de13951f30f6 | 287 | /* fill Wifi record header */ |
DiegoOstuni | 0:de13951f30f6 | 288 | NDEF_Buffer[FIRST_RECORD_OFFSET] = 0xD2; /* Record Flag */ |
DiegoOstuni | 0:de13951f30f6 | 289 | NDEF_Buffer[FIRST_RECORD_OFFSET+1] = WIFITOKEN_TYPE_STRING_LENGTH; |
DiegoOstuni | 0:de13951f30f6 | 290 | NDEF_Buffer[FIRST_RECORD_OFFSET+2] = 76; /* needs to be autocalculated - done at the end */ |
DiegoOstuni | 0:de13951f30f6 | 291 | |
DiegoOstuni | 0:de13951f30f6 | 292 | memcpy(&NDEF_Buffer[FIRST_RECORD_OFFSET+3], WIFITOKEN_TYPE_STRING, WIFITOKEN_TYPE_STRING_LENGTH); |
DiegoOstuni | 0:de13951f30f6 | 293 | |
DiegoOstuni | 0:de13951f30f6 | 294 | pPayload = &NDEF_Buffer[FIRST_RECORD_OFFSET+3+WIFITOKEN_TYPE_STRING_LENGTH]; |
DiegoOstuni | 0:de13951f30f6 | 295 | PayloadSize = 0; |
DiegoOstuni | 0:de13951f30f6 | 296 | |
DiegoOstuni | 0:de13951f30f6 | 297 | /* Compute credential length */ |
DiegoOstuni | 0:de13951f30f6 | 298 | |
DiegoOstuni | 0:de13951f30f6 | 299 | uint16_t credential_length = 5 + // Network index |
DiegoOstuni | 0:de13951f30f6 | 300 | 4 + // SSID type + length |
DiegoOstuni | 0:de13951f30f6 | 301 | strlen(pWifiTokenStruct->NetworkSSID) + // SSID |
DiegoOstuni | 0:de13951f30f6 | 302 | CONFIG_TOKEN_3 + |
DiegoOstuni | 0:de13951f30f6 | 303 | 2 + // Network key length |
DiegoOstuni | 0:de13951f30f6 | 304 | strlen(pWifiTokenStruct->NetworkKey) + // Network KEY |
DiegoOstuni | 0:de13951f30f6 | 305 | CONFIG_TOKEN_5; |
DiegoOstuni | 0:de13951f30f6 | 306 | |
DiegoOstuni | 0:de13951f30f6 | 307 | /* update credential length */ |
DiegoOstuni | 0:de13951f30f6 | 308 | configToken1[CONFIG_TOKEN_1_CREDENTIAL_LENGTH_INDEX + 1] = credential_length & 0xff; |
DiegoOstuni | 0:de13951f30f6 | 309 | configToken1[CONFIG_TOKEN_1_CREDENTIAL_LENGTH_INDEX] = credential_length>>8; |
DiegoOstuni | 0:de13951f30f6 | 310 | |
DiegoOstuni | 0:de13951f30f6 | 311 | |
DiegoOstuni | 0:de13951f30f6 | 312 | for(initStage=0;initStage<CONFIG_TOKEN_1;initStage++) |
DiegoOstuni | 0:de13951f30f6 | 313 | { |
DiegoOstuni | 0:de13951f30f6 | 314 | *pPayload =configToken1[initStage]; |
DiegoOstuni | 0:de13951f30f6 | 315 | pPayload++; |
DiegoOstuni | 0:de13951f30f6 | 316 | } |
DiegoOstuni | 0:de13951f30f6 | 317 | |
DiegoOstuni | 0:de13951f30f6 | 318 | /*Fill SSID length and SSID value*/ |
DiegoOstuni | 0:de13951f30f6 | 319 | SSIDSize = strlen(pWifiTokenStruct->NetworkSSID); |
DiegoOstuni | 0:de13951f30f6 | 320 | *pPayload = 0x00; pPayload++; |
DiegoOstuni | 0:de13951f30f6 | 321 | *pPayload = SSIDSize & 0x000000FF; pPayload++; |
DiegoOstuni | 0:de13951f30f6 | 322 | |
DiegoOstuni | 0:de13951f30f6 | 323 | strcpy((char*)pPayload,pWifiTokenStruct->NetworkSSID); |
DiegoOstuni | 0:de13951f30f6 | 324 | pPayload = pPayload + strlen(pWifiTokenStruct->NetworkSSID); |
DiegoOstuni | 0:de13951f30f6 | 325 | |
DiegoOstuni | 0:de13951f30f6 | 326 | for(initStage=0;initStage<CONFIG_TOKEN_3;initStage++) |
DiegoOstuni | 0:de13951f30f6 | 327 | { |
DiegoOstuni | 0:de13951f30f6 | 328 | *pPayload =configToken3[initStage]; |
DiegoOstuni | 0:de13951f30f6 | 329 | pPayload++; |
DiegoOstuni | 0:de13951f30f6 | 330 | } |
DiegoOstuni | 0:de13951f30f6 | 331 | |
DiegoOstuni | 0:de13951f30f6 | 332 | /* Fill the SSIDKey length and SSIDKey value */ |
DiegoOstuni | 0:de13951f30f6 | 333 | SSIDKeySize = strlen(pWifiTokenStruct->NetworkKey); |
DiegoOstuni | 0:de13951f30f6 | 334 | *pPayload = 0x00; pPayload++; |
DiegoOstuni | 0:de13951f30f6 | 335 | *pPayload = SSIDKeySize & 0x000000FF; pPayload++; |
DiegoOstuni | 0:de13951f30f6 | 336 | |
DiegoOstuni | 0:de13951f30f6 | 337 | memcpy((char*)pPayload,pWifiTokenStruct->NetworkKey,SSIDKeySize); |
DiegoOstuni | 0:de13951f30f6 | 338 | pPayload = pPayload + SSIDKeySize; |
DiegoOstuni | 0:de13951f30f6 | 339 | |
DiegoOstuni | 0:de13951f30f6 | 340 | for(initStage=0;initStage<CONFIG_TOKEN_5;initStage++) |
DiegoOstuni | 0:de13951f30f6 | 341 | { |
DiegoOstuni | 0:de13951f30f6 | 342 | *pPayload =configToken5[initStage]; |
DiegoOstuni | 0:de13951f30f6 | 343 | pPayload++; |
DiegoOstuni | 0:de13951f30f6 | 344 | } |
DiegoOstuni | 0:de13951f30f6 | 345 | |
DiegoOstuni | 0:de13951f30f6 | 346 | PayloadSize += CONFIG_TOKEN_1 + CONFIG_TOKEN_3 + CONFIG_TOKEN_5 + SSIDSize + SSIDKeySize + 4 ; // +4 is for SSID & Key length fields |
DiegoOstuni | 0:de13951f30f6 | 347 | |
DiegoOstuni | 0:de13951f30f6 | 348 | NDEF_Buffer[FIRST_RECORD_OFFSET+2] = (PayloadSize & 0x000000FF); |
DiegoOstuni | 0:de13951f30f6 | 349 | |
DiegoOstuni | 0:de13951f30f6 | 350 | DataSize = PayloadSize + 3 + WIFITOKEN_TYPE_STRING_LENGTH; |
DiegoOstuni | 0:de13951f30f6 | 351 | |
DiegoOstuni | 0:de13951f30f6 | 352 | /* Write NDEF */ |
DiegoOstuni | 0:de13951f30f6 | 353 | status = NDEF_WriteNDEF(DataSize , NDEF_Buffer, mi2cChannel); |
DiegoOstuni | 0:de13951f30f6 | 354 | |
DiegoOstuni | 0:de13951f30f6 | 355 | return status; |
DiegoOstuni | 0:de13951f30f6 | 356 | } |
DiegoOstuni | 0:de13951f30f6 | 357 | |
DiegoOstuni | 0:de13951f30f6 | 358 | |
DiegoOstuni | 0:de13951f30f6 | 359 | /** |
DiegoOstuni | 0:de13951f30f6 | 360 | * @} |
DiegoOstuni | 0:de13951f30f6 | 361 | */ |
DiegoOstuni | 0:de13951f30f6 | 362 | |
DiegoOstuni | 0:de13951f30f6 | 363 | |
DiegoOstuni | 0:de13951f30f6 | 364 | |
DiegoOstuni | 0:de13951f30f6 | 365 | /******************* (C) COPYRIGHT 2016 STMicroelectronics *****END OF FILE****/ |
DiegoOstuni | 0:de13951f30f6 | 366 | |
DiegoOstuni | 0:de13951f30f6 | 367 |