diff --git a/ChaletLora.X/Source/WiFiCtrl.c b/ChaletLora.X/Source/WiFiCtrl.c index 61e1211..33086b1 100644 --- a/ChaletLora.X/Source/WiFiCtrl.c +++ b/ChaletLora.X/Source/WiFiCtrl.c @@ -1,6 +1,6 @@ #include "WiFiCtrl.h" #include "string.h" -#include "driver/source/nmasic.h" +#include "driver/include/nmasic.h" #include //#include "socket/include/socket.h" #include "define.h" @@ -155,7 +155,7 @@ static void socket_cb(SOCKET sock, uint8 u8Msg, void *pvMsg) SendTerminalData("Bienvenue au chalet!\nLe chalet parle en anglais comme Mr. Pepin\nIf you need help... type help\n\n",strlen("Bienvenue au chalet!\nLe chalet parle en anglais comme Mr. Pepin\nIf you need help... type help\n\n")); // SendSyslogData("Terminal client connected\n",strlen("Terminal client connected\n")); printf("Terminal client connected\n"); - // m2m_periph_gpio_set_val(M2M_PERIPH_GPIO4,1); + m2m_periph_gpio_set_val(M2M_PERIPH_GPIO4,1); } else if(sock == SyslogServerSocket) { @@ -236,7 +236,7 @@ static void socket_cb(SOCKET sock, uint8 u8Msg, void *pvMsg) TerminalSocket = -1; // SendSyslogData("Terminal client disconnected\n",strlen("Terminal client disconnected\n")); printf("Terminal client disconnected\n"); - // m2m_periph_gpio_set_val(M2M_PERIPH_GPIO4,0); + m2m_periph_gpio_set_val(M2M_PERIPH_GPIO4,0); } else if(sock == SyslogSocket) { @@ -277,7 +277,7 @@ static void set_dev_name_to_mac(uint8 *name, uint8 *mac_addr) /* Name must be in the format WINC1500_00:00 */ uint16 len; - len = m2m_strlen(name); +// len = m2m_strlen(name); if (len >= 5) { name[len - 1] = MAIN_HEX2ASCII((mac_addr[5] >> 0) & 0x0f); name[len - 2] = MAIN_HEX2ASCII((mac_addr[5] >> 4) & 0x0f); @@ -304,7 +304,7 @@ static void wifi_cb(uint8 u8MsgType, void *pvMsg) printf("Wi-Fi connected\r\n"); #ifndef USE_STATIC_IP - m2m_wifi_request_dhcp_client(); +// m2m_wifi_request_dhcp_client(); #else m2m_wifi_set_static_ip(&mModuleIPConfig); gbConnectedWifi = true; @@ -326,7 +326,7 @@ static void wifi_cb(uint8 u8MsgType, void *pvMsg) case M2M_WIFI_REQ_DHCP_CONF: { - mCurIPAddress = *(uin32*)pvMsg; +// mCurIPAddress = *(uin32*)pvMsg; // unsigned char ip1,ip2,ip3,ip4; // ip1 = IPV4_BYTE(pu8IPAddress,0); @@ -432,11 +432,11 @@ int InitWiFi() mModuleIPConfig.u32SubnetMask = IP_TO_U32(SUBNET_MASK_1,SUBNET_MASK_2,SUBNET_MASK_3,SUBNET_MASK_4); /* Initialize the BSP. */ - nm_bsp_init(); +// nm_bsp_init(); /* Initialize Wi-Fi parameters structure. */ memset((uint8_t *)¶m, 0, sizeof(tstrWifiInitParam)); /* Initialize Wi-Fi driver with data and status callbacks. */ - param.pfAppWifiCb = wifi_cb; + param.pfAppWifiCb = (void*) &wifi_cb; ret = m2m_wifi_init(¶m); if (M2M_SUCCESS != ret) { @@ -466,7 +466,7 @@ int InitWiFi() //Use the MAC to define the SSID of the module set_dev_name_to_mac((uint8 *)gstrM2MAPConfig.au8SSID, gau8MacAddr); - m2m_wifi_set_device_name((uint8 *)gacDeviceName, (uint8)m2m_strlen((uint8 *)gacDeviceName)); +// m2m_wifi_set_device_name((uint8 *)gacDeviceName, (uint8)m2m_strlen((uint8 *)gacDeviceName)); #ifdef USE_STATIC_IP //Use static ip --> disable dhcp client before connecting @@ -581,7 +581,7 @@ void TickWiFi() // if(IsTimerExpired(WIFI_TICK_TIMER)) // { - m2m_wifi_handle_events(NULL); + m2m_wifi_handle_events(); // TimerStart(WIFI_TICK_TIMER,1); // } diff --git a/ChaletLora.X/Source/WiFiCtrl.h b/ChaletLora.X/Source/WiFiCtrl.h index 3a124f8..354b80b 100644 --- a/ChaletLora.X/Source/WiFiCtrl.h +++ b/ChaletLora.X/Source/WiFiCtrl.h @@ -11,6 +11,67 @@ #include "driver/include/m2m_wifi.h" #include "socket/include/socket.h" +/*! +* Used for code portability. +*/ + +/*! + * @typedef void (*tpfNmBspIsr) (void); + * @brief Pointer to function.\n + * Used as a data type of ISR function registered by \ref nm_bsp_register_isr + * @return None + */ +typedef void (*tpfNmBspIsr)(void); + + /*! + * @ingroup DataTypes + * @typedef unsigned char uint8; + * @brief Range of values between 0 to 255 + */ +typedef unsigned char uint8; + + /*! + * @ingroup DataTypes + * @typedef unsigned short uint16; + * @brief Range of values between 0 to 65535 + */ +typedef unsigned short uint16; + + /*! + * @ingroup Data Types + * @typedef unsigned long uint32; + * @brief Range of values between 0 to 4294967295 + */ +typedef unsigned long uint32; + + + /*! + * @ingroup Data Types + * @typedef signed char sint8; + * @brief Range of values between -128 to 127 + */ +typedef signed char sint8; + + /*! + * @ingroup DataTypes + * @typedef signed short sint16; + * @brief Range of values between -32768 to 32767 + */ +typedef signed short sint16; + + /*! + * @ingroup DataTypes + * @typedef signed long sint32; + * @brief Range of values between -2147483648 to 2147483647 + */ + +typedef signed long sint32; + //@} + + + + + int InitWiFi(); void TickWiFi(); @@ -188,7 +249,7 @@ static tstrM2MAPConfig gstrM2MAPConfig = {MAIN_M2M_SSID, MAIN_M2M_AP_SSID_MODE, MAIN_M2M_DHCP_SERVER_IP}; -static CONST char gacHttpProvDomainName[] = MAIN_HTTP_PROV_SERVER_DOMAIN_NAME; +static const char gacHttpProvDomainName[] = MAIN_HTTP_PROV_SERVER_DOMAIN_NAME; static uint8 gau8MacAddr[] = MAIN_MAC_ADDRESS; static sint8 gacDeviceName[] = MAIN_M2M_SSID; diff --git a/ChaletLora.X/Source/main.c b/ChaletLora.X/Source/main.c index edbcb9c..a2afc7d 100644 --- a/ChaletLora.X/Source/main.c +++ b/ChaletLora.X/Source/main.c @@ -14,6 +14,9 @@ Change description. */ +//JFM WINC TODO + + #include "define.h" diff --git a/ChaletLora.X/Source/winc3400_142/bsp/include/nm_bsp.h b/ChaletLora.X/Source/winc3400_142/bsp/include/nm_bsp.h new file mode 100644 index 0000000..a22ad5c --- /dev/null +++ b/ChaletLora.X/Source/winc3400_142/bsp/include/nm_bsp.h @@ -0,0 +1,85 @@ +/******************************************************************************* + File Name: + nm_common.h + + Summary: + This module contains WINC3400 BSP APIs declarations. + + Description: + This module contains WINC3400 BSP APIs declarations. + *******************************************************************************/ + +//DOM-IGNORE-BEGIN +/******************************************************************************* +* Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries. +* +* Subject to your compliance with these terms, you may use Microchip software +* and any derivatives exclusively with Microchip products. It is your +* responsibility to comply with third party license terms applicable to your +* use of third party software (including open source software) that may +* accompany Microchip software. +* +* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER +* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED +* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A +* PARTICULAR PURPOSE. +* +* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, +* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND +* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS +* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE +* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN +* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, +* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. +*******************************************************************************/ +//DOM-IGNORE-END + +/** @defgroup nm_bsp BSP + @brief + Description of the BSP (Board Support Package) module. + @{ + @defgroup DataT Data Types + @defgroup BSPDefine Defines + @defgroup BSPAPI Functions + @brief + Lists the available BSP (Board Support Package) APIs. + @} + */ + +/**@addtogroup BSPDefine + @{ + */ +#ifndef _NM_BSP_H_ +#define _NM_BSP_H_ + +#define BSP_MIN(x,y) ((x)>(y)?(y):(x)) +/*!< + * Computes the minimum value between \b x and \b y. + */ +/**@}*/ //BSPDefine + +/** + * @addtogroup BSPDefine + * @{ + */ + +#ifdef _NM_BSP_BIG_END +/*! Switch endianness of 32bit word (In the case that Host is BE) */ +#define NM_BSP_B_L_32(x) \ +((((x) & 0x000000FF) << 24) + \ +(((x) & 0x0000FF00) << 8) + \ +(((x) & 0x00FF0000) >> 8) + \ +(((x) & 0xFF000000) >> 24)) +/*! Switch endianness of 16bit word (In the case that Host is BE) */ +#define NM_BSP_B_L_16(x) \ +((((x) & 0x00FF) << 8) + \ +(((x) & 0xFF00) >> 8)) +#else +/*! Retain endianness of 32bit word (In the case that Host is LE) */ +#define NM_BSP_B_L_32(x) (x) +/*! Retain endianness of 16bit word (In the case that Host is LE) */ +#define NM_BSP_B_L_16(x) (x) +#endif +/**@}*/ //BSPDefine + +#endif /*_NM_BSP_H_*/ diff --git a/ChaletLora.X/Source/winc3400_142/common/include/nm_common.h b/ChaletLora.X/Source/winc3400_142/common/include/nm_common.h new file mode 100644 index 0000000..fd420dd --- /dev/null +++ b/ChaletLora.X/Source/winc3400_142/common/include/nm_common.h @@ -0,0 +1,172 @@ +/******************************************************************************* + File Name: + nm_common.h + + Summary: + This module contains common APIs declarations. + + Description: + This module contains common APIs declarations. + *******************************************************************************/ + +//DOM-IGNORE-BEGIN +/******************************************************************************* +* Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries. +* +* Subject to your compliance with these terms, you may use Microchip software +* and any derivatives exclusively with Microchip products. It is your +* responsibility to comply with third party license terms applicable to your +* use of third party software (including open source software) that may +* accompany Microchip software. +* +* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER +* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED +* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A +* PARTICULAR PURPOSE. +* +* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, +* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND +* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS +* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE +* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN +* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, +* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. +*******************************************************************************/ +//DOM-IGNORE-END + +/** @defgroup COMMON Common + @{ + @defgroup COMMONDEF Defines + @defgroup COMMONAPI Functions + @} + */ + +#ifndef _NM_COMMON_H_ +#define _NM_COMMON_H_ + +#include +#include "define.h" +#include "nm_bsp.h" +#include "nm_debug.h" + +/**@addtogroup COMMONDEF + * @{ + */ + +/*states*/ +#define M2M_SUCCESS ((int8_t)0) +#define M2M_ERR_SEND ((int8_t)-1) +#define M2M_ERR_RCV ((int8_t)-2) +#define M2M_ERR_MEM_ALLOC ((int8_t)-3) +#define M2M_ERR_TIME_OUT ((int8_t)-4) +#define M2M_ERR_INIT ((int8_t)-5) +#define M2M_ERR_BUS_FAIL ((int8_t)-6) +#define M2M_NOT_YET ((int8_t)-7) +#define M2M_ERR_FIRMWARE ((int8_t)-8) +#define M2M_SPI_FAIL ((int8_t)-9) +#define M2M_ERR_FIRMWARE_bURN ((int8_t)-10) +#define M2M_ACK ((int8_t)-11) +#define M2M_ERR_FAIL ((int8_t)-12) +#define M2M_ERR_FW_VER_MISMATCH ((int8_t)-13) +#define M2M_ERR_SCAN_IN_PROGRESS ((int8_t)-14) +#define M2M_ERR_INVALID_ARG ((int8_t)-15) + +#define NBIT31 (0x80000000) +#define NBIT30 (0x40000000) +#define NBIT29 (0x20000000) +#define NBIT28 (0x10000000) +#define NBIT27 (0x08000000) +#define NBIT26 (0x04000000) +#define NBIT25 (0x02000000) +#define NBIT24 (0x01000000) +#define NBIT23 (0x00800000) +#define NBIT22 (0x00400000) +#define NBIT21 (0x00200000) +#define NBIT20 (0x00100000) +#define NBIT19 (0x00080000) +#define NBIT18 (0x00040000) +#define NBIT17 (0x00020000) +#define NBIT16 (0x00010000) +#define NBIT15 (0x00008000) +#define NBIT14 (0x00004000) +#define NBIT13 (0x00002000) +#define NBIT12 (0x00001000) +#define NBIT11 (0x00000800) +#define NBIT10 (0x00000400) +#define NBIT9 (0x00000200) +#define NBIT8 (0x00000100) +#define NBIT7 (0x00000080) +#define NBIT6 (0x00000040) +#define NBIT5 (0x00000020) +#define NBIT4 (0x00000010) +#define NBIT3 (0x00000008) +#define NBIT2 (0x00000004) +#define NBIT1 (0x00000002) +#define NBIT0 (0x00000001) + +#ifndef BIG_ENDIAN +/*! Most significant byte of 32bit word (LE) */ +#define BYTE_0(word) ((uint8_t)(((word) >> 0) & 0x000000FFUL)) +/*! Second most significant byte of 32bit word (LE) */ +#define BYTE_1(word) ((uint8_t)(((word) >> 8) & 0x000000FFUL)) +/*! Third most significant byte of 32bit word (LE) */ +#define BYTE_2(word) ((uint8_t)(((word) >> 16) & 0x000000FFUL)) +/*! Least significant byte of 32bit word (LE) */ +#define BYTE_3(word) ((uint8_t)(((word) >> 24) & 0x000000FFUL)) +#else +/*! Most significant byte of 32bit word (BE) */ +#define BYTE_0(word) ((uint8_t)(((word) >> 24) & 0x000000FFUL)) +/*! Second most significant byte of 32bit word (BE) */ +#define BYTE_1(word) ((uint8_t)(((word) >> 16) & 0x000000FFUL)) +/*! Third most significant byte of 32bit word (BE) */ +#define BYTE_2(word) ((uint8_t)(((word) >> 8) & 0x000000FFUL)) +/*! Least significant byte of 32bit word (BE) */ +#define BYTE_3(word) ((uint8_t)(((word) >> 0) & 0x000000FFUL)) +#endif +/**@}*/ + +#ifdef __cplusplus +extern "C" { +#endif + +/*! + * @ingroup COMMONAPI + * @fn int8_t hexstr_2_bytes(uint8_t *pu8Out, uint8_t *pu8In, uint8_t u8SizeOut); + * @brief Converts a string of hex characters to bytes. + * @param[out] pu8Out + * Output buffer (eg {0x11, 0x12, 0x13,...}) + * @param[in] pu8In + * Input buffer (eg {0x31, 0x31, 0x31, 0x32, 0x31, 0x33, ...}) + * @param[in] u8SizeOut + * Length of output buffer (should be half of the length of the input buffer). + * @return @ref M2M_SUCCESS if successful, M2M_ERR_INVALID_ARG otherwise (eg unrecognised hexchar in input). + */ +int8_t hexstr_2_bytes(uint8_t *pu8Out, uint8_t *pu8In, uint8_t u8SizeOut); + +/*! + * @fn void nm_sleep(uint32_t u32TimeMsec); + * @brief Used to put the host to sleep for the specified duration (in milliseconds). + * Forcing the host to sleep for extended period may lead to host not being able to respond + * to WINC board events. It is important to be considerate while choosing the sleep period. + * @param [in] u32TimeMsec + * Time unit in milliseconds. + * @pre Initialize @ref nm_bsp_init first. + * @note Implementation of this function is host dependent. + * @warning Maximum value must nor exceed 4294967295 milliseconds which is equal to 4294967.295 seconds. + * @see nm_bsp_init + * @return None + */ +void nm_sleep(uint32_t u32TimeMsec); + +/*! + * @ingroup COMMONAPI + * @fn nm_reset + * @brief Reset NMC3400 SoC by setting CHIP_EN and RESET_N signals low, + * CHIP_EN high then RESET_N high + */ +void nm_reset(void); +/**@}*/ +#ifdef __cplusplus +} +#endif +#endif /*_NM_COMMON_H_*/ diff --git a/ChaletLora.X/Source/winc3400_142/common/include/nm_debug.h b/ChaletLora.X/Source/winc3400_142/common/include/nm_debug.h new file mode 100644 index 0000000..1a5ee88 --- /dev/null +++ b/ChaletLora.X/Source/winc3400_142/common/include/nm_debug.h @@ -0,0 +1,60 @@ +/******************************************************************************* + File Name: + nm_debug.h + + Summary: + This module contains debug API declarations. + + Description: + This module contains debug API declarations. + *******************************************************************************/ + +//DOM-IGNORE-BEGIN +/******************************************************************************* +* Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries. +* +* Subject to your compliance with these terms, you may use Microchip software +* and any derivatives exclusively with Microchip products. It is your +* responsibility to comply with third party license terms applicable to your +* use of third party software (including open source software) that may +* accompany Microchip software. +* +* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER +* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED +* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A +* PARTICULAR PURPOSE. +* +* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, +* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND +* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS +* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE +* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN +* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, +* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. +*******************************************************************************/ + +#ifndef _NM_DEBUG_H_ +#define _NM_DEBUG_H_ + + +//#include "wdrv_winc_debug.h" //JFM replaced include by direct definitions below +#define WDRV_DBG_VERBOSE_PRINT(...) +#define WDRV_DBG_TRACE_PRINT(...) +#define WDRV_DBG_INFORM_PRINT(...) +#define WDRV_DBG_ERROR_PRINT(...) + +/** + * @defgroup DebugDefines DebugDefines + * @ingroup WlanDefines + */ + +/**@{*/ + +#define M2M_ERR(...) WDRV_DBG_ERROR_PRINT(__VA_ARGS__) +#define M2M_INFO(...) WDRV_DBG_INFORM_PRINT(__VA_ARGS__) +#define M2M_DBG(...) WDRV_DBG_VERBOSE_PRINT(__VA_ARGS__) +#define M2M_PRINT(...) WDRV_DBG_VERBOSE_PRINT(__VA_ARGS__) + +/**@}*/ + +#endif /* _NM_DEBUG_H_ */ \ No newline at end of file diff --git a/ChaletLora.X/Source/winc3400_142/common/source/nm_common.c b/ChaletLora.X/Source/winc3400_142/common/source/nm_common.c new file mode 100644 index 0000000..a09dd72 --- /dev/null +++ b/ChaletLora.X/Source/winc3400_142/common/source/nm_common.c @@ -0,0 +1,100 @@ +/******************************************************************************* + File Name: + nm_common.c + + Summary: + This module contains common APIs implementations. + + Description: + This module contains common APIs implementations. + *******************************************************************************/ + +//DOM-IGNORE-BEGIN +/******************************************************************************* +* Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries. +* +* Subject to your compliance with these terms, you may use Microchip software +* and any derivatives exclusively with Microchip products. It is your +* responsibility to comply with third party license terms applicable to your +* use of third party software (including open source software) that may +* accompany Microchip software. +* +* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER +* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED +* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A +* PARTICULAR PURPOSE. +* +* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, +* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND +* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS +* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE +* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN +* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, +* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. +*******************************************************************************/ + +#include "nm_common.h" +//#include "wdrv_winc_common.h" +//#include "wdrv_winc_gpio.h" + +/*! + * @fn nm_sleep + * @brief Sleep in units of mSec + * @param[IN] u32TimeMsec + * Time in milliseconds + */ +void nm_sleep(uint32_t u32TimeMsec) +{ + //JFM WINC TODO + //WDRV_MSDelay(u32TimeMsec); +} + +/*! + * @fn nm_reset + * @brief Reset NMC3400 SoC by setting CHIP_EN and RESET_N signals low, + * CHIP_EN high then RESET_N high + */ +void nm_reset(void) +{ + //JFM WINC TODO + //WDRV_WINC_GPIOChipEnableDeassert(); + //WDRV_WINC_GPIOResetAssert(); + nm_sleep(100); + //WDRV_WINC_GPIOChipEnableAssert(); + nm_sleep(10); + //WDRV_WINC_GPIOResetDeassert(); + nm_sleep(10); +} + +/* Convert hexchar to value 0-15 */ +static uint8_t hexchar_2_val(uint8_t ch) +{ + ch -= 0x30; + if (ch <= 9) + return ch; + ch |= 0x20; + ch -= 0x31; + if (ch <= 5) + return ch + 10; + return 0xFF; +} + +/* Convert hexstring to bytes */ +int8_t hexstr_2_bytes(uint8_t *pu8Out, uint8_t *pu8In, uint8_t u8SizeOut) +{ + while(u8SizeOut--) + { + uint8_t u8Out = hexchar_2_val(*pu8In++); + if (u8Out > 0xF) + return M2M_ERR_INVALID_ARG; + *pu8Out = u8Out * 0x10; + u8Out = hexchar_2_val(*pu8In++); + if (u8Out > 0xF) + return M2M_ERR_INVALID_ARG; + *pu8Out += u8Out; + pu8Out++; + } + return M2M_SUCCESS; +} + +//DOM-IGNORE-END diff --git a/ChaletLora.X/Source/winc3400_142/driver/include/ecc_types.h b/ChaletLora.X/Source/winc3400_142/driver/include/ecc_types.h new file mode 100644 index 0000000..51d4065 --- /dev/null +++ b/ChaletLora.X/Source/winc3400_142/driver/include/ecc_types.h @@ -0,0 +1,300 @@ +/******************************************************************************* + File Name: + ecc_types.h + + Summary: + Elliptic Curve Cryptography Module Interface. + + Description: + Elliptic Curve Cryptography Module Interface. + *******************************************************************************/ + +//DOM-IGNORE-BEGIN +/******************************************************************************* +* Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries. +* +* Subject to your compliance with these terms, you may use Microchip software +* and any derivatives exclusively with Microchip products. It is your +* responsibility to comply with third party license terms applicable to your +* use of third party software (including open source software) that may +* accompany Microchip software. +* +* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER +* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED +* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A +* PARTICULAR PURPOSE. +* +* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, +* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND +* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS +* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE +* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN +* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, +* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. +*******************************************************************************/ + +#ifndef __ECC_TYPES_H__ +#define __ECC_TYPES_H__ + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +INCLUDES +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + +#include "m2m_types.h" + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +MACROS +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + + + +#define ECC_LARGEST_CURVE_SIZE (32) +/*!< + The size of the the largest supported EC. For now, assuming + the 256-bit EC is the largest supported curve type. +*/ + + +#define ECC_POINT_MAX_SIZE ECC_LARGEST_CURVE_SIZE +/*!< + Maximum size of one coordinate of an EC point. +*/ + + +#define ECC_POINT_MAX_SIZE_WORDS (ECC_POINT_MAX_SIZE / 4) +/*!< + SIZE in 32-bit words. +*/ + + +#define ECC_NUM_SUPP_CURVES ((sizeof(gastrECCSuppList)) / (sizeof(tstrEllipticCurve))) +/*!< +*/ + + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +DATA TYPES +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + +/**@addtogroup SSLEnums + * @{ + */ +/*! +@enum \ + tenuEcNamedCurve + +@brief EC Named Curves + + Defines a list of supported ECC named curves. +*/ +typedef enum EcNamedCurve{ + EC_SECP192R1 = 19, + /*!< + It is defined by NIST as P192 and by the SEC Group as secp192r1. + */ + EC_SECP256R1 = 23, + /*!< + It is defined by NIST as P256 and by the SEC Group as secp256r1. + */ + EC_SECP384R1 = 24, + /*!< + It is defined by NIST as P384 and by the SEC Group as secp384r1. + */ + EC_SECP521R1 = 25, + /*!< + It is defined by NIST as P521 and by the SEC Group as secp521r1. + */ + EC_UNKNOWN = 255 +}tenuEcNamedCurve; + + +/*! +@struct \ + tstrECPoint + +@brief Elliptic Curve point representation +*/ +typedef struct EcPoint{ + uint8_t X[ECC_POINT_MAX_SIZE]; + /*!< + The X-coordinate of the ec point. + */ + uint8_t Y[ECC_POINT_MAX_SIZE]; + /*!< + The Y-coordinate of the ec point. + */ + uint16_t u16Size; + /*!< + Point size in bytes (for each of the coordinates). + */ + uint16_t u16PrivKeyID; + /*!< + ID for the corresponding private key. + */ +}tstrECPoint; + + +/*! +@struct \ + tstrECDomainParam + +@brief ECC Curve Domain Parameters + + The structure defines the ECC domain parameters for curves defined over prime finite fields. +*/ +typedef struct EcDomainParam{ + uint32_t p[ECC_POINT_MAX_SIZE_WORDS]; + uint32_t a[ECC_POINT_MAX_SIZE_WORDS]; + uint32_t b[ECC_POINT_MAX_SIZE_WORDS]; + tstrECPoint G; +}tstrECDomainParam; + + +/*! +@struct \ + tstrEllipticCurve + +@brief + Definition of an elliptic curve +*/ +typedef struct{ + tenuEcNamedCurve enuType; + tstrECDomainParam strParam; +}tstrEllipticCurve; + +/*! +@enum \ + tenuEccREQ + +@brief ECC operations that may be requested by WINC. + + These are passed in the u16REQ field of @ref tstrEccReqInfo, which is used in both the request + from the WINC and the response to the WINC. +*/ +typedef enum{ + ECC_REQ_NONE, + /*!< */ + ECC_REQ_CLIENT_ECDH, + /*!< + Derive the shared secret from ECDHE key exchange as client. + */ + ECC_REQ_SERVER_ECDH, + /*!< + Derive the shared secret from ECDHE key exchange as server. + */ + ECC_REQ_GEN_KEY, + /*!< + Generate a key pair to be used in ECDHE key exchange as server. + */ + ECC_REQ_SIGN_GEN, + /*!< + Generate the signature for a given curve and value. The value needs to be retrieved via + @ref m2m_ssl_retrieve_hash. + */ + ECC_REQ_SIGN_VERIFY + /*!< + Verify a list of signatures. Each set of value/signature/key/curve information needs to be + retrieved via @ref m2m_ssl_retrieve_next_for_verifying. + */ +}tenuEccREQ; + +/*! +@struct \ + tstrEcdhReqInfo + +@brief Information relating to operations of type @ref ECC_REQ_CLIENT_ECDH, @ref ECC_REQ_GEN_KEY + and @ref ECC_REQ_SERVER_ECDH. +*/ +typedef struct{ + tstrECPoint strPubKey; + /*!< + Public key. Usage depends on type of operation. + */ + uint8_t au8Key[ECC_POINT_MAX_SIZE]; + /*!< + Pre master secret generated during operations of type @ref ECC_REQ_CLIENT_ECDH and + @ref ECC_REQ_SERVER_ECDH. + */ +}tstrEcdhReqInfo; + +/*! +@struct \ + tstrEcdsaVerifyReqInfo + +@brief Information relating to requests of type @ref ECC_REQ_SIGN_VERIFY. +*/ +typedef struct{ + uint32_t u32nSig; + /*!< + Number of sets of value/signature/key/curve information for verifying. Each set needs to be + retrieved via @ref m2m_ssl_retrieve_next_for_verifying. + */ +}tstrEcdsaVerifyReqInfo; + +/*! +@struct \ + tstrEcdsaSignReqInfo + +@brief Information relating to requests of type @ref ECC_REQ_SIGN_GEN. +*/ +typedef struct{ + uint16_t u16CurveType; + /*!< + The named curve to be used for signing, to be cast to type @ref tenuEcNamedCurve. + */ + uint16_t u16HashSz; + /*!< + The size of the value to be signed. The value needs to be retrieved via + @ref m2m_ssl_retrieve_hash. + */ +}tstrEcdsaSignReqInfo; + +/*! +@struct \ + tstrEccReqInfo + +@brief Information relating to ECC operations. + + This is used in both the request from the WINC and the response to the WINC. +*/ +typedef struct{ + uint16_t u16REQ; + /*!< + The requested ECC operation, to be cast to type @ref tenuEccREQ. + */ + uint16_t u16Status; + /*!< + The status of the operation: zero for success; non-zero for failure. + This field should be used in responses and ignored in requests. + */ + uint32_t u32UserData; + /*!< + This value is used internally. The value set in the response must match the value received + in the request. + */ + uint32_t u32SeqNo; + /*!< + This value is used internally. The value set in the response must match the value received + in the request. + */ + union{ + tstrEcdhReqInfo strEcdhREQ; + /*!< + Information relating to an operation of type @ref ECC_REQ_CLIENT_ECDH, + @ref ECC_REQ_GEN_KEY or @ref ECC_REQ_SERVER_ECDH. + */ + tstrEcdsaSignReqInfo strEcdsaSignREQ; + /*!< + Information relating to an operation of type @ref ECC_REQ_SIGN_GEN. + This field is used in requests and ignored in responses. + */ + tstrEcdsaVerifyReqInfo strEcdsaVerifyREQ; + /*!< + Information relating to an operation of type @ref ECC_REQ_SIGN_VERIFY. + This field is used in requests and ignored in responses. + */ + }; +}tstrEccReqInfo; +/**@}*/ //SSLEnums + +#endif /* __ECC_TYPES_H__ */ diff --git a/ChaletLora.X/Source/winc3400_142/driver/include/m2m_flash.h b/ChaletLora.X/Source/winc3400_142/driver/include/m2m_flash.h new file mode 100644 index 0000000..b279933 --- /dev/null +++ b/ChaletLora.X/Source/winc3400_142/driver/include/m2m_flash.h @@ -0,0 +1,188 @@ +/******************************************************************************* + WINC Flash Interface. + + File Name: + m2m_flash.h + + Summary: + WINC Flash Interface + + Description: + WINC Flash Interface + +*******************************************************************************/ + +//DOM-IGNORE-BEGIN +/******************************************************************************* +* Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries. +* +* Subject to your compliance with these terms, you may use Microchip software +* and any derivatives exclusively with Microchip products. It is your +* responsibility to comply with third party license terms applicable to your +* use of third party software (including open source software) that may +* accompany Microchip software. +* +* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER +* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED +* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A +* PARTICULAR PURPOSE. +* +* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, +* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND +* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS +* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE +* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN +* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, +* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. +*******************************************************************************/ +// DOM-IGNORE-END + +/**@defgroup FLASHAPI Flash +*/ + +#ifndef __M2M_FLASH_H__ +#define __M2M_FLASH_H__ + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +INCLUDES +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ +#include "nm_common.h" +#include "m2m_types.h" + +/**@defgroup FlashEnums Enumeration/Typedefs + * @ingroup FLASHAPI + * @{*/ + +/*! +@enum tenuWincFlashRegion + +@brief The regions of WINC flash available for accessing. +*/ +typedef enum +{ + /*! Access to the entire flash address space. */ + WINC_FLASH_REGION_RAW, + /*! Access to the active firmware region. */ + WINC_FLASH_REGION_FIRMWARE_ACTIVE, + /*! Access to the inactive firmware region. */ + WINC_FLASH_REGION_FIRMWARE_INACTIVE, + /*! Access to PLL table region. Erase is not supported for this region. */ + WINC_FLASH_REGION_PLL_TABLE, + /*! Access to gain table region. Erase is not supported for this region. */ + WINC_FLASH_REGION_GAIN_TABLE, + /*! Access to PLL and gain tables region. Erase is supported for this region. */ + WINC_FLASH_REGION_PLL_AND_GAIN_TABLES, + /*! Access to TLS root certificate region. */ + WINC_FLASH_REGION_ROOT_CERTS, + /*! Access to TLS local certificate region. */ + WINC_FLASH_REGION_LOCAL_CERTS, + /*! Access to connection parameters region. */ + WINC_FLASH_REGION_CONN_PARAM, + /*! Access to HTTP files region. This region is also erased/written by erase/write of firmware regions. */ + WINC_FLASH_REGION_HTTP_FILES, + /*! Number of regions. */ + WINC_FLASH_NUM_REGIONS +} tenuWincFlashRegion; + +/**@} +*/ + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +FUNCTION PROTOTYPES +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ +/** \defgroup FLASHFUNCTIONS Functions +* @ingroup FLASHAPI +*/ +/**@{*/ + +/*! +@fn \ + int8_t m2m_flash_erase_sector(tenuWincFlashRegion enuRegion, uint8_t u8StartSector, uint8_t u8NumSectors); + +@brief Erase one or more sectors within a WINC flash region. + +@param [in] enuRegion + Region of flash in which to erase. +@param [in] u8StartSector + Sector offset (within region) of first sector to erase. +@param [in] u8NumSectors + Number of sectors to erase. + +@return M2M_SUCCESS - The erase completed successfully. + M2M_ERR_INVALID_ARG - The parameters were invalid. + M2M_ERR_FAIL - The request encountered an error. + +@pre The WINC must have been initialised but must not be running. This can be done via + @ref m2m_wifi_init_hold, @ref m2m_wifi_reinit_hold or @ref m2m_wifi_download_mode. + +*/ +int8_t m2m_flash_erase_sector(tenuWincFlashRegion enuRegion, uint8_t u8StartSector, uint8_t u8NumSectors); + +/*! +@fn \ + int8_t m2m_flash_write(tenuWincFlashRegion enuRegion, void *pvBuffer, uint32_t u32Offset, uint32_t u32Size); + +@brief Write the number of bytes specified to a WINC flash location. + +@param [in] enuRegion + Region of flash in which to write. +@param [in] pvBuffer + Pointer to buffer containing the data to write. +@param [in] u32Offset + Byte offset (within region) of write location. +@param [in] u32Size + Number of bytes to write. + +@return M2M_SUCCESS - The write completed successfully. + M2M_ERR_INVALID_ARG - The parameters were invalid. + M2M_ERR_FAIL - The request encountered an error. + +@pre The WINC must have been initialised but must not be running. This can be done via + @ref m2m_wifi_init_hold, @ref m2m_wifi_reinit_hold or @ref m2m_wifi_download_mode. + +@warning The write operation does not erase the flash device first, therefore the user + must ensure the flash region has been erased first. +*/ +int8_t m2m_flash_write(tenuWincFlashRegion enuRegion, void *pvBuffer, uint32_t u32Offset, uint32_t u32Size); + +/*! +@fn \ + int8_t m2m_flash_read(tenuWincFlashRegion enuRegion, void *pvBuffer, uint32_t u32Offset, uint32_t u32Size); + +@brief Read the number of bytes specified from a WINC flash location. + +@param [in] enuRegion + Region of flash from which to read. +@param [out] pvBuffer + Pointer to buffer to populate with the data being read. +@param [in] u32Offset + Byte offset (within region) of read location. +@param [in] u32Size + Number of bytes to read. + +@return M2M_SUCCESS - The read completed successfully. + M2M_ERR_INVALID_ARG - The parameters were invalid. + M2M_ERR_FAIL - The request encountered an error. + +@pre The WINC must have been initialised but must not be running. This can be done via + @ref m2m_wifi_init_hold, @ref m2m_wifi_reinit_hold or @ref m2m_wifi_download_mode. +*/ +int8_t m2m_flash_read(tenuWincFlashRegion enuRegion, void *pvBuffer, uint32_t u32Offset, uint32_t u32Size); + +/*! +@fn \ + int8_t m2m_flash_switch_firmware(void); + +@brief Switch the active and inactive firmware images. + +@return M2M_SUCCESS - The firmware switch completed successfully. + M2M_ERR_FAIL - The request encountered an error. + +@pre The WINC must have been initialised but must not be running. This can be done via + @ref m2m_wifi_init_hold, @ref m2m_wifi_reinit_hold or @ref m2m_wifi_download_mode. + +*/ +int8_t m2m_flash_switch_firmware(void); + +/**@}*/ +#endif /* __M2M_FLASH_H__ */ diff --git a/ChaletLora.X/Source/winc3400_142/driver/include/m2m_hif.h b/ChaletLora.X/Source/winc3400_142/driver/include/m2m_hif.h new file mode 100644 index 0000000..4728429 --- /dev/null +++ b/ChaletLora.X/Source/winc3400_142/driver/include/m2m_hif.h @@ -0,0 +1,243 @@ +/******************************************************************************* + File Name: + m2m_hif.h + + Summary: + This module contains M2M host interface API definitions. + + Description: + This module contains M2M host interface API definitions. + *******************************************************************************/ + +//DOM-IGNORE-BEGIN +/******************************************************************************* +* Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries. +* +* Subject to your compliance with these terms, you may use Microchip software +* and any derivatives exclusively with Microchip products. It is your +* responsibility to comply with third party license terms applicable to your +* use of third party software (including open source software) that may +* accompany Microchip software. +* +* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER +* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED +* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A +* PARTICULAR PURPOSE. +* +* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, +* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND +* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS +* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE +* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN +* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, +* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. +*******************************************************************************/ +// DOM-IGNORE-END + +#ifndef _M2M_HIF_ +#define _M2M_HIF_ + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +INCLUDES +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + +#include "nm_common.h" + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +MACROS +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + +#define M2M_HIF_MAX_PACKET_SIZE (1600 - 4) +/*!< Maximum size of the buffer could be transferred between Host and Firmware. +*/ + +#define M2M_HIF_HDR_OFFSET (sizeof(tstrHifHdr) + 4) + + +/** +* @struct tstrHifHdr +* @brief Structure to hold HIF header +*/ +typedef struct +{ + uint8_t u8Gid; /*!< Group ID */ + uint8_t u8Opcode; /*!< OP code */ + uint16_t u16Length; /*!< Payload length */ +} tstrHifHdr; + +#ifdef __cplusplus +extern "C" { +#endif + +/*! +@typedef typedef void (*tpfHifCallBack)(uint8_t u8OpCode, uint16_t u16DataSize, uint32_t u32Addr); +@brief used to point to Wi-Fi call back function depend on Arduino project or other projects. +@param [in] u8OpCode + HIF Opcode type. +@param [in] u16DataSize + HIF data length. +@param [in] u32Addr + HIF address. +@param [in] grp + HIF group type. +*/ +typedef void (*tpfHifCallBack)(uint8_t u8OpCode, uint16_t u16DataSize, uint32_t u32Addr); +/** +* @fn int8_t hif_init(void *arg); +* @brief + To initialize HIF layer. +* @param [in] arg +* Pointer to the arguments. +* @return + The function shall return ZERO for successful operation and a negative value otherwise. +*/ +int8_t hif_init(void *arg); +/** +* @fn int8_t hif_deinit(void *arg); +* @brief + To Deinitialize HIF layer. +* @param [in] arg +* Pointer to the arguments. +* @return + The function shall return ZERO for successful operation and a negative value otherwise. +*/ +int8_t hif_deinit(void *arg); +/** +* @fn int8_t hif_check_compatibility(uint16_t u16HifInfo); +* @brief +* To check the compatibility of an image with the current driver. +* @param [in] u16HifInfo +* HIF info of image to be checked. +* @return The function shall return ZERO for compatible image and a negative value otherwise. +*/ +int8_t hif_check_compatibility(uint16_t u16HifInfo); +/** +* @fn int8_t hif_enable_access(void); +* @brief +* To enable access to HIF layer, based on HIF level of Firmware. +* This function reads HIF level directly from a register written by Firmware. +* @return The function shall return ZERO for full match operation and a negative value if operation is restricted. +*/ +int8_t hif_enable_access(void); +/** +* @fn int8_t hif_check_code(uint8_t u8Gid, uint8_t u8OpCode); +* @brief +* To check that a particular hif message is supported with the current driver/firmware pair. +* @param [in] u8Gid +* Group ID. +* @param [in] u8Opcode +* Operation ID. +* @return The function shall return ZERO for support and a negative value otherwise. +*/ +int8_t hif_check_code(uint8_t u8Gid, uint8_t u8OpCode); +/** +* @fn int8_t hif_send(uint8_t u8Gid, uint8_t u8Opcode, uint8_t *pu8CtrlBuf, uint16_t u16CtrlBufSize, + uint8_t *pu8DataBuf, uint16_t u16DataSize, uint16_t u16DataOffset) +* @brief Send packet using host interface. + +* @param [in] u8Gid +* Group ID. +* @param [in] u8Opcode +* Operation ID. +* @param [in] pu8CtrlBuf +* Pointer to the Control buffer. +* @param [in] u16CtrlBufSize + Control buffer size. +* @param [in] u16DataOffset + Packet Data offset. +* @param [in] pu8DataBuf +* Packet buffer Allocated by the caller. +* @param [in] u16DataSize + Packet buffer size (including the HIF header). +* @return The function shall return ZERO for successful operation and a negative value otherwise. +*/ +int8_t hif_send(uint8_t u8Gid, uint8_t u8Opcode, uint8_t *pu8CtrlBuf, uint16_t u16CtrlBufSize, + uint8_t *pu8DataBuf, uint16_t u16DataSize, uint16_t u16DataOffset); +/** +* @fn hif_receive +* @brief Host interface interrupt service routine +* @param [in] u32Addr +* Receive start address +* @param [out] pu8Buf +* Pointer to receive buffer. Allocated by the caller +* @param [in] u16Sz +* Receive buffer size +* @param [in] isDone +* If you don't need any more packets send True otherwise send false +* @return + The function shall return ZERO for successful operation and a negative value otherwise. +*/ + +int8_t hif_receive(uint32_t u32Addr, uint8_t *pu8Buf, uint16_t u16Sz, uint8_t isDone); +/** +* @fn hif_register_cb +* @brief + To set Callback function for every Component. + +* @param [in] u8Grp +* Group to which the Callback function should be set. + +* @param [in] fn +* function to be set to the specified group. +* @return + The function shall return ZERO for successful operation and a negative value otherwise. +*/ +int8_t hif_register_cb(uint8_t u8Grp, tpfHifCallBack fn); +/** +* @fn int8_t hif_chip_sleep(void); +* @brief + To make the chip sleep. +* @return + The function shall return ZERO for successful operation and a negative value otherwise. +*/ +int8_t hif_chip_sleep(void); +/** +* @fn int8_t hif_chip_wake(void); +* @brief + To Wakeup the chip. +* @return + The function shall return ZERO for successful operation and a negative value otherwise. +*/ + +int8_t hif_chip_wake(void); +/*! +@fn \ + void hif_set_sleep_mode(uint8_t u8Pstype); + +@brief + Set the sleep mode of the HIF layer. + +@param [in] u8Pstype + Sleep mode. + +@return + The function SHALL return 0 for success and a negative value otherwise. +*/ + +void hif_set_sleep_mode(uint8_t u8Pstype); +/*! +@fn \ + uint8_t hif_get_sleep_mode(void); + +@brief + Get the sleep mode of the HIF layer. + +@return + The function SHALL return the sleep mode of the HIF layer. +*/ + +uint8_t hif_get_sleep_mode(void); + +/** +* @fn hif_handle_isr(void) +* @brief + Handle interrupt received from WINC3400 firmware. +* @return + The function SHALL return 0 for success and a negative value otherwise. +*/ +int8_t hif_handle_isr(void); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/ChaletLora.X/Source/winc3400_142/driver/include/m2m_ota.h b/ChaletLora.X/Source/winc3400_142/driver/include/m2m_ota.h new file mode 100644 index 0000000..d6466d8 --- /dev/null +++ b/ChaletLora.X/Source/winc3400_142/driver/include/m2m_ota.h @@ -0,0 +1,370 @@ +/******************************************************************************* + WINC3400 OTA Upgrade API Interface + + File Name: + m2m_ota.h + + Summary: + WINC3400 OTA Upgrade API Interface + + Description: + WINC3400 OTA Upgrade API Interface + *******************************************************************************/ + +//DOM-IGNORE-BEGIN +/******************************************************************************* +* Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries. +* +* Subject to your compliance with these terms, you may use Microchip software +* and any derivatives exclusively with Microchip products. It is your +* responsibility to comply with third party license terms applicable to your +* use of third party software (including open source software) that may +* accompany Microchip software. +* +* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER +* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED +* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A +* PARTICULAR PURPOSE. +* +* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, +* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND +* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS +* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE +* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN +* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, +* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. +*******************************************************************************/ +// DOM-IGNORE-END + +/**@defgroup OTAAPI OTA + @brief + The WINC supports OTA (Over-The-Air) updates. Using the APIs described in this module, + it is possible to request an ATWINC15x0 to update its firmware, or safely rollback to + the previous firmware version.\n There are also APIs to download files and store them in + the WINC's Flash (supported by ATWINC1510 only), which can be used for Host MCU OTA + updates or accessing information stored remotely. + @{ + @defgroup OTACALLBACKS Callbacks + @brief + Lists the different callbacks that can be used during OTA updates.\n + Callbacks of type @ref tpfOtaNotifCb and @ref tpfOtaUpdateCb should be passed + onto @ref m2m_ota_init at system initialization. Other callbacks are provided + to handle the various steps of Host File Download. + + @defgroup OTADEFINE Defines + @brief + Specifies the macros and defines used by the OTA APIs. + + @defgroup OTATYPEDEF Enumerations and Typedefs + @brief + Specifies the enums and Data Structures used by the OTA APIs. + + @defgroup OTAFUNCTIONS Functions + @brief + Lists the full set of available APIs to manage OTA updates and Host File Downloads. + @} +*/ + +#ifndef __M2M_OTA_H__ +#define __M2M_OTA_H__ + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +INCLUDES +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + +#include "nm_common.h" +#include "m2m_types.h" +#include "nmdrv.h" + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +MACROS +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + +/*! +@typedef void (*tpfOtaUpdateCb)(uint8_t u8OtaUpdateStatusType, uint8_t u8OtaUpdateStatus); + +@brief + A callback to get OTA status update, the callback provides the status type and its status.\n + The OTA callback provides the download status, the switch to the downloaded firmware status + and roll-back status. + +@param[in] u8OtaUpdateStatusType + Possible values are listed in @ref tenuOtaUpdateStatusType. + +@param[in] u8OtaUpdateStatus + Possible values are listed as enumerated by @ref tenuOtaUpdateStatus. + +@note + Executes other callbacks passed to the OTA module. + +@see + tenuOtaUpdateStatusType + tenuOtaUpdateStatus + */ +typedef void (*tpfOtaUpdateCb)(uint8_t u8OtaUpdateStatusType, uint8_t u8OtaUpdateStatus); +/**@}*/ // OTACALLBACKS + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +FUNCTION PROTOTYPES +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + +#ifdef __cplusplus +extern "C" { +#endif + +/*! +@ingroup OTAFUNCTIONS +@fn \ + int8_t m2m_ota_init(tpfOtaUpdateCb pfOtaUpdateCb); + +@brief + Synchronous initialization function for the OTA layer by registering the update callback.\n + The notification callback is not supported at the current version. Calling this API is a + MUST for all the OTA API's. + +@param[in] pfOtaUpdateCb + OTA Update callback function. + +@return + The function returns @ref M2M_SUCCESS for successful operations and a negative value otherwise. + */ +int8_t m2m_ota_init(tpfOtaUpdateCb pfOtaUpdateCb); + +/*! +@ingroup OTAFUNCTIONS +@fn \ + int8_t m2m_ota_start_update(unsigned char * pcDownloadUrl); + +@brief + Request OTA start update using the download URL. The OTA module will download the OTA image, ensure integrity of the image + and update the validity of the image in the control structure. On completion, a callback of type @ref tpfOtaUpdateCb is called + (callback previously provided via @ref m2m_ota_init). Switching to the updated image additionally requires completion of + @ref m2m_ota_switch_firmware, followed by a WINC reset. + +@param[in] pcDownloadUrl + The download firmware URL, according to the application server. + +@warning + Calling this API does not guarantee OTA WINC image update, it depends on the connection with the + download server and the validity of the image.\n + Calling this API invalidates any previous valid rollback image, irrespective of the result, but when + the OTA succeeds, the current image will become the rollback image after @ref m2m_ota_switch_firmware. + +@pre + @ref m2m_ota_init is a prerequisite and must have been called before using @ref m2m_ota_start_update.\n + Switching to the newly downloaded image requires calling @ref m2m_ota_switch_firmware API. + +@sa + @ref m2m_ota_init + @ref m2m_ota_switch_firmware + @ref tpfOtaUpdateCb + +@return + The function returns @ref M2M_SUCCESS for successful operations and a negative value otherwise. + Note that successful operation in this context means the OTA update request has reached the firmware OTA module. + It does not indicate whether or not the image update succeeded. + +@section OTAExample Example + This example shows how an OTA image update and switch is carried out. + It demonstrates use of the following OTA APIs: + - @ref m2m_ota_init + - @ref tpfOtaUpdateCb + - @ref m2m_ota_start_update + - @ref m2m_ota_switch_firmware + - @ref m2m_ota_rollback + + It also makes use of @ref m2m_wifi_check_ota_rb in order to inform OTA decisions. +@code +static void OtaUpdateCb(uint8_t u8OtaUpdateStatusType, uint8_t u8OtaUpdateStatus) +{ + int8_t s8tmp; + tstrM2mRev strtmp; + M2M_INFO("%d %d\n", u8OtaUpdateStatusType, u8OtaUpdateStatus); + switch(u8OtaUpdateStatusType) + { + case DL_STATUS: + if(u8OtaUpdateStatus == OTA_STATUS_SUCCESS) + { + M2M_INFO("OTA download succeeded\n"); + s8tmp = m2m_wifi_check_ota_rb(); + if(s8tmp == M2M_ERR_FW_VER_MISMATCH) + { + // In this case the application SHOULD update the host driver before calling + // @ref m2m_ota_switch_firmware(). Switching firmware image and resetting without updating host + // driver would lead to severely limited functionality (i.e. OTA rollback only). + } + else if(s8tmp == M2M_SUCCESS) + { + // In this case the application MAY WANT TO update the host driver before calling + // @ref m2m_ota_switch_firmware(). Switching firmware image and resetting without + // updating host driver may lead to suboptimal functionality. + } + else + { + M2M_INFO("Cannot recognize downloaded image\n"); + // In this case the application MUST NOT update the host driver if such an update would change the + // driver HIF Major field. Firmware switch @ref using m2m_ota_switch_firmware() is blocked. + break; + } + // Switch to the upgraded firmware + M2M_INFO("Now switching active partition...\n"); + s8tmp = m2m_ota_switch_firmware(); + } + break; + case SW_STATUS: + case RB_STATUS: + if(u8OtaUpdateStatus == OTA_STATUS_SUCCESS) + { + M2M_INFO("Switch/Rollback succeeded\n"); + + // Start the host SW upgrade if required, then system reset is required (Reinitialize the driver) + + M2M_INFO("Now resetting the system...\n"); + system_reset(); + } + break; + } +} + +static void wifi_event_cb(uint8_t u8WiFiEvent, void *pvMsg) +{ + // ... + case M2M_WIFI_REQ_DHCP_CONF: + { + // After successful connection, start the OTA upgrade + m2m_ota_start_update(OTA_URL); + } + break; + default: + break; + // ... +} + +int main(void) +{ + tstrWifiInitParam param; + int8_t s8Ret = M2M_SUCCESS; + bool rollback_required = FALSE; + + // System init, etc should be here... + + memset((uint8_t *)¶m, 0, sizeof(param)); + param.pfAppWifiCb = wifi_event_cb; + + // Initialize the WINC Driver + s8Ret = m2m_wifi_init(¶m); + if(s8Ret == M2M_ERR_FW_VER_MISMATCH) + { + M2M_ERR("Firmware version mismatch\n"); + s8Ret = m2m_wifi_check_ota_rb(); + if(s8Ret == M2M_SUCCESS) + { + // In this case the image in the inactive partition has compatible HIF. We will switch/rollback to it + // after initializing the OTA module. + rollback_required = TRUE; + } + } + if(M2M_SUCCESS != s8Ret) + { + M2M_ERR("Driver Init Failed <%d>\n", s8Ret); + while(1); + } + // Initialize the OTA module + m2m_ota_init(OtaUpdateCb, NULL); + if(rollback_required) + { + // We need to call either @ref m2m_ota_rollback() or @ref m2m_ota_switch_firmware() (functionally equivalent). + m2m_ota_rollback(); + } + else + { + // Connect to AP that provides connection to the OTA server + m2m_wifi_default_connect(); + } + while(1) + { + // Handle the app state machine plus the WINC event handler + while(m2m_wifi_handle_events(NULL) != M2M_SUCCESS) { + } + } +} +@endcode + */ +int8_t m2m_ota_start_update(unsigned char *pcDownloadUrl); + +/*! +@ingroup OTAFUNCTIONS +@fn \ + int8_t m2m_ota_rollback(void); + +@brief + Request OTA Roll-back to the old (inactive) WINC image, the WINC firmware will check the validity of the inactive image + and activate it if valid. On completion, a callback of type @ref tpfOtaUpdateCb is called (application must previously have + provided the callback via @ref m2m_ota_init). If the callback indicates successful activation, the newly-activated image + will start running after next system reset. + +@warning + If rollback requires a host driver update in order to maintain HIF compatibility (HIF + major value change), then it is recommended to update the host driver prior to calling this API.\n + In the event of system reset with incompatible driver/firmware, compatibility can be + recovered by calling @ref m2m_ota_rollback or @ref m2m_ota_switch_firmware. See @ref OTAExample. + +@sa + m2m_ota_init + m2m_ota_start_update + +@return + The function returns @ref M2M_SUCCESS for successful operations and a negative value otherwise. + */ +int8_t m2m_ota_rollback(void); + +/*! +@ingroup OTAFUNCTIONS +@fn \ + int8_t m2m_ota_abort(void); + +@brief + Request the WINC to abort an OTA in progress.\n + If no download is in progress, the API will respond with failure. + +@sa + m2m_ota_init + m2m_ota_start_update + +@return + The function returns @ref M2M_SUCCESS for a successful operation and a negative value otherwise. + */ +int8_t m2m_ota_abort(void); + +/*! +@ingroup OTAFUNCTIONS +@fn \ + int8_t m2m_ota_switch_firmware(void); + +@brief + Request switch to the updated WINC image. The WINC firmware will check the validity of the + inactive image and activate it if valid. On completion, a callback of type @ref tpfOtaUpdateCb + is called (application must previously have provided the callback via @ref m2m_ota_init). + If the callback indicates successful activation, the newly-activated image will start running + after next system reset. + +@warning + If switch requires a host driver update in order to maintain HIF compatibility (HIF + major value change), then it is recommended to update the host driver prior to calling this API.\n + In the event of system reset with incompatible driver/firmware, compatibility can be + recovered by calling @ref m2m_ota_rollback or @ref m2m_ota_switch_firmware. See @ref OTAExample. + +@sa + m2m_ota_init + m2m_ota_start_update + +@return + The function returns @ref M2M_SUCCESS for successful operations and a negative value otherwise. + */ +int8_t m2m_ota_switch_firmware(void); + + +#ifdef __cplusplus +} +#endif +#endif /* __M2M_OTA_H__ */ diff --git a/ChaletLora.X/Source/winc3400_142/driver/include/m2m_periph.h b/ChaletLora.X/Source/winc3400_142/driver/include/m2m_periph.h new file mode 100644 index 0000000..27d1051 --- /dev/null +++ b/ChaletLora.X/Source/winc3400_142/driver/include/m2m_periph.h @@ -0,0 +1,307 @@ +/******************************************************************************* + WINC3400 Peripherals Application Interface. + + File Name: + m2m_periph.h + + Summary: + WINC3400 Peripherals Application Interface + + Description: + WINC3400 Peripherals Application Interface + *******************************************************************************/ + +//DOM-IGNORE-BEGIN +/******************************************************************************* +* Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries. +* +* Subject to your compliance with these terms, you may use Microchip software +* and any derivatives exclusively with Microchip products. It is your +* responsibility to comply with third party license terms applicable to your +* use of third party software (including open source software) that may +* accompany Microchip software. +* +* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER +* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED +* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A +* PARTICULAR PURPOSE. +* +* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, +* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND +* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS +* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE +* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN +* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, +* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. +*******************************************************************************/ +// DOM-IGNORE-END + +#ifndef _M2M_PERIPH_H_ +#define _M2M_PERIPH_H_ + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +INCLUDES +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + +#include "nm_common.h" +#include "m2m_types.h" + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +MACROS +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +DATA TYPES +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + +/*! +@struct \ + tstrPerphInitParam + +@brief + Peripheral module initialization parameters. +*/ +typedef struct { + void *arg; +} tstrPerphInitParam; + +/*! +@enum \ + tenuGpioNum + +@brief + A list of GPIO numbers configurable through the m2m_periph module. +*/ +typedef enum { + M2M_PERIPH_GPIO3, + M2M_PERIPH_GPIO4, + M2M_PERIPH_GPIO15, /*!< GPIO15 pad */ + M2M_PERIPH_GPIO16, /*!< GPIO16 pad */ + M2M_PERIPH_GPIO18, /*!< GPIO18 pad */ + M2M_PERIPH_GPIO_MAX +} tenuGpioNum; + +/*! +@enum \ + tenuI2cMasterSclMuxOpt + +@brief + Allowed pin multiplexing options for I2C master SCL signal. +*/ +typedef enum { + M2M_PERIPH_I2C_MASTER_SCL_MUX_OPT_HOST_WAKEUP, /*!< I2C master SCL is available on HOST_WAKEUP. */ + M2M_PERIPH_I2C_MASTER_SCL_MUX_OPT_SD_DAT3, /*!< I2C master SCL is available on SD_DAT3 (GPIO 7). */ + M2M_PERIPH_I2C_MASTER_SCL_MUX_OPT_GPIO13, /*!< I2C master SCL is available on GPIO 13. */ + M2M_PERIPH_I2C_MASTER_SCL_MUX_OPT_GPIO4, /*!< I2C master SCL is available on GPIO 4.*/ + M2M_PERIPH_I2C_MASTER_SCL_MUX_OPT_I2C_SCL, /*!< I2C master SCL is available on I2C slave SCL. */ + M2M_PERIPH_I2C_MASTER_SCL_MUX_OPT_NUM +} tenuI2cMasterSclMuxOpt; + +/*! +@enum \ + tenuI2cMasterSdaMuxOpt + +@brief + Allowed pin multiplexing options for I2C master SDA signal. +*/ +typedef enum { + M2M_PERIPH_I2C_MASTER_SDA_MUX_OPT_RTC_CLK, /*!< I2C master SDA is available on RTC_CLK. */ + M2M_PERIPH_I2C_MASTER_SDA_MUX_OPT_SD_CLK, /*!< I2C master SDA is available on SD_CLK (GPIO 8). */ + M2M_PERIPH_I2C_MASTER_SDA_MUX_OPT_GPIO14, /*!< I2C master SDA is available on GPIO 14. */ + M2M_PERIPH_I2C_MASTER_SDA_MUX_OPT_GPIO6, /*!< I2C master SDA is available on GPIO 6.*/ + M2M_PERIPH_I2C_MASTER_SDA_MUX_OPT_I2C_SDA, /*!< I2C master SDA is available on I2C slave SDA. */ + M2M_PERIPH_I2C_MASTER_SDA_MUX_OPT_NUM +} tenuI2cMasterSdaMuxOpt; + +/*! +@struct \ + tstrI2cMasterInitParam + +@brief + I2C master configuration parameters. +@sa + tenuI2cMasterSclMuxOpt + tenuI2cMasterSdaMuxOpt +*/ +typedef struct { + uint8_t enuSclMuxOpt; /*!< SCL multiplexing option. Allowed value are defined in tenuI2cMasterSclMuxOpt */ + uint8_t enuSdaMuxOpt; /*!< SDA multiplexing option. Allowed value are defined in tenuI2cMasterSdaMuxOpt */ + uint8_t u8ClkSpeedKHz; /*!< I2C master clock speed in KHz. */ +} tstrI2cMasterInitParam; + +/*! +@enum \ + tenuI2cMasterFlags + +@brief + Bitwise-ORed flags for use in m2m_periph_i2c_master_write and m2m_periph_i2c_master_read +@sa + m2m_periph_i2c_master_write + m2m_periph_i2c_master_read +*/ +typedef enum { + I2C_MASTER_NO_FLAGS = 0x00, + /*!< No flags. */ + I2C_MASTER_NO_STOP = 0x01, + /*!< No stop bit after this transaction. Useful for scattered buffer read/write operations. */ + I2C_MASTER_NO_START = 0x02, + /*!< No start bit at the beginning of this transaction. Useful for scattered buffer read/write operations.*/ +} tenuI2cMasterFlags; + +/*! +@enum \ + tenuPullupMask + +@brief + Bitwise-ORed flags for use in m2m_perph_pullup_ctrl. +@sa + m2m_periph_pullup_ctrl + +*/ +typedef enum { + M2M_PERIPH_PULLUP_DIS_HOST_WAKEUP = (1ul << 0), + M2M_PERIPH_PULLUP_DIS_RTC_CLK = (1ul << 1), + M2M_PERIPH_PULLUP_DIS_IRQN = (1ul << 2), + M2M_PERIPH_PULLUP_DIS_GPIO_3 = (1ul << 3), + M2M_PERIPH_PULLUP_DIS_GPIO_4 = (1ul << 4), + M2M_PERIPH_PULLUP_DIS_GPIO_5 = (1ul << 5), + M2M_PERIPH_PULLUP_DIS_GPIO_6 = (1ul << 6), + M2M_PERIPH_PULLUP_DIS_SD_CLK = (1ul << 7), + M2M_PERIPH_PULLUP_DIS_SD_CMD_SPI_SCK = (1ul << 8), + M2M_PERIPH_PULLUP_DIS_SD_DAT0_SPI_TXD = (1ul << 9), + M2M_PERIPH_PULLUP_DIS_SD_DAT1_SPI_SSN = (1ul << 10), + M2M_PERIPH_PULLUP_DIS_SD_DAT1_SPI_RXD = (1ul << 11), + M2M_PERIPH_PULLUP_DIS_SD_DAT3 = (1ul << 12), +} tenuPullupEnable1Mask; +typedef enum { + M2M_PERIPH_PULLUP_DIS_GPIO_13 = (1ul << 0), + M2M_PERIPH_PULLUP_DIS_GPIO_14 = (1ul << 1), + M2M_PERIPH_PULLUP_DIS_GPIO_15 = (1ul << 2), + M2M_PERIPH_PULLUP_DIS_GPIO_16 = (1ul << 3), + M2M_PERIPH_PULLUP_DIS_GPIO_17 = (1ul << 4), + M2M_PERIPH_PULLUP_DIS_GPIO_18 = (1ul << 5), + M2M_PERIPH_PULLUP_DIS_GPIO_19 = (1ul << 6), + M2M_PERIPH_PULLUP_DIS_GPIO_20 = (1ul << 7), + M2M_PERIPH_PULLUP_DIS_GPIO_22 = (1ul << 9), + M2M_PERIPH_PULLUP_DIS_GPIO_23 = (1ul << 10), + M2M_PERIPH_PULLUP_DIS_GPIO_38 = (1ul << 25), + M2M_PERIPH_PULLUP_DIS_GPIO_39 = (1ul << 26), + M2M_PERIPH_PULLUP_DIS_GPIO_40 = (1ul << 27), + M2M_PERIPH_PULLUP_DIS_GPIO_42 = (1ul << 29), +} tenuPullupEnable2Mask; +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +FUNCTION PROTOTYPES +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + +#ifdef __cplusplus +extern "C" { +#endif + +/*! +@fn \ + int8_t m2m_periph_init(tstrPerphInitParam * param); + +@brief + Initialize the WINC3400 peripheral driver module. + +@param [in] param + Peripheral module initialization structure. See members of tstrPerphInitParam. + +@return + The function SHALL return 0 for success and a negative value otherwise. + +@sa + tstrPerphInitParam +*/ +int8_t m2m_periph_init(tstrPerphInitParam *param); + +/*! +@fn \ + int8_t m2m_periph_gpio_set_dir(uint8_t u8GpioNum, uint8_t u8GpioDir); + +@brief + Configure a specific WINC3400 pad as a GPIO and sets its direction (input or output). + +@param [in] u8GpioNum + GPIO number. Allowed values are defined in tenuGpioNum. + +@param [in] u8GpioDir + GPIO direction: Zero = input. Non-zero = output. + +@return + The function SHALL return 0 for success and a negative value otherwise. + +@sa + tenuGpioNum +*/ +int8_t m2m_periph_gpio_set_dir(uint8_t u8GpioNum, uint8_t u8GpioDir); + +/*! +@fn \ + int8_t m2m_periph_gpio_set_val(uint8_t u8GpioNum, uint8_t u8GpioVal); + +@brief + Set an WINC3400 GPIO output level high or low. + +@param [in] u8GpioNum + GPIO number. Allowed values are defined in tenuGpioNum. + +@param [in] u8GpioVal + GPIO output value. Zero = low, non-zero = high. + +@return + The function SHALL return 0 for success and a negative value otherwise. + +@sa + tenuGpioNum +*/ +int8_t m2m_periph_gpio_set_val(uint8_t u8GpioNum, uint8_t u8GpioVal); + +/*! +@fn \ + int8_t m2m_periph_gpio_get_val(uint8_t u8GpioNum, uint8_t * pu8GpioVal); + +@brief + Read an WINC3400 GPIO input level. + +@param [in] u8GpioNum + GPIO number. Allowed values are defined in tenuGpioNum. + +@param [out] pu8GpioVal + GPIO input value. Zero = low, non-zero = high. + +@return + The function SHALL return 0 for success and a negative value otherwise. + +@sa + tenuGpioNum +*/ +int8_t m2m_periph_gpio_get_val(uint8_t u8GpioNum, uint8_t *pu8GpioVal); + +/*! +@fn \ + int8_t m2m_periph_pullup_ctrl(uint32_t pinmask, uint8_t enable); + +@brief + Control the programmable pull-up resistor on the chip pads . + + +@param [in] pinmask + Write operation bitwise-ORed mask for which pads to control. Allowed values are defined in tenuPullupMask. + +@param [in] enable + Set to 0 to disable pull-up resistor. Non-zero will enable the pull-up. + +@return + The function SHALL return 0 for success and a negative value otherwise. + +@sa + tenuPullupMask +*/ +int8_t m2m_periph_pullup_ctrl(uint32_t pinmask, uint8_t enable); + +#ifdef __cplusplus +} +#endif + +#endif /* _M2M_PERIPH_H_ */ \ No newline at end of file diff --git a/ChaletLora.X/Source/winc3400_142/driver/include/m2m_ssl.h b/ChaletLora.X/Source/winc3400_142/driver/include/m2m_ssl.h new file mode 100644 index 0000000..df22ef7 --- /dev/null +++ b/ChaletLora.X/Source/winc3400_142/driver/include/m2m_ssl.h @@ -0,0 +1,255 @@ +/******************************************************************************* + File Name: + m2m_ssl.h + + Summary: + WINC SSL Interface. + + Description: + WINC SSL Interface. + *******************************************************************************/ + +//DOM-IGNORE-BEGIN +/******************************************************************************* +* Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries. +* +* Subject to your compliance with these terms, you may use Microchip software +* and any derivatives exclusively with Microchip products. It is your +* responsibility to comply with third party license terms applicable to your +* use of third party software (including open source software) that may +* accompany Microchip software. +* +* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER +* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED +* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A +* PARTICULAR PURPOSE. +* +* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, +* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND +* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS +* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE +* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN +* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, +* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. +*******************************************************************************/ +//DOM-IGNORE-END + +/** @defgroup SSLAPI SSL + @brief + Provides a description of the SSL Layer. + @{ + @defgroup SSLCallbacks Callbacks + @brief + Provides detail on the available callbacks for the SSL Layer. + + @defgroup SSLEnums Enumerations and Typedefs + @brief + Specifies the enums and Data Structures used by the SSL APIs. + + @defgroup SSLFUNCTIONS Functions + @brief + Provides detail on the available APIs for the SSL Layer. + @} +*/ + +#ifndef __M2M_SSL_H__ +#define __M2M_SSL_H__ + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +INCLUDES +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + +#include "nm_common.h" +#include "m2m_types.h" +#include "nmdrv.h" +#include "ecc_types.h" +#include "socket.h" + +/*! +@ingroup SSLCallbacks +@typedef void (*tpfAppSSLCb)(uint8_t u8MsgType, void* pvMsg); +@brief A callback to get SSL notifications. +@param[in] u8MsgType + The type of the message received. +@param[in] pvMsg + A structure to provide notification payload. +*/ +typedef void (*tpfAppSSLCb)(uint8_t u8MsgType, void *pvMsg); + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +FUNCTION PROTOTYPES +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + +/*! +@ingroup SSLFUNCTIONS +@fn int8_t m2m_ssl_init(tpfAppSSLCb pfAppSSLCb); +@brief Initializes the SSL layer. +@param[in] pfAppSSLCb + Application SSL callback function. +@return The function returns @ref M2M_SUCCESS for success and a negative value otherwise. +*/ +int8_t m2m_ssl_init(tpfAppSSLCb pfAppSSLCb); + +/*! +@ingroup SSLFUNCTIONS +@fn int8_t m2m_ssl_handshake_rsp(tstrEccReqInfo* strECCResp, uint8_t* pu8RspDataBuff, uint16_t u16RspDataSz); +@brief Sends ECC responses to the WINC. +@param[in] strECCResp + ECC Response struct. +@param[in] pu8RspDataBuff + Pointer of the response data to be sent. +@param[in] u16RspDataSz + Response data size. +@return The function returns @ref M2M_SUCCESS for success and a negative value otherwise. +*/ +int8_t m2m_ssl_handshake_rsp(tstrEccReqInfo *strECCResp, uint8_t *pu8RspDataBuff, uint16_t u16RspDataSz); + +/*! +@ingroup SSLFUNCTIONS +@fn int8_t m2m_ssl_send_certs_to_winc(uint8_t* pu8Buffer, uint32_t u32BufferSz); +@brief Sends certificates to the WINC. +@param[in] pu8Buffer + Pointer to the certificates. The buffer format must match the format of @ref tstrTlsSrvSecHdr. +@param[in] u32BufferSz + Size of the certificates. +@return The function returns @ref M2M_SUCCESS for success and a negative value otherwise. +*/ +int8_t m2m_ssl_send_certs_to_winc(uint8_t *pu8Buffer, uint32_t u32BufferSz); + +/*! +@ingroup SSLFUNCTIONS +@fn int8_t m2m_ssl_retrieve_next_for_verifying(tenuEcNamedCurve *penuCurve, uint8_t *pu8Value, uint16_t *pu16ValueSz, uint8_t *pu8Sig, uint16_t *pu16SigSz, tstrECPoint *pstrKey); +@brief Retrieve the next set of information from the WINC for ECDSA verification. +@param[out] penuCurve + The named curve. +@param[out] pu8Value + Value retrieved for verification. This is the digest of the message, truncated/prepended to the appropriate size. +@param[inout] pu16ValueSz + in: Size of value buffer provided by caller. + out: Size of value retrieved (provided for convenience; the value size is in fact determined by the curve). +@param[out] pu8Sig + Signature retrieved for verification. +@param[inout] pu16SigSz + in: Size of signature buffer provided by caller. + out: Size of signature retrieved (provided for convenience; the signature size is in fact determined by the curve). +@param[out] pstrKey + Public key retrieved for verification. +@return The function returns @ref M2M_SUCCESS for success and a negative value otherwise. + +@pre This function should only be called after the application has been notified that + verification information is ready via @ref ECC_REQ_SIGN_VERIFY. + +@warning If this function returns @ref M2M_ERR_FAIL, then any remaining verification info from + the WINC is lost. +*/ +int8_t m2m_ssl_retrieve_next_for_verifying(tenuEcNamedCurve *penuCurve, uint8_t *pu8Value, uint16_t *pu16ValueSz, uint8_t *pu8Sig, uint16_t *pu16SigSz, tstrECPoint *pstrKey); + +/*! +@ingroup SSLFUNCTIONS +@fn int8_t m2m_ssl_retrieve_cert(uint16_t *pu16Curve, uint8_t *pu8Value, uint8_t *pu8Sig, tstrECPoint *pstrKey); +@brief Retrieve the next set of information from the WINC for ECDSA verification. +@param[out] pu16Curve + The named curve, to be cast to type @ref tenuEcNamedCurve. +@param[out] pu8Value + Value retrieved for verification. This is the digest of the message, truncated/prepended to the appropriate size. + The size of the value is equal to the field size of the curve, hence is determined by pu16Curve. +@param[out] pu8Sig + Signature retrieved for verification. + The size of the signature is equal to twice the field size of the curve, hence is determined by pu16Curve. +@param[out] pstrKey + Public key retrieved for verification. +@return The function returns @ref M2M_SUCCESS for success and a negative value otherwise. + +@pre This function should only be called after the application has been notified that + verification information is ready via @ref ECC_REQ_SIGN_VERIFY. + +@warning If this function returns @ref M2M_ERR_FAIL, then any remaining verification info from + the WINC is lost. + +@warning This API has been deprecated and is kept for legacy purposes only. It is recommended + that @ref m2m_ssl_retrieve_next_for_verifying is used instead. +*/ +int8_t m2m_ssl_retrieve_cert(uint16_t *pu16Curve, uint8_t *pu8Value, uint8_t *pu8Sig, tstrECPoint *pstrKey); + +/*! +@ingroup SSLFUNCTIONS +@fn int8_t m2m_ssl_retrieve_hash(uint8_t *pu8Value, uint16_t u16ValueSz) +@brief Retrieve the value from the WINC for ECDSA signing. +@param[out] pu8Value + Value retrieved for signing. This is the digest of the message, truncated/prepended to the appropriate size. +@param[in] u16ValueSz + Size of value to be retrieved. (The application should obtain this information, + along with the curve, from the associated @ref ECC_REQ_SIGN_GEN notification.) +@return The function returns @ref M2M_SUCCESS for success and a negative value otherwise. + +@pre This function should only be called after the application has been notified that + signing information is ready via @ref ECC_REQ_SIGN_GEN. + +@warning If this function returns @ref M2M_ERR_FAIL, then the value for signing is lost. +*/ +int8_t m2m_ssl_retrieve_hash(uint8_t *pu8Value, uint16_t u16ValueSz); + +/*! +@ingroup SSLFUNCTIONS +@fn void m2m_ssl_stop_retrieving(void); +@brief Allow SSL driver to tidy up when the application chooses not to retrieve all available + information. + +@return None. + +@warning The application must call this function if it has been notified (via + @ref ECC_REQ_SIGN_GEN or @ref ECC_REQ_SIGN_VERIFY) that information is available for + retrieving from the WINC, but chooses not to retrieve it all. + The application must not call this function if it has retrieved all the available + information, or if a retrieve function returned @ref M2M_ERR_FAIL indicating that any + remaining information has been lost. + +@see m2m_ssl_retrieve_next_for_verifying\n + m2m_ssl_retrieve_cert\n + m2m_ssl_retrieve_hash +*/ +void m2m_ssl_stop_retrieving(void); + +/*! +@ingroup SSLFUNCTIONS +@fn NMI_API void m2m_ssl_stop_processing_certs(void); +@brief Allow SSL driver to tidy up in case application does not read all available certificates. +@return None. + +@warning This API has been deprecated and is kept for legacy purposes only. It is recommended + that @ref m2m_ssl_stop_retrieving is used instead. +*/ +void m2m_ssl_stop_processing_certs(void); + +/*! +@ingroup SSLFUNCTIONS +@fn void m2m_ssl_ecc_process_done(void); +@brief Allow SSL driver to tidy up after application has finished processing ECC message. + +@return None. + +@warning The application should call this function after receiving an SSL callback with message + type @ref M2M_SSL_REQ_ECC, after retrieving any related information, and before + calling @ref m2m_ssl_handshake_rsp. +*/ +void m2m_ssl_ecc_process_done(void); + +/*! +@ingroup SSLFUNCTIONS +@fn int8_t m2m_ssl_set_active_ciphersuites(uint32_t u32SslCsBMP); +@brief Sets the active ciphersuites. +@details Override the default Active SSL ciphers in the SSL module with a certain combination selected by + the caller in the form of a bitmap containing the required ciphers to be on.\n + There is no need to call this function if the application will not change the default ciphersuites. +@param[in] u32SslCsBMP + Bitmap containing the desired ciphers to be enabled for the SSL module. The ciphersuites are defined in + @ref SSLCipherSuiteID. + The default ciphersuites are all ciphersuites supported by the firmware with the exception of ECC ciphersuites. + The caller can override the default with any desired combination. + If u32SslCsBMP does not contain any ciphersuites supported by firmware, then the current active list will not + change. +@return The function returns @ref M2M_SUCCESS for success and a negative value otherwise. +*/ +int8_t m2m_ssl_set_active_ciphersuites(uint32_t u32SslCsBMP); + +#endif /* __M2M_SSL_H__ */ \ No newline at end of file diff --git a/ChaletLora.X/Source/winc3400_142/driver/include/m2m_types.h b/ChaletLora.X/Source/winc3400_142/driver/include/m2m_types.h new file mode 100644 index 0000000..8b9015e --- /dev/null +++ b/ChaletLora.X/Source/winc3400_142/driver/include/m2m_types.h @@ -0,0 +1,2745 @@ +/******************************************************************************* + File Name: + m2m_types.h + + Summary: + WINC3400 Application Interface Internal Types + + Description: + WINC3400 Application Interface Internal Types + *******************************************************************************/ + +//DOM-IGNORE-BEGIN +/******************************************************************************* +* Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries. +* +* Subject to your compliance with these terms, you may use Microchip software +* and any derivatives exclusively with Microchip products. It is your +* responsibility to comply with third party license terms applicable to your +* use of third party software (including open source software) that may +* accompany Microchip software. +* +* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER +* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED +* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A +* PARTICULAR PURPOSE. +* +* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, +* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND +* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS +* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE +* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN +* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, +* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. +*******************************************************************************/ +// DOM-IGNORE-END + +#ifndef __M2M_WIFI_TYPES_H__ +#define __M2M_WIFI_TYPES_H__ + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +INCLUDES +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + +#include "nm_common.h" + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +MACROS +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ +/**@defgroup VERSION Version + */ +/**@defgroup VERSIONDEF Defines + * @ingroup VERSION + * @{ + */ + +/* + Layout of HIF_LEVEL fields + | x x x x x x | x x x x x x x x | + | 13:8 | 7:0 | + | HIF_MAJOR | HIF_MINOR | + | HIF_LEVEL | + + HIF compatibility (checked by driver after wifi boot). + + Check Firmware HIF_MAJOR + |___ + | Does not match Driver - Recommend either: + | > checking compatibility of Cortus image stored in inactive partition + | (m2m_wifi_check_ota_rb()) then swapping to it using OTA rollback or switch. + | > updating Host with out of band method. + |___ + Matches Driver - All APIs can be attempted. + | - Cortus image can be updated (OTA download) + | - New Host image can be retrieved (socket recv) + | + Check Firmware HIF_MINOR + |___ + | Less than Driver - Driver may refuse to execute some APIs (API would return M2M_ERR_SEND). + | - Recommend updating Cortus image using OTA download to use newer Firmware. + |___ + | Equal to Driver - Driver supports all APIs. + |___ + Greater than Driver - Driver supports all APIs. + - Recommend reviewing potential benefit of updating Host to use newer Driver. + + When an OTA update involving HIF_MAJOR change is required, the following order is recommended: + 1. m2m_ota_start_update() to store new Cortus image in inactive partition. + 2. Socket recv() to retrieve new Host image (including new WINC Driver). + 3. Application code to make Host image available in host flash. + 4. m2m_ota_switch_firmware() to switch active/inactive partition (will take effect after system_reset()). + 5. system_reset() to restart with new WINC Driver / Firmware. + Note that in the event of (unintentional) system reset after step (3), the remaining steps (4) and (5) should still be + run after the reset. +*/ +/* Selection of HIF_INFO +*/ +#define M2M_HIF_INFO_SHIFT (0) +#define M2M_HIF_INFO_MASK (0xffff) +/* Subdivision of HIF_INFO +*/ +#define M2M_HIF_BLOCK_SHIFT (14) +#define M2M_HIF_BLOCK_MASK (0x3) +#define M2M_HIF_LEVEL_SHIFT (0) +#define M2M_HIF_LEVEL_MASK (0x3fff) +/* Subdivision of HIF_LEVEL +*/ +#define M2M_HIF_MAJOR_SHIFT (8) +#define M2M_HIF_MAJOR_MASK (0x3f) +#define M2M_HIF_MINOR_SHIFT (0) +#define M2M_HIF_MINOR_MASK (0xff) + +#define M2M_GET_HIF_INFO(hif_ver) ((uint16_t)(((hif_ver) >> M2M_HIF_INFO_SHIFT) & M2M_HIF_INFO_MASK)) +#define M2M_GET_HIF_BLOCK(hif_info) ((uint8_t)(((hif_info) >> M2M_HIF_BLOCK_SHIFT) & M2M_HIF_BLOCK_MASK)) +#define M2M_GET_HIF_LEVEL(hif_info) ((uint16_t)(((hif_info) >> M2M_HIF_LEVEL_SHIFT) & M2M_HIF_LEVEL_MASK)) +#define M2M_GET_HIF_MAJOR(hif_info) ((uint8_t)(((hif_info) >> M2M_HIF_MAJOR_SHIFT) & M2M_HIF_MAJOR_MASK)) +#define M2M_GET_HIF_MINOR(hif_info) ((uint8_t)(((hif_info) >> M2M_HIF_MINOR_SHIFT) & M2M_HIF_MINOR_MASK)) + +#define M2M_MAKE_HIF_INFO(hif_level) ( \ + (((uint16_t)(hif_level) & M2M_HIF_LEVEL_MASK) << M2M_HIF_LEVEL_SHIFT) | \ + (((uint16_t)M2M_HIF_BLOCK_VALUE & M2M_HIF_BLOCK_MASK) << M2M_HIF_BLOCK_SHIFT) ) + +/*======*======*======*======* + HIF LEVEL + *======*======*======*======*/ + +#define M2M_HIF_BLOCK_VALUE (2) +/*!< Drv/Fw blocking compatibility check. + */ +#define M2M_HIF_MAJOR_VALUE (1) +/*!< Drv/Fw major compatibility check. +*/ +#define M2M_HIF_MINOR_VALUE (5) +/*!< Drv/Fw minor compatibility check. +*/ + +#define M2M_HIF_LEVEL ( \ + ((M2M_HIF_MAJOR_VALUE & M2M_HIF_MAJOR_MASK) << M2M_HIF_MAJOR_SHIFT) | \ + ((M2M_HIF_MINOR_VALUE & M2M_HIF_MINOR_MASK) << M2M_HIF_MINOR_SHIFT) \ + ) +/*!< HIF Level (Major/Minor) for Drv/Fw compatibility. +*/ + +/*======*======*======*======* + DRIVER VERSION NO INFO + *======*======*======*======*/ + +#define M2M_DRIVER_VERSION_MAJOR_NO (1) +/*!< Driver Major release version number. +*/ +#define M2M_DRIVER_VERSION_MINOR_NO (2) +/*!< Driver Minor release version number. +*/ +#define M2M_DRIVER_VERSION_PATCH_NO (0) +/*!< Driver patch release version number. +*/ + +/**@}*/ // VERSIONDEF + +/**@addtogroup WlanDefines + * @ingroup m2m_wifi + */ +/**@{*/ + +#define M2M_BUFFER_MAX_SIZE (1600UL - 4) +/*!< Maximum size for the shared packet buffer. + */ + +#define M2M_MAC_ADDRES_LEN 6 +/*!< The size of the 802 MAC address. + */ + +#define M2M_ETHERNET_HDR_OFFSET 34 +/*!< The offset of the Ethernet header within the WLAN Tx Buffer. + */ + + +#define M2M_ETHERNET_HDR_LEN 14 +/*!< Length of the Ethernet header in bytes. +*/ + + +#define M2M_MAX_SSID_LEN 33 +/*!< 1 more than the max SSID length. + This matches the size of SSID buffers (max SSID length + 1-byte length field). + */ + + +#define M2M_MAX_PSK_LEN 65 +/*!< 1 more than the WPA PSK length (in ASCII format). + This matches the size of the WPA PSK/Passphrase buffer (max ASCII contents + 1-byte length field). + Alternatively it matches the WPA PSK length (in ASCII format) + 1 byte NULL termination. + */ + +#define M2M_MIN_PSK_LEN 9 +/*!< 1 more than the minimum WPA PSK Passphrase length. + It matches the minimum WPA PSK Passphrase length + 1 byte NULL termination. + */ + +#define M2M_DEVICE_NAME_MAX 48 +/*!< Maximum Size for the device name including the NULL termination. + */ + +#define M2M_NTP_MAX_SERVER_NAME_LENGTH 32 +/*!< Maximum NTP server name length +*/ + +#define M2M_LISTEN_INTERVAL 1 +/*!< The STA uses the Listen Interval parameter to indicate to the AP how + many beacon intervals it shall sleep before it retrieves the queued frames + from the AP. +*/ + +#define MAX_HIDDEN_SITES 4 +/*!< + max number of hidden SSID supported by scan request +*/ + +#define M2M_CUST_IE_LEN_MAX 252 +/*!< The maximum size of IE (Information Element). +*/ + +#define M2M_CRED_STORE_FLAG 0x01 +/*!< Flag used in @ref tstrM2mConnCredHdr to indicate that Wi-Fi connection + credentials should be stored in WINC flash. +*/ +#define M2M_CRED_ENCRYPT_FLAG 0x02 +/*!< Flag used in @ref tstrM2mConnCredHdr to indicate that Wi-Fi connection + credentials should be encrypted when stored in WINC flash. +*/ +#define M2M_CRED_IS_STORED_FLAG 0x10 +/*!< Flag used in @ref tstrM2mConnCredHdr to indicate that Wi-Fi connection + credentials are stored in WINC flash. May only be set by WINC firmware. +*/ +#define M2M_CRED_IS_ENCRYPTED_FLAG 0x20 +/*!< Flag used in @ref tstrM2mConnCredHdr to indicate that Wi-Fi connection + credentials are encrypted in WINC flash. May only be set by WINC firmware. +*/ + +#define M2M_WIFI_CONN_BSSID_FLAG 0x01 +/*!< Flag used in @ref tstrM2mConnCredCmn to indicate that Wi-Fi connection + must be restricted to an AP with a certain BSSID. +*/ + +#define M2M_AUTH_1X_USER_LEN_MAX 100 +/*!< The maximum length (in ASCII characters) of domain name + username (including '@' or '\') + for authentication with Enterprise methods. +*/ +#define M2M_AUTH_1X_PASSWORD_LEN_MAX 256 +/*!< The maximum length (in ASCII characters) of password for authentication with Enterprise MSCHAPv2 methods. +*/ +#define M2M_AUTH_1X_PRIVATEKEY_LEN_MAX 256 +/*!< The maximum length (in bytes) of private key modulus for authentication with Enterprise TLS methods. + Private key exponent must be the same length as modulus, pre-padded with 0s if necessary. +*/ +#define M2M_AUTH_1X_CERT_LEN_MAX 1584 +/*!< The maximum length (in bytes) of certificate for authentication with Enterprise TLS methods. +*/ + +#define M2M_802_1X_UNENCRYPTED_USERNAME_FLAG 0x80 +/*!< Flag to indicate that the 802.1x user-name should be sent (unencrypted) in the initial EAP + identity response. Intended for use with EAP-TLS only. +*/ +#define M2M_802_1X_PREPEND_DOMAIN_FLAG 0x40 +/*!< Flag to indicate that the 802.1x domain name should be prepended to the user-name: + "Domain\Username". If the flag is not set then domain name is appended to the user-name: + "Username@Domain". (Note that the '@' or '\' must be included in the domain name.) +*/ +#define M2M_802_1X_MSCHAP2_FLAG 0x01 +/*!< Flag to indicate 802.1x MsChapV2 credentials: domain/user-name/password. +*/ +#define M2M_802_1X_TLS_FLAG 0x02 +/*!< Flag to indicate 802.1x TLS credentials: domain/user-name/private-key/certificate. +*/ + +#define M2M_802_1X_TLS_CLIENT_CERTIFICATE 1 +/*!< Info type used in @ref tstrM2mWifiAuthInfoHdr to indicate Enterprise TLS client certificate. +*/ +#define PSK_CALC_LEN 40 +/*!< PSK is 32 bytes generated either: + - from 64 ASCII characters + - by SHA1 operations on up to 63 ASCII characters + 40 byte array is required during SHA1 operations, so we define PSK_CALC_LEN as 40. +*/ + +/********************* + * + * WIFI GROUP requests + */ +#define M2M_CONFIG_CMD_BASE 1 +/*!< The base value of all the Host configuration commands opcodes. +*/ +#define M2M_STA_CMD_BASE 40 +/*!< The base value of all the Station mode host commands opcodes. +*/ +#define M2M_AP_CMD_BASE 70 +/*!< The base value of all the Access Point mode host commands opcodes. +*/ +/**@cond P2P_DOC + */ +#define M2M_P2P_CMD_BASE 90 +/*!< The base value of all the P2P mode host commands opcodes. +*/ +/**@endcond*/ //P2P_DOC +#define M2M_SERVER_CMD_BASE 100 +/*!< The base value of all the Power Save mode host commands codes. +*/ +#define M2M_GEN_CMD_BASE 105 +/*!< The base value of additional host wifi command opcodes. + * Usage restrictions (eg STA mode only) should always be made clear at the API layer in any case. +*/ +/********************** + * OTA GROUP requests + */ +#define M2M_OTA_CMD_BASE 100 +/*!< The base value of all the OTA mode host commands opcodes. + * The OTA messages have their own group so op codes can extended from 1 to M2M_MAX_GRP_NUM_REQ. +*/ + +#define M2M_MAX_GRP_NUM_REQ (127) +/*!< max number of request in one group equal to 127 as the last bit reserved for config or data pkt +*/ + +#define WEP_40_KEY_SIZE ((uint8_t)5) +/*!< The size in bytes of a 40-bit wep key. +*/ +#define WEP_104_KEY_SIZE ((uint8_t)13) +/*!< The size in bytes of a 104-bit wep key. +*/ + +#define WEP_40_KEY_STRING_SIZE ((uint8_t)10) +/*!< The string length of a 40-bit wep key. +*/ +#define WEP_104_KEY_STRING_SIZE ((uint8_t)26) +/*!< The string length of a 104-bit wep key. +*/ + +#define WEP_KEY_MAX_INDEX ((uint8_t)4) +/*!< WEP key index is in the range 1 to 4 inclusive. (This is decremented to + * result in an index in the range 0 to 3 on air.) +*/ +#define M2M_SCAN_DEFAULT_NUM_SLOTS (2) +/*!< The default number of scan slots used by the WINC board. +*/ +#define M2M_SCAN_DEFAULT_SLOT_TIME (20) +/*!< The active scan slot default duration in ms. +*/ +#define M2M_SCAN_DEFAULT_PASSIVE_SLOT_TIME (300) +/*!< The passive scan slot default duration in ms. +*/ +#define M2M_SCAN_DEFAULT_NUM_PROBE (2) +/*!< The default number of probes per slot. +*/ +#define M2M_FASTCONNECT_DEFAULT_RSSI_THRESH (-45) +/*!< The default threshold RSSI for fast reconnection to an AP. +*/ +#define M2M_SCAN_FAIL ((uint8_t)1) +/*!< Indicates the WINC board has failed to perform the scan operation. +*/ +#define M2M_JOIN_FAIL ((uint8_t)2) +/*!< Indicates the WINC board has failed to join the BSS. +*/ +#define M2M_AUTH_FAIL ((uint8_t)3) +/*!< Indicates the WINC board has failed to authenticate with the AP. +*/ +#define M2M_ASSOC_FAIL ((uint8_t)4) +/*!< Indicates the WINC board has failed to associate with the AP. +*/ + +#define M2M_SCAN_ERR_WIFI ((int8_t)-2) +/*!< Currently not used. +*/ +#define M2M_SCAN_ERR_IP ((int8_t)-3) +/*!< Currently not used. +*/ +#define M2M_SCAN_ERR_AP ((int8_t)-4) +/*!< Currently not used. +*/ +#define M2M_SCAN_ERR_P2P ((int8_t)-5) +/*!< Currently not used. +*/ +#define M2M_SCAN_ERR_WPS ((int8_t)-6) +/*!< Currently not used. +*/ + +/*======*======*======*======* + MONITORING MODE DEFINITIONS + *======*======*======*======*/ + +#define M2M_WIFI_FRAME_TYPE_ANY 0xFF +/*!< Receive any frame type when configured as Monitor Mode. +*/ +#define M2M_WIFI_FRAME_SUB_TYPE_ANY 0xFF +/*!< Receive frames with any sub type when configured as Monitor Mode. +*/ + +/*======*======*======*======* + TLS DEFINITIONS + *======*======*======*======*/ +#define TLS_FILE_NAME_MAX 48 +/*!< Maximum length for each TLS certificate file name. +*/ +#define TLS_SRV_SEC_MAX_FILES 8 +/*!< Maximum number of certificates allowed in TLS_SRV section. +*/ +#define TLS_SRV_SEC_START_PATTERN_LEN 8 +/*!< Length of certificate struct start pattern. +*/ + +/**@}*/ // WLANDefines + +/**@addtogroup OTADEFINE + * @{ + */ + +/*======*======*======*======* + OTA DEFINITIONS + *======*======*======*======*/ + +#define OTA_STATUS_VALID (0x12526285) +/*!< + Magic value in the control structure for a valid image after ROLLBACK. +*/ +#define OTA_STATUS_INVALID (0x23987718) +/*!< + Magic value in the control structure for a invalid image after ROLLBACK. +*/ +#define OTA_MAGIC_VALUE (0x1ABCDEF9) +/*!< + Magic value set at the beginning of the OTA image header. +*/ +#define OTA_SHA256_DIGEST_SIZE (32) +/*!< + SHA256 digest size in the OTA image. + The SHA256 digest is set at the beginning of image before the OTA header. +*/ +/**@}*/ // OTADEFINE + +/** +* @addtogroup WlanEnums +*/ +/**@{*/ + +/*! +@enum \ + tenuM2mReqGroup + +@brief +*/ +typedef enum { + M2M_REQ_GROUP_MAIN = 0, + M2M_REQ_GROUP_WIFI, + M2M_REQ_GROUP_IP, + M2M_REQ_GROUP_HIF, + M2M_REQ_GROUP_OTA, + M2M_REQ_GROUP_SSL, + M2M_REQ_GROUP_SIGMA, + M2M_REQ_GROUP_INTERNAL, +} tenuM2mReqGroup; + +/*! +@enum \ + tenuM2mReqpkt + +@brief +*/ +typedef enum { + M2M_REQ_CONFIG_PKT, + M2M_REQ_DATA_PKT = 0x80 /*BIT7*/ +} tenuM2mReqpkt; + +/*! +@enum \ + tenuM2mWepKeyIndex + +@brief + +*/ +typedef enum { + M2M_WIFI_WEP_KEY_INDEX_1 = ((uint8_t) 1), + /*!< Index 1 for WEP key Authentication */ + M2M_WIFI_WEP_KEY_INDEX_2, + /*!< Index 2 for WEP key Authentication */ + M2M_WIFI_WEP_KEY_INDEX_3, + /*!< Index 3 for WEP key Authentication */ + M2M_WIFI_WEP_KEY_INDEX_4, + /*!< Index 4 for WEP key Authentication */ +} tenuM2mWepKeyIndex; + +/*! +@enum \ + tenuM2mDefaultConnErrcode + +@brief + +*/ +typedef enum { + M2M_DEFAULT_CONN_INPROGRESS = ((int8_t)-23), + /*!< Failure response due to another connection being already in progress */ + M2M_DEFAULT_CONN_FAIL, + /*!< Failure to connect to the cached network */ + M2M_DEFAULT_CONN_SCAN_MISMATCH, + /*!< Failure to find any of the cached networks in the scan results. */ + M2M_DEFAULT_CONN_EMPTY_LIST + /*!< Failure due to empty network list. */ +} tenuM2mDefaultConnErrcode; + + +/*! +@enum \ + tenuM2mConnChangedErrcode + +@brief + +*/ +typedef enum { + M2M_ERR_SCAN_FAIL = ((uint8_t)1), + /*!< Failure to perform the scan operation. */ + M2M_ERR_JOIN_FAIL, + /*!< Failure to join the BSS. */ + M2M_ERR_AUTH_FAIL, + /*!< Failure to authenticate with the AP. */ + M2M_ERR_ASSOC_FAIL, + /*!< Failure to associate with the AP. */ + M2M_ERR_CONN_INPROGRESS, + /*!< Failure due to another connection being in progress. */ +} tenuM2mConnChangedErrcode; + + +/*! +@enum \ + tenuM2mSetGainsErrcode + +@brief + +*/ +typedef enum { + M2M_ERR_GAIN_TABLE_INVALID = ((int8_t)-10), + /*!< Failure response due to trying to use an invalid table */ + M2M_ERR_READ_GAIN_TABLE, + /*!< Failure to read gains from flash */ +} tenuM2mSetGainsErrcode; + +/*! +@enum \ + tenuM2mPwrMode + +@brief + +*/ +typedef enum { + PWR_AUTO = ((uint8_t) 1), + /*!< Automatic power mode. */ + PWR_LOW1, + /*!< Low power mode #1. RX current 60mA.*/ + PWR_LOW2, + /*!< Low power mode #2, RX current 55mA, sensitivity is less by 3dBm*/ + PWR_HIGH, + /*!< High power mode: RX current 100mA.*/ +} tenuM2mPwrMode; + +/*! +@enum \ + tenuM2mTxPwrLevel + +@brief + +*/ +typedef enum { + TX_PWR_HIGH = ((uint8_t) 1), + /*!< PPA Gain 6dbm PA Gain 18dbm */ + TX_PWR_MED, + /*!< PPA Gain 6dbm PA Gain 12dbm */ + TX_PWR_LOW, + /*!< PPA Gain 6dbm PA Gain 6dbm */ +} tenuM2mTxPwrLevel; + +/*! +@enum \ + tenuM2mConfigCmd + +@brief + This enum contains host commands used to configure the WINC board. + +*/ +typedef enum { + M2M_WIFI_REQ_RESTART = M2M_CONFIG_CMD_BASE, + /*!< Restart the WINC MAC layer, it's doesn't restart the IP layer. */ + M2M_WIFI_REQ_SET_MAC_ADDRESS, + /*!< Set the WINC mac address (not possible for production effused boards). */ + M2M_WIFI_REQ_CURRENT_RSSI, + /*!< Request the current connected AP RSSI. */ + M2M_WIFI_RESP_CURRENT_RSSI, + /*!< Response to M2M_WIFI_REQ_CURRENT_RSSI with the RSSI value. */ + M2M_WIFI_REQ_RESTRICT_BLE, + /*!< Request restrict ble. */ + M2M_WIFI_REQ_UNRESTRICT_BLE, + /*!< Request unrestrict ble. */ + M2M_WIFI_REQ_GET_CONN_INFO, + /*!< Request connection information. */ + M2M_WIFI_RESP_CONN_INFO, + /*!< Response to M2M_WIFI_REQ_GET_CONN_INFO with the connection information. */ + M2M_WIFI_REQ_SET_DEVICE_NAME, + /*!< Request to set WINC device name property. */ + M2M_WIFI_REQ_START_PROVISION_MODE_LEGACY, + /*!< Request to start provisioning mode. */ + M2M_WIFI_RESP_PROVISION_INFO, + /*!< Response to the host with the provisioning information.*/ + M2M_WIFI_REQ_STOP_PROVISION_MODE, + /*!< Request to stop provision mode. */ + M2M_WIFI_REQ_SET_SYS_TIME, + /*!< Request to set system time. */ + M2M_WIFI_REQ_ENABLE_SNTP_CLIENT, + /*!< Request to enable the simple network time protocol to get the + time from the Internet. This is required for security purposes. */ + M2M_WIFI_REQ_DISABLE_SNTP_CLIENT, + /*!< Request to disable the simple network time protocol for applications that + do not need it. */ + M2M_WIFI_RESP_MEMORY_RECOVER, + /*!< Reserved for debugging */ + M2M_WIFI_REQ_CUST_INFO_ELEMENT, + /*!< Request to add custom information to the Beacons IE. */ + M2M_WIFI_REQ_SCAN, + /*!< Request scan command. */ + M2M_WIFI_RESP_SCAN_DONE, + /*!< Response to notify scan complete. */ + M2M_WIFI_REQ_SCAN_RESULT, + /*!< Request for scan results. */ + M2M_WIFI_RESP_SCAN_RESULT, + /*!< Response to provide the scan results. */ + M2M_WIFI_REQ_SET_SCAN_OPTION, + /*!< Request to set scan options "slot time, slot number .. etc". */ + M2M_WIFI_REQ_SET_SCAN_REGION, + /*!< Request to set scan region. */ + M2M_WIFI_REQ_SET_POWER_PROFILE, + /*!< Request to set the Power Profile. */ + M2M_WIFI_REQ_SET_TX_POWER, + /*!< Request to set the TX Power. */ + M2M_WIFI_REQ_SET_BATTERY_VOLTAGE, + /*!< Request to set the Battery Voltage. */ + M2M_WIFI_REQ_SET_ENABLE_LOGS, + /*!< Request to enable logs. */ + M2M_WIFI_REQ_GET_SYS_TIME, + /*!< Request to get system time. */ + M2M_WIFI_RESP_GET_SYS_TIME, + /*!< Response to retrieve the system time. */ + M2M_WIFI_REQ_SEND_ETHERNET_PACKET, + /*!< Request to send Ethernet packet in bypass mode. */ + M2M_WIFI_RESP_ETHERNET_RX_PACKET, + /*!< Response to receive an Ethernet packet in bypass mode. */ + M2M_WIFI_REQ_SET_MAC_MCAST, + /*!< Request to set multicast filters. */ + M2M_WIFI_REQ_BLE_API_SEND, + /*!< Request to send an Encapsulated BLE API MSG */ + M2M_WIFI_RESP_BLE_API_RECV, + /*!< Response to receive an Encapsulated BLE API MSG */ + M2M_WIFI_REQ_SET_GAIN_TABLE, + /*!< Request to use Gain table from Flash */ + M2M_WIFI_RESP_SET_GAIN_TABLE, + /*!< Response to fail to use Gain table from Flash */ + M2M_WIFI_REQ_PASSIVE_SCAN, + /*!< Request a passive scan. */ + M2M_WIFI_REQ_CONFIG_SNTP, + /*!< Configure NTP servers. */ + M2M_WIFI_REQ_START_PROVISION_MODE, + /*!< Request to start provisioning mode. */ + + /* This enum is now 'full' in the sense that (M2M_WIFI_REQ_START_PROVISION_MODE+1) == M2M_STA_CMD_BASE. + * Any new config values should be placed in tenuM2mGenCmd. */ + M2M_WIFI_MAX_CONFIG_ALL +} tenuM2mConfigCmd; + +/*! +@enum \ + tenuM2mStaCmd + +@brief + This enum contains WINC commands while in Station mode. +*/ +typedef enum { + M2M_WIFI_REQ_CONNECT = M2M_STA_CMD_BASE, + /*!< Request to connect with a specified AP. This command is deprecated in favour of @ref M2M_WIFI_REQ_CONN. + */ + M2M_WIFI_REQ_DEFAULT_CONNECT, + /*!< Request to connect with a cached AP. */ + M2M_WIFI_RESP_DEFAULT_CONNECT, + /*!< Response for the default connect.*/ + M2M_WIFI_REQ_DISCONNECT, + /*!< Request to disconnect from the AP. */ + M2M_WIFI_RESP_CON_STATE_CHANGED, + /*!< Response to indicate a change in the connection state. */ + M2M_WIFI_REQ_SLEEP, + /*!< Request to sleep. */ + M2M_WIFI_REQ_WPS_SCAN, + /*!< Request to WPS scan. */ + M2M_WIFI_REQ_WPS, + /*!< Request to start WPS. */ + M2M_WIFI_REQ_START_WPS, + /*!< This command is for internal use by the WINC and + should not be used by the host driver. */ + M2M_WIFI_REQ_DISABLE_WPS, + /*!< Request to disable WPS. */ + M2M_WIFI_REQ_DHCP_CONF, + /*!< Response to indicate the obtained IP address.*/ + M2M_WIFI_RESP_IP_CONFIGURED, + /*!< This command is for internal use by the WINC and + should not be used by the host driver. */ + M2M_WIFI_RESP_IP_CONFLICT, + /*!< Response to indicate a conflict in obtained IP address. + The user should re attempt the DHCP request. */ + M2M_WIFI_REQ_ENABLE_MONITORING, + /*!< Request to enable monitor mode. */ + M2M_WIFI_REQ_DISABLE_MONITORING, + /*!< Request to disable monitor mode. */ + M2M_WIFI_RESP_WIFI_RX_PACKET, + /*!< Response to indicate a packet was received in monitor mode. */ + M2M_WIFI_REQ_SEND_WIFI_PACKET, + /*!< Request to send a packet in monitor mode. */ + M2M_WIFI_REQ_LSN_INT, + /*!< Request to set the listen interval. */ + M2M_WIFI_REQ_DOZE, + /*!< Request to doze */ + M2M_WIFI_REQ_GET_PRNG, + /*!< Request PRNG. */ + M2M_WIFI_RESP_GET_PRNG, + /*!< Response for PRNG. */ + M2M_WIFI_REQ_CONN, + /*!< New command to connect with AP. + This replaces M2M_WIFI_REQ_CONNECT. (Firmware continues to handle + M2M_WIFI_REQ_CONNECT for backwards compatibility purposes.) + */ + M2M_WIFI_IND_CONN_PARAM, + /*!< Provide extra information (such as Enterprise client certificate) required for connection. */ + M2M_WIFI_MAX_STA_ALL +} tenuM2mStaCmd; + +/*! +@enum \ + tenuM2mApCmd + +@brief + This enum contains WINC commands while in AP mode. +*/ +typedef enum { + M2M_WIFI_REQ_ENABLE_AP_LEGACY = M2M_AP_CMD_BASE, + /*!< Request to enable AP mode. */ + M2M_WIFI_REQ_DISABLE_AP, + /*!< Request to disable AP mode. */ + M2M_WIFI_REQ_ENABLE_AP, + /*!< Request to enable AP mode. */ + M2M_WIFI_MAX_AP_ALL, +} tenuM2mApCmd; + +/*! +@enum \ + tenuM2mP2pCmd + +@brief + This enum contains WINC commands while in P2P mode. +*/ +typedef enum { + M2M_WIFI_REQ_P2P_INT_CONNECT = M2M_P2P_CMD_BASE, + /*!< This command is for internal use by the WINC and + should not be used by the host driver. */ + M2M_WIFI_REQ_ENABLE_P2P, + /*!< Request to enable P2P mode.*/ + M2M_WIFI_REQ_DISABLE_P2P, + /*!< Request to disable P2P mode. */ + M2M_WIFI_REQ_P2P_REPOST, + /*!< This command is for internal use by the WINC and + should not be used by the host driver. + */ + M2M_WIFI_MAX_P2P_ALL, +} tenuM2mP2pCmd; + + +/*! +@enum \ + tenuM2mServerCmd + +@brief + These commands are currently not supported. +*/ +typedef enum { + M2M_WIFI_REQ_CLIENT_CTRL = M2M_SERVER_CMD_BASE, + /*!< Currently not supported.*/ + M2M_WIFI_RESP_CLIENT_INFO, + /*!< Currently not supported.*/ + M2M_WIFI_REQ_SERVER_INIT, + /*!< Currently not supported.*/ + M2M_WIFI_MAX_SERVER_ALL +} tenuM2mServerCmd; + +/*! +@enum \ + tenuM2mGenCmd + +@brief + This enum contains additional WINC commands (overflow of previous enums). +*/ +typedef enum { + M2M_WIFI_REQRSP_DELETE_APID = M2M_GEN_CMD_BASE, + /*!< Request/response to delete security credentials from WINC flash. + (Processing matches @ref tenuM2mConfigCmd.) */ + M2M_WIFI_REQ_ROAMING, + /*!< Request to enable/disable wifi roaming. + (Processing matches @ref tenuM2mConfigCmd.) + */ + M2M_WIFI_REQ_SCAN_SSID_LIST, + /*!< Request scan with list of hidden SSID plus the broadcast scan. + */ + M2M_WIFI_REQ_SET_STOP_SCAN_OPTION, + /*!< Set Scan option to stop on first result. + (Processing matches @ref tenuM2mConfigCmd.) + */ + M2M_WIFI_MAX_GEN_ALL +} tenuM2mGenCmd; + + +/*! +@enum \ + tenuM2mIpCmd + +@brief + This enum contains all the WINC commands related to IP. +*/ +typedef enum { + M2M_IP_REQ_STATIC_IP_CONF = ((uint8_t) 10), + /*!< Request to set static IP.*/ + M2M_IP_REQ_ENABLE_DHCP, + /*!< Request to enable DHCP.*/ + M2M_IP_REQ_DISABLE_DHCP + /*!< Request to disable DHCP.*/ +} tenuM2mIpCmd; + +/*! +@enum \ + tenuM2mSockCmd + +@brief + This enum contains all the WINC commands related to Sockets. +*/ +typedef enum { + SOCKET_CMD_BIND = 0x41, + /*!< Socket Binding command value. */ + SOCKET_CMD_LISTEN, + /*!< Socket Listening command value. */ + SOCKET_CMD_ACCEPT, + /*!< Socket Accepting command value. */ + SOCKET_CMD_CONNECT, + /*!< Socket Connecting command value. */ + SOCKET_CMD_SEND, + /*!< Socket send command value. */ + SOCKET_CMD_RECV, + /*!< Socket Receive command value. */ + SOCKET_CMD_SENDTO, + /*!< Socket sendTo command value. */ + SOCKET_CMD_RECVFROM, + /*!< Socket ReceiveFrom command value. */ + SOCKET_CMD_CLOSE, + /*!< Socket Close command value. */ + SOCKET_CMD_DNS_RESOLVE, + /*!< Socket DNS Resolve command value. */ + SOCKET_CMD_SSL_CONNECT, + /*!< SSL-Socket Connect command value. */ + SOCKET_CMD_SSL_SEND, + /*!< SSL-Socket Send command value. */ + SOCKET_CMD_SSL_RECV, + /*!< SSL-Socket Receive command value. */ + SOCKET_CMD_SSL_CLOSE, + /*!< SSL-Socket Close command value. */ + SOCKET_CMD_SET_SOCKET_OPTION, + /*!< Set Socket Option command value. */ + SOCKET_CMD_SSL_CREATE, + SOCKET_CMD_SSL_SET_SOCK_OPT, + SOCKET_CMD_PING, + SOCKET_CMD_SSL_CONNECT_ALPN, + /*!< SSL-Socket Connect with ALPN command value. */ + SOCKET_CMD_RAW_SET_SOCK_OPT, + SOCKET_CMD_SECURE + /*!< Make secure a previously opened socket. */ +} tenuM2mSockCmd; + +/*! +@enum \ + tenuM2mSigmaCmd + +@brief + This enum contains all the WINC commands related to Sigma. +*/ +typedef enum { + M2M_SIGMA_ENABLE = ((uint8_t) 3), + /*!< Enable Sigma.*/ + M2M_SIGMA_TA_START, + /*!< Start the traffic agent.*/ + M2M_SIGMA_TA_STATS, + /*!< Get traffic statistics.*/ + M2M_SIGMA_TA_RECEIVE_STOP, + /*!< Stop receiving from the traffic agent.*/ + M2M_SIGMA_ICMP_ARP, + /*!< Send ARP.*/ + M2M_SIGMA_ICMP_RX, + /*!< Receive ICMP.*/ + M2M_SIGMA_ICMP_TX, + /*!< Transmit ICMP.*/ + M2M_SIGMA_UDP_TX, + /*!< Transmit UDP.*/ + M2M_SIGMA_UDP_TX_DEFER, + /*!< Transmit UDP defer.*/ + M2M_SIGMA_SECURITY_POLICY, + /*!< Set security policy.*/ + M2M_SIGMA_SET_SYSTIME + /*!< Set system time.*/ +} tenuM2mSigmaCmd; + +/*! +@enum \ + tenuM2mConnState + +@brief + This enum contains all the Wi-Fi connection states. +*/ +typedef enum { + M2M_WIFI_DISCONNECTED = 0, + /*!< Wi-Fi state is disconnected. */ + M2M_WIFI_CONNECTED, + /*!< Wi-Fi state is connected. */ + M2M_WIFI_ROAMED, + /*!< Wi-Fi state is roamed to new AP. */ + M2M_WIFI_UNDEF = 0xff + /*!< Undefined Wi-Fi State. */ +} tenuM2mConnState; + +/*! +@enum \ + tenuM2mSecType + +@brief + This enum contains all the supported Wi-Fi security types. +*/ +typedef enum { + M2M_WIFI_SEC_INVALID = 0, + /*!< Invalid security type. */ + M2M_WIFI_SEC_OPEN, + /*!< Wi-Fi network is not secured. */ + M2M_WIFI_SEC_WPA_PSK, + /*!< Wi-Fi network is secured with WPA/WPA2 personal(PSK). */ + M2M_WIFI_SEC_WEP, + /*!< Security type WEP (40 or 104) OPEN OR SHARED. */ + M2M_WIFI_SEC_802_1X, + /*!< Wi-Fi network is secured with WPA/WPA2 Enterprise.IEEE802.1x. */ + M2M_WIFI_NUM_AUTH_TYPES + /*!< Upper limit for enum value. */ +} tenuM2mSecType; + + +/*! +@enum \ + tenuM2mSecType + +@brief + This enum contains all the supported Wi-Fi SSID types. +*/ +typedef enum { + SSID_MODE_VISIBLE = 0, + /*!< SSID is visible to others. */ + SSID_MODE_HIDDEN + /*!< SSID is hidden. */ +} tenuM2mSsidMode; + +/*! +@enum \ + tenuM2mScanCh + +@brief + This enum contains all the Wi-Fi RF channels. +*/ +typedef enum { + M2M_WIFI_CH_1 = ((uint8_t) 1), + /*!< Channel 1. */ + M2M_WIFI_CH_2, + /*!< Channel 2. */ + M2M_WIFI_CH_3, + /*!< Channel 3. */ + M2M_WIFI_CH_4, + /*!< Channel 4. */ + M2M_WIFI_CH_5, + /*!< Channel 5. */ + M2M_WIFI_CH_6, + /*!< Channel 6. */ + M2M_WIFI_CH_7, + /*!< Channel 7. */ + M2M_WIFI_CH_8, + /*!< Channel 8. */ + M2M_WIFI_CH_9, + /*!< Channel 9. */ + M2M_WIFI_CH_10, + /*!< Channel 10. */ + M2M_WIFI_CH_11, + /*!< Channel 11. */ + M2M_WIFI_CH_12, + /*!< Channel 12. */ + M2M_WIFI_CH_13, + /*!< Channel 13. */ + M2M_WIFI_CH_14, + /*!< Channel 14. */ + M2M_WIFI_CH_ALL = ((uint8_t) 255) +} tenuM2mScanCh; + +/*! +@enum \ + tenuM2mScanRegion + +@brief + This enum contains all the Wi-Fi channel regions. +*/ +typedef enum { + REG_CH_1 = ((uint16_t) 1 << 0), + /*!< Region channel 1. */ + REG_CH_2 = ((uint16_t) 1 << 1), + /*!< Region channel 2. */ + REG_CH_3 = ((uint16_t) 1 << 2), + /*!< Region channel 3. */ + REG_CH_4 = ((uint16_t) 1 << 3), + /*!< Region channel 4. */ + REG_CH_5 = ((uint16_t) 1 << 4), + /*!< Region channel 5. */ + REG_CH_6 = ((uint16_t) 1 << 5), + /*!< Region channel 6. */ + REG_CH_7 = ((uint16_t) 1 << 6), + /*!< Region channel 7. */ + REG_CH_8 = ((uint16_t) 1 << 7), + /*!< Region channel 8. */ + REG_CH_9 = ((uint16_t) 1 << 8), + /*!< Region channel 9. */ + REG_CH_10 = ((uint16_t) 1 << 9), + /*!< Region channel 10. */ + REG_CH_11 = ((uint16_t) 1 << 10), + /*!< Region channel 11. */ + REG_CH_12 = ((uint16_t) 1 << 11), + /*!< Region channel 12. */ + REG_CH_13 = ((uint16_t) 1 << 12), + /*!< Region channel 13. */ + REG_CH_14 = ((uint16_t) 1 << 13), + /*!< Region channel 14. */ + REG_CH_ALL = ((uint16_t) 0x3FFF), + /*!< Region for all channels. */ + NORTH_AMERICA = ((uint16_t) 0x7FF), + /*!< North America region with 11 channels*/ + EUROPE = ((uint16_t) 0x1FFF), + /*!u8Status == M2M_SUCCESS) + { + tstrNetworkId strNetworkId = {NULL, pstrProvInfo->au8SSID, (uint8_t)strlen((char*)(pstrProvInfo->au8SSID)), M2M_WIFI_CH_ALL}; + tstrAuthPsk strAuthPsk = {NULL, pstrProvInfo->au8Password, (uint8_t)strlen((char*)(pstrProvInfo->au8Password))}; + m2m_wifi_connect_psk(WIFI_CRED_SAVE_ENCRYPTED, &strNetworkId, &strAuthPsk); + + printf("PROV SSID : %s\n",pstrProvInfo->au8SSID); + printf("PROV PSK : %s\n",pstrProvInfo->au8Password); + } + else + { + printf("(ERR) Provisioning Failed\n"); + } + } + break; + + default: + break; + } +} + +int main() +{ + tstrWifiInitParam param; + param.pfAppWifiCb = wifi_event_cb; + if(!m2m_wifi_init(¶m)) + { + tstrM2MAPConfig apConfig; + uint8_t bEnableRedirect = 1; + + strcpy(apConfig.au8SSID, "WINC_SSID"); + apConfig.u8ListenChannel = 1; + apConfig.u8SecType = M2M_WIFI_SEC_OPEN; + apConfig.u8SsidHide = 0; + + // IP Address + apConfig.au8DHCPServerIP[0] = 192; + apConfig.au8DHCPServerIP[1] = 168; + apConfig.au8DHCPServerIP[2] = 1; + apConfig.au8DHCPServerIP[3] = 1; + + m2m_wifi_start_provision_mode(&apConfig, "atmelwincconf.com", bEnableRedirect); + + while(1) + { + m2m_wifi_handle_events(NULL); + } + } +} +@endcode +*/ +int8_t m2m_wifi_start_provision_mode(tstrM2MAPConfig *pstrAPConfig, char *pcHttpServerDomainName, uint8_t bEnableHttpRedirect); + +/*! +@ingroup WLANPROVISION +@fn \ + int8_t m2m_wifi_start_provision_mode_ext(tstrM2MAPModeConfig *pstrAPModeConfig, char *pcHttpServerDomainName, uint8_t bEnableHttpRedirect); + +@brief + Asynchronous API for control of Wi-Fi provisioning functionality with extended options. + +@details + This function allows the application to start the WINC in 'provisioning mode', a special mode + that triggers the WINC to create a Wi-Fi access point, DHCP server, and HTTP server. + + The HTTP server presents a provisioning page to a connected client which lists the access points + detected in the vicinity of the WINC, and allows one of these to be selected and any appropriate + credentials to be entered. This allows a headless system to be provisioned (configured to + connect with an access point). + + Provisioning status is returned in an event @ref M2M_WIFI_RESP_PROVISION_INFO. + +@param[in] pstrAPModeConfig + AP configuration parameters as defined in @ref tstrM2MAPModeConfig configuration structure. + A NULL value passed in, will result in a negative error @ref M2M_ERR_FAIL. + +@param[in] pcHttpServerDomainName + Domain name of the HTTP Provision WEB server which others will use to load the provisioning Home page. + The domain name can have one of the following 3 forms: + - 1. "wincprov.com" + - 2. "http://wincprov.com" + - 3. "https://wincprov.com" + + The forms 1 and 2 are equivalent, they both will start a plain http server, while form 3 + will start a secure HTTP provisioning Session (HTTP over SSL connection). + +@param[in] bEnableHttpRedirect + A flag to enable/disable the HTTP redirect feature. If Secure provisioning is enabled (i.e. the server + domain name uses "https" prefix) this flag is ignored (no meaning for redirect in HTTPS). + Possible values are: + - Zero: DO NOT use HTTP Redirect. In this case the associated device could open the + provisioning page ONLY when the HTTP Provision URL of the WINC HTTP Server is + correctly written on the browser. + - Non-Zero: Use HTTP Redirect. In this case, all http traffic (http://URL) from the + associated device (Phone, PC, ...etc) will be redirected to the WINC HTTP + Provisioning Home page. + +@return + The function returns @ref M2M_SUCCESS if the command has been successfully queued to the WINC and a negative value otherwise. + +@pre + - A Wi-Fi notification callback of type @ref tpfAppWifiCb MUST be implemented and registered at startup. + Registering the callback is done through passing it to the initialization @ref m2m_wifi_init function. + - The event @ref M2M_WIFI_RESP_CONN_INFO must be handled in the callback to receive the requested connection info. + +@warning + Do not use ".local" in the pcHttpServerDomainName. + +@see + tpfAppWifiCb + m2m_wifi_init + M2M_WIFI_RESP_PROVISION_INFO + m2m_wifi_stop_provision_mode + tstrM2MAPModeConfig + +@section WIFIExample12 Example + The example demonstrates a code snippet for how provisioning is triggered and the response event + received accordingly. + +@code + #include "m2m_wifi.h" + #include "m2m_types.h" + + void wifi_event_cb(uint8_t u8WiFiEvent, void * pvMsg) + { + switch(u8WiFiEvent) + { + case M2M_WIFI_RESP_PROVISION_INFO: + { + tstrM2MProvisionInfo *pstrProvInfo = (tstrM2MProvisionInfo*)pvMsg; + if(pstrProvInfo->u8Status == M2M_SUCCESS) + { + tstrNetworkId strNetworkId = {NULL, pstrProvInfo->au8SSID, (uint8_t)strlen((char*)(pstrProvInfo->au8SSID)), M2M_WIFI_CH_ALL}; + tstrAuthPsk strAuthPsk = {NULL, pstrProvInfo->au8Password, (uint8_t)strlen((char*)(pstrProvInfo->au8Password))}; + m2m_wifi_connect_psk(WIFI_CRED_SAVE_ENCRYPTED, &strNetworkId, &strAuthPsk); + + printf("PROV SSID : %s\n",pstrProvInfo->au8SSID); + printf("PROV PSK : %s\n",pstrProvInfo->au8Password); + } + else + { + printf("(ERR) Provisioning Failed\n"); + } + } + break; + + default: + break; + } + } + + int main() + { + tstrWifiInitParam param; + + param.pfAppWifiCb = wifi_event_cb; + if(!m2m_wifi_init(¶m)) + { + tstrM2MAPModeConfig apModeConfig; + uint8_t bEnableRedirect = 1; + + strcpy(apModeConfig.au8SSID, "WINC_SSID"); + apModeConfig.strApConfig.u8ListenChannel = 1; + apModeConfig.strApConfig.u8SecType = M2M_WIFI_SEC_OPEN; + apModeConfig.strApConfig.u8SsidHide = 0; + + // IP Address + apModeConfig.strApConfig.au8DHCPServerIP[0] = 192; + apModeConfig.strApConfig.au8DHCPServerIP[1] = 168; + apModeConfig.strApConfig.au8DHCPServerIP[2] = 1; + apModeConfig.strApConfig.au8DHCPServerIP[3] = 1; + + // Default router IP + memcpy(apModeConfig.strApConfigExt.au8DefRouterIP, apModeConfig.strApConfig.au8DHCPServerIP, 4); + + // DNS Server IP + memcpy(apModeConfig.strApConfigExt.au8DNSServerIP, apModeConfig.strApConfig.au8DHCPServerIP, 4); + + // Subnet mask + apModeConfig.strApConfigExt.au8SubnetMask[0] = 255; + apModeConfig.strApConfigExt.au8SubnetMask[1] = 255; + apModeConfig.strApConfigExt.au8SubnetMask[2] = 255; + apModeConfig.strApConfigExt.au8SubnetMask[3] = 0; + + m2m_wifi_start_provision_mode_ext(&apModeConfig, "atmelwincconf.com", bEnableRedirect); + + while(1) + { + m2m_wifi_handle_events(NULL); + } + } + } +@endcode +*/ +int8_t m2m_wifi_start_provision_mode_ext(tstrM2MAPModeConfig *pstrAPModeConfig, char *pcHttpServerDomainName, uint8_t bEnableHttpRedirect); + +/*! +@ingroup WLANPROVISION +@fn \ + int8_t m2m_wifi_stop_provision_mode(void); + +@brief + Synchronous API for terminating provisioning mode on the WINC. + +@details + This function will terminate any currently active provisioning mode on the WINC, returning the WINC to idle. + +@pre + An active provisioning session must be active before it is terminated through this function. + +@return + The function returns @ref M2M_SUCCESS for success and a negative value otherwise. + +@see + m2m_wifi_start_provision_mode +*/ +int8_t m2m_wifi_stop_provision_mode(void); + +/*! +@ingroup WLANCONNECT +@fn \ + int8_t m2m_wifi_get_connection_info(void); + +@brief + Asynchronous API for retrieving the WINC connection status. + +@details + Requests the connection status from the WINC including information regarding any access + point to which it is currently connected, or any non-AP station that is connected to the WINC. + All information will be returned to the application via the Wi-Fi notification callback through + the event @ref M2M_WIFI_RESP_CONN_INFO. + + The connection info can be retrieved using the structure @ref tstrM2MConnInfo which contains: + - Connection Security + - Connection RSSI + - Remote MAC address + - Remote IP address + - SSID of the network (in cases where the WINC is in non-AP mode) + +@pre + - A Wi-Fi notification callback of type @ref tpfAppWifiCb MUST be implemented and registered at + startup. Registering the callback is done through passing it to the initialization + @ref m2m_wifi_init function. + - The event @ref M2M_WIFI_RESP_CONN_INFO must be handled in the callback to receive the + requested connection info. + +@warning + - In case the WINC is operating in AP mode or P2P mode, the SSID field will be returned as a NULL string. + +@return + The function returns @ref M2M_SUCCESS if the command has been successfully queued to the WINC and a negative value otherwise. + +@see + M2M_WIFI_RESP_CONN_INFO, + tstrM2MConnInfo +@section WIFIExample2 Example + The code snippet shows an example of how wi-fi connection information is retrieved . +@code +#include "m2m_wifi.h" +#include "m2m_types.h" + +void wifi_event_cb(uint8_t u8WiFiEvent, void * pvMsg) +{ + switch(u8WiFiEvent) + { + case M2M_WIFI_RESP_CONN_INFO: + { + tstrM2MConnInfo *pstrConnInfo = (tstrM2MConnInfo*)pvMsg; + + printf("CONNECTED AP INFO\n"); + printf("SSID : %s\n",pstrConnInfo->acSSID); + printf("SEC TYPE : %d\n",pstrConnInfo->u8SecType); + printf("Signal Strength : %d\n", pstrConnInfo->s8RSSI); + printf("Local IP Address : %d.%d.%d.%d\n", + pstrConnInfo->au8IPAddr[0] , pstrConnInfo->au8IPAddr[1], pstrConnInfo->au8IPAddr[2], pstrConnInfo->au8IPAddr[3]); + } + break; + + case M2M_WIFI_REQ_DHCP_CONF: + { + // Get the current AP information. + m2m_wifi_get_connection_info(); + } + break; + default: + break; + } +} + +int main() +{ + tstrWifiInitParam param; + + param.pfAppWifiCb = wifi_event_cb; + if(!m2m_wifi_init(¶m)) + { + // connect to the default AP + m2m_wifi_default_connect(); + + while(1) + { + m2m_wifi_handle_events(NULL); + } + } +} +@endcode +*/ +int8_t m2m_wifi_get_connection_info(void); + +/*! +@ingroup WLANCONF +@fn \ + int8_t m2m_wifi_set_mac_address(uint8_t au8MacAddress[6]); + +@brief + Asynchronous API for assigning a MAC address to the WINC. + +@details + This function is intended to allow non-production software to assign a MAC address to the WINC. + +@warning + This function is intended for development use only and not for use in production software. + +@param[in] au8MacAddress + MAC Address to be provisioned to the WINC. + +@return + The function returns @ref M2M_SUCCESS if the command has been successfully queued to the WINC and a negative value otherwise. +*/ +int8_t m2m_wifi_set_mac_address(uint8_t au8MacAddress[6]); + +/*! +@ingroup WLANWPS +@fn \ + int8_t m2m_wifi_wps(uint8_t u8TriggerType,const char * pcPinNumber); + +@brief + Asynchronous API to engage the WINC Wi-Fi Protected Setup (enrollee) function. + +@details + This function can be called to make the WINC enter WPS (Wi-Fi Protected Setup) mode. The result + is passed to the Wi-Fi notification callback with the event @ref M2M_WIFI_REQ_WPS. + +@param[in] u8TriggerType + WPS Trigger method. This may be: + - @ref WPS_PIN_TRIGGER Push button method + - @ref WPS_PBC_TRIGGER Pin method + +@param[in] pcPinNumber + Valid only if the u8TriggerType is @ref WPS_PIN_TRIGGER, this parameter contains the PIN number. + The number must follow the format as given in the WSC1.0 specification. + +@return + The function returns @ref M2M_SUCCESS if the command has been successfully queued to the WINC and a negative value otherwise. + +@warning + This function is not allowed in AP or P2P modes. + +@pre + - A Wi-Fi notification callback of type (@ref tpfAppWifiCb MUST be implemented and registered at + startup. Registering the callback is done through passing it to @ref m2m_wifi_init. + - The event @ref M2M_WIFI_REQ_WPS must be handled in the callback to receive the WPS status. + - The WINC device MUST be in IDLE or STA mode. If AP or P2P mode is active, the WPS will not be performed. + - The @ref m2m_wifi_handle_events MUST be called periodically to receive + the responses in the callback. + +@see + tpfAppWifiCb + m2m_wifi_init + M2M_WIFI_REQ_WPS + tenuWPSTrigger + tstrM2MWPSInfo + +@section WIFIExample3 Example + The code snippet shows an example of how wi-fi WPS is triggered . +@code +#include "m2m_wifi.h" +#include "m2m_types.h" + +void wifi_event_cb(uint8_t u8WiFiEvent, void * pvMsg) +{ + switch(u8WiFiEvent) + { + case M2M_WIFI_REQ_WPS: + { + tstrM2MWPSInfo *pstrWPS = (tstrM2MWPSInfo*)pvMsg; + if(pstrWPS->u8AuthType != 0) + { + // establish Wi-Fi connection + tstrNetworkId strNetworkId = {NULL, pstrWPS->au8SSID, (uint8_t)strlen((char*)(pstrWPS->au8SSID)), pstrWPS->u8Ch}; + if(pstrWPS->u8AuthType == M2M_WIFI_SEC_OPEN) + { + m2m_wifi_connect_open(WIFI_CRED_SAVE_ENCRYPTED, &strNetworkId); + } + else + { + tstrAuthPsk strAuthPsk = {NULL, pstrWPS->au8PSK, (uint8_t)strlen((char*)(pstrWPS->au8PSK))}; + m2m_wifi_connect_psk(WIFI_CRED_SAVE_ENCRYPTED, &strNetworkId, &strAuthPsk); + } + + printf("WPS SSID : %s\n",pstrWPS->au8SSID); + printf("WPS PSK : %s\n",pstrWPS->au8PSK); + printf("WPS SSID Auth Type : %s\n",pstrWPS->u8AuthType == M2M_WIFI_SEC_OPEN ? "OPEN" : "WPA/WPA2"); + printf("WPS Channel : %d\n",pstrWPS->u8Ch); + } + else + { + printf("(ERR) WPS Is not enabled OR Timed out\n"); + } + } + break; + + default: + break; + } +} + +int main() +{ + tstrWifiInitParam param; + + param.pfAppWifiCb = wifi_event_cb; + if(!m2m_wifi_init(¶m)) + { + // Trigger WPS in Push button mode. + m2m_wifi_wps(WPS_PBC_TRIGGER, NULL); + + while(1) + { + m2m_wifi_handle_events(NULL); + } + } +} +@endcode +*/ +int8_t m2m_wifi_wps(uint8_t u8TriggerType, const char *pcPinNumber); + +/*! +@ingroup WLANWPS +@fn \ + int8_t m2m_wifi_wps_disable(void); + +@brief + Asynchronous API that disables Wi-Fi Protected Setup mode in the WINC. + +@pre + WINC should be already in WPS mode using @ref m2m_wifi_wps. + +@return + The function returns @ref M2M_SUCCESS if the command has been successfully queued to the WINC and a negative value otherwise. + +@see + m2m_wifi_wps +*/ +int8_t m2m_wifi_wps_disable(void); + +/**@cond P2P_DOC + */ +/*! +@ingroup WLANP2P +@fn \ + int8_t m2m_wifi_p2p(uint8_t u8Channel); + +@brief + Asynchronous API for enabling Wi-Fi Direct (P2P) mode in the WINC. + +@param[in] u8Channel + P2P Listen RF channel. According to the P2P standard, it must hold only one of the following values 1, 6 or 11. + +@pre + - A Wi-Fi notification callback of type @ref tpfAppWifiCb MUST be implemented and registered at + initialization. Registering the callback is done through passing it to the @ref m2m_wifi_init. + - The events @ref M2M_WIFI_RESP_CON_STATE_CHANGED and @ref M2M_WIFI_REQ_DHCP_CONF must be handled in the callback. + - The @ref m2m_wifi_handle_events MUST be called to receive the responses in the callback. + +@warning + - This function is not available in the WINC 3400 + - This function is not allowed in AP or STA modes. + +@return + The function returns @ref M2M_SUCCESS if the command has been successfully queued to the WINC and a negative value otherwise. + +@see + tpfAppWifiCb + m2m_wifi_init + M2M_WIFI_RESP_CON_STATE_CHANGED + M2M_WIFI_REQ_DHCP_CONF + tstrM2mWifiStateChanged + +@section WIFIExample4 Example + The code snippet shows an example of how the p2p mode operates. +@code +#include "m2m_wifi.h" +#include "m2m_types.h" + +void wifi_event_cb(uint8_t u8WiFiEvent, void * pvMsg) +{ + switch(u8WiFiEvent) + { + case M2M_WIFI_RESP_CON_STATE_CHANGED: + { + tstrM2mWifiStateChanged *pstrWifiState = (tstrM2mWifiStateChanged*)pvMsg; + M2M_INFO("Wifi State :: %s :: ErrCode %d\n", pstrWifiState->u8CurrState? "CONNECTED":"DISCONNECTED",pstrWifiState->u8ErrCode); + + // Do something + } + break; + + case M2M_WIFI_REQ_DHCP_CONF: + { + uint8_t *pu8IPAddress = (uint8_t*)pvMsg; + + printf("P2P IP Address \"%u.%u.%u.%u\"\n",pu8IPAddress[0],pu8IPAddress[1],pu8IPAddress[2],pu8IPAddress[3]); + } + break; + + default: + break; + } +} + +int main() +{ + tstrWifiInitParam param; + + param.pfAppWifiCb = wifi_event_cb; + if(!m2m_wifi_init(¶m)) + { + // Trigger P2P + m2m_wifi_p2p(M2M_WIFI_CH_1); + + while(1) + { + m2m_wifi_handle_events(NULL); + } + } +} +@endcode +*/ +int8_t m2m_wifi_p2p(uint8_t u8Channel); + +/*! +@ingroup WLANP2P +@fn \ + int8_t m2m_wifi_p2p_disconnect(void); + +@brief + Synchronous API to disable Wi-Fi Direct (P2P) Mode on the WINC. + +@pre + The p2p mode must be enabled and active before a disconnect can be called. + +@return + The function returns @ref M2M_SUCCESS if the command has been successfully queued to the WINC and a negative value otherwise. + +@see + m2m_wifi_p2p +*/ +int8_t m2m_wifi_p2p_disconnect(void); +/**@endcond*/ //P2P_DOC + +/*! +@ingroup WLANAP +@fn \ + int8_t m2m_wifi_enable_ap(const tstrM2MAPConfig* pstrM2MAPConfig); + +@brief + Asynchronous API to enable access point (AKA "hot-spot") mode on the WINC. + +@details + The WINC supports the ability to operate as an access point with the following limitations: + - Only 1 station may be associated at any given time. + - Open system and WEP are the only security suites supported. + +@param[in] pstrM2MAPConfig + A structure holding the AP configurations. + +@return + The function returns @ref M2M_SUCCESS if the command has been successfully queued to the WINC and a negative value otherwise. + +@warning + This function is not allowed in P2P or STA modes. + +@pre + - A Wi-Fi notification callback of type @ref tpfAppWifiCb MUST be implemented and registered at initialization. Registering the callback + is done through passing it to the @ref m2m_wifi_init. + - The event @ref M2M_WIFI_REQ_DHCP_CONF must be handled in the callback. + - The @ref m2m_wifi_handle_events MUST be called to receive the responses in the callback. + +@see + tpfAppWifiCb + tenuM2mSecType + m2m_wifi_init + M2M_WIFI_REQ_DHCP_CONF + tstrM2mWifiStateChanged + tstrM2MAPConfig + +@section WIFIExample5 Example + The code snippet demonstrates how the AP mode is enabled after the driver is initialized in the application's main function and the handling + of the event @ref M2M_WIFI_REQ_DHCP_CONF, to indicate successful connection. +@code +#include "m2m_wifi.h" +#include "m2m_types.h" + +void wifi_event_cb(uint8_t u8WiFiEvent, void * pvMsg) +{ + switch(u8WiFiEvent) + { + case M2M_WIFI_REQ_DHCP_CONF: + { + uint8_t *pu8IPAddress = (uint8_t*)pvMsg; + + printf("Associated STA has IP Address \"%u.%u.%u.%u\"\n",pu8IPAddress[0],pu8IPAddress[1],pu8IPAddress[2],pu8IPAddress[3]); + } + break; + + default: + break; + } +} + +int main() +{ + tstrWifiInitParam param; + param.pfAppWifiCb = wifi_event_cb; + if(!m2m_wifi_init(¶m)) + { + tstrM2MAPConfig apConfig; + + strcpy(apConfig.au8SSID, "WINC_SSID"); + apConfig.u8ListenChannel = 1; + apConfig.u8SecType = M2M_WIFI_SEC_OPEN; + apConfig.u8SsidHide = 0; + + // IP Address + apConfig.au8DHCPServerIP[0] = 192; + apConfig.au8DHCPServerIP[1] = 168; + apConfig.au8DHCPServerIP[2] = 1; + apConfig.au8DHCPServerIP[3] = 1; + + // Trigger AP + m2m_wifi_enable_ap(&apConfig); + + while(1) + { + m2m_wifi_handle_events(NULL); + } + } +} +@endcode +*/ +int8_t m2m_wifi_enable_ap(const tstrM2MAPConfig *pstrM2MAPConfig); + +/*! +@ingroup WLANAP +@fn \ + int8_t m2m_wifi_enable_ap_ext(const tstrM2MAPModeConfig *pstrM2MAPModeConfig); + +@brief + Asynchronous API to enable access point (AKA "hot-spot") mode on the WINC with extended options. + +@details + The WINC supports the ability to operate as an access point with the following limitations: + - Only 1 station may be associated at any given time. + - Open system and WEP are the only security suites supported. + +@param[in] pstrM2MAPModeConfig + A structure holding the AP configurations. + +@return + The function returns @ref M2M_SUCCESS if the command has been successfully queued to the WINC and a negative value otherwise. + +@warning + This function is not allowed in P2P or STA modes. + +@pre + - A Wi-Fi notification callback of type @ref tpfAppWifiCb MUST be implemented and registered at initialization. Registering the callback + is done through passing it to the @ref m2m_wifi_init. + - The event @ref M2M_WIFI_REQ_DHCP_CONF must be handled in the callback. + - The @ref m2m_wifi_handle_events MUST be called to receive the responses in the callback. + +@see + tpfAppWifiCb + tenuM2mSecType + m2m_wifi_init + M2M_WIFI_REQ_DHCP_CONF + tstrM2mWifiStateChanged + tstrM2MAPModeConfig + +@section WIFIExample13 Example + The code snippet demonstrates how the AP mode is enabled after the driver is initialized in the application's main function and the handling + of the event @ref M2M_WIFI_REQ_DHCP_CONF, to indicate successful connection. +@code + #include "m2m_wifi.h" + #include "m2m_types.h" + + void wifi_event_cb(uint8_t u8WiFiEvent, void * pvMsg) + { + switch(u8WiFiEvent) + { + case M2M_WIFI_REQ_DHCP_CONF: + { + uint8_t *pu8IPAddress = (uint8_t*)pvMsg; + + printf("Associated STA has IP Address \"%u.%u.%u.%u\"\n",pu8IPAddress[0],pu8IPAddress[1],pu8IPAddress[2],pu8IPAddress[3]); + } + break; + + default: + break; + } + } + + int main() + { + tstrWifiInitParam param; + + param.pfAppWifiCb = wifi_event_cb; + if(!m2m_wifi_init(¶m)) + { + tstrM2MAPModeConfig apModeConfig; + + strcpy(apModeConfig.strApConfig.au8SSID, "WINC_SSID"); + apModeConfig.strApConfig.u8ListenChannel = 1; + apModeConfig.strApConfig.u8SecType = M2M_WIFI_SEC_OPEN; + apModeConfig.strApConfig.u8SsidHide = 0; + + // IP Address + apModeConfig.strApConfig.au8DHCPServerIP[0] = 192; + apModeConfig.strApConfig.au8DHCPServerIP[1] = 168; + apModeConfig.strApConfig.au8DHCPServerIP[2] = 1; + apModeConfig.strApConfig.au8DHCPServerIP[3] = 1; + + // Default router IP + memcpy(apModeConfig.strApConfigExt.au8DefRouterIP, apModeConfig.strApConfig.au8DHCPServerIP, 4); + + // DNS Server IP + memcpy(apModeConfig.strApConfigExt.au8DNSServerIP, apModeConfig.strApConfig.au8DHCPServerIP, 4); + + // Subnet mask + apModeConfig.strApConfigExt.au8SubnetMask[0] = 255; + apModeConfig.strApConfigExt.au8SubnetMask[1] = 255; + apModeConfig.strApConfigExt.au8SubnetMask[2] = 255; + apModeConfig.strApConfigExt.au8SubnetMask[3] = 0; + + // Trigger AP + m2m_wifi_enable_ap_ext(&apModeConfig); + + while(1) + { + m2m_wifi_handle_events(NULL); + } + } + } +@endcode +*/ +int8_t m2m_wifi_enable_ap_ext(const tstrM2MAPModeConfig *pstrM2MAPModeConfig); + +/*! +@ingroup WLANAP +@fn \ + int8_t m2m_wifi_disable_ap(void); + +@brief + Synchronous API to disable access point mode on the WINC. + +@details + Must be called only when the AP is enabled through the @ref m2m_wifi_enable_ap + function. Otherwise the call to this function will not be useful. + +@return + The function returns @ref M2M_SUCCESS if the command has been successfully queued to the WINC and a negative value otherwise. + +@see + m2m_wifi_enable_ap +*/ +int8_t m2m_wifi_disable_ap(void); + +/*! +@ingroup WLANCONNECT +@fn \ + int8_t m2m_wifi_set_static_ip(tstrM2MIPConfig * pstrStaticIPConf); + +@brief + Asynchronous API to manually assign a (static) IP address to the WINC. + +@details + Assigns a static IP address to the WINC. + Typically an infrastructure access point will be able to provide an IP address to all clients + after they associate. The WINC will request configuration via DHCP automatically after + successfully connecting to an access point. + This function should only be called in the event that the network has no DHCP server or in case the application + wants to assign a predefined known IP address and the application. + This function can be used to assign a static IP address in case the application knows the specifics of the network. + The user must keep in mind that assigning a static IP address might + result in an IP address conflict. In case of an IP address conflict observed + by the WINC the user will get a response of @ref M2M_WIFI_RESP_IP_CONFLICT + in the wifi callback. The application is then responsible to either solve the + conflict or assign another IP address. + +@pre + The application must first call @ref m2m_wifi_enable_dhcp to request that DHCP functionality is + disabled prior to calling this function. + +@warning + Exercise caution using this function. + DHCP is the preferred method for configuring IP addresses. + +@param[in] pstrStaticIPConf + Pointer to a structure holding the static IP configuration (IP, Gateway, subnet mask and DNS address). + +@return + The function returns @ref M2M_SUCCESS if the command has been successfully queued to the WINC and a negative value otherwise. + +@see + tstrM2MIPConfig +*/ +int8_t m2m_wifi_set_static_ip(tstrM2MIPConfig *pstrStaticIPConf); + +/*! +@ingroup WLANCONNECT +@fn \ + int8_t m2m_wifi_request_dhcp_client(void); + +@brief + Legacy (deprecated) Asynchronous API for starting a DHCP client on the WINC. + +@details + This is a legacy API and is no longer supported. Calls to this API will not result in any + changes being made to the state of the WINC. + +@warning + This function has been deprecated. DHCP is used automatically when the WINC connects. + +@return + This function always returns @ref M2M_SUCCESS. +*/ +int8_t m2m_wifi_request_dhcp_client(void); + +/*! +@ingroup WLANCONNECT +@fn \ + int8_t m2m_wifi_request_dhcp_server(uint8_t* addr); + +@brief + Legacy (deprecated) asynchronous function to start a DHCP client on the WINC. + +@details + This is a legacy API and is no longer supported. Calls to this API will not result in any + changes being made to the state of the WINC. + +@param[in] addr + The address to issue to a connected client (only one client is supported) + +@warning + This function is legacy and exists only for compatibility with older applications. + DHCP server is started automatically when enabling the AP mode. + +@return + This function always returns @ref M2M_SUCCESS. +*/ +int8_t m2m_wifi_request_dhcp_server(uint8_t *addr); + +/*! +@ingroup WLANCONNECT +@fn \ + int8_t m2m_wifi_enable_dhcp(uint8_t u8DhcpEn); + +@brief + Asynchronous function to control the DHCP client functionality within the WINC. + +@details + This function allows the application to control the behaviour of the DHCP client function within + the WINC once it has associated with an access point. DHCP client functionality is enabled by + default. + +@param[in] u8DhcpEn + The state of the DHCP client feature after successful association with an access point: + - 1: Enables DHCP client after connection. + - 0: Disables DHCP client after connection. + +@return + The function returns @ref M2M_SUCCESS for successful operation and a negative value otherwise. + +@warning + DHCP client is enabled by default. + This Function should be called to disable DHCP client operation before using @ref m2m_wifi_set_static_ip. + +@see + m2m_wifi_set_static_ip +*/ +int8_t m2m_wifi_enable_dhcp(uint8_t u8DhcpEn); + +/*! +@ingroup WLANSCAN +@fn \ + int8_t m2m_wifi_set_scan_options(tstrM2MScanOption* ptstrM2MScanOption); + +@brief + Synchronous API for configuring the behaviour of the WINC network scanning functions. + +@details + This function allows the application to tune the scanning behaviour of the WINC using the + parameters described in @ref tstrM2MScanOption. + +@param[in] ptstrM2MScanOption; + Pointer to the structure holding the Scan Parameters. + +@return + The function returns @ref M2M_SUCCESS if the command has been successfully queued to the WINC and a negative value otherwise. + +@see + tenuM2mScanCh + m2m_wifi_request_scan + tstrM2MScanOption +*/ +int8_t m2m_wifi_set_scan_options(tstrM2MScanOption *ptstrM2MScanOption); + +/*! +@ingroup WLANSCAN +@fn \ + int8_t m2m_wifi_set_stop_scan_on_first(uint8_t u8StopScanOption); + +@brief + Synchronous API for enabling/disabling the stop scan on first result of the WINC IC's network scanning functions. + +@details + Allows for enabling/disabling of stop scan on first result. When enabled, the WINC will stop the scan as soon as + it detects a network and return the results to the host. Setting is persistent and will need to be explicitly + reverted back by the application if it no longer wishes for it to be enabled. + +@param[in] u8StopScanOption; + Setting for enabling or disabling Stopping Scan on first result. + 1 = Enabled, 0 = Disabled (Default) + +@return + The function returns @ref M2M_SUCCESS if the command has been successfully queued to the WINC and a negative value otherwise. + +@see + tenuM2mScanCh + tstrM2MScanOption + tstrM2MStopScanOption + m2m_wifi_request_scan + m2m_wifi_set_scan_options +*/ +int8_t m2m_wifi_set_stop_scan_on_first(uint8_t u8StopScanOption); + +/*! +@ingroup WLANSCAN +@fn \ + int8_t m2m_wifi_set_scan_region(uint16_t ScanRegion); + +@brief + Synchronous API for configuring the regulatory restrictions that may affect the WINC scanning behaviour. + +@details + This function sets a property called the scan region, a parameter that affects the range of + channels that the WINC may legally scan given a geographic region. + + For 2.4GHz, supported in the current release, the requested scan region cannot exceed the + maximum number of channels (14). + +@param[in] ScanRegion + @ref ASIA + @ref EUROPE + @ref NORTH_AMERICA + +@return + The function returns @ref M2M_SUCCESS if the command has been successfully queued to the WINC and a negative value otherwise. + +@see + tenuM2mScanRegion + m2m_wifi_request_scan +*/ +int8_t m2m_wifi_set_scan_region(uint16_t ScanRegion); + +/*! +@ingroup WLANSCAN +@fn \ + int8_t m2m_wifi_request_scan(uint8_t ch); + +@brief + Asynchronous API to request the WINC to scan for networks. + +@details + Scan statuses are delivered to the application via the Wi-Fi event callback (@ref tpfAppWifiCb) in + three stages. The first step involves the event @ref M2M_WIFI_RESP_SCAN_DONE which, if successful, + provides the number of detected networks (access points). The application must then read the list + of access points via multiple calls to the asynchronous @ref m2m_wifi_req_scan_result API. For + each call to this function, the application will receive (step three) the event + @ref M2M_WIFI_RESP_SCAN_RESULT. + +@param[in] ch + RF Channel ID for SCAN operation. It should be set according to @ref tenuM2mScanCh, with a + value of @ref M2M_WIFI_CH_ALL to scan all channels. + +@return + The function returns @ref M2M_SUCCESS if the command has been successfully queued to the WINC and a negative value otherwise. + +@pre + - A Wi-Fi notification callback of type @ref tpfAppWifiCb MUST be implemented and registered at + initialization. Registration of the callback is done via @ref m2m_wifi_init. + - The events @ref M2M_WIFI_RESP_SCAN_DONE and @ref M2M_WIFI_RESP_SCAN_RESULT must be handled in + the (@ref tpfAppWifiCb) callback. + - The @ref m2m_wifi_handle_events function MUST be called to receive the responses in the + callback. + +@warning + This API is valid only for STA mode, it may be called regardless of connection state (connected or disconnected states). + +@see + M2M_WIFI_RESP_SCAN_DONE + M2M_WIFI_RESP_SCAN_RESULT + tpfAppWifiCb + tstrM2mWifiscanResult + tenuM2mScanCh + m2m_wifi_init + m2m_wifi_handle_events + m2m_wifi_req_scan_result + +@section WIFIExample6 Example + The code snippet demonstrates an example of how the scan request is called from the application's main function and the handling of + the events received in response. +@code +#include "m2m_wifi.h" +#include "m2m_types.h" + +void wifi_event_cb(uint8_t u8WiFiEvent, void * pvMsg) +{ + static uint8_t u8ScanResultIdx = 0; + + switch(u8WiFiEvent) + { + case M2M_WIFI_RESP_SCAN_DONE: + { + tstrM2mScanDone *pstrInfo = (tstrM2mScanDone*)pvMsg; + + printf("Num of AP found %d\n",pstrInfo->u8NumofCh); + if(pstrInfo->s8ScanState == M2M_SUCCESS) + { + u8ScanResultIdx = 0; + if(pstrInfo->u8NumofCh >= 1) + { + m2m_wifi_req_scan_result(u8ScanResultIdx); + u8ScanResultIdx ++; + } + else + { + printf("No AP Found Rescan\n"); + m2m_wifi_request_scan(M2M_WIFI_CH_ALL); + } + } + else + { + printf("(ERR) Scan fail with error <%d>\n",pstrInfo->s8ScanState); + } + } + break; + + case M2M_WIFI_RESP_SCAN_RESULT: + { + tstrM2mWifiscanResult *pstrScanResult = (tstrM2mWifiscanResult*)pvMsg; + uint8_t u8NumFoundAPs = m2m_wifi_get_num_ap_found(); + + printf(">>%02d RI %d SEC %s CH %02d BSSID %02X:%02X:%02X:%02X:%02X:%02X SSID %s\n", + pstrScanResult->u8index,pstrScanResult->s8rssi, + pstrScanResult->u8AuthType, + pstrScanResult->u8ch, + pstrScanResult->au8BSSID[0], pstrScanResult->au8BSSID[1], pstrScanResult->au8BSSID[2], + pstrScanResult->au8BSSID[3], pstrScanResult->au8BSSID[4], pstrScanResult->au8BSSID[5], + pstrScanResult->au8SSID); + + if(u8ScanResultIdx < u8NumFoundAPs) + { + // Read the next scan result + m2m_wifi_req_scan_result(index); + u8ScanResultIdx ++; + } + } + break; + default: + break; + } +} + +int main() +{ + tstrWifiInitParam param; + param.pfAppWifiCb = wifi_event_cb; + if(!m2m_wifi_init(¶m)) + { + // Scan all channels + m2m_wifi_request_scan(M2M_WIFI_CH_ALL); + + while(1) + { + m2m_wifi_handle_events(NULL); + } + } +} +@endcode +*/ +int8_t m2m_wifi_request_scan(uint8_t ch); + +/*! +@ingroup WLANSCAN +@fn \ + int8_t m2m_wifi_request_scan_passive(uint8_t ch); + +@brief + Similar to @ref m2m_wifi_request_scan but performs passive scanning instead of active scanning. + +@param[in] ch + RF Channel ID for SCAN operation. It should be set according to @ref tenuM2mScanCh. + With a value of @ref M2M_WIFI_CH_ALL, means to scan all channels. + + +@warning + This function is not allowed in P2P or AP modes. It works only for STA mode (both connected or disconnected states). + +@pre + - A Wi-Fi notification callback of type @ref tpfAppWifiCb MUST be implemented and registered at initialization. Registering the callback + is done through passing it to the @ref m2m_wifi_init. + - The events @ref M2M_WIFI_RESP_SCAN_DONE and @ref M2M_WIFI_RESP_SCAN_RESULT. + must be handled in the callback. + - The @ref m2m_wifi_handle_events function MUST be called to receive the responses in the callback. + +@see + M2M_WIFI_RESP_SCAN_DONE + M2M_WIFI_RESP_SCAN_RESULT + tpfAppWifiCb + tstrM2MScanOption + tstrM2mWifiscanResult + tenuM2mScanCh + m2m_wifi_init + m2m_wifi_request_scan + m2m_wifi_handle_events + m2m_wifi_req_scan_result + +@return + The function returns @ref M2M_SUCCESS for successful operations and a negative value otherwise. +*/ +int8_t m2m_wifi_request_scan_passive(uint8_t ch); + +/*! +@ingroup WLANSCAN +@fn \ + int8_t m2m_wifi_request_scan_ssid_list(uint8_t ch, uint8_t *u8SsidList); + +@brief + Asynchronous wi-fi scan request on the given channel and the hidden scan list. + +@details + The scan status is delivered in the wi-fi event callback and then the application + is to read the scan results sequentially. + The number of APs found (N) is returned in event @ref M2M_WIFI_RESP_SCAN_DONE with the number of found + APs. + The application could read the list of APs by calling the function @ref m2m_wifi_req_scan_result N times. + +@param[in] ch + RF Channel ID for SCAN operation. It should be set according to @ref tenuM2mScanCh. + With a value of @ref M2M_WIFI_CH_ALL, means to scan all channels. + +@param[in] u8SsidList + u8SsidList is a buffer containing a list of hidden SSIDs to + include during the scan. The first byte in the buffer, u8SsidList[0], + is the number of SSIDs encoded in the string. The number of hidden SSIDs + cannot exceed @ref MAX_HIDDEN_SITES. All SSIDs are concatenated in the following + bytes and each SSID is prefixed with a one-byte header containing its length. + The total number of bytes in u8SsidList buffer, including length byte, cannot + exceed 133 bytes (MAX_HIDDEN_SITES SSIDs x 32 bytes each, which is max SSID length). + For instance, encoding the two hidden SSIDs "DEMO_AP" and "TEST" + results in the following buffer content: + +@code + uint8_t u8SsidList[14]; + u8SsidList[0] = 2; // Number of SSIDs is 2 + u8SsidList[1] = 7; // Length of the string "DEMO_AP" without NULL termination + memcpy(&u8SsidList[2], "DEMO_AP", 7); // Bytes index 2-9 containing the string DEMO_AP + u8SsidList[9] = 4; // Length of the string "TEST" without NULL termination + memcpy(&u8SsidList[10], "TEST", 4); // Bytes index 10-13 containing the string TEST +@endcode + +@note + It works with STA/AP mode (connected or disconnected). + +@pre + - A Wi-Fi notification callback of type @ref tpfAppWifiCb MUST be implemented and registered at initialization. Registering the callback + is done through passing it to the @ref m2m_wifi_init. + - The events @ref M2M_WIFI_RESP_SCAN_DONE and @ref M2M_WIFI_RESP_SCAN_RESULT. + must be handled in the callback. + - The @ref m2m_wifi_handle_events function MUST be called to receive the responses in the callback. + +@see + M2M_WIFI_RESP_SCAN_DONE + M2M_WIFI_RESP_SCAN_RESULT + tpfAppWifiCb + tstrM2mWifiscanResult + tenuM2mScanCh + m2m_wifi_init + m2m_wifi_handle_events + m2m_wifi_req_scan_result + +@return + The function returns @ref M2M_SUCCESS for successful operations and a negative value otherwise. + +\section WIFIExample6b Example + The code snippet demonstrates an example of how the scan request is called from the application's main function and the handling of + the events received in response. +@code +#include "m2m_wifi.h" +#include "m2m_types.h" + +static void request_scan_hidden_demo_ap(void); + +void wifi_event_cb(uint8_t u8WiFiEvent, void * pvMsg) +{ + static uint8_t u8ScanResultIdx = 0; + + switch(u8WiFiEvent) + { + case M2M_WIFI_RESP_SCAN_DONE: + { + tstrM2mScanDone *pstrInfo = (tstrM2mScanDone*)pvMsg; + + printf("Num of AP found %d\n",pstrInfo->u8NumofCh); + if(pstrInfo->s8ScanState == M2M_SUCCESS) + { + u8ScanResultIdx = 0; + if(pstrInfo->u8NumofCh >= 1) + { + m2m_wifi_req_scan_result(u8ScanResultIdx); + u8ScanResultIdx ++; + } + else + { + printf("No AP Found Rescan\n"); + request_scan_hidden_demo_ap(); + } + } + else + { + printf("(ERR) Scan fail with error <%d>\n",pstrInfo->s8ScanState); + } + } + break; + case M2M_WIFI_RESP_SCAN_RESULT: + { + tstrM2mWifiscanResult *pstrScanResult =(tstrM2mWifiscanResult*)pvMsg; + uint8_t u8NumFoundAPs = m2m_wifi_get_num_ap_found(); + + printf(">>%02d RI %d SEC %s CH %02d BSSID %02X:%02X:%02X:%02X:%02X:%02X SSID %s\n", + pstrScanResult->u8index,pstrScanResult->s8rssi, + pstrScanResult->u8AuthType, + pstrScanResult->u8ch, + pstrScanResult->au8BSSID[0], pstrScanResult->au8BSSID[1], pstrScanResult->au8BSSID[2], + pstrScanResult->au8BSSID[3], pstrScanResult->au8BSSID[4], pstrScanResult->au8BSSID[5], + pstrScanResult->au8SSID); + + if(u8ScanResultIdx < u8NumFoundAPs) + { + // Read the next scan result + m2m_wifi_req_scan_result(index); + u8ScanResultIdx ++; + } + } + break; + default: + break; + } +} + +static void request_scan_hidden_demo_ap(void) +{ + uint8_t list[9]; + char ssid[] = "DEMO_AP"; + uint8 len = (uint8_t)(sizeof(ssid)-1); + + list[0] = 1; + list[1] = len; + memcpy(&list[2], ssid, len); // copy 7 bytes + // Scan all channels + m2m_wifi_request_scan_ssid_list(M2M_WIFI_CH_ALL, list); +} + +int main() +{ + tstrWifiInitParam param; + + param.pfAppWifiCb = wifi_event_cb; + if(!m2m_wifi_init(¶m)) + { + request_scan_hidden_demo_ap(); + while(1) + { + m2m_wifi_handle_events(NULL); + } + } +} +@endcode +*/ +int8_t m2m_wifi_request_scan_ssid_list(uint8_t ch, uint8_t *u8Ssidlist); + +/*! +@ingroup WLANSCAN +@fn \ + uint8_t m2m_wifi_get_num_ap_found(void); + +@brief + Synchronous function to retrieve the number of AP's found during the last scan operation. + +@details + This function allows the application to recover the number of access points discovered during + the most recent scan activity. This is achieved via a global variable in the WINC driver that + is populated when receiving the @ref M2M_WIFI_RESP_SCAN_DONE event. + Function to be used in STA mode only. + +@see + m2m_wifi_request_scan + M2M_WIFI_RESP_SCAN_DONE + M2M_WIFI_RESP_SCAN_RESULT + +@pre + m2m_wifi_request_scan must be called first to ensure up to date results are available. + - A Wi-Fi notification callback of type @ref tpfAppWifiCb MUST be implemented and registered at initialization. Registering the callback + is done through passing it to the @ref m2m_wifi_init. + - The event @ref M2M_WIFI_RESP_SCAN_DONE must be handled in the callback to receive the requested scan information. + +@warning + This function must be called only in the wi-fi callback function when the events + @ref M2M_WIFI_RESP_SCAN_DONE or @ref M2M_WIFI_RESP_SCAN_RESULT are received. + Calling this function in any other place will result in undefined/outdated numbers. + +@return + Returns the number of AP's found in the last Scan Request. + +@section WIFIExample7 Example + The code snippet demonstrates an example of how the scan request is called from the application's main function and the handling of + the events received in response. +@code +#include "m2m_wifi.h" +#include "m2m_types.h" + +void wifi_event_cb(uint8_t u8WiFiEvent, void * pvMsg) +{ + static uint8_t u8ScanResultIdx = 0; + + switch(u8WiFiEvent) + { + case M2M_WIFI_RESP_SCAN_DONE: + { + tstrM2mScanDone *pstrInfo = (tstrM2mScanDone*)pvMsg; + + printf("Num of AP found %d\n",pstrInfo->u8NumofCh); + if(pstrInfo->s8ScanState == M2M_SUCCESS) + { + u8ScanResultIdx = 0; + if(pstrInfo->u8NumofCh >= 1) + { + m2m_wifi_req_scan_result(u8ScanResultIdx); + u8ScanResultIdx ++; + } + else + { + printf("No AP Found Rescan\n"); + m2m_wifi_request_scan(M2M_WIFI_CH_ALL); + } + } + else + { + printf("(ERR) Scan fail with error <%d>\n",pstrInfo->s8ScanState); + } + } + break; + case M2M_WIFI_RESP_SCAN_RESULT: + { + tstrM2mWifiscanResult *pstrScanResult =(tstrM2mWifiscanResult*)pvMsg; + uint8_t u8NumFoundAPs = m2m_wifi_get_num_ap_found(); + + printf(">>%02d RI %d SEC %s CH %02d BSSID %02X:%02X:%02X:%02X:%02X:%02X SSID %s\n", + pstrScanResult->u8index,pstrScanResult->s8rssi, + pstrScanResult->u8AuthType, + pstrScanResult->u8ch, + pstrScanResult->au8BSSID[0], pstrScanResult->au8BSSID[1], pstrScanResult->au8BSSID[2], + pstrScanResult->au8BSSID[3], pstrScanResult->au8BSSID[4], pstrScanResult->au8BSSID[5], + pstrScanResult->au8SSID); + + if(u8ScanResultIdx < u8NumFoundAPs) + { + // Read the next scan result + m2m_wifi_req_scan_result(index); + u8ScanResultIdx ++; + } + } + break; + default: + break; + } +} + +int main() +{ + tstrWifiInitParam param; + + param.pfAppWifiCb = wifi_event_cb; + if(!m2m_wifi_init(¶m)) + { + // Scan all channels + m2m_wifi_request_scan(M2M_WIFI_CH_ALL); + + while(1) + { + m2m_wifi_handle_events(NULL); + } + } +} +@endcode +*/ +uint8_t m2m_wifi_get_num_ap_found(void); + +/*! +@ingroup WLANSCAN +@fn \ + int8_t m2m_wifi_req_scan_result(uint8_t index); + +@brief + Asynchronous API to request the information of an access point discovered via scanning. + +@details + This function allows the information of any discovered access point to be retrieved. When a + scan is completed, the application is informed of the number of networks (access points) + discovered. Calling this function with an index, N, will return the information for the Nth + access point. The information will be returned to the application via a + @ref M2M_WIFI_RESP_SCAN_RESULT event, and the response data may be obtained through casting + the pointer (pvMsg) to @ref tstrM2mWifiscanResult. + +@param[in] index + Index for the requested result, the index range start from 0 till number of AP's found. + +@return + The function returns @ref M2M_SUCCESS if the command has been successfully queued to the WINC and a negative value otherwise. + +@see + tstrM2mWifiscanResult + m2m_wifi_get_num_ap_found + m2m_wifi_request_scan + +@pre + - @ref m2m_wifi_request_scan must be called first to ensure up to date results are available. + - A Wi-Fi notification callback of type @ref tpfAppWifiCb MUST be implemented and registered + in order to receive scan data after calling this function. Registration of the callback + is done via the @ref m2m_wifi_init function. + - The event @ref M2M_WIFI_RESP_SCAN_RESULT must be handled in the callback to receive the + requested scan information. + +@warning + - This API is valid only for STA mode, it may be called regardless of the connection state (connected or disconnected). + - Calling this function without first issuing a scan request may lead to stale data being recovered. + - Application code should refrain from introducing significant delays between issuing the scan + request and scan result requests. + +@section WIFIExample8 Example + The code snippet demonstrates an example of how the scan request is called from the application's main function and + the handling of the events received in the response. +@code +#include "m2m_wifi.h" +#include "m2m_types.h" + +void wifi_event_cb(uint8_t u8WiFiEvent, void * pvMsg) +{ + static uint8_t u8ScanResultIdx = 0; + + switch(u8WiFiEvent) + { + case M2M_WIFI_RESP_SCAN_DONE: + { + tstrM2mScanDone *pstrInfo = (tstrM2mScanDone*)pvMsg; + + printf("Num of AP found %d\n",pstrInfo->u8NumofCh); + if(pstrInfo->s8ScanState == M2M_SUCCESS) + { + u8ScanResultIdx = 0; + if(pstrInfo->u8NumofCh >= 1) + { + m2m_wifi_req_scan_result(u8ScanResultIdx); + u8ScanResultIdx ++; + } + else + { + printf("No AP Found Rescan\n"); + m2m_wifi_request_scan(M2M_WIFI_CH_ALL); + } + } + else + { + printf("(ERR) Scan fail with error <%d>\n",pstrInfo->s8ScanState); + } + } + break; + + case M2M_WIFI_RESP_SCAN_RESULT: + { + tstrM2mWifiscanResult *pstrScanResult =(tstrM2mWifiscanResult*)pvMsg; + uint8_t u8NumFoundAPs = m2m_wifi_get_num_ap_found(); + + printf(">>%02d RI %d SEC %s CH %02d BSSID %02X:%02X:%02X:%02X:%02X:%02X SSID %s\n", + pstrScanResult->u8index,pstrScanResult->s8rssi, + pstrScanResult->u8AuthType, + pstrScanResult->u8ch, + pstrScanResult->au8BSSID[0], pstrScanResult->au8BSSID[1], pstrScanResult->au8BSSID[2], + pstrScanResult->au8BSSID[3], pstrScanResult->au8BSSID[4], pstrScanResult->au8BSSID[5], + pstrScanResult->au8SSID); + + if(u8ScanResultIdx < u8NumFoundAPs) + { + // Read the next scan result + m2m_wifi_req_scan_result(index); + u8ScanResultIdx ++; + } + } + break; + default: + break; + } +} + +int main() +{ + tstrWifiInitParam param; + + param.pfAppWifiCb = wifi_event_cb; + if(!m2m_wifi_init(¶m)) + { + // Scan all channels + m2m_wifi_request_scan(M2M_WIFI_CH_ALL); + + while(1) + { + m2m_wifi_handle_events(NULL); + } + } +} +@endcode +*/ +int8_t m2m_wifi_req_scan_result(uint8_t index); + +/*! +@ingroup WLANCONNECT +@fn \ + int8_t m2m_wifi_req_curr_rssi(void); + +@brief + Asynchronous API to request the current Receive Signal Strength (RSSI) of the current connection. + +@details + This function will result in the application receiving the RSSI via a + @ref M2M_WIFI_RESP_CURRENT_RSSI event. + +@pre + - A Wi-Fi notification callback of type @ref tpfAppWifiCb MUST be implemented and registered + during initialization. Registration of the callback is done through passing it to @ref m2m_wifi_init + via the @ref tstrWifiInitParam initialization structure. + - The event @ref M2M_WIFI_RESP_CURRENT_RSSI must be handled in the callback to receive the requested Rssi information. + +@return + The function returns @ref M2M_SUCCESS if the command has been successfully queued to the WINC and a negative value otherwise. + +@section WIFIExample9 Example + The code snippet demonstrates how the RSSI request is called in the application's main function and the handling of the event received in the callback. +@code +#include "m2m_wifi.h" +#include "m2m_types.h" + +void wifi_event_cb(uint8_t u8WiFiEvent, void * pvMsg) +{ + static uint8_t u8ScanResultIdx = 0; + + switch(u8WiFiEvent) + { + case M2M_WIFI_RESP_CURRENT_RSSI: + { + int8_t *rssi = (int8_t*)pvMsg; + M2M_INFO("ch rssi %d\n",*rssi); + } + break; + default: + break; + } +} + +int main() +{ + tstrWifiInitParam param; + + param.pfAppWifiCb = wifi_event_cb; + if(!m2m_wifi_init(¶m)) + { + // Scan all channels + m2m_wifi_req_curr_rssi(); + + while(1) + { + m2m_wifi_handle_events(NULL); + } + } +} +@endcode +*/ +int8_t m2m_wifi_req_curr_rssi(void); + +/*! +@ingroup WLANCONF +@fn \ + int8_t m2m_wifi_get_otp_mac_address(uint8_t *pu8MacAddr, uint8_t * pu8IsValid); + +@brief + Synchronous API to query the MAC address programmed into the WINC OTP memory. + +@details + This function attempts to read the device's MAC address from the One Time Programmable (OTP) + memory on the WINC. The presence (yes or no) of a MAC address in the OTP memory and, in the case + of it being present, its value is returned via RAM pointed to by the input arguments. + + Request the MAC address stored on the One Time Programmable(OTP) memory of the device. + The function is blocking until the response is received. + +@pre + Prior call to @ref m2m_wifi_init is required before any WIFI/socket function. + +@param[out] pu8MacAddr + Output MAC address buffer 6 bytes in size. Valid only if *pu8Valid=1. + +@param[out] pu8IsValid + A boolean value set by the callee to indicate the validity of pu8MacAddr in OTP. If no MAC has + been programmed in the OTP the value of this parameter will be zero; otherwise it will be non-zero. + +@return + The function returns @ref M2M_SUCCESS for success and a negative value otherwise. + +@see + m2m_wifi_get_mac_address +*/ +int8_t m2m_wifi_get_otp_mac_address(uint8_t *pu8MacAddr, uint8_t *pu8IsValid); + +/*! +@ingroup WLANCONF +@fn \ + int8_t m2m_wifi_get_mac_address(uint8_t *pu8MacAddr) + +@brief + Synchronous API to retrieve the MAC address currently in use by the device. + +@details + This function obtains the MAC address that is currently in use by the device. If the function + returns with @ref M2M_SUCCESS then the content of the memory referenced by pu8MacAddr will be + populated with the 6 byte MAC address; otherwise, that memory will be left unchanged. + +@pre + Prior call to @ref m2m_wifi_init is required before any WIFI/socket function. + +@param[out] pu8MacAddr + Pointer to a buffer in memory containing a 6-byte MAC address (provided function returns @ref M2M_SUCCESS). + +@return + The function returns @ref M2M_SUCCESS for successful operation and a negative value otherwise. + +@see + m2m_wifi_get_otp_mac_address +*/ +int8_t m2m_wifi_get_mac_address(uint8_t *pu8MacAddr); + +/*! +@ingroup WLANPS +@fn \ + int8_t m2m_wifi_set_sleep_mode(uint8_t PsTyp, uint8_t BcastEn); + +@brief + Synchronous API to set the power-save mode of the WINC. + +@details + This is one of the two synchronous power-save setting functions that allow the host MCU application + to tweak the system power consumption. Such tweaking can be done through one of two ways: + - 1) Changing the power save mode, to one of the allowed power save modes (see @ref tenuPowerSaveModes). This is done by setting the first parameter. + - 2) Configuring DTIM monitoring: Configuring beacon monitoring parameters by enabling or disabling the reception of broadcast/multicast data. + This is done by setting the second parameter. + +@param[in] PsTyp + Desired power saving mode. Supported types are enumerated in @ref tenuPowerSaveModes. + +@param[in] BcastEn + Broadcast reception enable flag. + If set to 1, the WINC will wake for each DTIM beacon to ensure broadcast traffic can be received. + If set to 0, the WINC will not wakeup at the DTIM beacon, ignoring broadcast traffic, instead it will + wake every N beacon periods, as per the negotiated Listen Interval. + +@warning + The function called once after initialization. + +@return + The function returns @ref M2M_SUCCESS for successful operation and a negative value otherwise. + +@see + tenuPowerSaveModes + m2m_wifi_get_sleep_mode + m2m_wifi_set_lsn_int +*/ +int8_t m2m_wifi_set_sleep_mode(uint8_t PsTyp, uint8_t BcastEn); + +/*! +@ingroup WLANPS +@fn \ + int8_t m2m_wifi_request_sleep(uint32_t u32SlpReqTime); + +@brief + Asynchronous API to place the WINC into sleep mode for a specified period of time. + +@details + Power-save sleep request function, which requests the WINC device to sleep in the currently configured + power save mode, as set using @ref m2m_wifi_set_sleep_mode, for a specific time as defined by the parameter + u32SlpReqTime (measured in milliseconds). + This function should be used when the WINC is running in @ref M2M_PS_MANUAL power save mode only. + A wake up request is automatically performed by the WINC device when any host driver API function, e.g. Wi-Fi or socket operation is called. + +@param[in] u32SlpReqTime + Request sleep time in ms.\n + The best recommended sleep duration is left to be determined by the application. + Taking into account that if the application sends notifications very rarely, + sleeping for a long time can be a power-efficient decision. + In contrast, applications that are sensitive for long periods of absence can experience + performance degradation in the connection if long sleeping times are used. + +@return + The function returns @ref M2M_SUCCESS for successful operation and a negative value otherwise. + +@warning + - This API is currently unsupported on the WINC3400 + +@see + tenuPowerSaveModes + m2m_wifi_set_sleep_mode +*/ +int8_t m2m_wifi_request_sleep(uint32_t u32SlpReqTime); + +/*! +@ingroup WLANPS +@fn \ + uint8_t m2m_wifi_get_sleep_mode(void); + +@brief + Synchronous API to retrieve the current power save mode of the WINC. + +@return + The current operating power saving mode. The value will be one of those from the enumerated type @ref tenuPowerSaveModes. + +@see + tenuPowerSaveModes + m2m_wifi_set_sleep_mode +*/ +uint8_t m2m_wifi_get_sleep_mode(void); + +/*! +@ingroup WLANCONF +@fn \ + int8_t m2m_wifi_set_device_name(uint8_t *pu8DeviceName, uint8_t u8DeviceNameLength); + +@brief + Asynchronous API to set the Wi-Fi Direct "Device Name" of the WINC. + +@details + Sets the WINC device name. The name string is used as a device name in DHCP + hostname (option 12). + If a device is not set through this function a default name is assigned. + The default name is WINC-XX-YY, where XX and YY are the last 2 octets of the OTP + MAC address. If OTP (eFuse) is programmed, then the default name is WINC-00-00. + +@warning + The function called once after initialization.\n + Used for DHCP client hostname option (12).\n + Device name shall contain only characters allowed in valid internet host name as + defined in RFC 952 and 1123. + +@param[in] pu8DeviceName + Buffer holding the device name. Device name is a null terminated C string. + +@param[in] u8DeviceNameLength + Length of the device name. Should not exceed the maximum device name's + length @ref M2M_DEVICE_NAME_MAX (including null character). + +@return + The function returns @ref M2M_SUCCESS if the command has been successfully queued to the WINC and a negative value otherwise. +*/ +int8_t m2m_wifi_set_device_name(uint8_t *pu8DeviceName, uint8_t u8DeviceNameLength); + +/*! +@ingroup WLANTIME +@fn \ + int8_t m2m_wifi_configure_sntp(uint8_t *pu8NTPServerName, uint8_t u8NTPServerNameLength, tenuSNTPUseDHCP useDHCP); + +@brief + Configures what NTP server the SNTP client should use. + +@details + Configures what NTP server the SNTP client should use. Only 1 server name can be provided, if the configured server name begins with an asterisk then it will be treated as a server pool. + The SNTP client can also use the NTP server provided by the DHCP server through option 42. + By default the NTP server provided by DHCP will be tried first, then the built-in default NTP server (time.nist.gov) will be used. + +@param[in] pu8NTPServerName + Buffer holding the NTP server name. If the first character is an asterisk (*) then it will be treated as a server pool, where the asterisk will + be replaced with an incrementing value from 0 to 3 each time a server fails (example: *.pool.ntp.org). + +@param[in] u8NTPServerNameLength + Length of the NTP server name. Should not exceed the maximum NTP server name length of @ref M2M_NTP_MAX_SERVER_NAME_LENGTH. + +@param[in] useDHCP + Explicity tell the WINC if it should use the NTP server provided by the DHCP server or not. + +@warning + SNTP should be configured before the connection takes place. If SNTP is configured after the device connects to a + network, the new configuration can take a minimum of 24h to be applied. However, it can take even longer since it is + triggered by DHCP renewal. + Currently there is also a known issue in which if the WINC obtains the NTP server from DHCP and then connects to a + different network, it will still use the NTP from the previous network. + Configuring a server name will overwrite the built-in default server until next reboot. + +@return + The function returns @ref M2M_SUCCESS for success and a negative value otherwise. +*/ +int8_t m2m_wifi_configure_sntp(uint8_t *pu8NTPServerName, uint8_t u8NTPServerNameLength, tenuSNTPUseDHCP useDHCP); + +/*! +@ingroup WLANPS +@fn \ + int8_t m2m_wifi_set_lsn_int(tstrM2mLsnInt * pstrM2mLsnInt); + +@brief + Asynchronous API to set Wi-Fi listen interval for power save operation. + +@details + This is one of the two synchronous power-save setting functions that + allow the host MCU application to tweak the system power consumption. Such tweaking can be done by modifying the + Wi-Fi listen interval. The listen interval is how many beacon periods the station can sleep before it wakes up to receive data buffered in the AP. + It is represented in units of AP beacon periods(100ms). + +@warning + The function should be called once after initialization. + +@param[in] pstrM2mLsnInt + Structure holding the listen interval configuration. + +@pre + The function @ref m2m_wifi_set_sleep_mode shall be called first, to set the power saving mode required. + +@return + The function returns @ref M2M_SUCCESS if the command has been successfully queued to the WINC and a negative value otherwise. + +@see + tstrM2mLsnInt + m2m_wifi_set_sleep_mode +*/ +int8_t m2m_wifi_set_lsn_int(tstrM2mLsnInt *pstrM2mLsnInt); + +/**@cond MON_DOC + */ +/*! +@ingroup WLANMON +@fn \ + int8_t m2m_wifi_enable_monitoring_mode(tstrM2MWifiMonitorModeCtrl *, uint8_t *, uint16_t , uint16_t); + +@brief + Asynchronous call to enable Wi-Fi monitoring (promiscuous receive) mode. + +@details + Wi-Fi monitoring mode (Promiscuous mode) enabling function. + This function enables the monitoring mode, thus allowing two operations to be + performed: + 1) Transmission of manually configured frames, through using the + @ref m2m_wifi_send_wlan_pkt function. + 2) Reception of frames based on a defined filtering criteria. + + Enabling monitoring mode allows for reception of all frames that satisfy the filter criteria in the input parameter and are received on the current wireless channel. + All packets that meet the filtering criteria are passed to the application layer, to + be handled by the assigned monitoring callback function. + The monitoring callback function must be implemented before starting the monitoring mode, + in-order to handle the packets received. + + A dedicated callback function, @ref tpfAppMonCb, must be registered to handle frames received in + promiscuous mode. This is done via an instance of a @ref tstrWifiInitParam structure and a call to + the @ref m2m_wifi_init function. + +@param[in] pstrMtrCtrl + Pointer to @ref tstrM2MWifiMonitorModeCtrl structure holding the filtering parameters. + +@param[in] pu8PayloadBuffer + Pointer to a Buffer allocated by the application. The buffer SHALL hold the Data field of + the WIFI RX Packet (Or a part from it). If it is set to NULL, the WIFI data payload will + be discarded by the monitoring driver. + +@param[in] u16BufferSize + The total size of the pu8PayloadBuffer in bytes. + +@param[in] u16DataOffset + Starting offset in the DATA FIELD of the received WIFI packet. The application may be interested + in reading specific information from the received packet. It must assign the offset to the starting + position of it relative to the DATA payload start.\n + \e Example, \e if \e the \e SSID \e is \e needed \e to \e be \e read \e from \e a \e PROBE \e REQ + \e packet, \e the \e u16Offset \e MUST \e be \e set \e to \e 0. + +@warning + This mode is available as sniffer ONLY, the WINC cannot be connected in any modes (Station, Access Point or P2P). + +@return + The function returns @ref M2M_SUCCESS if the command has been successfully queued to the WINC and a negative value otherwise. + +@see + tstrM2MWifiMonitorModeCtrl + tstrM2MWifiRxPacketInfo + tstrWifiInitParam + tenuM2mScanCh + m2m_wifi_disable_monitoring_mode + m2m_wifi_send_wlan_pkt + m2m_wifi_send_ethernet_pkt + +@section WIFIExample10 Example + The example demonstrates the main function where-by the monitoring enable function is called after + the initialization of the driver and the packets are handled in the callback function. + +@code +#include "m2m_wifi.h" +#include "m2m_types.h" + +//Declare receive buffer +uint8_t gmgmt[1600]; + +//Callback functions +void wifi_cb(uint8_t u8WiFiEvent, void * pvMsg) +{ + ; +} + +void wifi_monitoring_cb(tstrM2MWifiRxPacketInfo *pstrWifiRxPacket, uint8_t *pu8Payload, uint16_t u16PayloadSize) +{ + if((NULL != pstrWifiRxPacket) && (0 != u16PayloadSize)) { + if(MANAGEMENT == pstrWifiRxPacket->u8FrameType) { + M2M_INFO("***# MGMT PACKET #***\n"); + } else if(DATA_BASICTYPE == pstrWifiRxPacket->u8FrameType) { + M2M_INFO("***# DATA PACKET #***\n"); + } else if(CONTROL == pstrWifiRxPacket->u8FrameType) { + M2M_INFO("***# CONTROL PACKET #***\n"); + } + } +} + +int main() +{ + //Register wifi_monitoring_cb + tstrWifiInitParam param; + param.pfAppWifiCb = wifi_cb; + param.pfAppMonCb = wifi_monitoring_cb; + + nm_bsp_init(); + + if(!m2m_wifi_init(¶m)) { + //Enable Monitor Mode with filter to receive all data frames on channel 1 + tstrM2MWifiMonitorModeCtrl strMonitorCtrl = {0}; + strMonitorCtrl.u8ChannelID = M2M_WIFI_CH_1; + strMonitorCtrl.u8FrameType = DATA_BASICTYPE; + strMonitorCtrl.u8FrameSubtype = M2M_WIFI_FRAME_SUB_TYPE_ANY; //Receive any subtype of data frame + m2m_wifi_enable_monitoring_mode(&strMonitorCtrl, gmgmt, sizeof(gmgmt), 0); + + while(1) { + m2m_wifi_handle_events(NULL); + } + } + return 0; +} +@endcode +*/ +int8_t m2m_wifi_enable_monitoring_mode(tstrM2MWifiMonitorModeCtrl *pstrMtrCtrl, uint8_t *pu8PayloadBuffer, + uint16_t u16BufferSize, uint16_t u16DataOffset); + +/*! +@ingroup WLANMON +@fn \ + int8_t m2m_wifi_disable_monitoring_mode(void); + +@brief + Asynchronous API to disable Wi-Fi monitoring (promiscuous receive) mode. + +@details + Disable Wi-Fi monitoring mode (Promiscuous mode). Expected to be called if the monitoring mode is already enabled, + but if it was called without enabling it, no negative impact will reside. + +@return + The function returns @ref M2M_SUCCESS if the command has been successfully queued to the WINC and a negative value otherwise. + +@see + m2m_wifi_enable_monitoring_mode + */ +int8_t m2m_wifi_disable_monitoring_mode(void); + +/*! +@ingroup WLANMON +@fn \ + int8_t m2m_wifi_send_wlan_pkt(uint8_t *, uint16_t, uint16_t); + +@brief + Asynchronous API to queue a raw Wi-Fi packet for transmission by the WINC. + +@pre + Enable Monitoring mode first using @ref m2m_wifi_enable_monitoring_mode + +@note + The application is responsible for the packets. + +@warning + This function is only useful when operating in monitoring mode. + +@param[in] pu8WlanPacket + Pointer to a buffer holding the whole WIFI frame. + +@param[in] u16WlanHeaderLength + The size of the WIFI packet header ONLY. + +@param[in] u16WlanPktSize + The size of the whole packet in bytes. + +@return + The function returns @ref M2M_SUCCESS if the command has been successfully queued to the WINC and a negative value otherwise. + +@see + m2m_wifi_enable_monitoring_mode + m2m_wifi_disable_monitoring_mode +*/ +int8_t m2m_wifi_send_wlan_pkt(uint8_t *pu8WlanPacket, uint16_t u16WlanHeaderLength, uint16_t u16WlanPktSize); +/**@endcond*/ //MON_DOC + +/*! +@ingroup WLANETH +@fn \ + int8_t m2m_wifi_send_ethernet_pkt(uint8_t* pu8Packet,uint16_t u16PacketSize); + +@brief + Asynchronous API to queue an Ethernet packet for transmission by the WINC. + +@details + Transmit a packet directly in ETHERNET/bypass mode where the TCP/IP stack is disabled + and the implementation of this packet is left to the application developer. + The Ethernet packet composition is left to the application developer. + +@note + Packets are the user's responsibility. + +@warning + This function available in ETHERNET/Bypass mode ONLY. Make sure that application defines ETH_MODE. + +@param[in] pu8Packet + Pointer to a buffer holding the whole Ethernet frame. + +@param[in] u16PacketSize + The size of the whole packet in bytes. + +@return + The function returns @ref M2M_SUCCESS if the command has been successfully queued to the WINC and a negative value otherwise. + +@see + m2m_wifi_enable_mac_mcast + m2m_wifi_set_receive_buffer +*/ +int8_t m2m_wifi_send_ethernet_pkt(uint8_t *pu8Packet, uint16_t u16PacketSize); + +/*! +@ingroup WLANTIME +@fn \ + int8_t m2m_wifi_enable_sntp(uint8_t); + +@brief + Asynchronous API to enable or disable the native Simple Network Time Protocol(SNTP) client running on the WINC. + +@details + The SNTP client is enabled by default during chip initialization. This function can be used to + disable or subsequently re-enable the service. + + The service is capable of synchronizing the WINC system clock to the UTC time from a well-known + (and trusted) time server, for example "time.nist.gov". By default the SNTP client will update the + system time once every 24 hours. The ability to track the time accurately is important for various + applications such as checking expiry of X509 certificates during TLS (Transport Layer Security) + session establishment. + + It is highly recommended to leave SNTP enabled if there is no alternative source of timing + information. For systems including an RTC device, SNTP may not be needed and the WINC's time + may be set using the @ref m2m_wifi_set_system_time function. + +@param[in] bEnable + Enables or disables the SNTP service + '0' :disable SNTP + '1' :enable SNTP + +@return + The function returns @ref M2M_SUCCESS if the command has been successfully queued to the WINC and a negative value otherwise. + +@see + m2m_wifi_set_system_time + */ +int8_t m2m_wifi_enable_sntp(uint8_t bEnable); + +/*! +@ingroup WLANTIME +@fn \ + int8_t m2m_wifi_set_system_time(uint32_t); + +@brief + Asynchronous function for setting the system time within the WINC. + +@details + Function for setting the system time in time/date format (@ref uint32_t). + The @ref tstrSystemTime structure can be used as a reference to the time values that + should be set and pass its value as @ref uint32_t. + +@param[in] u32UTCSeconds + Seconds elapsed since January 1, 1900 (NTP Timestamp). + +@return + The function returns @ref M2M_SUCCESS if the command has been successfully queued to the WINC and a negative value otherwise. + +@see + m2m_wifi_enable_sntp + tstrSystemTime + +@note + If there is an RTC on the host MCU, the SNTP may be disabled and the host may set the system + time within the firmware using the API @ref m2m_wifi_set_system_time. + */ +int8_t m2m_wifi_set_system_time(uint32_t u32UTCSeconds); + +/*! +@ingroup WLANTIME +@fn \ + int8_t m2m_wifi_get_system_time(void); + +@brief + Asynchronous API to obtain the system time in use by the WINC. + +@details + This function will request the WINC to report its current system time to the application. The + information will arrive at the application via the @ref tpfAppWifiCb and event @ref M2M_WIFI_RESP_GET_SYS_TIME. + Response time retrieved is parsed into the members defined in the structure @ref tstrSystemTime. + +@note + - A Wi-Fi notification callback of type @ref tpfAppWifiCb MUST be implemented and registered + during initialization. Registration of the callback is done via the @ref m2m_wifi_init API. + - The event @ref M2M_WIFI_RESP_GET_SYS_TIME must be handled in the callback. + +@return + The function returns @ref M2M_SUCCESS if the command has been successfully queued to the WINC and a negative value otherwise. + +@see + m2m_wifi_enable_sntp + tstrSystemTime + */ +int8_t m2m_wifi_get_system_time(void); + +/*! +@ingroup WLANCONF +@fn \ + int8_t m2m_wifi_set_cust_InfoElement(uint8_t*); + +@brief + Asynchronous API to add or remove a user-defined Information Element. + +@details + This function allows the application to provide a custom Information Element to the + WINC that will be included in all beacon and probe response frames, while in Access Point mode. + If it is required to delete these IEs, fill the buffer with zeros. + +@param[in] pau8M2mCustInfoElement + Pointer to Buffer containing the IE to be used. It is the application developer's + responsibility to ensure on the correctness of the information element's ordering + passed in. + If the pointer is null, this removes any current custom IE. If non-null, the pointer + must reference data in the following format: + +@verbatim + --------------- ---------- ---------- ------------------- -------- -------- ----------- ----------------------- + | Byte[0] | Byte[1] | Byte[2] | Byte[3:length1+2] | ..... | Byte[n] | Byte[n+1] | Byte[n+2:lengthx+2] | + |---------------|----------|----------|-------------------|-------- --------|-----------|---------------------| + | #of all Bytes | IE1 ID | Length1 | Data1(Hex Coded) | ..... | IEx ID | Lengthx | Datax(Hex Coded) | + --------------- ---------- ---------- ------------------- -------- -------- ----------- ----------------------- +@endverbatim + +@warning + Size of All elements combined must not exceed 255 byte. + Used in Access Point Mode. + +@note + IEs Format will follow the above format. + +@return + The function returns @ref M2M_SUCCESS if the command has been successfully queued to the WINC and a negative value otherwise. + +@see + m2m_wifi_enable_sntp + +@section WIFIExample11 Example + The example demonstrates how the information elements are set using this function. +@code +char elementData[21]; +static char state = 0; // To Add, Append, and Delete +if(0 == state) { //Add 3 IEs + state = 1; + //Total Number of Bytes + elementData[0]=12; + //First IE + elementData[1]=200; elementData[2]=1; elementData[3]='A'; + //Second IE + elementData[4]=201; elementData[5]=2; elementData[6]='B'; elementData[7]='C'; + //Third IE + elementData[8]=202; elementData[9]=3; elementData[10]='D'; elementData[11]=0; elementData[12]='F'; +} else if(1 == state) { + //Append 2 IEs to others, Notice that we keep old data in array starting with\n + //element 13 and total number of bytes increased to 20 + state = 2; + //Total Number of Bytes + elementData[0]=20; + //Fourth IE + elementData[13]=203; elementData[14]=1; elementData[15]='G'; + //Fifth IE + elementData[16]=204; elementData[17]=3; elementData[18]='X'; elementData[19]=5; elementData[20]='Z'; +} else if(2 == state) { //Delete All IEs + state = 0; + //Total Number of Bytes + elementData[0]=0; +} +m2m_wifi_set_cust_InfoElement(elementData); +@endcode + */ +int8_t m2m_wifi_set_cust_InfoElement(uint8_t *pau8M2mCustInfoElement); + +/*! +@ingroup WLANPS +@fn \ + int8_t m2m_wifi_set_power_profile(uint8_t u8PwrMode); + +@brief + Change the power profile mode. + +@param[in] u8PwrMode + Change the WINC power profile to different mode based on the enumeration @ref tenuM2mPwrMode.\n + Not implemented in WINC3400 firmware. + +@warning + May only be called after initialization, before any connection request, and may not be used to change + the power mode thereafter. + +@return + The function returns @ref M2M_SUCCESS for success and a negative value otherwise. + +@see + tenuM2mPwrMode + m2m_wifi_init +*/ +int8_t m2m_wifi_set_power_profile(uint8_t u8PwrMode); + +/*! +@ingroup WLANCONF +@fn \ + int8_t m2m_wifi_set_tx_power(uint8_t u8TxPwrLevel); + +@brief + Set the TX power tenuM2mTxPwrLevel. + +@param[in] u8TxPwrLevel + Change the TX power based on the enumeration @ref tenuM2mTxPwrLevel. + +@pre + Must be called after the initialization and before any connection request and can't be changed in runtime. + +@return + The function returns @ref M2M_SUCCESS for success and a negative value otherwise. + +@see + tenuM2mTxPwrLevel + m2m_wifi_init +*/ +int8_t m2m_wifi_set_tx_power(uint8_t u8TxPwrLevel); + +/*! +@ingroup WLANLOG +@fn \ + int8_t m2m_wifi_enable_firmware_logs(uint8_t u8Enable); + +@brief + Enable or Disable logs in run time. + +@details + Enable or Disable logs in run time (Disable Firmware logs will enhance the firmware start-up time and performance). + +@param[in] u8Enable + Set 1 to enable the logs, 0 for disable. + +@pre + Must be called after initialization through the following function @ref m2m_wifi_init. + +@return + The function returns @ref M2M_SUCCESS for success and a negative value otherwise. + +@see + __DISABLE_FIRMWARE_LOGS__ (build option to disable logs from initializations) + m2m_wifi_init +*/ +int8_t m2m_wifi_enable_firmware_logs(uint8_t u8Enable); + +/*! +@ingroup WLANCONF +@fn \ + int8_t m2m_wifi_set_battery_voltage(uint16_t u16BattVoltx100); + +@brief + Set the battery voltage to update the firmware calculations.\n + Not implemented in WINC3400 firmware. + +@pre + Must be called after initialization through the following function @ref m2m_wifi_init. + +@param [in] u16BattVoltx100 + Battery voltage multiplied by 100 + +@return + The function returns @ref M2M_SUCCESS for success and a negative value otherwise. + +@see + m2m_wifi_init +*/ +int8_t m2m_wifi_set_battery_voltage(uint16_t u16BattVoltx100); + +/*! +@ingroup WLANETH +@fn \ + int8_t m2m_wifi_enable_mac_mcast(uint8_t *pu8MulticastMacAddress, uint8_t u8AddRemove); + +@brief + Asynchronous API to add or remove MAC addresses to the multicast filter. + +@details + This function will configure the WINC to receive/ignore multicast packets from certain + MAC address groups when operating in bypass mode. + This function requests the given MAC addresses to be added/removed from the multicast filter. + +@param[in] pu8MulticastMacAddress + Pointer to MAC address + +@param[in] u8AddRemove + A flag to add or remove the MAC ADDRESS, based on the following values: + - 0 : remove MAC address + - 1 : add MAC address + +@note + Maximum number of MAC addresses that could be added is 8. + +@warning + This function is available in ETHERNET/bypass mode ONLY. + Make sure that the application defines ETH_MODE. + +@return + The function returns @ref M2M_SUCCESS if the command has been successfully queued to the WINC and a negative value otherwise. + +@see + m2m_wifi_set_receive_buffer + m2m_wifi_send_ethernet_pkt + */ +int8_t m2m_wifi_enable_mac_mcast(uint8_t *pu8MulticastMacAddress, uint8_t u8AddRemove); + +/*! +@ingroup WLANETH +@fn \ + int8_t m2m_wifi_set_receive_buffer(void *pvBuffer, uint16_t u16BufferLen); + +@brief + Synchronous function for setting or modifying the receiver buffer's length. + +@details + Synchronous function for setting or modifying the receiver buffer's length. + In the ETHERNET/bypass mode the application should define a callback of type + @ref tpfAppEthCb, through which the application handles the received + ethernet frames. It is through this callback function that the user can + dynamically modify the length of the currently used receiver buffer. + +@param[in] pvBuffer + Pointer to Buffer to receive data. + NULL pointer causes a negative error @ref M2M_ERR_FAIL. + +@param[in] u16BufferLen + Length of data to be received. Maximum length of data should not exceed the size defined by TCP/IP + defined as @ref SOCKET_BUFFER_MAX_LENGTH + +@warning + This function is available in the Ethernet/bypass mode ONLY. Make sure that the application defines ETH_MODE. + +@return + The function returns @ref M2M_SUCCESS if the command has been successfully queued to the WINC and a negative value otherwise. + +@see + m2m_wifi_enable_mac_mcast + m2m_wifi_send_ethernet_pkt +*/ +int8_t m2m_wifi_set_receive_buffer(void *pvBuffer, uint16_t u16BufferLen); + +/*! +@ingroup WLANCRYPTO +@fn \ + int8_t m2m_wifi_prng_get_random_bytes(uint8_t* pu8PrngBuff,uint16_t u16PrngSize); + +@brief + Asynchronous function for retrieving from the firmware a pseudo-random set of bytes. + +@details + Asynchronous function for retrieving from the firmware a pseudo-random set of bytes as specified in the size passed in as a parameter. + The registered wifi-cb function retrieves the random bytes through the response @ref M2M_WIFI_RESP_GET_PRNG + +@param[in] pu8PrngBuff + Pointer to a buffer to receive data. + +@param[in] u16PrngSize + Request size in bytes + +@warning + Size greater than the maximum specified (@ref M2M_BUFFER_MAX_SIZE - sizeof(tstrPrng)) + causes a negative error @ref M2M_ERR_FAIL. + +@return + The function returns @ref M2M_SUCCESS for successful operations and a negative value otherwise. +*/ +int8_t m2m_wifi_prng_get_random_bytes(uint8_t *pu8PrngBuff, uint16_t u16PrngSize); + +/*! +@ingroup WLANROAMING +@fn \ + int8_t m2m_wifi_enable_roaming(uint8_t bEnableDhcp); + +@brief + Enable WiFi STA roaming. + +@details + m2m_wifi_enable_roaming enables the firmware to trigger the roaming algorithm/steps on link loss with the current AP. + If roaming is successful, the @ref M2M_WIFI_RESP_CON_STATE_CHANGED message with state as @ref M2M_WIFI_ROAMED is sent to the host. + Additionally a @ref M2M_WIFI_REQ_DHCP_CONF message with new DHCP lease details is sent to host (only if bEnableDhcp=1). + If roaming is unsuccessful, a @ref M2M_WIFI_RESP_CON_STATE_CHANGED message with state as @ref M2M_WIFI_DISCONNECTED is sent to host. + +@param[in] bEnableDhcp + 0 : Disables DHCP client execution after roaming to new AP + 1 : Enables DHCP client execution after roaming to new AP + +@pre + Must be called after the initialization. + The roaming algorithm/procedure is internal to the firmware. + +@return + The function returns @ref M2M_SUCCESS for successful operations and a negative value otherwise. + +@see + m2m_wifi_init +*/ +int8_t m2m_wifi_enable_roaming(uint8_t bEnableDhcp); + +/*! +@ingroup WLANROAMING +@fn \ + int8_t m2m_wifi_disable_roaming(void); + +@brief + Disable WiFi STA roaming. + +@pre + Must be called after the initialization. + +@return + The function returns @ref M2M_SUCCESS for successful operations and a negative value otherwise. + +@see + m2m_wifi_init +*/ +int8_t m2m_wifi_disable_roaming(void); + +/*! +@ingroup WLANINIT +@fn \ + uint8_t m2m_wifi_get_state(void); + +@brief + Get the wifi state. + +@return + The function returns the current wifi state (see @ref tenuWifiState for the possible states). + +@note + Check the WINC state. See @ref tenuWifiState for possible states.\n + @ref WIFI_STATE_INIT state represents WINC initialized but not started, this is a suitable state + for safe flash access. + +@sa + m2m_wifi_init + m2m_wifi_download_mode +*/ +uint8_t m2m_wifi_get_state(void); + +/*! +@ingroup BLEAPI +@fn \ + int8_t m2m_wifi_ble_api_send(const uint8_t* const msg, const uint32_t len); + +@brief + Asynchronous API to send an encapsulated Atmel BLE message over the Wifi Host Interface. + +@param[in] msg + Pointer to the start of the BLE message to transfer down to the WINC. + +@param[in] len + The length of the message in octets. + +@return + The function returns @ref M2M_SUCCESS if the command has been successfully queued to the WINC, + and a negative value otherwise. + */ +int8_t m2m_wifi_ble_api_send(uint8_t *msg, uint32_t len); + +/*! +@ingroup BLEAPI +@fn \ + int8_t m2m_wifi_ble_set_gain_table(uint8_t table_idx); + +@brief + Asynchronous API that notifies the WINC with the Gain Table index from Flash that should use to configure the WiFi and BLE gains. + +@return + The function returns @ref M2M_SUCCESS if the command has been successfully queued to the WINC, + and a negative value otherwise. +*/ +int8_t m2m_wifi_ble_set_gain_table(uint8_t table_idx); + +/*! +@ingroup BLEAPI +@fn \ + int8_t m2m_wifi_req_restrict_ble(void); + +@brief + Asynchronous API to request restricting of BLE functionality by placing the BLE processor in a low power state. + It is recommended to do this if it is known that BLE functionality will not be used for any significant length of time. + +@return + The function returns @ref M2M_SUCCESS if the command has been successfully queued to the WINC, + and a negative value otherwise. + +@pre + - A Wi-Fi notification callback of type @ref tpfAppWifiCb MUST be implemented and registered + during initialization. Registration of the callback is done via the @ref m2m_wifi_init API. +*/ +int8_t m2m_wifi_req_restrict_ble(void); + +/*! +@ingroup BLEAPI +@fn \ + int8_t m2m_wifi_req_unrestrict_ble(void); + +@brief + Asynchronous API to request un-restricting of BLE functionality by reverting the BLE processor to full power mode. + +@return + The function returns @ref M2M_SUCCESS if the command has been successfully queued to the WINC, + and a negative value otherwise. + +@pre + - A Wi-Fi notification callback of type @ref tpfAppWifiCb MUST be implemented and registered + during initialization. Registration of the callback is done via the @ref m2m_wifi_init API. +*/ +int8_t m2m_wifi_req_unrestrict_ble(void); + +/** @defgroup VERSION Version + @brief + Describes the APIs for reading the version information of the WINC firmware. + @{ + @defgroup VERSIONDEF Defines + @brief + Specifies the macros and defines used by the version APIs. + + @defgroup VERSIONAPI Functions + @brief + Lists the APIs for reading the version information of the WINC firmware. + @} + */ + +/*! +@ingroup VERSIONAPI +@fn \ + int8_t m2m_wifi_get_firmware_version(tstrM2mRev* pstrRev); + +@brief + Synchronous API to obtain the firmware version currently running on the WINC. + +@details + Get the Firmware version info from the active partition, as defined in the structure @ref tstrM2mRev. + +@param[out] pstrRev + Pointer to a variable of type @ref tstrM2mRev, which contains the firmware version parameters. + +@return + The function returns @ref M2M_SUCCESS for success and a negative value otherwise. + +@pre + Must be called after initialization through the following function @ref m2m_wifi_init. + +@sa + m2m_wifi_init +*/ +int8_t m2m_wifi_get_firmware_version(tstrM2mRev *pstrRev); + +/*! +@ingroup VERSIONAPI +@fn \ + int8_t m2m_ota_get_firmware_version(tstrM2mRev *pstrRev); + +@brief + Synchronous API to obtain the firmware version of the WINC image that is stored in the inactive flash partition. + This is the image that would run on the WINC if @ref m2m_ota_switch_firmware or @ref m2m_ota_rollback were called, + followed by a reset of the WINC. + +@param[out] pstrRev + Pointer to variable of type @ref tstrM2mRev which contains the ota fw version parameters. + +@return + The function returns @ref M2M_SUCCESS for success and a negative value otherwise. +*/ +int8_t m2m_ota_get_firmware_version(tstrM2mRev *pstrRev); + +/*! +@fn \ + uint32_t m2m_wifi_get_chipId(void) + +@brief + Synchronous API to obtain the firmware WINC chip ID. + +@return + The function returns chipID > 0 or 0 for failure. +*/ +uint32_t m2m_wifi_get_chipId(void); + +/*! +@ingroup OTAFUNCTIONS +@fn int8_t m2m_wifi_check_ota_rb(void); + +@brief + Synchronous API to check presence and compatibility of the WINC image that is stored in the inactive flash partition. + This is the image that would run on the WINC if @ref m2m_ota_switch_firmware or @ref m2m_ota_rollback were called, + followed by a reset of the WINC. + +@return + The function returns @ref M2M_SUCCESS for compatible image and a negative value otherwise. +*/ +int8_t m2m_wifi_check_ota_rb(void); + +#ifdef __cplusplus +} +#endif +#endif /* __M2M_WIFI_H__ */ + diff --git a/ChaletLora.X/Source/winc3400_142/driver/include/nmasic.h b/ChaletLora.X/Source/winc3400_142/driver/include/nmasic.h new file mode 100644 index 0000000..5b20c40 --- /dev/null +++ b/ChaletLora.X/Source/winc3400_142/driver/include/nmasic.h @@ -0,0 +1,148 @@ +/******************************************************************************* + This module contains WINC3400 ASIC specific internal APIs. + + File Name: + nmasic.h + + Summary: + This module contains WINC3400 ASIC specific internal APIs. + + Description: + This module contains WINC3400 ASIC specific internal APIs. + *******************************************************************************/ + +//DOM-IGNORE-BEGIN +/******************************************************************************* +* Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries. +* +* Subject to your compliance with these terms, you may use Microchip software +* and any derivatives exclusively with Microchip products. It is your +* responsibility to comply with third party license terms applicable to your +* use of third party software (including open source software) that may +* accompany Microchip software. +* +* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER +* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED +* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A +* PARTICULAR PURPOSE. +* +* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, +* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND +* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS +* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE +* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN +* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, +* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. +*******************************************************************************/ +// DOM-IGNORE-END + +#ifndef _NMASIC_H_ +#define _NMASIC_H_ + +#include "nm_common.h" + +#define NMI_PERIPH_REG_BASE 0x1000 +#define NMI_CHIPID (NMI_PERIPH_REG_BASE) +#define rNMI_GP_REG_0 (0x149c) +#define rNMI_GP_REG_1 (0x14A0) +#define rNMI_GLB_RESET (0x1400) +#define rNMI_BOOT_RESET_MUX (0x1118) +#define NMI_STATE_REG (0x108c) +#define BOOTROM_REG (0xc000c) +#define NMI_REV_REG (0x207ac) /*Also, Used to load ATE firmware from SPI Flash and to ensure that it is running too*/ +#define M2M_WAIT_FOR_HOST_REG (0x207bc) +#define M2M_FINISH_INIT_STATE 0x02532636UL +#define M2M_FINISH_BOOT_ROM 0x10add09eUL +#define M2M_START_FIRMWARE 0xef522f61UL +#define M2M_START_PS_FIRMWARE 0x94992610UL + +#define M2M_ATE_FW_START_VALUE (0x3C1CD57D) /*Also, Change this value in boot_firmware if it will be changed here*/ +#define M2M_ATE_FW_IS_UP_VALUE (0xD75DC1C3) /*Also, Change this value in ATE (Burst) firmware if it will be changed here*/ + +#define REV_2B0 (0x2B0) +#define REV_B0 (0x2B0) +#define REV_3A0 (0x3A0) +#define CHIP_ID_3000D (0x3000D0) + +#define GET_CHIPID() nmi_get_chipid() +#define ISNMC1000(id) (((id & 0xfffff000) == 0x100000) ? 1 : 0) +#define ISNMC1500(id) (((id & 0xfffff000) == 0x150000) ? 1 : 0) +#define ISNMC3400(id) (((id & 0xfff0f000) == 0x300000) ? 1 : 0) +#define REV(id) ( ((id) & 0x00000fff ) ) +#define EFUSED_MAC(value) (value & 0xffff0000) + +#define rHAVE_SDIO_IRQ_GPIO_BIT (NBIT0) +#define rHAVE_USE_PMU_BIT (NBIT1) +#define rHAVE_SLEEP_CLK_SRC_RTC_BIT (NBIT2) +#define rHAVE_SLEEP_CLK_SRC_XO_BIT (NBIT3) +#define rHAVE_EXT_PA_INV_TX_RX (NBIT4) +#define rHAVE_LEGACY_RF_SETTINGS (NBIT5) +#define rHAVE_LOGS_DISABLED_BIT (NBIT6) +#define rHAVE_ETHERNET_MODE_BIT (NBIT7) + +typedef struct { + uint32_t u32Mac_efuse_mib; + uint32_t u32Firmware_Ota_rev; +} tstrGpRegs; + +#ifdef __cplusplus +extern "C" { +#endif +/** + * @fn nm_clkless_wake + * @brief Wakeup the chip using clockless registers + * @return ZERO in case of success and M2M_ERR_BUS_FAIL in case of failure + */ +int8_t nm_clkless_wake(void); + +int8_t chip_wake(void); + +int8_t chip_sleep(void); + +void chip_idle(void); + +void enable_rf_blocks(void); + +int8_t enable_interrupts(void); + +int8_t cpu_start(void); + +uint32_t nmi_get_chipid(void); + +uint32_t nmi_get_rfrevid(void); + +void restore_pmu_settings_after_global_reset(void); + +void nmi_update_pll(void); + +void nmi_set_sys_clk_src_to_xo(void); + +int8_t chip_reset(void); + +int8_t wait_for_bootrom(uint8_t); + +int8_t wait_for_firmware_start(uint8_t); + +int8_t chip_deinit(void); + +int8_t chip_reset_and_cpu_halt(void); + +int8_t set_gpio_dir(uint8_t gpio, uint8_t dir); + +int8_t set_gpio_val(uint8_t gpio, uint8_t val); + +int8_t get_gpio_val(uint8_t gpio, uint8_t* val); + +int8_t pullup_ctrl(uint32_t pinmask, uint8_t enable); + +int8_t nmi_get_otp_mac_address(uint8_t *pu8MacAddr, uint8_t * pu8IsValid); + +int8_t nmi_get_mac_address(uint8_t *pu8MacAddr); + +int8_t chip_apply_conf(uint32_t u32conf); + +#ifdef __cplusplus +} +#endif + +#endif /*_NMASIC_H_*/ diff --git a/ChaletLora.X/Source/winc3400_142/driver/include/nmbus.h b/ChaletLora.X/Source/winc3400_142/driver/include/nmbus.h new file mode 100644 index 0000000..dd13e9a --- /dev/null +++ b/ChaletLora.X/Source/winc3400_142/driver/include/nmbus.h @@ -0,0 +1,137 @@ +/******************************************************************************* + This module contains WINC3400 bus APIs implementation. + + File Name: + nmbus.h + + Summary: + This module contains WINC3400 bus APIs implementation. + + Description: + This module contains WINC3400 bus APIs implementation. + *******************************************************************************/ + +//DOM-IGNORE-BEGIN +/******************************************************************************* +* Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries. +* +* Subject to your compliance with these terms, you may use Microchip software +* and any derivatives exclusively with Microchip products. It is your +* responsibility to comply with third party license terms applicable to your +* use of third party software (including open source software) that may +* accompany Microchip software. +* +* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER +* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED +* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A +* PARTICULAR PURPOSE. +* +* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, +* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND +* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS +* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE +* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN +* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, +* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. +*******************************************************************************/ +// DOM-IGNORE-END + +#ifndef _NMBUS_H_ +#define _NMBUS_H_ + +#include "nm_common.h" + +#ifdef __cplusplus +extern "C" { +#endif +/** + * @fn nm_bus_iface_init + * @brief Initialize bus interface + * @return @ref M2M_SUCCESS in case of success and @ref M2M_ERR_BUS_FAIL in case of failure + */ +int8_t nm_bus_iface_init(void *pvInitVal); + +/** + * @fn nm_bus_iface_deinit + * @brief Deinitialize bus interface + * @return @ref M2M_SUCCESS in case of success and @ref M2M_ERR_BUS_FAIL in case of failure + */ +int8_t nm_bus_iface_deinit(void); + +/** + * @fn nm_bus_reset + * @brief Reset bus interface + * @return @ref M2M_SUCCESS in case of success and @ref M2M_ERR_BUS_FAIL in case of failure + * @version 1.0 + */ +int8_t nm_bus_reset(void); + +/** + * @fn nm_bus_iface_reconfigure + * @brief Reconfigure bus interface + * @return @ref M2M_SUCCESS in case of success and @ref M2M_ERR_BUS_FAIL in case of failure + */ +int8_t nm_bus_iface_reconfigure(void *ptr); + +/** + * @fn nm_read_reg + * @brief Read register + * @param[in] u32Addr + * Register address + * @return Register value + */ +uint32_t nm_read_reg(uint32_t u32Addr); + +/** + * @fn nm_read_reg_with_ret + * @brief Read register with error code return + * @param[in] u32Addr + * Register address + * @param[out] pu32RetVal + * Pointer to u32 variable used to return the read value + * @return @ref M2M_SUCCESS in case of success and @ref M2M_ERR_BUS_FAIL in case of failure + */ +int8_t nm_read_reg_with_ret(uint32_t u32Addr, uint32_t* pu32RetVal); + +/** + * @fn nm_write_reg + * @brief Write register + * @param[in] u32Addr + * Register address + * @param[in] u32Val + * Value to be written to the register + * @return @ref M2M_SUCCESS in case of success and @ref M2M_ERR_BUS_FAIL in case of failure + */ +int8_t nm_write_reg(uint32_t u32Addr, uint32_t u32Val); + +/** + * @fn nm_read_block + * @brief Read block of data + * @param[in] u32Addr + * Start address + * @param[out] puBuf + * Pointer to a buffer used to return the read data + * @param[in] u32Sz + * Number of bytes to read. The buffer size must be >= u32Sz + * @return @ref M2M_SUCCESS in case of success and @ref M2M_ERR_BUS_FAIL in case of failure + */ +int8_t nm_read_block(uint32_t u32Addr, uint8_t *puBuf, uint32_t u32Sz); + +/** + * @fn nm_write_block + * @brief Write block of data + * @param[in] u32Addr + * Start address + * @param[in] puBuf + * Pointer to the buffer holding the data to be written + * @param[in] u32Sz + * Number of bytes to write. The buffer size must be >= u32Sz + * @return @ref M2M_SUCCESS in case of success and @ref M2M_ERR_BUS_FAIL in case of failure + */ +int8_t nm_write_block(uint32_t u32Addr, uint8_t *puBuf, uint32_t u32Sz); + +#ifdef __cplusplus +} +#endif + +#endif /* _NMBUS_H_ */ diff --git a/ChaletLora.X/Source/winc3400_142/driver/include/nmdrv.h b/ChaletLora.X/Source/winc3400_142/driver/include/nmdrv.h new file mode 100644 index 0000000..063e624 --- /dev/null +++ b/ChaletLora.X/Source/winc3400_142/driver/include/nmdrv.h @@ -0,0 +1,176 @@ +/******************************************************************************* + This module contains WINC3400 M2M driver APIs declarations. + + File Name: + nmdrv.h + + Summary: + This module contains WINC3400 M2M driver APIs declarations. + + Description: + This module contains WINC3400 M2M driver APIs declarations. + *******************************************************************************/ + +//DOM-IGNORE-BEGIN +/******************************************************************************* +* Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries. +* +* Subject to your compliance with these terms, you may use Microchip software +* and any derivatives exclusively with Microchip products. It is your +* responsibility to comply with third party license terms applicable to your +* use of third party software (including open source software) that may +* accompany Microchip software. +* +* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER +* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED +* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A +* PARTICULAR PURPOSE. +* +* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, +* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND +* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS +* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE +* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN +* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, +* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. +*******************************************************************************/ +// DOM-IGNORE-END + +#ifndef _NMDRV_H_ +#define _NMDRV_H_ + +#include "nm_common.h" + +/*! +@enum \ + tenuNmState +@brief + Enumeration for WINC state + The following is used to track the state of the WINC (not initialized, initialized or started) + +@remarks + This is useful when putting the WINC in "download mode" to access the flash via SPI. By using + @ref nm_get_state and checking against the desired state, it is possible to validate whether + it is safe to proceed with SPI Flash access. +*/ +typedef enum { + NM_STATE_DEINIT, + /*!< WINC is not initialized */ + NM_STATE_INIT, + /*!< WINC has been initialized. SPI flash access is possible. */ + NM_STATE_START, + /*!< WINC has started */ +} tenuNmState; + +/** +* @struct tstrM2mRev +* @brief Structure holding firmware version parameters and build date/time +*/ +typedef struct { + uint16_t u16FirmwareHifInfo; /* Fw HIF Info */ + uint8_t u8FirmwareMajor; /* Version Major Number */ + uint8_t u8FirmwareRsvd; /* Reserved */ + uint8_t u8FirmwareMinor; /* Version Minor */ + uint8_t u8FirmwarePatch; /* Patch Number */ + uint8_t BuildDate[sizeof(__DATE__)]; // 12 bytes + uint8_t BuildTime[sizeof(__TIME__)]; // 9 bytes +} tstrM2mRev; + +#ifdef __cplusplus +extern "C" { +#endif +/** + * @fn nm_get_hif_info(uint16_t *pu16FwHifInfo, uint16_t *pu16OtaHifInfo); + * @brief Get Hif info of images in both partitions (Firmware and Ota). + * @param[out] pu16FwHifInfo + * Pointer holding Hif info of image in the active partition. + * @param[out] pu16OtaHifInfo + * Pointer holding Hif info of image in the inactive partition. + * @return @ref M2M_SUCCESS in case of success and Negative error code in case of failure + */ +int8_t nm_get_hif_info(uint16_t *pu16FwHifInfo, uint16_t *pu16OtaHifInfo); + +/** + * @fn nm_get_firmware_full_info(tstrM2mRev* pstrRev) + * @brief Get Firmware version info + * @param[out] pstrRev + * Pointer holds address of structure @ref tstrM2mRev that contains the version parameters + * of image in the active partition. + * @return @ref M2M_SUCCESS in case of success and Negative error code in case of failure + */ +int8_t nm_get_firmware_full_info(tstrM2mRev* pstrRev); + +/** + * @fn nm_get_ota_firmware_info(tstrM2mRev* pstrRev) + * @brief Get Firmware version info + * @param[out] pstrRev + * Pointer holds address of structure @ref tstrM2mRev that contains the version parameters + * of image in the inactive partition. + * @return @ref M2M_SUCCESS in case of success and Negative error code in case of failure + */ +int8_t nm_get_ota_firmware_info(tstrM2mRev* pstrRev); + +/** + * @fn nm_drv_init_download_mode + * @brief Initialize NMC1000 driver in download mode + * @return @ref M2M_SUCCESS in case of success and Negative error code in case of failure + */ +int8_t nm_drv_init_download_mode(void); + +/** + * @fn nm_drv_init + * @brief Initialize NMC1000 driver + * @param[in] arg + * Generic argument passed on to nm_drv_init_start + * @return @ref M2M_SUCCESS in case of success and Negative error code in case of failure + */ +int8_t nm_drv_init(void* arg); + +/** + * @fn nm_drv_init_hold + * @brief First part of nm_drv_init, up to the point of initializing spi for flash access. + * @see nm_drv_init + * @return @ref M2M_SUCCESS in case of success and Negative error code in case of failure + */ +int8_t nm_drv_init_hold(void); + +/** + * @fn nm_drv_init_start + * @brief Second part of nm_drv_init, continuing from where nm_drv_init_hold left off. + * @see nm_drv_init + * @param[in] arg + * Parameter inherited from nm_drv_init + * @return @ref M2M_SUCCESS in case of success and Negative error code in case of failure + */ +int8_t nm_drv_init_start(void* arg); + +/** + * @fn nm_drv_deinit + * @brief Deinitialize NMC1000 driver + * @param[in] arg + * Generic argument unused. + * @return @ref M2M_SUCCESS in case of success and Negative error code in case of failure + */ +int8_t nm_drv_deinit(void* arg); + +/** + * @fn nm_cpu_start(void) + * @brief Start CPU from the WINC module + * @return @ref M2M_SUCCESS in case of success and Negative error code in case of failure + */ +int8_t nm_cpu_start(void); + +/** + * @fn nm_get_state(void) + * @brief Get the current state of the WINC module + * @return The current state of the WINC module + */ +tenuNmState nm_get_state(void); + +#ifdef __cplusplus +} +#endif + +#endif /*_NMDRV_H_*/ + + diff --git a/ChaletLora.X/Source/winc3400_142/driver/include/nmspi.h b/ChaletLora.X/Source/winc3400_142/driver/include/nmspi.h new file mode 100644 index 0000000..75fd0e8 --- /dev/null +++ b/ChaletLora.X/Source/winc3400_142/driver/include/nmspi.h @@ -0,0 +1,137 @@ +/******************************************************************************* + This module contains WINC3400 SPI protocol bus APIs implementation. + + File Name: + nmspi.h + + Summary: + This module contains WINC3400 SPI protocol bus APIs implementation. + + Description: + This module contains WINC3400 SPI protocol bus APIs implementation. + *******************************************************************************/ + +//DOM-IGNORE-BEGIN +/******************************************************************************* +* Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries. +* +* Subject to your compliance with these terms, you may use Microchip software +* and any derivatives exclusively with Microchip products. It is your +* responsibility to comply with third party license terms applicable to your +* use of third party software (including open source software) that may +* accompany Microchip software. +* +* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER +* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED +* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A +* PARTICULAR PURPOSE. +* +* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, +* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND +* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS +* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE +* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN +* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, +* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. +*******************************************************************************/ +// DOM-IGNORE-END + +#ifndef _NMSPI_H_ +#define _NMSPI_H_ + +#include "nm_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @fn nm_spi_init + * @brief Initialize the SPI + * @return @ref M2M_SUCCESS in case of success and @ref M2M_ERR_BUS_FAIL in case of failure +*/ +int8_t nm_spi_init(void); + +/** +* @fn nm_spi_lock_init +* @brief Initialize the SPI lock +* @return None +*/ +void nm_spi_lock_init(void); + +/** + * @fn nm_spi_reset + * @brief Reset the SPI + * @return @ref M2M_SUCCESS in case of success and @ref M2M_ERR_BUS_FAIL in case of failure + */ +int8_t nm_spi_reset(void); + +/** + * @fn nm_spi_deinit + * @brief DeInitialize the SPI + * @return @ref M2M_SUCCESS in case of success and @ref M2M_ERR_BUS_FAIL in case of failure + */ +int8_t nm_spi_deinit(void); + +/** + * @fn nm_spi_read_reg + * @brief Read register + * @param[in] u32Addr + * Register address + * @return Register value + */ +uint32_t nm_spi_read_reg(uint32_t u32Addr); + +/** + * @fn nm_spi_read_reg_with_ret + * @brief Read register with error code return + * @param[in] u32Addr + * Register address + * @param[out] pu32RetVal + * Pointer to u32 variable used to return the read value + * @return @ref M2M_SUCCESS in case of success and @ref M2M_ERR_BUS_FAIL in case of failure + */ +int8_t nm_spi_read_reg_with_ret(uint32_t u32Addr, uint32_t* pu32RetVal); + +/** + * @fn nm_spi_write_reg + * @brief Write register + * @param[in] u32Addr + * Register address + * @param[in] u32Val + * Value to be written to the register + * @return @ref M2M_SUCCESS in case of success and @ref M2M_ERR_BUS_FAIL in case of failure + */ +int8_t nm_spi_write_reg(uint32_t u32Addr, uint32_t u32Val); + +/** + * @fn nm_spi_read_block + * @brief Read block of data + * @param[in] u32Addr + * Start address + * @param[out] puBuf + * Pointer to a buffer used to return the read data + * @param[in] u16Sz + * Number of bytes to read. The buffer size must be >= u16Sz + * @return @ref M2M_SUCCESS in case of success and @ref M2M_ERR_BUS_FAIL in case of failure + */ +int8_t nm_spi_read_block(uint32_t u32Addr, uint8_t *puBuf, uint16_t u16Sz); + +/** + * @fn nm_spi_write_block + * @brief Write block of data + * @param[in] u32Addr + * Start address + * @param[in] puBuf + * Pointer to the buffer holding the data to be written + * @param[in] u16Sz + * Number of bytes to write. The buffer size must be >= u16Sz + * @return @ref M2M_SUCCESS in case of success and @ref M2M_ERR_BUS_FAIL in case of failure + */ +int8_t nm_spi_write_block(uint32_t u32Addr, uint8_t *puBuf, uint16_t u16Sz); + +#ifdef __cplusplus +} +#endif + +#endif /* _NMSPI_H_ */ diff --git a/ChaletLora.X/Source/winc3400_142/driver/source/m2m_flash.c b/ChaletLora.X/Source/winc3400_142/driver/source/m2m_flash.c new file mode 100644 index 0000000..3ff97dc --- /dev/null +++ b/ChaletLora.X/Source/winc3400_142/driver/source/m2m_flash.c @@ -0,0 +1,354 @@ +/******************************************************************************* + File Name: + m2m_flash.c + + Summary: + This module contains the WINC flash interface. + + Description: + This module contains the WINC flash interface. + *******************************************************************************/ + +//DOM-IGNORE-BEGIN +/******************************************************************************* +* Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries. +* +* Subject to your compliance with these terms, you may use Microchip software +* and any derivatives exclusively with Microchip products. It is your +* responsibility to comply with third party license terms applicable to your +* use of third party software (including open source software) that may +* accompany Microchip software. +* +* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER +* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED +* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A +* PARTICULAR PURPOSE. +* +* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, +* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND +* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS +* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE +* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN +* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, +* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. +*******************************************************************************/ + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +INCLUDES +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ +#include "m2m_flash.h" +#include "spi_flash.h" +#include "spi_flash_map.h" +#include "nmdrv.h" + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +TYPEDEFS +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + +typedef struct +{ + uint32_t address; + uint32_t size; +} tstrFlashMapEntry; + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +GLOBALS +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + +static const tstrFlashMapEntry flashMap[] = +{ + {0 , 0 }, // WINC_FLASH_REGION_RAW, + {0 , OTA_IMAGE_SIZE }, // WINC_FLASH_REGION_FIRMWARE_ACTIVE, + {0 , OTA_IMAGE_SIZE }, // WINC_FLASH_REGION_FIRMWARE_INACTIVE, + {M2M_PLL_FLASH_OFFSET , M2M_PLL_FLASH_SZ }, // WINC_FLASH_REGION_PLL_TABLE, + {M2M_GAIN_FLASH_OFFSET , M2M_GAIN_FLASH_SZ }, // WINC_FLASH_REGION_GAIN_TABLE, + {M2M_PLL_FLASH_OFFSET , M2M_PLL_FLASH_SZ+M2M_GAIN_FLASH_SZ}, // WINC_FLASH_REGION_PLL_AND_GAIN_TABLES, + {M2M_TLS_ROOTCER_FLASH_OFFSET , M2M_TLS_ROOTCER_FLASH_SZ }, // WINC_FLASH_REGION_ROOT_CERTS, + {M2M_TLS_SERVER_FLASH_OFFSET , M2M_TLS_SERVER_FLASH_SZ }, // WINC_FLASH_REGION_LOCAL_CERTS, + {M2M_CACHED_CONNS_FLASH_OFFSET , M2M_CACHED_CONNS_FLASH_SZ }, // WINC_FLASH_REGION_CONN_PARAM, + {0 , M2M_HTTP_MEM_FLASH_SZ }, // WINC_FLASH_REGION_HTTP_FILES, +}; + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +FUNCTIONS +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + +static int8_t winc_flash_compare(uint8_t *pu8Buf, uint32_t u32Offset, uint32_t u32Size) +{ + int8_t ret = M2M_SUCCESS; + uint8_t buf[128]; + uint32_t offset = 0; + + while(offset < u32Size) + { + uint32_t chunk_sz = sizeof(buf); + if(chunk_sz > u32Size - offset) + chunk_sz = u32Size - offset; + ret = spi_flash_read(buf, u32Offset + offset, chunk_sz); + if(ret != M2M_SUCCESS) + break; + ret = memcmp(buf, pu8Buf + offset, chunk_sz); + if(ret != 0) + break; + offset += chunk_sz; + } + return ret; +} +static int8_t winc_flash_write_verify(uint8_t *pu8Buf, uint32_t u32Offset, uint32_t u32Size) +{ + int8_t ret = M2M_ERR_FAIL; + uint8_t count = 20; + + while((ret != M2M_SUCCESS) && (count-- > 0)) + { + ret = spi_flash_write(pu8Buf, u32Offset, u32Size); + if(ret == M2M_SUCCESS) + ret = winc_flash_compare(pu8Buf, u32Offset, u32Size); + } + return ret; +} + +/* Some internal functions for accessing the control structure. */ +static uint8_t crc7(uint8_t crc, const uint8_t *buff, uint16_t len) +{ + uint8_t reg = crc; + uint16_t i; + for(i = 0; i < len; i++) + { + uint16_t g; + for(g = 0; g < 8; g++) + { + uint8_t inv = (((buff[i] << g) & 0x80) >> 7) ^ ((reg >> 6) & 1); + reg = ((reg << 1) & 0x7f) ^ (9 * inv); + } + } + return reg; +} +static int8_t verify_control_structure(tstrOtaControlSec *pstrControlSec) +{ + int8_t s8Ret = M2M_SUCCESS; + + if(pstrControlSec->u32OtaMagicValue != OTA_MAGIC_VALUE) + s8Ret = M2M_ERR_FAIL; + if(pstrControlSec->u32OtaControlSecCrc != crc7(0x7f, (uint8_t *)pstrControlSec, sizeof(tstrOtaControlSec) - 4)) + s8Ret = M2M_ERR_FAIL; + + return s8Ret; +} +static int8_t read_control_structure(tstrOtaControlSec *pstrControlSec) +{ + int8_t s8Ret; + + s8Ret = spi_flash_read((uint8_t *)pstrControlSec, M2M_CONTROL_FLASH_OFFSET, sizeof(tstrOtaControlSec)); + if(s8Ret == M2M_SUCCESS) + s8Ret = verify_control_structure(pstrControlSec); + + if(s8Ret != M2M_SUCCESS) + { + s8Ret = spi_flash_read((uint8_t *)pstrControlSec, M2M_BACKUP_FLASH_OFFSET, sizeof(tstrOtaControlSec)); + if(s8Ret == M2M_SUCCESS) + s8Ret = verify_control_structure(pstrControlSec); + } + + return s8Ret; +} +static int8_t update_control_structure(tstrOtaControlSec *pstrControlSec) +{ + int8_t ret = M2M_ERR_FAIL; + + ret = spi_flash_erase(M2M_BACKUP_FLASH_OFFSET, M2M_BACKUP_FLASH_SZ); + if(ret == M2M_SUCCESS) + { + pstrControlSec->u32OtaSequenceNumber++; + pstrControlSec->u32OtaControlSecCrc = crc7(0x7f, (uint8_t *)pstrControlSec, sizeof(tstrOtaControlSec) - 4); + ret = winc_flash_write_verify((uint8_t *)pstrControlSec, M2M_BACKUP_FLASH_OFFSET, sizeof(tstrOtaControlSec)); + if(ret == M2M_SUCCESS) + { + ret = spi_flash_erase(M2M_CONTROL_FLASH_OFFSET, M2M_CONTROL_FLASH_SZ); + if(ret == M2M_SUCCESS) + { + pstrControlSec->u32OtaSequenceNumber++; + pstrControlSec->u32OtaControlSecCrc = crc7(0x7f, (uint8_t *)pstrControlSec, sizeof(tstrOtaControlSec) - 4); + ret = winc_flash_write_verify((uint8_t *)pstrControlSec, M2M_CONTROL_FLASH_OFFSET, sizeof(tstrOtaControlSec)); + } + } + } + return ret; +} + +static bool find_flash_section(tenuWincFlashRegion enuRegion, uint32_t *pu32StartAddr, uint32_t *pu32Size) +{ + /* Ensure the pointers and region are valid. */ + if((NULL == pu32StartAddr) || (NULL == pu32Size) || (enuRegion >= WINC_FLASH_NUM_REGIONS)) + return false; + + /* For the raw region resolve the full flash space, otherwise lookup + the region location and size from the flexible flash map. */ + switch(enuRegion) + { + case WINC_FLASH_REGION_RAW: + *pu32StartAddr = 0; + *pu32Size = spi_flash_get_size() << 17; + break; + case WINC_FLASH_REGION_FIRMWARE_ACTIVE: + case WINC_FLASH_REGION_FIRMWARE_INACTIVE: + case WINC_FLASH_REGION_HTTP_FILES: + { + /* In these cases we need to read the control structure to find the appropriate flash address. */ + tstrOtaControlSec strControl; + + /* Check the WINC is initialised and not running. */ + if (NM_STATE_INIT != nm_get_state()) + return false; + + /* Read control structure from flash. */ + if (M2M_SUCCESS != read_control_structure(&strControl)) + return false; + + if (WINC_FLASH_REGION_FIRMWARE_INACTIVE == enuRegion) + *pu32StartAddr = strControl.u32OtaRollbackImageOffset; + else if (WINC_FLASH_REGION_FIRMWARE_ACTIVE == enuRegion) + *pu32StartAddr = strControl.u32OtaCurrentWorkingImagOffset; + else if (WINC_FLASH_REGION_HTTP_FILES == enuRegion) + *pu32StartAddr = strControl.u32OtaCurrentWorkingImagOffset + (M2M_HTTP_MEM_FLASH_OFFSET - M2M_OTA_IMAGE1_OFFSET); + + *pu32Size = flashMap[enuRegion].size; + break; + } + default: + *pu32StartAddr = flashMap[enuRegion].address; + *pu32Size = flashMap[enuRegion].size; + break; + } + + M2M_INFO("Flash lookup %2d: 0x%06" PRIx32 " %0" PRId32 "\r\n", enuRegion, *pu32StartAddr, *pu32Size); + + return true; +} + +int8_t m2m_flash_erase_sector(tenuWincFlashRegion enuRegion, uint8_t u8StartSector, uint8_t u8NumSectors) +{ + uint32_t flashAddress; + uint32_t flashRegionSize; + + /* Check the WINC is initialised and not running. */ + if(NM_STATE_INIT != nm_get_state()) + return M2M_ERR_FAIL; + + /* Check the region is valid. */ + if(enuRegion >= WINC_FLASH_NUM_REGIONS) + return M2M_ERR_INVALID_ARG; + + /* Find region address and size. */ + if(false == find_flash_section(enuRegion, &flashAddress, &flashRegionSize)) + return M2M_ERR_FAIL; + + /* Erase is only supported for regions which begin on a sector boundary. */ + if(flashAddress & (FLASH_SECTOR_SZ-1)) + return M2M_ERR_INVALID_ARG; + + /* Check requested size fits within region size. */ + if((((uint32_t)u8StartSector + u8NumSectors) * FLASH_SECTOR_SZ) > flashRegionSize) + return M2M_ERR_FAIL; + + /* Find start address of area within requested region. */ + flashAddress += (u8StartSector * FLASH_SECTOR_SZ); + + /* Erase the requested sectors. */ + if(M2M_SUCCESS != spi_flash_erase(flashAddress, u8NumSectors * FLASH_SECTOR_SZ)) + return M2M_ERR_FAIL; + + return M2M_SUCCESS; +} + +int8_t m2m_flash_write(tenuWincFlashRegion enuRegion, void *pvBuffer, uint32_t u32Offset, uint32_t u32Size) +{ + uint32_t flashAddress; + uint32_t flashRegionSize; + + /* Check the WINC is initialised and not running. */ + if(NM_STATE_INIT != nm_get_state()) + return M2M_ERR_FAIL; + + /* Check the buffer pointer and region are valid. */ + if((NULL == pvBuffer) || (enuRegion >= WINC_FLASH_NUM_REGIONS)) + return M2M_ERR_INVALID_ARG; + + /* Find region address and size. */ + if(false == find_flash_section(enuRegion, &flashAddress, &flashRegionSize)) + return M2M_ERR_FAIL; + + /* Check requested size fits within region size. Also check for wraparound. */ + if(((u32Offset + u32Size) > flashRegionSize) || ((uint32_t)(u32Offset + u32Size) < u32Offset)) + return M2M_ERR_FAIL; + + /* Find start address of area within requested region. */ + flashAddress += u32Offset; + + /* Write data to flash. */ + if(M2M_SUCCESS != spi_flash_write(pvBuffer, flashAddress, u32Size)) + return M2M_ERR_FAIL; + + return M2M_SUCCESS; +} + +int8_t m2m_flash_read(tenuWincFlashRegion enuRegion, void *pvBuffer, uint32_t u32Offset, uint32_t u32Size) +{ + uint32_t flashAddress; + uint32_t flashRegionSize; + + /* Check the WINC is initialised and not running. */ + if(NM_STATE_INIT != nm_get_state()) + return M2M_ERR_FAIL; + + /* Check the buffer pointer and region are valid. */ + if((NULL == pvBuffer) || (enuRegion >= WINC_FLASH_NUM_REGIONS)) + return M2M_ERR_INVALID_ARG; + + /* Find region address and size. */ + if(false == find_flash_section(enuRegion, &flashAddress, &flashRegionSize)) + return M2M_ERR_FAIL; + + /* Check requested size fits within region size. Also check for wraparound. */ + if(((u32Offset + u32Size) > flashRegionSize) || ((uint32_t)(u32Offset + u32Size) < u32Offset)) + return M2M_ERR_FAIL; + + /* Find start address of area within requested region. */ + flashAddress += u32Offset; + + /* Read data from flash. */ + if(M2M_SUCCESS != spi_flash_read(pvBuffer, flashAddress, u32Size)) + return M2M_ERR_FAIL; + + return M2M_SUCCESS; +} + +int8_t m2m_flash_switch_firmware(void) +{ + tstrOtaControlSec strControl; + uint32_t u32Tmp; + + /* Check the WINC is initialised and not running. */ + if(NM_STATE_INIT != nm_get_state()) + return M2M_ERR_FAIL; + + /* Read control structure from flash. */ + if(M2M_SUCCESS != read_control_structure(&strControl)) + return M2M_ERR_FAIL; + + /* Switch active and inactive. */ + u32Tmp = strControl.u32OtaRollbackImageOffset; + strControl.u32OtaRollbackImageOffset = strControl.u32OtaCurrentWorkingImagOffset; + strControl.u32OtaCurrentWorkingImagOffset = u32Tmp; + + /* Ensure the inactive image is marked as invalid. This protects m2m_ota_switch_firmware from + switching to an image whose validity is unknown. Switching remains possible via this API. */ + strControl.u32OtaRollbackImageValidStatus = OTA_STATUS_INVALID; + + if(M2M_SUCCESS != update_control_structure(&strControl)) + return M2M_ERR_FAIL; + + return M2M_SUCCESS; +} + +//DOM-IGNORE-END diff --git a/ChaletLora.X/Source/winc3400_142/driver/source/m2m_hif.c b/ChaletLora.X/Source/winc3400_142/driver/source/m2m_hif.c new file mode 100644 index 0000000..774645b --- /dev/null +++ b/ChaletLora.X/Source/winc3400_142/driver/source/m2m_hif.c @@ -0,0 +1,857 @@ +/******************************************************************************* + File Name: + m2m_hif.c + + Summary: + This module contains M2M host interface API implementations. + + Description: + This module contains M2M host interface API implementations. + *******************************************************************************/ + +//DOM-IGNORE-BEGIN +/******************************************************************************* +* Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries. +* +* Subject to your compliance with these terms, you may use Microchip software +* and any derivatives exclusively with Microchip products. It is your +* responsibility to comply with third party license terms applicable to your +* use of third party software (including open source software) that may +* accompany Microchip software. +* +* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER +* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED +* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A +* PARTICULAR PURPOSE. +* +* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, +* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND +* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS +* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE +* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN +* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, +* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. +*******************************************************************************/ + +#include "nm_common.h" +#include "nmbus.h" +#include "nmdrv.h" +#include "nm_bsp.h" +#include "m2m_hif.h" +#include "m2m_types.h" +#include "nmasic.h" +#include "m2m_periph.h" +//#include "wdrv_winc_common.h" +//#include "osal/osal.h" + +#define NMI_AHB_DATA_MEM_BASE 0x30000 +#define NMI_AHB_SHARE_MEM_BASE 0xd0000 + +#define WIFI_HOST_RCV_CTRL_0 (0x1070) +#define WIFI_HOST_RCV_CTRL_1 (0x1084) +#define WIFI_HOST_RCV_CTRL_2 (0x1078) +#define WIFI_HOST_RCV_CTRL_3 (0x106c) +#define WIFI_HOST_RCV_CTRL_4 (0x150400) + + +#define INTERRUPT_CORTUS_0_3000D0 (0x10a8) +#define INTERRUPT_CORTUS_1_3000D0 (0x10ac) +#define INTERRUPT_CORTUS_2_3000D0 (0x10b0) +#define INTERRUPT_CORTUS_3_3000D0 (0x10b4) + +//static OSAL_SEM_HANDLE_TYPE hifSemaphore; + +typedef struct { + uint8_t u8ChipMode; + uint8_t u8ChipSleep; + uint8_t u8HifRXDone; + uint8_t u8Interrupt; + uint32_t u32RxAddr; + uint32_t u32RxSize; + tpfHifCallBack pfWifiCb; + tpfHifCallBack pfIpCb; + tpfHifCallBack pfOtaCb; + tpfHifCallBack pfSigmaCb; + tpfHifCallBack pfHifCb; + //tpfHifCallBack pfCryptoCb; + tpfHifCallBack pfSslCb; +} tstrHifContext; + +volatile tstrHifContext gstrHifCxt; + +/* + Special codes for managing HIF restriction to OTA rollback/switch only +*/ +#define HIF_OTA_RB_ONLY 0xFFFF +#define HIFCODE_OTA_RB ((M2M_REQ_GROUP_OTA << 8) | M2M_OTA_REQ_ROLLBACK) +#define HIFCODE_OTA_SW ((M2M_REQ_GROUP_OTA << 8) | M2M_OTA_REQ_SWITCH_FIRMWARE) +/* + Codes for new HIF messages (since last HIF major increase). + Only need ones which are host->winc. + Each entry is formed of ((GroupId << 8) | OpCode) +*/ +#define HIFCODE_SSL_WRITECERT ((M2M_REQ_GROUP_SSL << 8) | M2M_SSL_REQ_WRITE_OWN_CERTS) +#define HIFCODE_WIFI_PASSIVESCAN ((M2M_REQ_GROUP_WIFI << 8) | M2M_WIFI_REQ_PASSIVE_SCAN) +#define HIFCODE_WIFI_CONN ((M2M_REQ_GROUP_WIFI << 8) | M2M_WIFI_REQ_CONN) +#define HIFCODE_WIFI_CONN_PARAM ((M2M_REQ_GROUP_WIFI << 8) | M2M_WIFI_IND_CONN_PARAM) +#define HIFCODE_WIFI_DELETE_CRED ((M2M_REQ_GROUP_WIFI << 8) | M2M_WIFI_REQRSP_DELETE_APID) +#define HIFCODE_WIFI_START_PROV_MODE ((M2M_REQ_GROUP_WIFI << 8) | M2M_WIFI_REQ_START_PROVISION_MODE) +#define HIFCODE_WIFI_ENABLE_AP ((M2M_REQ_GROUP_WIFI << 8) | M2M_WIFI_REQ_ENABLE_AP) +#define HIFCODE_IP_RAW_SOCK_OPT ((M2M_REQ_GROUP_IP << 8) | SOCKET_CMD_RAW_SET_SOCK_OPT) +#define HIFCODE_WIFI_ROAMING ((M2M_REQ_GROUP_WIFI << 8) | M2M_WIFI_REQ_ROAMING) +#define HIFCODE_IP_SECURE ((M2M_REQ_GROUP_IP << 8) | SOCKET_CMD_SECURE) +#define HIFCODE_WIFI_SCAN_SSID_LIST ((M2M_REQ_GROUP_WIFI << 8) | M2M_WIFI_REQ_SCAN_SSID_LIST) +#define HIFCODE_WIFI_SET_STOP_SCAN_OPTION ((M2M_REQ_GROUP_WIFI << 8) | M2M_WIFI_REQ_SET_STOP_SCAN_OPTION) + +/* + List of new HIF messages (since last HIF major increase). + Only need to list ones which are host->winc. + Additionally, entry 0 used to indicate OTA RB/SW only. +*/ +#define NEW_HIF_LIST \ + HIF_OTA_RB_ONLY, \ + HIFCODE_SSL_WRITECERT, \ + HIFCODE_WIFI_PASSIVESCAN, \ + HIFCODE_WIFI_CONN, \ + HIFCODE_WIFI_CONN_PARAM, \ + HIFCODE_WIFI_DELETE_CRED, \ + HIFCODE_WIFI_START_PROV_MODE, \ + HIFCODE_WIFI_ENABLE_AP, \ + HIFCODE_IP_RAW_SOCK_OPT, \ + HIFCODE_WIFI_ROAMING, \ + HIFCODE_IP_SECURE, \ + HIFCODE_WIFI_SCAN_SSID_LIST, \ + HIFCODE_WIFI_SET_STOP_SCAN_OPTION +/* + Array of HIF messages which are not supported by Firmware. + During hif_init() this array is rebased using an offset determined by Firmware HIF level. +*/ +static uint16_t gau16HifBlacklist[] = {NEW_HIF_LIST}; +#define HIF_BLACKLIST_SZ (sizeof(gau16HifBlacklist)/sizeof(gau16HifBlacklist[0])) +static uint8_t gu8HifBlOffset = 0; + + +static int8_t hif_set_rx_done(void) +{ + uint32_t reg; + int8_t ret = M2M_SUCCESS; + gstrHifCxt.u8HifRXDone = 0; + if (ISNMC3400(nmi_get_chipid())) { + ret = nm_write_reg(INTERRUPT_CORTUS_0_3000D0, 1); + if(ret != M2M_SUCCESS)goto ERR1; + } else { + ret = nm_read_reg_with_ret(WIFI_HOST_RCV_CTRL_0, ®); + if(ret != M2M_SUCCESS)goto ERR1; + + /* Set RX Done */ + reg |= NBIT1; + ret = nm_write_reg(WIFI_HOST_RCV_CTRL_0, reg); + if(ret != M2M_SUCCESS)goto ERR1; + } + +ERR1: + return ret; +} + +/** + * @fn static void m2m_hif_cb(uint8_t u8OpCode, uint16_t u16DataSize, uint32_t u32Addr) + * @brief WiFi call back function + * @param[in] u8OpCode + * HIF Opcode type. + * @param[in] u16DataSize + * HIF data length. + * @param[in] u32Addr + * HIF address. + */ +static void m2m_hif_cb(uint8_t u8OpCode, uint16_t u16DataSize, uint32_t u32Addr) +{ +} + +/** + * @fn int8_t hif_chip_wake(void); + * @brief To Wakeup the chip. + * @return The function shall return ZERO for successful operation and a negative value otherwise. + */ +int8_t hif_chip_wake(void) +{ + int8_t ret = M2M_SUCCESS; + if(gstrHifCxt.u8HifRXDone) + { + /* chip already wake for the rx not done no need to send wake request */ + return ret; + } + if(gstrHifCxt.u8ChipSleep == 0) + { + if(gstrHifCxt.u8ChipMode != M2M_NO_PS) + { + ret = chip_wake(); + if(ret != M2M_SUCCESS)goto ERR1; + } + } + gstrHifCxt.u8ChipSleep++; +ERR1: + return ret; +} +/*! +@fn \ + void hif_set_sleep_mode(uint8_t u8Pstype); + +@brief + Set the sleep mode of the HIF layer. + +@param [in] u8Pstype + Sleep mode. + +@return + The function SHALL return 0 for success and a negative value otherwise. +*/ + +void hif_set_sleep_mode(uint8_t u8Pstype) +{ + gstrHifCxt.u8ChipMode = u8Pstype; +} + +/*! +@fn \ + uint8_t hif_get_sleep_mode(void); + +@brief + Get the sleep mode of the HIF layer. + +@return + The function SHALL return the sleep mode of the HIF layer. +*/ +uint8_t hif_get_sleep_mode(void) +{ + return gstrHifCxt.u8ChipMode; +} + +/** + * @fn int8_t hif_chip_sleep_sc(void); + * @brief To clear the chip sleep but keep the chip sleep + * @return The function shall return ZERO for successful operation and a negative value otherwise. + */ +static int8_t hif_chip_sleep_sc(void) +{ + if(gstrHifCxt.u8ChipSleep >= 1) + { + gstrHifCxt.u8ChipSleep--; + } + return M2M_SUCCESS; +} + + +/** + * @fn int8_t hif_chip_sleep(void); + * @brief To make the chip sleep. + * @return The function shall return ZERO for successful operation and a negative value otherwise. + */ +int8_t hif_chip_sleep(void) +{ + int8_t ret = M2M_SUCCESS; + + if(gstrHifCxt.u8ChipSleep >= 1) + { + gstrHifCxt.u8ChipSleep--; + } + + if(gstrHifCxt.u8ChipSleep == 0) + { + if(gstrHifCxt.u8ChipMode != M2M_NO_PS) + ret = chip_sleep(); + } + return ret; +} + +/** + * @fn int8_t hif_init(void * arg); + * @brief To initialize HIF layer. + * @param[in] arg + * Pointer to the arguments. + * @return The function shall return ZERO for successful operation and a negative value otherwise. + */ +int8_t hif_init(void *arg) +{ + memset((uint8_t*)&gstrHifCxt, 0, sizeof(tstrHifContext)); + +// if (OSAL_RESULT_TRUE != OSAL_SEM_Create(&hifSemaphore, OSAL_SEM_TYPE_BINARY, 1, 1)) +// return M2M_ERR_INIT; + + hif_register_cb(M2M_REQ_GROUP_HIF, m2m_hif_cb); + return hif_chip_sleep(); +} + +/** + * @fn int8_t hif_deinit(void * arg); + * @brief To Deinitialize HIF layer. + * @param[in] arg + * Pointer to the arguments. + * @return The function shall return ZERO for successful operation and a negative value otherwise. + */ +int8_t hif_deinit(void *arg) +{ + int8_t ret = M2M_SUCCESS; + ret = hif_chip_wake(); + memset((uint8_t*)&gstrHifCxt, 0, sizeof(tstrHifContext)); + return ret; +} + +/** + * @fn int8_t hif_check_compatibility(uint16_t u16HifInfo); + * @brief + * To check the compatibility of an image with the current driver. + * @param [in] u16HifInfo + * HIF info of image to be checked. + * @return The function shall return ZERO for compatible image and a negative value otherwise. + */ +int8_t hif_check_compatibility(uint16_t u16HifInfo) +{ + int8_t ret = M2M_ERR_FW_VER_MISMATCH; + if((M2M_GET_HIF_BLOCK(u16HifInfo) == M2M_HIF_BLOCK_VALUE) && (M2M_GET_HIF_MAJOR(u16HifInfo) == M2M_HIF_MAJOR_VALUE)) + { + ret = M2M_SUCCESS; + } + return ret; +} + +/** + * @fn int8_t hif_enable_access(void); + * @brief + * To enable access to HIF layer, based on HIF level of Firmware. + * This function reads HIF level directly from a register written by Firmware. + * @return The function shall return ZERO for full match operation and a negative value if operation is restricted. + */ +int8_t hif_enable_access(void) +{ + int8_t ret = M2M_SUCCESS; + uint16_t fw_hif_info = 0; + + ret = nm_get_hif_info(&fw_hif_info, NULL); + if(ret == M2M_SUCCESS) + { + ret = hif_check_compatibility(fw_hif_info); + if(ret == M2M_SUCCESS) + { + switch(M2M_GET_HIF_MINOR(fw_hif_info)) + { + case 0: + gu8HifBlOffset = 1; + break; + case 1: + gu8HifBlOffset = 2; + break; + case 2: + gu8HifBlOffset = 2; + break; + case 3: + gu8HifBlOffset = 3; + break; + case 4: + gu8HifBlOffset = 10; + break; + case 5: + gu8HifBlOffset = 13; + break; + // Additional case to be added each time hif minor increments. + // All additional cases to be removed in the event of a hif major increment. + // Default catches all cases in which hif minor is greater in Firmware than in Driver. + default: + gu8HifBlOffset = HIF_BLACKLIST_SZ; + break; + } + } + else + { + gu8HifBlOffset = 0; + M2M_ERR("HIF access limited to OTA Switch/Rollback only\r\n"); + } + } + return ret; +} + +/** + * @fn int8_t hif_check_code(uint8_t u8Gid, uint8_t u8OpCode); + * @brief + * To check that a particular hif message is supported with the current driver/firmware pair. + * @param[in] u8Gid + * Group ID. + * @param[in] u8Opcode + * Operation ID. + * @return The function shall return @ref M2M_SUCCESS for success and a negative value otherwise. + */ +int8_t hif_check_code(uint8_t u8Gid, uint8_t u8OpCode) +{ + uint8_t u8BlId; + uint16_t u16HifCode = ((uint16_t)u8Gid<<8) | u8OpCode; + if((u16HifCode == HIFCODE_OTA_RB) || (u16HifCode == HIFCODE_OTA_SW)) + { + return M2M_SUCCESS; + } + if(gu8HifBlOffset == 0) + { + M2M_ERR("HIF OTA rb/sw only\r\n"); + return M2M_ERR_SEND; + } + for(u8BlId = gu8HifBlOffset; u8BlId < HIF_BLACKLIST_SZ; u8BlId++) + { + if(u16HifCode == gau16HifBlacklist[u8BlId]) + { + M2M_ERR("HIF message unsupported\r\n"); + return M2M_ERR_SEND; + } + } + return M2M_SUCCESS; +} + +/** + * @fn int8_t hif_send(uint8_t u8Gid,uint8_t u8Opcode,uint8_t *pu8CtrlBuf,uint16_t u16CtrlBufSize, + * uint8_t *pu8DataBuf,uint16_t u16DataSize, uint16_t u16DataOffset) + * @brief Send packet using host interface. + * + * @param[in] u8Gid + * Group ID. + * @param[in] u8Opcode + * Operation ID. + * @param[in] pu8CtrlBuf + * Pointer to the Control buffer. + * @param[in] u16CtrlBufSize + * Control buffer size. + * @param[in] u16DataOffset + * Packet Data offset. + * @param[in] pu8DataBuf + * Packet buffer Allocated by the caller. + * @param[in] u16DataSize + * Packet buffer size (including the HIF header). + * @return The function shall return @ref M2M_SUCCESS for successful operation and a negative value otherwise. + */ +int8_t hif_send(uint8_t u8Gid, uint8_t u8Opcode, uint8_t *pu8CtrlBuf, uint16_t u16CtrlBufSize, + uint8_t *pu8DataBuf, uint16_t u16DataSize, uint16_t u16DataOffset) +{ + int8_t ret = M2M_ERR_SEND; + tstrHifHdr strHif; + uint32_t u32CtrlDataGap = u16DataOffset; + +// while (OSAL_RESULT_FALSE == OSAL_SEM_Pend(&hifSemaphore, OSAL_WAIT_FOREVER)) + { + } + + strHif.u8Opcode = u8Opcode&(~NBIT7); + strHif.u8Gid = u8Gid; + strHif.u16Length = M2M_HIF_HDR_OFFSET; + + if(pu8CtrlBuf != NULL) + { + if(u16CtrlBufSize > M2M_HIF_MAX_PACKET_SIZE-M2M_HIF_HDR_OFFSET) + { + M2M_ERR("HIF %s (%dB) exceeds max (%dB)\n", "Ctrl", u16CtrlBufSize, M2M_HIF_MAX_PACKET_SIZE-M2M_HIF_HDR_OFFSET); + goto ERR2; + } + strHif.u16Length += u16CtrlBufSize; + u32CtrlDataGap -= u16CtrlBufSize; + } + if(pu8DataBuf != NULL) + { + if((uint32_t)u16DataOffset + u16DataSize > M2M_HIF_MAX_PACKET_SIZE-M2M_HIF_HDR_OFFSET) + { + M2M_ERR("HIF %s (%luB) exceeds max (%luB)\n", "Data", (uint32_t)u16DataOffset + u16DataSize, (uint32_t)M2M_HIF_MAX_PACKET_SIZE-M2M_HIF_HDR_OFFSET); + goto ERR2; + } + strHif.u16Length += u32CtrlDataGap + u16DataSize; + } + + ret = hif_check_code(strHif.u8Gid, strHif.u8Opcode); + if(ret != M2M_SUCCESS) + { + goto ERR2; + } + + if(strHif.u16Length <= M2M_HIF_MAX_PACKET_SIZE) + { + ret = hif_chip_wake(); + if(ret == M2M_SUCCESS) + { + volatile uint32_t reg, dma_addr = 0; + volatile uint16_t cnt = 0; + + reg = 0UL; + reg |= (uint32_t)u8Gid; + reg |= ((uint32_t)u8Opcode<<8); + reg |= ((uint32_t)strHif.u16Length<<16); + ret = nm_write_reg(NMI_STATE_REG, reg); + if(M2M_SUCCESS != ret) goto ERR1; + + reg = 0UL; + reg |= NBIT1; + ret = nm_write_reg(WIFI_HOST_RCV_CTRL_2, reg); + if(M2M_SUCCESS != ret) goto ERR1; + + + if (ISNMC3400(nmi_get_chipid())) { + + ret = nm_write_reg(INTERRUPT_CORTUS_1_3000D0, 1); + if(M2M_SUCCESS != ret) goto ERR1; + } + + dma_addr = 0; + + for(cnt = 0; cnt < 1000*5; cnt ++) + { + ret = nm_read_reg_with_ret(WIFI_HOST_RCV_CTRL_2, (uint32_t *)®); + if(ret != M2M_SUCCESS) break; + /* + * If it takes too long to get a response, the slow down to + * avoid back-to-back register read operations. + */ + if(cnt >= 1000) { + if(cnt == 1000) { + M2M_INFO("Slowing down...\n"); + } + nm_sleep(5); + } + if(!(reg & NBIT1)) + { + ret = nm_read_reg_with_ret(WIFI_HOST_RCV_CTRL_4, (uint32_t *)&dma_addr); + if(ret != M2M_SUCCESS) { + /*in case of read error clear the DMA address and return error*/ + dma_addr = 0; + goto ERR1; + } + /*in case of success break */ + break; + } + } + + if (dma_addr != 0) + { + volatile uint32_t u32CurrAddr; + u32CurrAddr = dma_addr; + strHif.u16Length=NM_BSP_B_L_16(strHif.u16Length); + M2M_DBG("Writing into %" PRIx32 " %d\r\n", dma_addr, strHif.u16Length); + ret = nm_write_block(u32CurrAddr, (uint8_t*)&strHif, M2M_HIF_HDR_OFFSET); + if(M2M_SUCCESS != ret) goto ERR1; + u32CurrAddr += M2M_HIF_HDR_OFFSET; + if(pu8CtrlBuf != NULL) + { + ret = nm_write_block(u32CurrAddr, pu8CtrlBuf, u16CtrlBufSize); + if(M2M_SUCCESS != ret) goto ERR1; + u32CurrAddr += u16CtrlBufSize; + } + if(pu8DataBuf != NULL) + { + u32CurrAddr += u32CtrlDataGap; + ret = nm_write_block(u32CurrAddr, pu8DataBuf, u16DataSize); + if(M2M_SUCCESS != ret) goto ERR1; + u32CurrAddr += u16DataSize; + } + + reg = dma_addr << 2; + + /* Following line of code is to generate the interrupt which is not strictly needed for 3400, + but has no noticeable side effects + */ + reg |= NBIT1; + ret = nm_write_reg(WIFI_HOST_RCV_CTRL_3, reg); + if(M2M_SUCCESS != ret) goto ERR1; + + if (ISNMC3400(nmi_get_chipid())) { + ret = nm_write_reg(INTERRUPT_CORTUS_2_3000D0, 1); + if(M2M_SUCCESS != ret) goto ERR1; + } + } + else + { + ret = hif_chip_sleep(); + M2M_DBG("Failed to alloc rx size\r\n"); + ret = M2M_ERR_MEM_ALLOC; + goto ERR2; + } + } + else + { + M2M_ERR("(HIF)Failed to wakeup the chip\r\n"); + goto ERR2; + } + } + else + { + M2M_ERR("HIF message length (%d) exceeds max length (%d)\r\n", strHif.u16Length, M2M_HIF_MAX_PACKET_SIZE); + ret = M2M_ERR_SEND; + goto ERR2; + } +// OSAL_SEM_Post(&hifSemaphore); + /*actual sleep ret = M2M_SUCCESS*/ + ret = hif_chip_sleep(); + return ret; +ERR1: + /*reset the count but no actual sleep as it already bus error*/ + hif_chip_sleep_sc(); +ERR2: + /*logical error*/ +// OSAL_SEM_Post(&hifSemaphore); + return ret; +} +/** + * @fn hif_isr + * @brief Host interface interrupt service routine + * @return @ref M2M_SUCCESS in case of success or a negative vale otherwise + */ +static int8_t hif_isr(void) +{ + int8_t ret = M2M_SUCCESS; + uint32_t reg; + volatile tstrHifHdr strHif; + +// while (OSAL_RESULT_FALSE == OSAL_SEM_Pend(&hifSemaphore, OSAL_WAIT_FOREVER)) + { + } + + ret = nm_read_reg_with_ret(WIFI_HOST_RCV_CTRL_0, ®); + if(M2M_SUCCESS == ret) + { + if(reg & 0x1) /* New interrupt has been received */ + { + uint16_t size; + + /*Clearing RX interrupt*/ + ret = nm_read_reg_with_ret(WIFI_HOST_RCV_CTRL_0, ®); + if(ret != M2M_SUCCESS)goto ERR1; + reg &= ~NBIT0; + ret = nm_write_reg(WIFI_HOST_RCV_CTRL_0, reg); + if(ret != M2M_SUCCESS)goto ERR1; + /* read the rx size */ + ret = nm_read_reg_with_ret(WIFI_HOST_RCV_CTRL_0, ®); + if(M2M_SUCCESS != ret) + { + M2M_ERR("(hif) WIFI_HOST_RCV_CTRL_0 bus fail\r\n"); + goto ERR1; + } + gstrHifCxt.u8HifRXDone = 1; + size = (uint16_t)((reg >> 2) & 0xfff); + if (size > 0) { + uint32_t address = 0; + /** + start bus transfer + **/ + ret = nm_read_reg_with_ret(WIFI_HOST_RCV_CTRL_1, &address); + if(M2M_SUCCESS != ret) + { + M2M_ERR("(hif) WIFI_HOST_RCV_CTRL_1 bus fail\r\n"); + goto ERR1; + } + gstrHifCxt.u32RxAddr = address; + gstrHifCxt.u32RxSize = size; + ret = nm_read_block(address, (uint8_t*)&strHif, sizeof(tstrHifHdr)); + strHif.u16Length = NM_BSP_B_L_16(strHif.u16Length); + if(M2M_SUCCESS != ret) + { + M2M_ERR("(hif) address bus fail\r\n"); + goto ERR1; + } + if(strHif.u16Length != size) + { + if((size - strHif.u16Length) > 4) + { + M2M_ERR("(hif) Corrupted packet Size = %u \r\n", + size, strHif.u16Length, strHif.u8Gid, strHif.u8Opcode); + ret = M2M_ERR_BUS_FAIL; + goto ERR1; + } + } + +// OSAL_SEM_Post(&hifSemaphore); + + if(M2M_REQ_GROUP_WIFI == strHif.u8Gid) + { + if(gstrHifCxt.pfWifiCb) + gstrHifCxt.pfWifiCb(strHif.u8Opcode, strHif.u16Length - M2M_HIF_HDR_OFFSET, address + M2M_HIF_HDR_OFFSET); + else + M2M_ERR("WIFI callback is not registered\r\n"); + } + else if(M2M_REQ_GROUP_IP == strHif.u8Gid) + { + if(gstrHifCxt.pfIpCb) + gstrHifCxt.pfIpCb(strHif.u8Opcode, strHif.u16Length - M2M_HIF_HDR_OFFSET, address + M2M_HIF_HDR_OFFSET); + else + M2M_ERR("Socket callback is not registered\r\n"); + } + else if(M2M_REQ_GROUP_OTA == strHif.u8Gid) + { + if(gstrHifCxt.pfOtaCb) + gstrHifCxt.pfOtaCb(strHif.u8Opcode, strHif.u16Length - M2M_HIF_HDR_OFFSET, address + M2M_HIF_HDR_OFFSET); + else + M2M_ERR("OTA callback is not registered\r\n"); + } + else if(M2M_REQ_GROUP_SIGMA == strHif.u8Gid) + { + if(gstrHifCxt.pfSigmaCb) + gstrHifCxt.pfSigmaCb(strHif.u8Opcode, strHif.u16Length - M2M_HIF_HDR_OFFSET, address + M2M_HIF_HDR_OFFSET); + else + M2M_ERR("Sigma callback is not registered\r\n"); + } + else if(M2M_REQ_GROUP_SSL == strHif.u8Gid) + { + if(gstrHifCxt.pfSslCb) + gstrHifCxt.pfSslCb(strHif.u8Opcode, strHif.u16Length - M2M_HIF_HDR_OFFSET, address + M2M_HIF_HDR_OFFSET); + else + M2M_ERR("SSL callback is not registered\r\n"); + } + else + { + M2M_ERR("(hif) invalid group ID\r\n"); + return M2M_ERR_BUS_FAIL; + } + if(gstrHifCxt.u8HifRXDone) + { + M2M_ERR("(hif) host app didn't set RX Done <%u><%X>\n", strHif.u8Gid, strHif.u8Opcode); + return hif_set_rx_done(); + } + + return M2M_SUCCESS; + } + else + { + ret = M2M_ERR_RCV; + M2M_ERR("(hif) Wrong Size\r\n"); + goto ERR1; + } + } + else + { + M2M_ERR("(hif) False interrupt %lx\r\n",reg); + ret = M2M_ERR_FAIL; + } + } + else + { + M2M_ERR("(hif) Failed to read interrupt reg\r\n"); + } + +ERR1: +// OSAL_SEM_Post(&hifSemaphore); + return ret; +} + +/** +* @fn hif_handle_isr(void) +* @brief Handle interrupt received from WINC3400 firmware. +* @return The function SHALL return 0 for success and a negative value otherwise. +*/ +int8_t hif_handle_isr(void) +{ + int8_t ret = M2M_SUCCESS; + + ret = hif_isr(); + if (M2M_SUCCESS != ret) + { + M2M_ERR("(hif) Failed to handle interrupt %d try Again..\r\n",ret); + } + + return ret; +} + +/** + * @fn hif_receive + * @brief Host interface interrupt service routine + * @param[in] u32Addr + * Receive start address + * @param[out] pu8Buf + * Pointer to receive buffer. Allocated by the caller + * @param[in] u16Sz + * Receive buffer size + * @param[in] isDone + * If you don't need any more packets send True otherwise send false + * @return The function shall return ZERO for successful operation and a negative value otherwise. + */ +int8_t hif_receive(uint32_t u32Addr, uint8_t *pu8Buf, uint16_t u16Sz, uint8_t isDone) +{ + int8_t ret = M2M_SUCCESS; + + if((u32Addr == 0) || (pu8Buf == NULL) || (u16Sz == 0)) + { + if(isDone) + { + ret = hif_set_rx_done(); + } + else + { + ret = M2M_ERR_FAIL; + M2M_ERR(" hif_receive: Invalid argument\r\n"); + } + goto ERR1; + } + + if(u16Sz > gstrHifCxt.u32RxSize) + { + ret = M2M_ERR_FAIL; + M2M_ERR("APP Requested Size is larger than the received buffer size <%d><%" PRId32 ">\r\n", u16Sz, gstrHifCxt.u32RxSize); + goto ERR1; + } + if((u32Addr < gstrHifCxt.u32RxAddr)||((u32Addr + u16Sz)>(gstrHifCxt.u32RxAddr+gstrHifCxt.u32RxSize))) + { + ret = M2M_ERR_FAIL; + M2M_ERR("APP Requested Address beyond the received buffer address and length\r\n"); + goto ERR1; + } + + /* Receive the payload */ + ret = nm_read_block(u32Addr, pu8Buf, u16Sz); + if(ret != M2M_SUCCESS)goto ERR1; + + /* check if this is the last packet */ + if( + isDone + || (((gstrHifCxt.u32RxAddr+gstrHifCxt.u32RxSize) - (u32Addr+u16Sz)) <= 3) + /* Length in the RCV CTRL 0 register is rounded off to 4 by the firmware, + but length inside the HIF header is not, hence consider done if fewer than + 4 bytes left to read */ + ) + { + /* set RX done */ + ret = hif_set_rx_done(); + } +ERR1: + return ret; +} + +/** + * @fn hif_register_cb + * @brief To set Callback function for every component + * @param[in] u8Grp + * Group to which the Callback function should be set. + * @param[in] fn + * function to be set + * @return The function shall return ZERO for successful operation and a negative value otherwise. + */ + +int8_t hif_register_cb(uint8_t u8Grp, tpfHifCallBack fn) +{ + int8_t ret = M2M_SUCCESS; + switch(u8Grp) + { + case M2M_REQ_GROUP_IP: + gstrHifCxt.pfIpCb = fn; + break; + case M2M_REQ_GROUP_WIFI: + gstrHifCxt.pfWifiCb = fn; + break; + case M2M_REQ_GROUP_OTA: + gstrHifCxt.pfOtaCb = fn; + break; + case M2M_REQ_GROUP_HIF: + gstrHifCxt.pfHifCb = fn; + break; + case M2M_REQ_GROUP_SIGMA: + gstrHifCxt.pfSigmaCb = fn; + break; + case M2M_REQ_GROUP_SSL: + gstrHifCxt.pfSslCb = fn; + break; + default: + M2M_ERR("GRp ? %d\r\n", u8Grp); + ret = M2M_ERR_FAIL; + break; + } + return ret; +} + +//DOM-IGNORE-END \ No newline at end of file diff --git a/ChaletLora.X/Source/winc3400_142/driver/source/m2m_ota.c b/ChaletLora.X/Source/winc3400_142/driver/source/m2m_ota.c new file mode 100644 index 0000000..a38285d --- /dev/null +++ b/ChaletLora.X/Source/winc3400_142/driver/source/m2m_ota.c @@ -0,0 +1,190 @@ +/******************************************************************************* + WINC3400 IoT OTA Interface. + + File Name: + m2m_ota.c + + Summary: + WINC3400 IoT OTA Interface + + Description: + WINC3400 IoT OTA Interface + *******************************************************************************/ + +//DOM-IGNORE-BEGIN +/******************************************************************************* +* Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries. +* +* Subject to your compliance with these terms, you may use Microchip software +* and any derivatives exclusively with Microchip products. It is your +* responsibility to comply with third party license terms applicable to your +* use of third party software (including open source software) that may +* accompany Microchip software. +* +* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER +* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED +* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A +* PARTICULAR PURPOSE. +* +* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, +* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND +* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS +* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE +* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN +* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, +* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. +*******************************************************************************/ + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +INCLUDES +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ +#include "nm_common.h" +#include "m2m_types.h" +#include "m2m_ota.h" +#include "m2m_wifi.h" +#include "m2m_hif.h" + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +MACROS +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +DATA TYPES +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ +static tpfOtaUpdateCb gpfOtaUpdateCb = NULL; + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +FUNCTION PROTOTYPES +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + +/** +@fn m2m_ota_cb(uint8_t u8OpCode, uint16_t u16DataSize, uint32_t u32Addr) +@brief Internal OTA call back function. +@param[in] u8OpCode + HIF Opcode type. +@param[in] u16DataSize + HIF data length. +@param[in] u32Addr + HIF address. +*/ +static void m2m_ota_cb(uint8_t u8OpCode, uint16_t u16DataSize, uint32_t u32Addr) +{ + int8_t s8Ret = M2M_SUCCESS; + if (u8OpCode == M2M_OTA_RESP_UPDATE_STATUS) + { + tstrOtaUpdateStatusResp strOtaUpdateStatusResp; + memset((uint8_t*)&strOtaUpdateStatusResp, 0, sizeof(tstrOtaUpdateStatusResp)); + s8Ret = hif_receive(u32Addr, (uint8_t*)&strOtaUpdateStatusResp, sizeof(tstrOtaUpdateStatusResp), 0); + if(s8Ret == M2M_SUCCESS) + { + if(gpfOtaUpdateCb) + gpfOtaUpdateCb(strOtaUpdateStatusResp.u8OtaUpdateStatusType, strOtaUpdateStatusResp.u8OtaUpdateStatus); + } + } + else + { + M2M_ERR("Invalid OTA resp %d ?\r\n", u8OpCode); + } +} + +/*! +@fn int8_t m2m_ota_init(tpfOtaUpdateCb pfOtaUpdateCb) +@brief Initialize the OTA layer. +@param[in] pfOtaUpdateCb + OTA Update callback function. +@return The function returns @ref M2M_SUCCESS for success and a negative value otherwise. +*/ +int8_t m2m_ota_init(tpfOtaUpdateCb pfOtaUpdateCb) +{ + int8_t ret = M2M_SUCCESS; + + if (pfOtaUpdateCb) { + gpfOtaUpdateCb = pfOtaUpdateCb; + } else { + M2M_ERR("Invalid OTA update callback\r\n"); + } + + hif_register_cb(M2M_REQ_GROUP_OTA, m2m_ota_cb); + + return ret; +} + +/*! +@fn int8_t m2m_ota_start_update(unsigned char * pcDownloadUrl) +@brief Request OTA start update using the downloaded URL. +@param[in] pcDownloadUrl + The download firmware URL, you get it from device info. +@return The function returns @ref M2M_SUCCESS for success and a negative value otherwise. +*/ +int8_t m2m_ota_start_update(unsigned char *pcDownloadUrl) +{ + int8_t ret = M2M_SUCCESS; + uint16_t u16DurlSize = strlen((char*)pcDownloadUrl) + 1; + + ret = hif_send(M2M_REQ_GROUP_OTA, M2M_OTA_REQ_START_UPDATE, pcDownloadUrl, u16DurlSize, NULL, 0, 0); + return ret; +} + +/*! +@fn int8_t m2m_ota_rollback(void) +@brief Request OTA Rollback image. +@return The function returns @ref M2M_SUCCESS for success and a negative value otherwise. +*/ +int8_t m2m_ota_rollback(void) +{ + int8_t ret = M2M_SUCCESS; + tstrM2mRev strRev; + + ret = m2m_ota_get_firmware_version(&strRev); + if(ret == M2M_SUCCESS) + { + if(M2M_GET_HIF_BLOCK(strRev.u16FirmwareHifInfo) == M2M_HIF_BLOCK_VALUE) + { + ret = hif_send(M2M_REQ_GROUP_OTA, M2M_OTA_REQ_ROLLBACK, NULL, 0, NULL, 0, 0); + } + else + { + ret = M2M_ERR_FAIL; + } + } + return ret; +} + +/*! +@fn int8_t m2m_ota_abort(void) +@brief Request OTA Abort. +@return The function returns @ref M2M_SUCCESS for success and a negative value otherwise. +*/ +int8_t m2m_ota_abort(void) +{ + int8_t ret = M2M_SUCCESS; + ret = hif_send(M2M_REQ_GROUP_OTA, M2M_OTA_REQ_ABORT, NULL, 0, NULL, 0, 0); + return ret; +} + +/*! +@fn int8_t m2m_ota_switch_firmware(void) +@brief Switch to the upgraded Firmware. +@return The function returns @ref M2M_SUCCESS for success and a negative value otherwise. +*/ +int8_t m2m_ota_switch_firmware(void) +{ + int8_t ret = M2M_SUCCESS; + tstrM2mRev strRev; + + ret = m2m_ota_get_firmware_version(&strRev); + if(ret == M2M_SUCCESS) + { + if(M2M_GET_HIF_BLOCK(strRev.u16FirmwareHifInfo) == M2M_HIF_BLOCK_VALUE) + { + ret = hif_send(M2M_REQ_GROUP_OTA, M2M_OTA_REQ_SWITCH_FIRMWARE, NULL, 0, NULL, 0, 0); + } + else + { + ret = M2M_ERR_FAIL; + } + } + return ret; +} + +//DOM-IGNORE-END \ No newline at end of file diff --git a/ChaletLora.X/Source/winc3400_142/driver/source/m2m_periph.c b/ChaletLora.X/Source/winc3400_142/driver/source/m2m_periph.c new file mode 100644 index 0000000..82dbbaf --- /dev/null +++ b/ChaletLora.X/Source/winc3400_142/driver/source/m2m_periph.c @@ -0,0 +1,129 @@ +/******************************************************************************* + WINC3400 Peripherials Application Interface. + + File Name: + m2m_periph.c + + Summary: + WINC3400 Peripherals Application Interface + + Description: + WINC3400 Peripherals Application Interface + *******************************************************************************/ + +//DOM-IGNORE-BEGIN +/******************************************************************************* +* Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries. +* +* Subject to your compliance with these terms, you may use Microchip software +* and any derivatives exclusively with Microchip products. It is your +* responsibility to comply with third party license terms applicable to your +* use of third party software (including open source software) that may +* accompany Microchip software. +* +* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER +* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED +* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A +* PARTICULAR PURPOSE. +* +* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, +* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND +* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS +* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE +* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN +* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, +* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. +*******************************************************************************/ + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +INCLUDES +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + +#include "m2m_periph.h" +#include "nmasic.h" +#include "m2m_hif.h" + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +MACROS +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ +#define GPIO_OP_DIR 0 +#define GPIO_OP_SET 1 +#define GPIO_OP_GET 2 + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +DATA TYPES +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +STATIC FUNCTIONS +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ +static int8_t get_gpio_idx(uint8_t u8GpioNum) +{ + if(u8GpioNum >= M2M_PERIPH_GPIO_MAX) return -1; + if(u8GpioNum == M2M_PERIPH_GPIO15) { + return 15; + } else if(u8GpioNum == M2M_PERIPH_GPIO16) { + return 16; + } else if(u8GpioNum == M2M_PERIPH_GPIO18) { + return 18; + } else { + return -2; + } +} +/* + * GPIO read/write skeleton with wakeup/sleep capability. + */ +static int8_t gpio_ioctl(uint8_t op, uint8_t u8GpioNum, uint8_t u8InVal, uint8_t *pu8OutVal) +{ + int8_t ret, gpio; + + ret = hif_chip_wake(); + if(ret != M2M_SUCCESS) goto _EXIT; + + gpio = get_gpio_idx(u8GpioNum); + if(gpio < 0) goto _EXIT1; + + if(op == GPIO_OP_DIR) { + ret = set_gpio_dir((uint8_t)gpio, u8InVal); + } else if(op == GPIO_OP_SET) { + ret = set_gpio_val((uint8_t)gpio, u8InVal); + } else if(op == GPIO_OP_GET) { + ret = get_gpio_val((uint8_t)gpio, pu8OutVal); + } + if(ret != M2M_SUCCESS) goto _EXIT1; + +_EXIT1: + ret = hif_chip_sleep(); +_EXIT: + return ret; +} + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +FUNCTION IMPLEMENTATION +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ +int8_t m2m_periph_init(tstrPerphInitParam *param) +{ + return M2M_SUCCESS; +} + +int8_t m2m_periph_gpio_set_dir(uint8_t u8GpioNum, uint8_t u8GpioDir) +{ + return gpio_ioctl(GPIO_OP_DIR, u8GpioNum, u8GpioDir, NULL); +} + +int8_t m2m_periph_gpio_set_val(uint8_t u8GpioNum, uint8_t u8GpioVal) +{ + return gpio_ioctl(GPIO_OP_SET, u8GpioNum, u8GpioVal, NULL); +} + +int8_t m2m_periph_gpio_get_val(uint8_t u8GpioNum, uint8_t *pu8GpioVal) +{ + return gpio_ioctl(GPIO_OP_GET, u8GpioNum, 0, pu8GpioVal); +} + +int8_t m2m_periph_pullup_ctrl(uint32_t pinmask, uint8_t enable) +{ + return pullup_ctrl(pinmask, enable); +} + +//DOM-IGNORE-END diff --git a/ChaletLora.X/Source/winc3400_142/driver/source/m2m_ssl.c b/ChaletLora.X/Source/winc3400_142/driver/source/m2m_ssl.c new file mode 100644 index 0000000..f4a5f44 --- /dev/null +++ b/ChaletLora.X/Source/winc3400_142/driver/source/m2m_ssl.c @@ -0,0 +1,495 @@ +/******************************************************************************* + File Name: + m2m_ssl.c + + Summary: + WINC SSL Interface. + + Description: + WINC SSL Interface. + *******************************************************************************/ + +//DOM-IGNORE-BEGIN +/******************************************************************************* +* Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries. +* +* Subject to your compliance with these terms, you may use Microchip software +* and any derivatives exclusively with Microchip products. It is your +* responsibility to comply with third party license terms applicable to your +* use of third party software (including open source software) that may +* accompany Microchip software. +* +* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER +* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED +* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A +* PARTICULAR PURPOSE. +* +* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, +* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND +* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS +* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE +* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN +* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, +* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. +*******************************************************************************/ + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +INCLUDES +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + +#include "m2m_ssl.h" +#include "m2m_hif.h" +#include "nmasic.h" + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +MACROS +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + +#define min(a,b) (((a) < (b)) ? (a) : (b)) + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +DATA TYPES +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ +static tpfAppSSLCb gpfAppSSLCb = NULL; +static uint32_t gu32HIFAddr = 0; +static tenuTlsFlashStatus genuStatus = TLS_FLASH_ERR_UNKNOWN; + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +FUNCTION PROTOTYPES +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + +/*! +@fn void m2m_ssl_cb(uint8_t u8OpCode, uint16_t u16DataSize, uint32_t u32Addr) +@brief Internal SSL callback function. +@param [in] u8OpCode + HIF Opcode type. +@param [in] u16DataSize + HIF data length. +@param [in] u32Addr + HIF address. +*/ +static void m2m_ssl_cb(uint8_t u8OpCode, uint16_t u16DataSize, uint32_t u32Addr) +{ + int8_t s8tmp = M2M_SUCCESS; + switch(u8OpCode) + { + case M2M_SSL_REQ_ECC: + { + tstrEccReqInfo strEccREQ; + s8tmp = hif_receive(u32Addr, (uint8_t *)&strEccREQ, sizeof(tstrEccReqInfo), 0); + if(s8tmp == M2M_SUCCESS) + { + if(gpfAppSSLCb) + { + gu32HIFAddr = u32Addr + sizeof(tstrEccReqInfo); + gpfAppSSLCb(M2M_SSL_REQ_ECC, &strEccREQ); + } + } + } + break; + case M2M_SSL_RESP_SET_CS_LIST: + { + tstrSslSetActiveCsList strCsList; + s8tmp = hif_receive(u32Addr, (uint8_t *)&strCsList, sizeof(tstrSslSetActiveCsList), 0); + if(s8tmp == M2M_SUCCESS) + { + if(gpfAppSSLCb) + gpfAppSSLCb(M2M_SSL_RESP_SET_CS_LIST, &strCsList); + } + } + break; + case M2M_SSL_RESP_WRITE_OWN_CERTS: + { + tstrTlsSrvChunkHdr strTlsSrvChunkRsp; + uint8_t bCallApp = 1; + + s8tmp = hif_receive(u32Addr, (uint8_t *)&strTlsSrvChunkRsp, sizeof(tstrTlsSrvChunkHdr), 0); + if(s8tmp == M2M_SUCCESS) + { + uint16_t offset = strTlsSrvChunkRsp.u16Offset32; + uint16_t chunk_size = strTlsSrvChunkRsp.u16Size32; + uint16_t total_size = strTlsSrvChunkRsp.u16TotalSize32; + tenuTlsFlashStatus status = (tenuTlsFlashStatus)(strTlsSrvChunkRsp.u16Sig); + + /* If first chunk, reset status. */ + if(offset == 0) + genuStatus = TLS_FLASH_OK_NO_CHANGE; + /* Only send status to app when processing last chunk. */ + if(offset + chunk_size != total_size) + bCallApp = 0; + + switch(status) + { + case TLS_FLASH_OK: + // Good flash write. Update status if no errors yet. + if(genuStatus == TLS_FLASH_OK_NO_CHANGE) + genuStatus = status; + break; + case TLS_FLASH_OK_NO_CHANGE: + // No change, don't update status. + break; + case TLS_FLASH_ERR_CORRUPT: + // Corrupt. Always update status. + genuStatus = status; + break; + case TLS_FLASH_ERR_NO_CHANGE: + // Failed flash write. Update status if no more serious error. + if((genuStatus != TLS_FLASH_ERR_CORRUPT) && (genuStatus != TLS_FLASH_ERR_UNKNOWN)) + genuStatus = status; + break; + default: + // Don't expect any other case. Ensure we don't mask a previous corrupt error. + if(genuStatus != TLS_FLASH_ERR_CORRUPT) + genuStatus = TLS_FLASH_ERR_UNKNOWN; + break; + } + } + if(bCallApp && gpfAppSSLCb) + gpfAppSSLCb(M2M_SSL_RESP_WRITE_OWN_CERTS, &genuStatus); + } + break; + } + if(s8tmp != M2M_SUCCESS) + { + M2M_ERR("Error receiving SSL from the HIF\n"); + } +} + +/*! +@fn int8_t m2m_ssl_init(tpfAppSSLCb pfAppSSLCb) +@brief Initializes the SSL layer. +@param [in] pfAppSslCb + Application SSL callback function. +@return The function returns @ref M2M_SUCCESS for success and a negative value otherwise. +*/ +int8_t m2m_ssl_init(tpfAppSSLCb pfAppSSLCb) +{ + int8_t s8Ret = M2M_SUCCESS; + gpfAppSSLCb = pfAppSSLCb; + gu32HIFAddr = 0; + genuStatus = TLS_FLASH_ERR_UNKNOWN; + s8Ret = hif_register_cb(M2M_REQ_GROUP_SSL, m2m_ssl_cb); + if(s8Ret != M2M_SUCCESS) + { + M2M_ERR("hif_register_cb() failed with ret=%d", s8Ret); + } + return s8Ret; +} + +/*! +@fn int8_t m2m_ssl_handshake_rsp(tstrEccReqInfo* strECCResp, uint8_t* pu8RspDataBuff, uint16_t u16RspDataSz) +@brief Sends ECC responses to the WINC. +@param[in] strECCResp + ECC Response struct. +@param[in] pu8RspDataBuff + Pointer of the response data to be sent. +@param[in] u16RspDataSz + Response data size. +@return The function returns @ref M2M_SUCCESS for success and a negative value otherwise. +*/ +int8_t m2m_ssl_handshake_rsp(tstrEccReqInfo *strECCResp, uint8_t *pu8RspDataBuff, uint16_t u16RspDataSz) +{ + int8_t s8Ret = M2M_SUCCESS; + + s8Ret = hif_send(M2M_REQ_GROUP_SSL, (M2M_SSL_RESP_ECC | M2M_REQ_DATA_PKT), (uint8_t *)strECCResp, sizeof(tstrEccReqInfo), pu8RspDataBuff, u16RspDataSz, sizeof(tstrEccReqInfo)); + + return s8Ret; +} + +/*! +@fn int8_t m2m_ssl_send_certs_to_winc(uint8_t *pu8Buffer, uint32_t u32BufferSz) +@brief Sends certificates to the WINC +@param[in] pu8Buffer + Pointer to the certificates. +@param[in] u32BufferSz + Size of the certificates. +@return The function returns @ref M2M_SUCCESS for success and a negative value otherwise. +*/ +int8_t m2m_ssl_send_certs_to_winc(uint8_t *pu8Buffer, uint32_t u32BufferSz) +{ + int8_t s8Ret = M2M_SUCCESS; +#define TXLIMIT (256 * 6) + + if(u32BufferSz <= TXLIMIT) + { + // set chunk header for one chunk + tstrTlsSrvChunkHdr *pchkhdr = (tstrTlsSrvChunkHdr *)pu8Buffer; + pchkhdr->u16Sig = TLS_CERTS_CHUNKED_SIG_VALUE; + pchkhdr->u16TotalSize32 = (u32BufferSz + 3) >> 2; + pchkhdr->u16Offset32 = 0; + pchkhdr->u16Size32 = (u32BufferSz + 3) >> 2; + s8Ret = hif_send(M2M_REQ_GROUP_SSL, (M2M_SSL_REQ_WRITE_OWN_CERTS | M2M_REQ_DATA_PKT), NULL, 0, pu8Buffer, u32BufferSz, 0); + M2M_INFO("Transferred %" PRIu32 " bytes of cert data NON-CHUNKED\r\n", u32BufferSz); + } + else + { + // chunk it + // We are sneaking in a header - tstrTlsSrvChunkHdr +#define CHUNKHDRSZ (sizeof(tstrTlsSrvChunkHdr)) +#define CHUNKSZ (TXLIMIT - 256) // divisible by 4 + uint8_t saveblob[CHUNKHDRSZ]; + uint32_t ofs = 0; + uint32_t thischunksz = 0; + + // first is special - over writing our header + memcpy(saveblob, &pu8Buffer[ofs], CHUNKHDRSZ); + thischunksz = min(CHUNKSZ, u32BufferSz-ofs); // no need to round up to quad words this time + + tstrTlsSrvChunkHdr *pchkhdr = (tstrTlsSrvChunkHdr *)&pu8Buffer[ofs]; + pchkhdr->u16Sig = TLS_CERTS_CHUNKED_SIG_VALUE; + pchkhdr->u16TotalSize32 = ((u32BufferSz + 3) >> 2); + pchkhdr->u16Offset32 = ((ofs + 3) >> 2); + pchkhdr->u16Size32 = ((thischunksz + 3) >> 2); + s8Ret = hif_send(M2M_REQ_GROUP_SSL, (M2M_SSL_REQ_WRITE_OWN_CERTS | M2M_REQ_DATA_PKT), NULL, 0, &pu8Buffer[ofs], thischunksz, 0); + M2M_INFO("Transferred %" PRIu32 " bytes of cert data CHUNKED to offset %" PRIu32 " total %" PRIu32 "\r\n", thischunksz, ofs, u32BufferSz); + memcpy(&pu8Buffer[ofs], saveblob, CHUNKHDRSZ); + ofs += thischunksz; + + while(ofs < u32BufferSz) + { + // Subsequent chunks write header before and send a little more + memcpy(saveblob, &pu8Buffer[ofs-CHUNKHDRSZ], CHUNKHDRSZ); + thischunksz = min(CHUNKSZ, u32BufferSz-ofs); + thischunksz = (thischunksz + 3) & 0xFFFFFFFC; // needs to round up to quad word length + pchkhdr = (tstrTlsSrvChunkHdr *)&pu8Buffer[ofs - CHUNKHDRSZ]; + pchkhdr->u16Sig = TLS_CERTS_CHUNKED_SIG_VALUE; + pchkhdr->u16TotalSize32 = ((u32BufferSz + 3) >> 2); + pchkhdr->u16Offset32 = ((ofs + 3) >> 2); + pchkhdr->u16Size32 = ((thischunksz + 3) >> 2); + s8Ret = hif_send(M2M_REQ_GROUP_SSL, (M2M_SSL_REQ_WRITE_OWN_CERTS | M2M_REQ_DATA_PKT), NULL, 0, &pu8Buffer[ofs - CHUNKHDRSZ], thischunksz + CHUNKHDRSZ, 0); + M2M_INFO("Transferred %" PRIu32 " bytes of cert data CHUNKED to offset %" PRIu32 " total %" PRIu32 "\n", thischunksz, ofs, u32BufferSz); + memcpy(&pu8Buffer[ofs - CHUNKHDRSZ], saveblob, CHUNKHDRSZ); + ofs += thischunksz; + } + } + + return s8Ret; +} + +/*! +@fn int8_t m2m_ssl_retrieve_next_for_verifying(tenuEcNamedCurve *penuCurve, uint8_t *pu8Value, uint16_t *pu16ValueSz, uint8_t *pu8Sig, uint16_t *pu16SigSz, tstrECPoint *pstrKey); +@brief Retrieve the next set of information from the WINC for ECDSA verification. +@param[out] penuCurve + The named curve. +@param[out] pu8Value + Value retrieved for verification. This is the digest of the message, truncated/prepended to the appropriate size. +@param[inout] pu16ValueSz + in: Size of value buffer provided by caller. + out: Size of value retrieved (provided for convenience; the value size is in fact determined by the curve). +@param[out] pu8Sig + Signature retrieved for verification. +@param[inout] pu16SigSz + in: Size of signature buffer provided by caller. + out: Size of signature retrieved (provided for convenience; the signature size is in fact determined by the curve). +@param[out] pstrKey + Public key retrieved for verification. +@return The function returns @ref M2M_SUCCESS for success and a negative value otherwise. + +@pre This function should only be called after the application has been notified that + verification information is ready via @ref ECC_REQ_SIGN_VERIFY. + +@warning If this function returns @ref M2M_ERR_FAIL, then any remaining verification info from + the WINC is lost. +*/ +int8_t m2m_ssl_retrieve_next_for_verifying(tenuEcNamedCurve *penuCurve, uint8_t *pu8Value, uint16_t *pu16ValueSz, uint8_t *pu8Sig, uint16_t *pu16SigSz, tstrECPoint *pstrKey) +{ + int8_t s8Ret = M2M_ERR_FAIL; + uint16_t u16HashSz, u16SigSz, u16KeySz; + + if(gu32HIFAddr == 0) return M2M_ERR_FAIL; + + if((NULL == penuCurve) || (NULL == pu8Value) || (NULL == pu16ValueSz) || (NULL == pu8Sig) || (NULL == pu16SigSz) || (NULL == pstrKey)) + { + s8Ret = M2M_ERR_INVALID_ARG; + goto __ERR; + } + + if(hif_receive(gu32HIFAddr, (uint8_t *)&u16KeySz, 2, 0) != M2M_SUCCESS) goto __ERR; + *penuCurve = _htons(u16KeySz); + gu32HIFAddr += 2; + + if(hif_receive(gu32HIFAddr, (uint8_t *)&u16KeySz, 2, 0) != M2M_SUCCESS) goto __ERR; + u16KeySz = _htons(u16KeySz); + if(u16KeySz > sizeof(pstrKey->X)) goto __ERR; + pstrKey->u16Size = u16KeySz; + gu32HIFAddr += 2; + + if(hif_receive(gu32HIFAddr, (uint8_t *)&u16HashSz, 2, 0) != M2M_SUCCESS) goto __ERR; + u16HashSz = _htons(u16HashSz); + if(u16HashSz > *pu16ValueSz) goto __ERR; + *pu16ValueSz = u16HashSz; + gu32HIFAddr += 2; + + if(hif_receive(gu32HIFAddr, (uint8_t *)&u16SigSz, 2, 0) != M2M_SUCCESS) goto __ERR; + u16SigSz = _htons(u16SigSz); + if(u16SigSz > *pu16SigSz) goto __ERR; + *pu16SigSz = u16SigSz; + gu32HIFAddr += 2; + + if(hif_receive(gu32HIFAddr, pstrKey->X, u16KeySz, 0) != M2M_SUCCESS) goto __ERR; + gu32HIFAddr += u16KeySz; + if(hif_receive(gu32HIFAddr, pstrKey->Y, u16KeySz, 0) != M2M_SUCCESS) goto __ERR; + gu32HIFAddr += u16KeySz; + + if(hif_receive(gu32HIFAddr, pu8Value, u16HashSz, 0) != M2M_SUCCESS) goto __ERR; + gu32HIFAddr += u16HashSz; + + if(hif_receive(gu32HIFAddr, pu8Sig, u16SigSz, 0) != M2M_SUCCESS) goto __ERR; + gu32HIFAddr += u16SigSz; + + return M2M_SUCCESS; + +__ERR: + hif_receive(0, NULL, 0, 1); + return s8Ret; +} + +/*! +@fn int8_t m2m_ssl_retrieve_cert(uint16_t *pu16Curve, uint8_t *pu8Value, uint8_t *pu8Sig, tstrECPoint *pstrKey); +@brief Retrieve the next set of information from the WINC for ECDSA verification. +@param[out] pu16Curve + The named curve, to be cast to type @ref tenuEcNamedCurve. +@param[out] pu8Value + Value retrieved for verification. This is the digest of the message, truncated/prepended to the appropriate size. + The size of the value is equal to the field size of the curve, hence is determined by pu16Curve. +@param[out] pu8Sig + Signature retrieved for verification. + The size of the signature is equal to twice the field size of the curve, hence is determined by pu16Curve. +@param[out] pstrKey + Public key retrieved for verification. +@return The function returns @ref M2M_SUCCESS for success and a negative value otherwise. + +@pre This function should only be called after the application has been notified that + verification information is ready via @ref ECC_REQ_SIGN_VERIFY. + +@warning If this function returns @ref M2M_ERR_FAIL, then any remaining verification info from + the WINC is lost. + +@warning This API has been deprecated and is kept for legacy purposes only. It is recommended + that @ref m2m_ssl_retrieve_next_for_verifying is used instead. +*/ +int8_t m2m_ssl_retrieve_cert(uint16_t *pu16Curve, uint8_t *pu8Value, uint8_t *pu8Sig, tstrECPoint *pstrKey) +{ + uint16_t u16ValueSz = 32, u16SigSz = 64; + + return m2m_ssl_retrieve_next_for_verifying((tenuEcNamedCurve *)pu16Curve, pu8Value, &u16ValueSz, pu8Sig, &u16SigSz, pstrKey); +} + +/*! +@fn int8_t m2m_ssl_retrieve_hash(uint8_t *pu8Value, uint16_t u16ValueSz) +@brief Retrieve the value from the WINC for ECDSA signing. +@param[out] pu8Value + Value retrieved for signing. This is the digest of the message, truncated/prepended to the appropriate size. +@param[in] u16ValueSz + Size of value to be retrieved. (The application should obtain this information, + along with the curve, from the associated @ref ECC_REQ_SIGN_GEN notification.) +@return The function returns @ref M2M_SUCCESS for success and a negative value otherwise. + +@pre This function should only be called after the application has been notified that + signing information is ready via @ref ECC_REQ_SIGN_GEN. + +@warning If this function returns @ref M2M_ERR_FAIL, then the value for signing is lost. +*/ +int8_t m2m_ssl_retrieve_hash(uint8_t *pu8Value, uint16_t u16ValueSz) +{ + int8_t s8Ret = M2M_ERR_FAIL; + + if(gu32HIFAddr == 0) return M2M_ERR_FAIL; + + if(NULL == pu8Value) + { + s8Ret = M2M_ERR_INVALID_ARG; + goto __ERR; + } + + if(hif_receive(gu32HIFAddr, pu8Value, u16ValueSz, 0) != M2M_SUCCESS) goto __ERR; + + return M2M_SUCCESS; + +__ERR: + hif_receive(0, NULL, 0, 1); + return s8Ret; +} + +/*! +@fn void m2m_ssl_stop_retrieving(void); +@brief Allow SSL driver to tidy up when the application chooses not to retrieve all available + information. + +@return None. + +@warning The application must call this function if it has been notified (via + @ref ECC_REQ_SIGN_GEN or @ref ECC_REQ_SIGN_VERIFY) that information is available for + retrieving from the WINC, but chooses not to retrieve it all. + The application must not call this function if it has retrieved all the available + information, or if a retrieve function returned @ref M2M_ERR_FAIL indicating that any + remaining information has been lost. + +@see m2m_ssl_retrieve_next_for_verifying\n + m2m_ssl_retrieve_cert\n + m2m_ssl_retrieve_hash +*/ +void m2m_ssl_stop_retrieving(void) +{ + hif_receive(0, NULL, 0, 1); +} + +/*! +@fn void m2m_ssl_stop_processing_certs(void); +@brief Allow SSL driver to tidy up in case application does not read all available certificates. +@return None. + +@warning This API has been deprecated and is kept for legacy purposes only. It is recommended + that @ref m2m_ssl_stop_retrieving is used instead. +*/ +void m2m_ssl_stop_processing_certs(void) +{ + m2m_ssl_stop_retrieving(); +} + +/*! +@fn void m2m_ssl_ecc_process_done(void); +@brief Allow SSL driver to tidy up after application has finished processing ECC message. + +@return None. + +@warning The application should call this function after receiving an SSL callback with message + type @ref M2M_SSL_REQ_ECC, after retrieving any related information, and before + calling @ref m2m_ssl_handshake_rsp. +*/ +void m2m_ssl_ecc_process_done(void) +{ + gu32HIFAddr = 0; +} + +/*! +@fn int8_t m2m_ssl_set_active_ciphersuites(uint32_t u32SslCsBMP) +@brief Sets the active ciphersuites. +@details Override the default Active SSL ciphers in the SSL module with a certain combination selected by the caller in the form of + a bitmap containing the required ciphers to be on. + There is no need to call this function if the application will not change the default ciphersuites. + +@param [in] u32SslCsBMP + Bitmap containing the desired ciphers to be enabled for the SSL module. The ciphersuites are defined in + @ref SSLCipherSuiteID. + The default ciphersuites are all ciphersuites supported by the firmware with the exception of ECC ciphersuites. + The caller can override the default with any desired combination. + If u32SslCsBMP does not contain any ciphersuites supported by firmware, then the current active list will not + change. + +@return + - @ref SOCK_ERR_NO_ERROR + - @ref SOCK_ERR_INVALID_ARG +*/ +int8_t m2m_ssl_set_active_ciphersuites(uint32_t u32SslCsBMP) +{ + int8_t s8Ret = M2M_SUCCESS; + tstrSslSetActiveCsList strCsList; + + strCsList.u32CsBMP = u32SslCsBMP; + s8Ret = hif_send(M2M_REQ_GROUP_SSL, M2M_SSL_REQ_SET_CS_LIST, (uint8_t *)&strCsList, sizeof(tstrSslSetActiveCsList), NULL, 0, 0); + + return s8Ret; +} + +//DOM-IGNORE-END \ No newline at end of file diff --git a/ChaletLora.X/Source/winc3400_142/driver/source/m2m_wifi.c b/ChaletLora.X/Source/winc3400_142/driver/source/m2m_wifi.c new file mode 100644 index 0000000..45d2b13 --- /dev/null +++ b/ChaletLora.X/Source/winc3400_142/driver/source/m2m_wifi.c @@ -0,0 +1,2061 @@ +/******************************************************************************* + File Name: + m2m_wifi.c + + Summary: + This module contains M2M Wi-Fi APIs implementation. + + Description: + This module contains M2M Wi-Fi APIs implementation. + *******************************************************************************/ + +//DOM-IGNORE-BEGIN +/******************************************************************************* +* Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries. +* +* Subject to your compliance with these terms, you may use Microchip software +* and any derivatives exclusively with Microchip products. It is your +* responsibility to comply with third party license terms applicable to your +* use of third party software (including open source software) that may +* accompany Microchip software. +* +* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER +* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED +* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A +* PARTICULAR PURPOSE. +* +* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, +* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND +* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS +* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE +* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN +* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, +* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. +*******************************************************************************/ + +#include "m2m_wifi.h" +#include "m2m_hif.h" +#include "nmasic.h" + +#define WIFI_1X_TLS_HS_FLAGS_PEER_AUTH NBIT1 +#define WIFI_1X_TLS_HS_FLAGS_PEER_CERTTIMECHECK NBIT2 +#define WIFI_1X_TLS_HS_FLAGS_REQUIRE_TIME NBIT3 +#define WIFI_1X_TLS_HS_FLAGS_SESSION_CACHING NBIT4 +#define WIFI_1X_TLS_HS_FLAGS_SPECIFY_ROOTCERT NBIT6 + +#define WIFI_1X_TLS_HS_FLAGS_DEFAULT ( \ + WIFI_1X_TLS_HS_FLAGS_PEER_AUTH \ + | WIFI_1X_TLS_HS_FLAGS_PEER_CERTTIMECHECK \ + | WIFI_1X_TLS_HS_FLAGS_SESSION_CACHING \ +) + +static volatile uint8_t gu8ChNum; +static tpfAppWifiCb gpfAppWifiCb = NULL; + +static uint32_t gu321xTlsHsFlags = WIFI_1X_TLS_HS_FLAGS_DEFAULT; +static uint8_t gau81xRootSha1[20] = {0}; + +static tpfAppEthCb gpfAppEthCb = NULL; +static uint8_t *gau8ethRcvBuf = NULL; +static uint16_t gu16ethRcvBufSize; + +/** +* @fn m2m_wifi_cb(uint8_t u8OpCode, uint16_t u16DataSize, uint32_t u32Addr, uint8_t grp) +* @brief WiFi call back function +* @param [in] u8OpCode +* HIF Opcode type. +* @param [in] u16DataSize +* HIF data length. +* @param [in] u32Addr +* HIF address. +* @param [in] grp +* HIF group type. +*/ +static void m2m_wifi_cb(uint8_t u8OpCode, uint16_t u16DataSize, uint32_t u32Addr) +{ + uint8_t rx_buf[8]; + if (u8OpCode == M2M_WIFI_RESP_CON_STATE_CHANGED) + { + tstrM2mWifiStateChanged strState; + if (hif_receive(u32Addr, (uint8_t*)&strState, sizeof(tstrM2mWifiStateChanged), 0) == M2M_SUCCESS) + { + if (gpfAppWifiCb) + gpfAppWifiCb(M2M_WIFI_RESP_CON_STATE_CHANGED, &strState); + } + } + else if (u8OpCode == M2M_WIFI_RESP_GET_SYS_TIME) + { + tstrSystemTime strSysTime; + if (hif_receive(u32Addr, (uint8_t*)&strSysTime, sizeof(tstrSystemTime), 0) == M2M_SUCCESS) + { + if (gpfAppWifiCb) + gpfAppWifiCb(M2M_WIFI_RESP_GET_SYS_TIME, &strSysTime); + } + } + else if (u8OpCode == M2M_WIFI_RESP_CONN_INFO) + { + tstrM2MConnInfo strConnInfo; + if (hif_receive(u32Addr, (uint8_t*)&strConnInfo, sizeof(tstrM2MConnInfo), 1) == M2M_SUCCESS) + { + if (gpfAppWifiCb) + gpfAppWifiCb(M2M_WIFI_RESP_CONN_INFO, &strConnInfo); + } + } + else if (u8OpCode == M2M_WIFI_RESP_MEMORY_RECOVER) + { + } + else if (u8OpCode == M2M_WIFI_REQ_DHCP_CONF) + { + tstrM2MIPConfig strIpConfig; + if (hif_receive(u32Addr, (uint8_t*)&strIpConfig, sizeof(tstrM2MIPConfig), 0) == M2M_SUCCESS) + { + if (gpfAppWifiCb) + gpfAppWifiCb(M2M_WIFI_REQ_DHCP_CONF, (uint8_t*)&strIpConfig); + } + } + else if (u8OpCode == M2M_WIFI_REQ_WPS) + { + tstrM2MWPSInfo strWps; + memset((uint8_t*)&strWps, 0, sizeof(tstrM2MWPSInfo)); + if (hif_receive(u32Addr, (uint8_t*)&strWps, sizeof(tstrM2MWPSInfo), 0) == M2M_SUCCESS) + { + if (gpfAppWifiCb) + gpfAppWifiCb(M2M_WIFI_REQ_WPS, &strWps); + } + } + else if (u8OpCode == M2M_WIFI_RESP_IP_CONFLICT) + { + uint32_t u32ConflictedIP; + if (hif_receive(u32Addr, (uint8_t*)&u32ConflictedIP, sizeof(u32ConflictedIP), 0) == M2M_SUCCESS) + { + M2M_INFO("Conflicted IP \" %u.%u.%u.%u \"\r\n", + BYTE_0(u32ConflictedIP), BYTE_1(u32ConflictedIP), BYTE_2(u32ConflictedIP), BYTE_3(u32ConflictedIP)); + if (gpfAppWifiCb) + gpfAppWifiCb(M2M_WIFI_RESP_IP_CONFLICT, NULL); + } + } + else if (u8OpCode == M2M_WIFI_RESP_SCAN_DONE) + { + tstrM2mScanDone strState; + if (hif_receive(u32Addr, (uint8_t*)&strState, sizeof(tstrM2mScanDone), 0) == M2M_SUCCESS) + { + gu8ChNum = strState.u8NumofCh; + if (gpfAppWifiCb) + gpfAppWifiCb(M2M_WIFI_RESP_SCAN_DONE, &strState); + } + } + else if (u8OpCode == M2M_WIFI_RESP_SCAN_RESULT) + { + tstrM2mWifiscanResult strScanResult; + if (hif_receive(u32Addr, (uint8_t*)&strScanResult, sizeof(tstrM2mWifiscanResult), 0) == M2M_SUCCESS) + { + if (gpfAppWifiCb) + gpfAppWifiCb(M2M_WIFI_RESP_SCAN_RESULT, &strScanResult); + } + } + else if (u8OpCode == M2M_WIFI_RESP_CURRENT_RSSI) + { + if (hif_receive(u32Addr, rx_buf, 4, 0) == M2M_SUCCESS) + { + if (gpfAppWifiCb) + gpfAppWifiCb(M2M_WIFI_RESP_CURRENT_RSSI, rx_buf); + } + } + else if (u8OpCode == M2M_WIFI_RESP_CLIENT_INFO) + { + if (hif_receive(u32Addr, rx_buf, 4, 0) == M2M_SUCCESS) + { + if (gpfAppWifiCb) + gpfAppWifiCb(M2M_WIFI_RESP_CLIENT_INFO, rx_buf); + } + } + else if (u8OpCode == M2M_WIFI_RESP_PROVISION_INFO) + { + tstrM2MProvisionInfo strProvInfo; + if (hif_receive(u32Addr, (uint8_t*)&strProvInfo, sizeof(tstrM2MProvisionInfo), 1) == M2M_SUCCESS) + { + if (gpfAppWifiCb) + gpfAppWifiCb(M2M_WIFI_RESP_PROVISION_INFO, &strProvInfo); + } + } + else if (u8OpCode == M2M_WIFI_RESP_DEFAULT_CONNECT) + { + tstrM2MDefaultConnResp strResp; + if (hif_receive(u32Addr, (uint8_t*)&strResp, sizeof(tstrM2MDefaultConnResp), 1) == M2M_SUCCESS) + { + if (gpfAppWifiCb) + gpfAppWifiCb(M2M_WIFI_RESP_DEFAULT_CONNECT, &strResp); + } + } + else if (u8OpCode == M2M_WIFI_REQRSP_DELETE_APID) + { + tstrM2MGenericResp strResp; + if (hif_receive(u32Addr, (uint8_t*)&strResp, sizeof(tstrM2MGenericResp), 0) == M2M_SUCCESS) + { + if (gpfAppWifiCb) + gpfAppWifiCb(M2M_WIFI_REQRSP_DELETE_APID, &strResp); + } + } + else if (u8OpCode == M2M_WIFI_RESP_BLE_API_RECV) + { + //Read the length + if (hif_receive(u32Addr, rx_buf, 2, 0) == M2M_SUCCESS) + { + uint16_t u16BleMsgLen = (rx_buf[1] << 8) + rx_buf[0]; + tstrM2mBleApiMsg bleRx;// = (tstrM2mBleApiMsg*)OSAL_Malloc(u16BleMsgLen + sizeof(tstrM2mBleApiMsg)); + + // if (bleRx == NULL) + // M2M_ERR("No Mem for BLE msg\r\n"); + // else + { + bleRx.u16Len = u16BleMsgLen; + + //Read the rest of the message + if (hif_receive(u32Addr+2, bleRx.data, bleRx.u16Len, 1)== M2M_SUCCESS) + { + if (gpfAppWifiCb) + gpfAppWifiCb(M2M_WIFI_RESP_BLE_API_RECV, &bleRx); + } + // OSAL_Free(bleRx); + } + } + } + else if (u8OpCode == M2M_WIFI_RESP_GET_PRNG) + { + tstrPrng strPrng; + if (hif_receive(u32Addr, (uint8_t*)&strPrng, sizeof(tstrPrng), 0) == M2M_SUCCESS) + { + if (hif_receive(u32Addr + sizeof(tstrPrng), strPrng.pu8RngBuff, strPrng.u16PrngSize, 1) == M2M_SUCCESS) + { + if (gpfAppWifiCb) { + gpfAppWifiCb(M2M_WIFI_RESP_GET_PRNG, &strPrng); + } + } + } + } + else if (u8OpCode == M2M_WIFI_RESP_SET_GAIN_TABLE) + { + tstrM2MGainTableRsp strGainRsp; + if (hif_receive(u32Addr, (uint8_t*)&strGainRsp, sizeof(tstrM2MGainTableRsp), 0) == M2M_SUCCESS) + { + if (gpfAppWifiCb) + gpfAppWifiCb(M2M_WIFI_RESP_SET_GAIN_TABLE, &strGainRsp); + } + } + else if (u8OpCode == M2M_WIFI_RESP_ETHERNET_RX_PACKET) + { + tstrM2mIpRsvdPkt strM2mRsvd; + if (hif_receive(u32Addr, (uint8_t*)&strM2mRsvd, sizeof(tstrM2mIpRsvdPkt), 0) == M2M_SUCCESS) + { + tstrM2mIpCtrlBuf strM2mIpCtrlBuf; + uint16_t u16Offset = strM2mRsvd.u16PktOffset; + + strM2mIpCtrlBuf.u16RemainingDataSize = strM2mRsvd.u16PktSz; + if ((gpfAppEthCb) &&(gau8ethRcvBuf)&& (gu16ethRcvBufSize > 0)) + { + while (strM2mIpCtrlBuf.u16RemainingDataSize > 0) + { + if (strM2mIpCtrlBuf.u16RemainingDataSize > gu16ethRcvBufSize) + { + strM2mIpCtrlBuf.u16DataSize = gu16ethRcvBufSize ; + } + else + { + strM2mIpCtrlBuf.u16DataSize = strM2mIpCtrlBuf.u16RemainingDataSize; + } + + if (hif_receive(u32Addr+u16Offset, gau8ethRcvBuf, strM2mIpCtrlBuf.u16DataSize, 0) == M2M_SUCCESS) + { + strM2mIpCtrlBuf.u16RemainingDataSize -= strM2mIpCtrlBuf.u16DataSize; + u16Offset += strM2mIpCtrlBuf.u16DataSize; + gpfAppEthCb(M2M_WIFI_RESP_ETHERNET_RX_PACKET, gau8ethRcvBuf, &(strM2mIpCtrlBuf)); + } + else + { + break; + } + } + } + } + } + else + { + M2M_ERR("REQ Not defined %d\r\n", u8OpCode); + } +} + +int8_t m2m_wifi_download_mode(void) +{ + int8_t ret = M2M_SUCCESS; + + /* Apply device specific initialization. */ + ret = nm_drv_init_download_mode(); + if (ret != M2M_SUCCESS) goto _EXIT0; + + enable_interrupts(); + +_EXIT0: + return ret; +} + +static int8_t m2m_validate_ap_parameters(const tstrM2MAPModeConfig *pstrM2MAPModeConfig) +{ + int8_t s8Ret = M2M_SUCCESS; + /* Check for incoming pointer */ + if (pstrM2MAPModeConfig == NULL) + { + M2M_ERR("INVALID POINTER\r\n"); + s8Ret = M2M_ERR_FAIL; + goto ERR1; + } + /* Check for SSID */ + if ((strlen((const char*)pstrM2MAPModeConfig->strApConfig.au8SSID) <= 0) || (strlen((const char*)pstrM2MAPModeConfig->strApConfig.au8SSID) >= M2M_MAX_SSID_LEN)) + { + M2M_ERR("INVALID SSID\r\n"); + s8Ret = M2M_ERR_FAIL; + goto ERR1; + } + /* Check for Channel */ + if (pstrM2MAPModeConfig->strApConfig.u8ListenChannel > M2M_WIFI_CH_14 || pstrM2MAPModeConfig->strApConfig.u8ListenChannel < M2M_WIFI_CH_1) + { + M2M_ERR("INVALID CH\r\n"); + s8Ret = M2M_ERR_FAIL; + goto ERR1; + } + /* Check for DHCP Server IP address */ + if (!(pstrM2MAPModeConfig->strApConfig.au8DHCPServerIP[0] || pstrM2MAPModeConfig->strApConfig.au8DHCPServerIP[1])) + { + if (!(pstrM2MAPModeConfig->strApConfig.au8DHCPServerIP[2])) + { + M2M_ERR("INVALID DHCP SERVER IP\r\n"); + s8Ret = M2M_ERR_FAIL; + goto ERR1; + } + } + /* Check for Security */ + if (pstrM2MAPModeConfig->strApConfig.u8SecType == M2M_WIFI_SEC_OPEN) + { + goto ERR1; + } + else if (pstrM2MAPModeConfig->strApConfig.u8SecType == M2M_WIFI_SEC_WEP) + { + /* Check for WEP Key index */ + if ((pstrM2MAPModeConfig->strApConfig.u8KeyIndx == 0) || (pstrM2MAPModeConfig->strApConfig.u8KeyIndx > WEP_KEY_MAX_INDEX)) + { + M2M_ERR("INVALID KEY INDEX\r\n"); + s8Ret = M2M_ERR_FAIL; + goto ERR1; + } + /* Check for WEP Key size */ + if ((pstrM2MAPModeConfig->strApConfig.u8KeySz != WEP_40_KEY_STRING_SIZE) && + (pstrM2MAPModeConfig->strApConfig.u8KeySz != WEP_104_KEY_STRING_SIZE)) + { + M2M_ERR("INVALID KEY SIZE\r\n"); + s8Ret = M2M_ERR_FAIL; + goto ERR1; + } + /* Check for WEP Key */ + if ((pstrM2MAPModeConfig->strApConfig.au8WepKey == NULL) || (strlen((const char*)pstrM2MAPModeConfig->strApConfig.au8WepKey) <= 0) || (strlen((const char*)pstrM2MAPModeConfig->strApConfig.au8WepKey) > WEP_104_KEY_STRING_SIZE)) + { + M2M_ERR("INVALID WEP KEY\r\n"); + s8Ret = M2M_ERR_FAIL; + goto ERR1; + } + } + else + { + M2M_ERR("INVALID AUTHENTICATION MODE\r\n"); + s8Ret = M2M_ERR_FAIL; + goto ERR1; + } + +ERR1: + return s8Ret; +} + +static int8_t m2m_validate_scan_options(tstrM2MScanOption *ptstrM2MScanOption) +{ + int8_t s8Ret = M2M_SUCCESS; + /* Check for incoming pointer */ + if (ptstrM2MScanOption == NULL) + { + M2M_ERR("INVALID POINTER\r\n"); + s8Ret = M2M_ERR_FAIL; + } + else + { + /* Check for valid No of slots */ + if (ptstrM2MScanOption->u8NumOfSlot == 0) + { + M2M_ERR("INVALID No of scan slots!\r\n"); + s8Ret = M2M_ERR_FAIL; + } + /* Check for valid time of slots */ + if ((ptstrM2MScanOption->u8SlotTime < 10) || (ptstrM2MScanOption->u8SlotTime > 250)) + { + M2M_ERR("INVALID scan slot time!\r\n"); + s8Ret = M2M_ERR_FAIL; + } + /* Check for valid No of probe requests per slot */ + if ((ptstrM2MScanOption->u8ProbesPerSlot == 0) || (ptstrM2MScanOption->u8ProbesPerSlot > M2M_SCAN_DEFAULT_NUM_PROBE)) + { + M2M_ERR("INVALID No of probe requests per scan slot\r\n"); + s8Ret = M2M_ERR_FAIL; + } + /* Check for valid RSSI threshold */ + if (ptstrM2MScanOption->s8RssiThresh >= 0) + { + M2M_ERR("INVALID RSSI threshold %d\r\n", ptstrM2MScanOption->s8RssiThresh); + s8Ret = M2M_ERR_FAIL; + } + } + return s8Ret; +} + +int8_t m2m_wifi_ble_set_gain_table(uint8_t table_idx) +{ + int8_t s8Ret = M2M_ERR_FAIL; + tstrM2MGainTable strGainTable = {0}; + + strGainTable.u8GainTable = table_idx; + + s8Ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_SET_GAIN_TABLE, (uint8_t*)&strGainTable, sizeof(tstrM2MGainTable), NULL, 0, 0); + return s8Ret; +} + +int8_t m2m_wifi_init_hold(void) +{ + int8_t ret = M2M_ERR_FAIL; + + /* Apply device specific initialization. */ + ret = nm_drv_init_hold(); + + return ret; +} + +int8_t m2m_wifi_init_start(tstrWifiInitParam *pWifiInitParam) +{ + tstrM2mRev strtmp; + int8_t ret = M2M_SUCCESS; + uint8_t u8WifiMode = M2M_WIFI_MODE_NORMAL; + + if (pWifiInitParam == NULL) { + ret = M2M_ERR_FAIL; + goto _EXIT0; + } + + gpfAppWifiCb = pWifiInitParam->pfAppWifiCb; + + gpfAppEthCb = pWifiInitParam->strEthInitParam.pfAppEthCb; + gau8ethRcvBuf = pWifiInitParam->strEthInitParam.au8ethRcvBuf; + gu16ethRcvBufSize = pWifiInitParam->strEthInitParam.u16ethRcvBufSize; + u8WifiMode = pWifiInitParam->strEthInitParam.u8EthernetEnable; + + if (pWifiInitParam->strEthInitParam.u8EthernetEnable) + u8WifiMode = M2M_WIFI_MODE_ETHERNET; + + + /* Initialize host interface module */ + /* Do this before bringing up the WINC as it can send HIF messages very quickly from bootup and we need to be ready */ + ret = hif_init(NULL); + if (ret != M2M_SUCCESS) goto _EXIT1; + + hif_register_cb(M2M_REQ_GROUP_WIFI, m2m_wifi_cb); + + /* Apply device specific initialization. */ + ret = nm_drv_init_start(&u8WifiMode); + if (ret != M2M_SUCCESS) goto _EXIT0; + + M2M_INFO("Curr driver ver: %u.%u.%u\r\n", M2M_DRIVER_VERSION_MAJOR_NO, M2M_DRIVER_VERSION_MINOR_NO, M2M_DRIVER_VERSION_PATCH_NO); + M2M_INFO("Curr driver HIF Level: (%u) %u.%u\r\n", M2M_HIF_BLOCK_VALUE, M2M_HIF_MAJOR_VALUE, M2M_HIF_MINOR_VALUE); + ret = m2m_wifi_get_firmware_version(&strtmp); + m2m_ota_get_firmware_version(&strtmp); + + if (ret == M2M_SUCCESS) + { + ret = hif_enable_access(); + if (ret == M2M_SUCCESS) + { + m2m_wifi_ble_set_gain_table(pWifiInitParam->GainTableIndex); + } + } + goto _EXIT0; + +_EXIT1: + nm_drv_deinit(NULL); + +_EXIT0: + return ret; +} + +int8_t m2m_wifi_init(tstrWifiInitParam *pWifiInitParam) +{ + int8_t ret = M2M_SUCCESS; + + ret = m2m_wifi_init_hold(); + if (ret == M2M_SUCCESS) + { + ret = m2m_wifi_init_start(pWifiInitParam); + } + return ret; +} + +int8_t m2m_wifi_deinit(void *arg) +{ + hif_deinit(NULL); + nm_drv_deinit(NULL); + + return M2M_SUCCESS; +} + +int8_t m2m_wifi_reinit_hold(void) +{ + m2m_wifi_deinit(NULL); + return m2m_wifi_init_hold(); +} + +int8_t m2m_wifi_reinit_start(tstrWifiInitParam *pWifiInitParam) +{ + return m2m_wifi_init_start(pWifiInitParam); +} + +int8_t m2m_wifi_reinit(tstrWifiInitParam *pWifiInitParam) +{ + int8_t ret = M2M_ERR_FAIL; + ret = m2m_wifi_reinit_hold(); + if (ret == M2M_SUCCESS) { + ret = m2m_wifi_reinit_start(pWifiInitParam); + } + return ret; +} + +uint8_t m2m_wifi_get_state(void) +{ + switch(nm_get_state()) + { + case NM_STATE_DEINIT: + return WIFI_STATE_DEINIT; + case NM_STATE_INIT: + return WIFI_STATE_INIT; + case NM_STATE_START: + return WIFI_STATE_START; + } + return WIFI_STATE_DEINIT; +} + +int8_t m2m_wifi_handle_events(void) +{ + if(WIFI_STATE_START == m2m_wifi_get_state()) + return hif_handle_isr(); + + return M2M_SUCCESS; +} + +int8_t m2m_wifi_delete_sc(char *pcSsid, uint8_t u8SsidLen) +{ + tstrM2mWifiApId strApId; + memset((uint8_t*)&strApId, 0, sizeof(strApId)); + strApId.au8SSID[0] = 0xFF; // Special value used to cause fw to delete all entries. + return hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQRSP_DELETE_APID, (uint8_t*)&strApId, sizeof(tstrM2mWifiApId), NULL, 0, 0); +} + +int8_t m2m_wifi_default_connect(void) +{ + return hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_DEFAULT_CONNECT, NULL, 0, NULL, 0, 0); +} + +/*************************************************************************************************/ +/* WIFI CONNECT INTERNAL FUNCTIONS */ +/*************************************************************************************************/ +static int8_t m2m_wifi_connect_prepare_msg( + tenuCredStoreOption enuCredStoreOption, + tenuM2mSecType enuAuthType, + uint16_t u16AuthSize, + tstrNetworkId *pstrNetworkId, + tstrM2mWifiConnHdr *pstrWifiConn +) +{ + int8_t ret = M2M_ERR_FAIL; + uint16_t u16CredSize = sizeof(tstrM2mConnCredCmn) + u16AuthSize; + /* Check application params. */ + if ( + (pstrNetworkId == NULL) + || (pstrNetworkId->pu8Ssid == NULL) + || (pstrNetworkId->u8SsidLen >= M2M_MAX_SSID_LEN) + ) + goto INVALID_ARG; + + if (pstrWifiConn != NULL) + { + tstrM2mConnCredHdr *pstrHdr = &pstrWifiConn->strConnCredHdr; + tstrM2mConnCredCmn *pstrCmn = &pstrWifiConn->strConnCredCmn; + + memset((uint8_t*)pstrWifiConn, 0, sizeof(tstrM2mWifiConnHdr)); + + pstrHdr->u16CredSize = u16CredSize; + switch(enuCredStoreOption) + { + case WIFI_CRED_SAVE_ENCRYPTED: + pstrHdr->u8CredStoreFlags |= M2M_CRED_ENCRYPT_FLAG; + // intentional fall through... + case WIFI_CRED_SAVE_UNENCRYPTED: + pstrHdr->u8CredStoreFlags |= M2M_CRED_STORE_FLAG; + // intentional fall through... + case WIFI_CRED_DONTSAVE: + break; + default: + goto INVALID_ARG; + } + + if (pstrNetworkId->enuChannel == M2M_WIFI_CH_ALL) + pstrHdr->u8Channel = (uint8_t)(pstrNetworkId->enuChannel); + else if ((pstrNetworkId->enuChannel <= M2M_WIFI_CH_14) && (pstrNetworkId->enuChannel >= M2M_WIFI_CH_1)) + pstrHdr->u8Channel = (uint8_t)(pstrNetworkId->enuChannel) - 1; + else + goto INVALID_ARG; + + if ((enuAuthType == M2M_WIFI_SEC_INVALID) || (enuAuthType >= M2M_WIFI_NUM_AUTH_TYPES)) + goto INVALID_ARG; + pstrCmn->u8AuthType = (uint8_t)enuAuthType; + + pstrCmn->u8SsidLen = pstrNetworkId->u8SsidLen; + memcpy(pstrCmn->au8Ssid, pstrNetworkId->pu8Ssid, pstrNetworkId->u8SsidLen); + if (pstrNetworkId->pu8Bssid != NULL) + { + pstrCmn->u8Options = M2M_WIFI_CONN_BSSID_FLAG; + memcpy(pstrCmn->au8Bssid, pstrNetworkId->pu8Bssid, M2M_MAC_ADDRES_LEN); + } + /* Everything is ok, set return value. */ + ret = M2M_SUCCESS; + } + return ret; +INVALID_ARG: + return M2M_ERR_INVALID_ARG; +} + +static int8_t legacy_connect_prepare_msg(tstrM2mWifiConnHdr *pstrConnHdr, tstrM2mWifiConnectLegacy_1_2 *pstrConnect) +{ + int8_t s8Ret = M2M_ERR_FAIL; + + if ( + !(pstrConnHdr->strConnCredHdr.u8CredStoreFlags & M2M_CRED_ENCRYPT_FLAG) + && !(pstrConnHdr->strConnCredCmn.u8Options & M2M_WIFI_CONN_BSSID_FLAG) + ) + { + memset((uint8_t*)pstrConnect, 0, sizeof(tstrM2mWifiConnectLegacy_1_2)); + memcpy(pstrConnect->au8SSID, pstrConnHdr->strConnCredCmn.au8Ssid, pstrConnHdr->strConnCredCmn.u8SsidLen); + pstrConnect->au8SSID[pstrConnHdr->strConnCredCmn.u8SsidLen] = 0; + pstrConnect->u16Ch = pstrConnHdr->strConnCredHdr.u8Channel; + pstrConnect->u8NoSaveCred = !(pstrConnHdr->strConnCredHdr.u8CredStoreFlags & M2M_CRED_STORE_FLAG); + pstrConnect->strSec.u8SecType = pstrConnHdr->strConnCredCmn.u8AuthType; + s8Ret = M2M_SUCCESS; + } + return s8Ret; +} + +/*************************************************************************************************/ +/* WIFI CONNECT APIS */ +/*************************************************************************************************/ +int8_t m2m_wifi_connect_open( + tenuCredStoreOption enuCredStoreOption, + tstrNetworkId *pstrNetworkId +) +{ + int8_t ret = M2M_ERR_INVALID_ARG; + tstrM2mWifiConnHdr strConnHdr; + + ret = m2m_wifi_connect_prepare_msg(enuCredStoreOption, M2M_WIFI_SEC_OPEN, 0, pstrNetworkId, &strConnHdr); + if (ret == M2M_SUCCESS) + { + ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_CONN, + (uint8_t*)&strConnHdr, sizeof(strConnHdr), + NULL, 0, 0); + if (ret != M2M_SUCCESS) + { + /* Might just be that we are talking to legacy firmware - try the legacy connect message instead. */ + tstrM2mWifiConnectLegacy_1_2 strConnectLegacy; + ret = legacy_connect_prepare_msg(&strConnHdr, &strConnectLegacy); + if (ret == M2M_SUCCESS) + { + ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_CONNECT, + (uint8_t*)&strConnectLegacy, sizeof(tstrM2mWifiConnectLegacy_1_2), + NULL, 0, 0); + } + } + } + return ret; +} + +int8_t m2m_wifi_connect_wep( + tenuCredStoreOption enuCredStoreOption, + tstrNetworkId *pstrNetworkId, + tstrAuthWep *pstrAuthWep +) +{ + int8_t ret = M2M_ERR_INVALID_ARG; + + if ( + (pstrAuthWep != NULL) && (pstrAuthWep->pu8WepKey != NULL) + && (pstrAuthWep->u8KeyIndx > 0) && (pstrAuthWep->u8KeyIndx <= WEP_KEY_MAX_INDEX) + && ((pstrAuthWep->u8KeySz == WEP_104_KEY_STRING_SIZE) || (pstrAuthWep->u8KeySz == WEP_40_KEY_STRING_SIZE)) + ) + { + tstrM2mWifiConnHdr strConnHdr; + + ret = m2m_wifi_connect_prepare_msg(enuCredStoreOption, + M2M_WIFI_SEC_WEP, + sizeof(tstrM2mWifiWep), + pstrNetworkId, + &strConnHdr); + + if (ret == M2M_SUCCESS) + { + tstrM2mWifiWep pstrWep;// = (tstrM2mWifiWep *)OSAL_Malloc(sizeof(tstrM2mWifiWep)); + // if (pstrWep == NULL) + // ret = M2M_ERR_MEM_ALLOC; + // else + { + pstrWep.u8KeyIndex = pstrAuthWep->u8KeyIndx - 1; + pstrWep.u8KeyLen = pstrAuthWep->u8KeySz/2; + hexstr_2_bytes(pstrWep.au8WepKey, (pstrAuthWep->pu8WepKey), pstrWep.u8KeyLen); + + ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_CONN | M2M_REQ_DATA_PKT, + (uint8_t*)&strConnHdr, sizeof(tstrM2mWifiConnHdr), + (uint8_t*)&pstrWep, sizeof(tstrM2mWifiWep), sizeof(tstrM2mWifiConnHdr)); +// OSAL_Free(pstrWep); + } + if (ret != M2M_SUCCESS) + { + /* Might just be that we are talking to legacy firmware - try the legacy connect message instead. */ + tstrM2mWifiConnectLegacy_1_2 strConnectLegacy; + ret = legacy_connect_prepare_msg(&strConnHdr, &strConnectLegacy); + if (ret == M2M_SUCCESS) + { + tstrM2mWifiWepParamsLegacy_1_2 *pstrWepLegacy = &strConnectLegacy.strSec.uniAuth.strWepInfo; + pstrWepLegacy->u8KeyIndx = pstrAuthWep->u8KeyIndx - 1; + pstrWepLegacy->u8KeySz = pstrAuthWep->u8KeySz; + memcpy(pstrWepLegacy->au8WepKey, pstrAuthWep->pu8WepKey, pstrAuthWep->u8KeySz); + ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_CONNECT, + (uint8_t*)&strConnectLegacy, sizeof(tstrM2mWifiConnectLegacy_1_2), + NULL, 0, 0); + } + } + } + } + return ret; +} + +int8_t m2m_wifi_connect_psk( + tenuCredStoreOption enuCredStoreOption, + tstrNetworkId *pstrNetworkId, + tstrAuthPsk *pstrAuthPsk +) +{ + int8_t ret = M2M_ERR_INVALID_ARG; + + if (pstrAuthPsk != NULL) + { + tstrM2mWifiConnHdr strConnHdr; + + ret = m2m_wifi_connect_prepare_msg(enuCredStoreOption, + M2M_WIFI_SEC_WPA_PSK, + sizeof(tstrM2mWifiPsk), + pstrNetworkId, + &strConnHdr); + + if (ret == M2M_SUCCESS) + { + tstrM2mWifiPsk pstrPsk;// = (tstrM2mWifiPsk *)OSAL_Malloc(sizeof(tstrM2mWifiPsk)); + // if (pstrPsk == NULL) + // ret = M2M_ERR_MEM_ALLOC; + // else + { + memset((uint8_t*)&pstrPsk, 0, sizeof(tstrM2mWifiPsk)); + if (pstrAuthPsk->pu8Psk != NULL) + { + if (pstrAuthPsk->pu8Passphrase != NULL) + ret = M2M_ERR_INVALID_ARG; + else + { + pstrPsk.u8PassphraseLen = M2M_MAX_PSK_LEN-1; + /* Use hexstr_2_bytes to verify pu8Psk input. */ + if (M2M_SUCCESS != hexstr_2_bytes(pstrPsk.au8Passphrase, pstrAuthPsk->pu8Psk, pstrPsk.u8PassphraseLen/2)) + ret = M2M_ERR_INVALID_ARG; + memcpy(pstrPsk.au8Passphrase, pstrAuthPsk->pu8Psk, pstrPsk.u8PassphraseLen); + } + } + else if (pstrAuthPsk->pu8Passphrase != NULL) + { + if (pstrAuthPsk->u8PassphraseLen > M2M_MAX_PSK_LEN-1) + ret = M2M_ERR_INVALID_ARG; + else + { + pstrPsk.u8PassphraseLen = pstrAuthPsk->u8PassphraseLen; + memcpy(pstrPsk.au8Passphrase, pstrAuthPsk->pu8Passphrase, pstrPsk.u8PassphraseLen); + } + } + else + ret = M2M_ERR_INVALID_ARG; + if (ret == M2M_SUCCESS) + { + ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_CONN | M2M_REQ_DATA_PKT, + (uint8_t*)&strConnHdr, sizeof(tstrM2mWifiConnHdr), + (uint8_t*)&pstrPsk, sizeof(tstrM2mWifiPsk), sizeof(tstrM2mWifiConnHdr)); + } + // OSAL_Free(pstrPsk); + } + if (ret != M2M_SUCCESS) + { + /* Might just be that we are talking to legacy firmware - try the legacy connect message instead. */ + tstrM2mWifiConnectLegacy_1_2 strConnectLegacy; + ret = legacy_connect_prepare_msg(&strConnHdr, &strConnectLegacy); + if (ret == M2M_SUCCESS) + { + uint8_t *pu8PskLegacy = strConnectLegacy.strSec.uniAuth.au8PSK; + memcpy(pu8PskLegacy, pstrAuthPsk->pu8Passphrase, pstrAuthPsk->u8PassphraseLen); + ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_CONNECT, + (uint8_t*)&strConnectLegacy, sizeof(tstrM2mWifiConnectLegacy_1_2), + NULL, 0, 0); + } + } + } + } + return ret; +} + +int8_t m2m_wifi_1x_set_option(tenu1xOption enuOptionName, const void *pOptionValue, size_t OptionLen) +{ + if((pOptionValue == NULL) && (OptionLen > 0)) + return M2M_ERR_INVALID_ARG; + switch(enuOptionName) + { + case WIFI_1X_BYPASS_SERVER_AUTH: + if(OptionLen != sizeof(int)) + return M2M_ERR_INVALID_ARG; + switch(*(int*)pOptionValue) + { + case 1: + gu321xTlsHsFlags &= ~WIFI_1X_TLS_HS_FLAGS_PEER_AUTH; + break; + case 0: + gu321xTlsHsFlags |= WIFI_1X_TLS_HS_FLAGS_PEER_AUTH; + break; + default: + return M2M_ERR_INVALID_ARG; + } + break; + case WIFI_1X_TIME_VERIF_MODE: + if(OptionLen != sizeof(tenuTlsCertExpSettings)) + return M2M_ERR_INVALID_ARG; + switch(*(tenuTlsCertExpSettings*)pOptionValue) + { + case TLS_CERT_EXP_CHECK_DISABLE: + gu321xTlsHsFlags &= ~WIFI_1X_TLS_HS_FLAGS_PEER_CERTTIMECHECK; + gu321xTlsHsFlags &= ~WIFI_1X_TLS_HS_FLAGS_REQUIRE_TIME; + break; + case TLS_CERT_EXP_CHECK_ENABLE: + gu321xTlsHsFlags |= WIFI_1X_TLS_HS_FLAGS_PEER_CERTTIMECHECK; + gu321xTlsHsFlags |= WIFI_1X_TLS_HS_FLAGS_REQUIRE_TIME; + break; + case TLS_CERT_EXP_CHECK_EN_IF_SYS_TIME: + gu321xTlsHsFlags |= WIFI_1X_TLS_HS_FLAGS_PEER_CERTTIMECHECK; + gu321xTlsHsFlags &= ~WIFI_1X_TLS_HS_FLAGS_REQUIRE_TIME; + break; + default: + return M2M_ERR_INVALID_ARG; + } + break; + case WIFI_1X_SESSION_CACHING: + if(OptionLen != sizeof(int)) + return M2M_ERR_INVALID_ARG; + switch(*(int*)pOptionValue) + { + case 1: + gu321xTlsHsFlags |= WIFI_1X_TLS_HS_FLAGS_SESSION_CACHING; + break; + case 0: + gu321xTlsHsFlags &= ~WIFI_1X_TLS_HS_FLAGS_SESSION_CACHING; + break; + default: + return M2M_ERR_INVALID_ARG; + } + break; + case WIFI_1X_SPECIFIC_ROOTCERT: + switch(OptionLen) + { + case 20: + gu321xTlsHsFlags |= WIFI_1X_TLS_HS_FLAGS_SPECIFY_ROOTCERT; + memcpy(gau81xRootSha1, (uint8_t*)pOptionValue, sizeof(gau81xRootSha1)); + break; + case 0: + gu321xTlsHsFlags &= ~WIFI_1X_TLS_HS_FLAGS_SPECIFY_ROOTCERT; + memset(gau81xRootSha1, 0, sizeof(gau81xRootSha1)); + default: + return M2M_ERR_INVALID_ARG; + } + break; + default: + return M2M_ERR_INVALID_ARG; + } + return M2M_SUCCESS; +} + +int8_t m2m_wifi_1x_get_option(tenu1xOption enuOptionName, void *pOptionValue, size_t *pOptionLen) +{ + if(pOptionValue == NULL) + return M2M_ERR_INVALID_ARG; + switch(enuOptionName) + { + case WIFI_1X_BYPASS_SERVER_AUTH: + if(*pOptionLen < sizeof(int)) + return M2M_ERR_INVALID_ARG; + *pOptionLen = sizeof(int); + *(int*)pOptionValue = (gu321xTlsHsFlags & WIFI_1X_TLS_HS_FLAGS_PEER_AUTH) ? 0 : 1; + break; + case WIFI_1X_TIME_VERIF_MODE: + if(*pOptionLen < sizeof(tenuTlsCertExpSettings)) + return M2M_ERR_INVALID_ARG; + *pOptionLen = sizeof(tenuTlsCertExpSettings); + if(!(gu321xTlsHsFlags & WIFI_1X_TLS_HS_FLAGS_PEER_CERTTIMECHECK)) + *(tenuTlsCertExpSettings*)pOptionValue = TLS_CERT_EXP_CHECK_DISABLE; + else if(gu321xTlsHsFlags & WIFI_1X_TLS_HS_FLAGS_REQUIRE_TIME) + *(tenuTlsCertExpSettings*)pOptionValue = TLS_CERT_EXP_CHECK_ENABLE; + else + *(tenuTlsCertExpSettings*)pOptionValue = TLS_CERT_EXP_CHECK_EN_IF_SYS_TIME; + break; + case WIFI_1X_SESSION_CACHING: + if(*pOptionLen < sizeof(int)) + return M2M_ERR_INVALID_ARG; + *pOptionLen = sizeof(int); + *(int*)pOptionValue = (gu321xTlsHsFlags & WIFI_1X_TLS_HS_FLAGS_SESSION_CACHING) ? 1 : 0; + break; + case WIFI_1X_SPECIFIC_ROOTCERT: + if (gu321xTlsHsFlags & WIFI_1X_TLS_HS_FLAGS_SPECIFY_ROOTCERT) + { + if (*pOptionLen < sizeof(gau81xRootSha1)) + return M2M_ERR_INVALID_ARG; + *pOptionLen = sizeof(gau81xRootSha1); + memcpy((uint8_t*)pOptionValue, gau81xRootSha1, sizeof(gau81xRootSha1)); + } + else + *pOptionLen = 0; + break; + default: + return M2M_ERR_INVALID_ARG; + } + return M2M_SUCCESS; +} + +int8_t m2m_wifi_connect_1x_mschap2( + tenuCredStoreOption enuCredStoreOption, + tstrNetworkId *pstrNetworkId, + tstrAuth1xMschap2 *pstrAuth1xMschap2 +) +{ + int8_t ret = M2M_ERR_INVALID_ARG; + if (pstrAuth1xMschap2 != NULL) + { + if (pstrAuth1xMschap2->pu8Domain == NULL) + pstrAuth1xMschap2->u16DomainLen = 0; + if ( + (pstrAuth1xMschap2->pu8UserName != NULL) + && (pstrAuth1xMschap2->pu8Password != NULL) + && ((uint32_t)(pstrAuth1xMschap2->u16DomainLen) + pstrAuth1xMschap2->u16UserNameLen <= M2M_AUTH_1X_USER_LEN_MAX) + && (pstrAuth1xMschap2->u16PasswordLen <= M2M_AUTH_1X_PASSWORD_LEN_MAX) + ) + { + tstrM2mWifiConnHdr strConnHdr; + uint16_t u16AuthSize = sizeof(tstrM2mWifi1xHdr) + + pstrAuth1xMschap2->u16DomainLen + + pstrAuth1xMschap2->u16UserNameLen + + pstrAuth1xMschap2->u16PasswordLen; + + ret = m2m_wifi_connect_prepare_msg(enuCredStoreOption, + M2M_WIFI_SEC_802_1X, + u16AuthSize, + pstrNetworkId, + &strConnHdr); + + if (ret == M2M_SUCCESS) + { + tstrM2mWifi1xHdr pstr1xHdr;// = (tstrM2mWifi1xHdr *)OSAL_Malloc(u16AuthSize); + // if (pstr1xHdr == NULL) + // ret = M2M_ERR_MEM_ALLOC; + // else + { + uint8_t *pu8AuthPtr = pstr1xHdr.au81xAuthDetails; + memset((uint8_t*)&pstr1xHdr, 0, u16AuthSize); + + pstr1xHdr.u8Flags = M2M_802_1X_MSCHAP2_FLAG; + if (pstrAuth1xMschap2->bUnencryptedUserName == true) + pstr1xHdr.u8Flags |= M2M_802_1X_UNENCRYPTED_USERNAME_FLAG; + if (pstrAuth1xMschap2->bPrependDomain == true) + pstr1xHdr.u8Flags |= M2M_802_1X_PREPEND_DOMAIN_FLAG; + + pstr1xHdr.u8HdrLength = sizeof(tstrM2mWifi1xHdr); + pstr1xHdr.u32TlsHsFlags = gu321xTlsHsFlags; + memcpy(pstr1xHdr.au8TlsSpecificRootNameSha1, gau81xRootSha1, sizeof(gau81xRootSha1)); + + pstr1xHdr.u8DomainLength = 0; + if (pstrAuth1xMschap2->pu8Domain != NULL) + { + pstr1xHdr.u8DomainLength = (uint8_t)(pstrAuth1xMschap2->u16DomainLen); + memcpy(pu8AuthPtr, pstrAuth1xMschap2->pu8Domain, pstr1xHdr.u8DomainLength); + pu8AuthPtr += pstr1xHdr.u8DomainLength; + } + + pstr1xHdr.u8UserNameLength = (pstrAuth1xMschap2->u16UserNameLen); + memcpy(pu8AuthPtr, pstrAuth1xMschap2->pu8UserName, pstr1xHdr.u8UserNameLength); + pu8AuthPtr += pstr1xHdr.u8UserNameLength; + + pstr1xHdr.u16PrivateKeyOffset = pu8AuthPtr - pstr1xHdr.au81xAuthDetails; + pstr1xHdr.u16PrivateKeyLength = pstrAuth1xMschap2->u16PasswordLen; + memcpy(pu8AuthPtr, pstrAuth1xMschap2->pu8Password, pstr1xHdr.u16PrivateKeyLength); + + ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_CONN | M2M_REQ_DATA_PKT, + (uint8_t*)&strConnHdr, sizeof(tstrM2mWifiConnHdr), + (uint8_t*)&pstr1xHdr, u16AuthSize, + sizeof(tstrM2mWifiConnHdr)); + // OSAL_Free(pstr1xHdr); + } + } + } + } + return ret; +} + +int8_t m2m_wifi_connect_1x_tls( + tenuCredStoreOption enuCredStoreOption, + tstrNetworkId *pstrNetworkId, + tstrAuth1xTls *pstrAuth1xTls +) +{ + int8_t ret = M2M_ERR_INVALID_ARG; + if (pstrAuth1xTls != NULL) + { + if (pstrAuth1xTls->pu8Domain == NULL) + pstrAuth1xTls->u16DomainLen = 0; + if ( + (pstrAuth1xTls->pu8UserName != NULL) + && (pstrAuth1xTls->pu8PrivateKey_Mod != NULL) + && (pstrAuth1xTls->pu8PrivateKey_Exp != NULL) + && (pstrAuth1xTls->pu8Certificate != NULL) + && ((uint32_t)(pstrAuth1xTls->u16DomainLen) + pstrAuth1xTls->u16UserNameLen <= M2M_AUTH_1X_USER_LEN_MAX) + && (pstrAuth1xTls->u16PrivateKeyLen <= M2M_AUTH_1X_PRIVATEKEY_LEN_MAX) + && (pstrAuth1xTls->u16CertificateLen <= M2M_AUTH_1X_CERT_LEN_MAX) + ) + { + tstrM2mWifiConnHdr strConnHdr; + uint16_t u16AuthSize = sizeof(tstrM2mWifi1xHdr) + + pstrAuth1xTls->u16DomainLen + + pstrAuth1xTls->u16UserNameLen + + (2 * pstrAuth1xTls->u16PrivateKeyLen) + + pstrAuth1xTls->u16CertificateLen; + + ret = m2m_wifi_connect_prepare_msg(enuCredStoreOption, + M2M_WIFI_SEC_802_1X, + u16AuthSize, + pstrNetworkId, + &strConnHdr); + + if (ret == M2M_SUCCESS) + { + uint16_t u16Payload1Size = u16AuthSize - pstrAuth1xTls->u16CertificateLen; + tstrM2mWifi1xHdr pstr1xHdr;// = (tstrM2mWifi1xHdr *)OSAL_Malloc(u16Payload1Size); + // if (pstr1xHdr == NULL) + // ret = M2M_ERR_MEM_ALLOC; + // else + { + tstrM2mWifiAuthInfoHdr strInfoHdr = {0}; + + uint8_t *pu8AuthPtr = pstr1xHdr.au81xAuthDetails; + memset((uint8_t*)&pstr1xHdr, 0, u16Payload1Size); + + pstr1xHdr.u8Flags = M2M_802_1X_TLS_FLAG; + if (pstrAuth1xTls->bUnencryptedUserName == true) + pstr1xHdr.u8Flags |= M2M_802_1X_UNENCRYPTED_USERNAME_FLAG; + if (pstrAuth1xTls->bPrependDomain == true) + pstr1xHdr.u8Flags |= M2M_802_1X_PREPEND_DOMAIN_FLAG; + + pstr1xHdr.u8HdrLength = sizeof(tstrM2mWifi1xHdr); + pstr1xHdr.u32TlsHsFlags = gu321xTlsHsFlags; + memcpy(pstr1xHdr.au8TlsSpecificRootNameSha1, gau81xRootSha1, sizeof(gau81xRootSha1)); + + pstr1xHdr.u8DomainLength = 0; + if (pstrAuth1xTls->pu8Domain != NULL) + { + pstr1xHdr.u8DomainLength = (uint8_t)(pstrAuth1xTls->u16DomainLen); + memcpy(pu8AuthPtr, pstrAuth1xTls->pu8Domain, pstr1xHdr.u8DomainLength); + pu8AuthPtr += pstr1xHdr.u8DomainLength; + } + + pstr1xHdr.u8UserNameLength = (pstrAuth1xTls->u16UserNameLen); + memcpy(pu8AuthPtr, pstrAuth1xTls->pu8UserName, pstr1xHdr.u8UserNameLength); + pu8AuthPtr += pstr1xHdr.u8UserNameLength; + + pstr1xHdr.u16PrivateKeyOffset = pu8AuthPtr - pstr1xHdr.au81xAuthDetails; + pstr1xHdr.u16PrivateKeyLength = pstrAuth1xTls->u16PrivateKeyLen; + memcpy(pu8AuthPtr, pstrAuth1xTls->pu8PrivateKey_Mod, pstr1xHdr.u16PrivateKeyLength); + pu8AuthPtr += pstr1xHdr.u16PrivateKeyLength; + memcpy(pu8AuthPtr, pstrAuth1xTls->pu8PrivateKey_Exp, pstr1xHdr.u16PrivateKeyLength); + pu8AuthPtr += pstr1xHdr.u16PrivateKeyLength; + + pstr1xHdr.u16CertificateOffset = pu8AuthPtr - pstr1xHdr.au81xAuthDetails; + pstr1xHdr.u16CertificateLength = pstrAuth1xTls->u16CertificateLen; + + strInfoHdr.u8Type = M2M_802_1X_TLS_CLIENT_CERTIFICATE; + strInfoHdr.u16InfoPos = pstr1xHdr.u16CertificateOffset; + strInfoHdr.u16InfoLen = pstr1xHdr.u16CertificateLength; + ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_IND_CONN_PARAM | M2M_REQ_DATA_PKT, + (uint8_t*)&strInfoHdr, sizeof(tstrM2mWifiAuthInfoHdr), + pstrAuth1xTls->pu8Certificate, pstrAuth1xTls->u16CertificateLen, + sizeof(tstrM2mWifiAuthInfoHdr)); + + if (ret == M2M_SUCCESS) + { + ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_CONN | M2M_REQ_DATA_PKT, + (uint8_t*)&strConnHdr, sizeof(tstrM2mWifiConnHdr), + (uint8_t*)&pstr1xHdr, u16Payload1Size, + sizeof(tstrM2mWifiConnHdr)); + } + // OSAL_Free(pstr1xHdr); + } + } + } + } + return ret; +} + +int8_t m2m_wifi_connect(char *pcSsid, uint8_t u8SsidLen, uint8_t u8SecType, void *pvAuthInfo, uint16_t u16Ch) +{ + return m2m_wifi_connect_sc(pcSsid, u8SsidLen, u8SecType, pvAuthInfo, u16Ch, 0); +} + +int8_t m2m_wifi_connect_sc(char *pcSsid, uint8_t u8SsidLen, uint8_t u8SecType, void *pvAuthInfo, uint16_t u16Ch, uint8_t u8NoSaveCred) +{ + int8_t s8Ret = M2M_ERR_INVALID_ARG; + tstrNetworkId strNetworkId = {NULL, (uint8_t*)pcSsid, u8SsidLen, (tenuM2mScanCh)u16Ch}; + tenuCredStoreOption enuCredStoreOption = u8NoSaveCred ? WIFI_CRED_DONTSAVE : WIFI_CRED_SAVE_ENCRYPTED; + + /* This API does not support SSIDs which contain '\0'. If there is a '\0' character within the + * first u8SsidLen characters, then assume that the input u8SsidLen was incorrect - set length + * to strlen(pcSsid) and continue. This is to avoid a change from the behaviour of previously + * released drivers. */ + if (u8SsidLen < M2M_MAX_SSID_LEN) + while(u8SsidLen--) + if (strNetworkId.pu8Ssid[u8SsidLen] == 0) + strNetworkId.u8SsidLen = u8SsidLen; + + switch((tenuM2mSecType)u8SecType) + { + case M2M_WIFI_SEC_OPEN: + s8Ret = m2m_wifi_connect_open(enuCredStoreOption, &strNetworkId); + break; + case M2M_WIFI_SEC_WPA_PSK: + if (pvAuthInfo != NULL) + { + tstrAuthPsk strAuthPsk = {NULL, NULL, 0}; + uint16_t len = strlen((const char*)pvAuthInfo); + + if (len == M2M_MAX_PSK_LEN-1) + { + strAuthPsk.pu8Psk = (uint8_t*)pvAuthInfo; + } + else + { + strAuthPsk.pu8Passphrase = (uint8_t*)pvAuthInfo; + strAuthPsk.u8PassphraseLen = len; + } + s8Ret = m2m_wifi_connect_psk(enuCredStoreOption, &strNetworkId, &strAuthPsk); + } + break; + case M2M_WIFI_SEC_WEP: + if (pvAuthInfo != NULL) + { + tstrM2mWifiWepParams *pstrWepParams = (tstrM2mWifiWepParams *)pvAuthInfo; + tstrAuthWep strAuthWep = {pstrWepParams->au8WepKey, pstrWepParams->u8KeySz-1, pstrWepParams->u8KeyIndx}; + + s8Ret = m2m_wifi_connect_wep(enuCredStoreOption, &strNetworkId, &strAuthWep); + } + break; + default: + break; + } + return s8Ret; +} + +int8_t m2m_wifi_disconnect(void) +{ + return hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_DISCONNECT, NULL, 0, NULL, 0, 0); +} + +int8_t m2m_wifi_set_mac_address(uint8_t au8MacAddress[6]) +{ + tstrM2mSetMacAddress strTmp; + memcpy((uint8_t*)strTmp.au8Mac, (uint8_t*)au8MacAddress, 6); + return hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_SET_MAC_ADDRESS, + (uint8_t*)&strTmp, sizeof(tstrM2mSetMacAddress), NULL, 0, 0); +} + +int8_t m2m_wifi_set_static_ip(tstrM2MIPConfig *pstrStaticIPConf) +{ + pstrStaticIPConf->u32DNS = NM_BSP_B_L_32(pstrStaticIPConf->u32DNS); + pstrStaticIPConf->u32Gateway = NM_BSP_B_L_32(pstrStaticIPConf->u32Gateway); + pstrStaticIPConf->u32StaticIP = NM_BSP_B_L_32( + pstrStaticIPConf->u32StaticIP); + pstrStaticIPConf->u32SubnetMask = NM_BSP_B_L_32( + pstrStaticIPConf->u32SubnetMask); + return hif_send(M2M_REQ_GROUP_IP, M2M_IP_REQ_STATIC_IP_CONF, + (uint8_t*)pstrStaticIPConf, sizeof(tstrM2MIPConfig), NULL, 0, 0); +} + +int8_t m2m_wifi_enable_dhcp(uint8_t u8DhcpEn) +{ + uint8_t u8Req; + u8Req = u8DhcpEn ? M2M_IP_REQ_ENABLE_DHCP : M2M_IP_REQ_DISABLE_DHCP; + return hif_send(M2M_REQ_GROUP_IP, u8Req, NULL, 0, NULL, 0, 0); +} + +int8_t m2m_wifi_set_lsn_int(tstrM2mLsnInt *pstrM2mLsnInt) +{ + return hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_LSN_INT, (uint8_t*)pstrM2mLsnInt, sizeof(tstrM2mLsnInt), NULL, 0, 0); +} + +int8_t m2m_wifi_set_cust_InfoElement(uint8_t *pau8M2mCustInfoElement) +{ + return hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_CUST_INFO_ELEMENT, (uint8_t*)pau8M2mCustInfoElement, pau8M2mCustInfoElement[0]+1, NULL, 0, 0); +} + +int8_t m2m_wifi_set_scan_options(tstrM2MScanOption *ptstrM2MScanOption) +{ + int8_t s8Ret = M2M_ERR_FAIL; + if (m2m_validate_scan_options(ptstrM2MScanOption) == M2M_SUCCESS) + { + s8Ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_SET_SCAN_OPTION, (uint8_t*)ptstrM2MScanOption, sizeof(tstrM2MScanOption), NULL, 0, 0); + } + return s8Ret; +} + +int8_t m2m_wifi_set_stop_scan_on_first(uint8_t u8StopScanOption) +{ + int8_t s8Ret = M2M_ERR_FAIL; + + tstrM2MStopScanOption StopScanOption = { 0 }; + + if(1 >= u8StopScanOption) + { + StopScanOption.u8StopOnFirstResult = u8StopScanOption; + + s8Ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_SET_STOP_SCAN_OPTION, (uint8_t*)&StopScanOption, sizeof(tstrM2MStopScanOption), NULL, 0, 0); + + M2M_INFO("Scan will %sstop on first result.\r\n", StopScanOption.u8StopOnFirstResult ? "" : "NOT "); + } + else + { + s8Ret = M2M_ERR_INVALID_ARG; + } + + return s8Ret; +} + +int8_t m2m_wifi_set_scan_region(uint16_t ScanRegion) +{ + int8_t s8Ret = M2M_ERR_FAIL; + tstrM2MScanRegion strScanRegion; + strScanRegion.u16ScanRegion = ScanRegion; + s8Ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_SET_SCAN_REGION, (uint8_t*)&strScanRegion, sizeof(tstrM2MScanRegion), NULL, 0, 0); + return s8Ret; +} + +int8_t m2m_wifi_request_scan(uint8_t ch) +{ + int8_t s8Ret = M2M_SUCCESS; + + if (((ch >= M2M_WIFI_CH_1) && (ch <= M2M_WIFI_CH_14)) || (ch == M2M_WIFI_CH_ALL)) + { + tstrM2MScan strtmp; + strtmp.u8ChNum = ch; + s8Ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_SCAN, (uint8_t*)&strtmp, sizeof(tstrM2MScan), NULL, 0, 0); + } + else + { + s8Ret = M2M_ERR_INVALID_ARG; + } + return s8Ret; +} + +int8_t m2m_wifi_request_scan_passive(uint8_t ch) +{ + int8_t s8Ret = M2M_SUCCESS; + + if (((ch >= M2M_WIFI_CH_1) && (ch <= M2M_WIFI_CH_14)) || (ch == M2M_WIFI_CH_ALL)) + { + tstrM2MScan strtmp; + strtmp.u8ChNum = ch; + + s8Ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_PASSIVE_SCAN, (uint8_t*)&strtmp, sizeof(tstrM2MScan), NULL, 0, 0); + } + else + { + s8Ret = M2M_ERR_INVALID_ARG; + } + return s8Ret; +} + +int8_t m2m_wifi_request_scan_ssid_list(uint8_t ch, uint8_t *u8Ssidlist) +{ + int8_t s8Ret = M2M_ERR_INVALID_ARG; + + if((((ch >= M2M_WIFI_CH_1) && (ch <= M2M_WIFI_CH_14)) || (ch == M2M_WIFI_CH_ALL))&&(u8Ssidlist != NULL)) + { + tstrM2MScan strtmp; + uint16_t u16Lsize = 0; + uint8_t u8Apnum = u8Ssidlist[u16Lsize]; + if(u8Apnum <= MAX_HIDDEN_SITES) + { + u16Lsize++; + while(u8Apnum) + { + if(u8Ssidlist[u16Lsize] >= M2M_MAX_SSID_LEN) { + goto EXIT; + } else { + u16Lsize += u8Ssidlist[u16Lsize] + 1; + u8Apnum--; + } + } + strtmp.u8ChNum = ch; + s8Ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_SCAN_SSID_LIST|M2M_REQ_DATA_PKT, (uint8_t*)&strtmp, sizeof(tstrM2MScan), u8Ssidlist, u16Lsize, sizeof(tstrM2MScan)); + } + } +EXIT: + return s8Ret; +} +int8_t m2m_wifi_wps(uint8_t u8TriggerType, const char *pcPinNumber) +{ + tstrM2MWPSConnect strtmp; + + strtmp.u8TriggerType = u8TriggerType; + /*If WPS is using PIN METHOD*/ + if (u8TriggerType == WPS_PIN_TRIGGER) + memcpy((uint8_t*)strtmp.acPinNumber, (uint8_t*)pcPinNumber, 8); + return hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_WPS, (uint8_t*)&strtmp, sizeof(tstrM2MWPSConnect), NULL, 0, 0); +} + +int8_t m2m_wifi_wps_disable(void) +{ + int8_t ret = M2M_SUCCESS; + ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_DISABLE_WPS, NULL, 0, NULL, 0, 0); + return ret; +} + +int8_t m2m_wifi_p2p(uint8_t u8Channel) +{ + int8_t ret = M2M_SUCCESS; + if ((u8Channel == M2M_WIFI_CH_1) || (u8Channel == M2M_WIFI_CH_6) || (u8Channel == M2M_WIFI_CH_11)) + { + tstrM2MP2PConnect strtmp; + strtmp.u8ListenChannel = u8Channel; + ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_ENABLE_P2P, (uint8_t*)&strtmp, sizeof(tstrM2MP2PConnect), NULL, 0, 0); + } + else + { + M2M_ERR("Listen channel should only be 1, 6 or 11\r\n"); + ret = M2M_ERR_FAIL; + } + return ret; +} + +int8_t m2m_wifi_p2p_disconnect(void) +{ + int8_t ret = M2M_SUCCESS; + ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_DISABLE_P2P, NULL, 0, NULL, 0, 0); + return ret; +} +int8_t m2m_wifi_enable_ap(const tstrM2MAPConfig *pstrM2MAPConfig) +{ + tstrM2MAPModeConfig strM2MAPModeConfig; + + memcpy((uint8_t*)&strM2MAPModeConfig.strApConfig, (uint8_t*)pstrM2MAPConfig, sizeof(tstrM2MAPConfig)); + + memcpy(strM2MAPModeConfig.strApConfigExt.au8DefRouterIP, (uint8_t*)pstrM2MAPConfig->au8DHCPServerIP, 4); + memcpy(strM2MAPModeConfig.strApConfigExt.au8DNSServerIP, (uint8_t*)pstrM2MAPConfig->au8DHCPServerIP, 4); + strM2MAPModeConfig.strApConfigExt.au8SubnetMask[0] = 0; + + return m2m_wifi_enable_ap_ext(&strM2MAPModeConfig); +} + +int8_t m2m_wifi_enable_ap_ext(const tstrM2MAPModeConfig *pstrM2MAPModeConfig) +{ + int8_t ret = M2M_ERR_FAIL; + if (M2M_SUCCESS == m2m_validate_ap_parameters(pstrM2MAPModeConfig)) + { + ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_ENABLE_AP, (uint8_t*)pstrM2MAPModeConfig, sizeof(tstrM2MAPModeConfig), NULL, 0, 0); + + if (ret == M2M_ERR_SEND) + { + // Send again using legacy HIF message. + ret = hif_send( + M2M_REQ_GROUP_WIFI, + M2M_WIFI_REQ_ENABLE_AP_LEGACY, + (uint8_t*)&pstrM2MAPModeConfig->strApConfig, + sizeof(tstrM2MAPConfig), + NULL, + 0, + 0 + ); + } + } + return ret; +} + +int8_t m2m_wifi_disable_ap(void) +{ + int8_t ret = M2M_SUCCESS; + ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_DISABLE_AP, NULL, 0, NULL, 0, 0); + return ret; +} +/*! +@fn int8_t m2m_wifi_req_curr_rssi(void); +@brief Request the current RSSI for the current connected AP, + the response received in wifi_cb M2M_WIFI_RESP_CURRENT_RSSI +@sa M2M_WIFI_RESP_CURRENT_RSSI +@return The function shall return M2M_SUCCESS for success and a negative value otherwise. +*/ +int8_t m2m_wifi_req_curr_rssi(void) +{ + int8_t ret = M2M_SUCCESS; + ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_CURRENT_RSSI, NULL, 0, NULL, 0, 0); + return ret; +} + +int8_t m2m_wifi_req_restrict_ble(void) +{ + int8_t ret = M2M_SUCCESS; + ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_RESTRICT_BLE, NULL, 0, NULL, 0, 0); + return ret; +} + +int8_t m2m_wifi_req_unrestrict_ble(void) +{ + int8_t ret = M2M_SUCCESS; + ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_UNRESTRICT_BLE, NULL, 0, NULL, 0, 0); + return ret; +} + +int8_t m2m_wifi_send_ethernet_pkt(uint8_t *pu8Packet, uint16_t u16PacketSize) +{ + int8_t s8Ret = -1; + if ((pu8Packet != NULL)&&(u16PacketSize>0)) + { + tstrM2MWifiTxPacketInfo strTxPkt; + + strTxPkt.u16PacketSize = u16PacketSize; + strTxPkt.u16HeaderLength = M2M_ETHERNET_HDR_LEN; + s8Ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_SEND_ETHERNET_PACKET | M2M_REQ_DATA_PKT, + (uint8_t*)&strTxPkt, sizeof(tstrM2MWifiTxPacketInfo), pu8Packet, u16PacketSize, M2M_ETHERNET_HDR_OFFSET - M2M_HIF_HDR_OFFSET); + } + return s8Ret; +} + +/*! +@fn int8_t m2m_wifi_get_otp_mac_address(uint8_t *pu8MacAddr, uint8_t * pu8IsValid); +@brief Request the MAC address stored on the OTP (one time programmable) memory of the device. + (the function is Blocking until response received) +@param [out] pu8MacAddr + Output MAC address buffer of 6 bytes size. Valid only if *pu8Valid=1. +@param [out] pu8IsValid + A output boolean value to indicate the validity of pu8MacAddr in OTP. + Output zero if the OTP memory is not programmed, non-zero otherwise. +@return The function shall return M2M_SUCCESS for success and a negative value otherwise. +@sa m2m_wifi_get_mac_address +@pre m2m_wifi_init required to call any WIFI/socket function +*/ +int8_t m2m_wifi_get_otp_mac_address(uint8_t *pu8MacAddr, uint8_t *pu8IsValid) +{ + int8_t ret = M2M_SUCCESS; + ret = hif_chip_wake(); + if (ret == M2M_SUCCESS) + { + ret = nmi_get_otp_mac_address(pu8MacAddr, pu8IsValid); + hif_chip_sleep(); + } + return ret; +} + +/*! +@fn int8_t m2m_wifi_get_mac_address(uint8_t *pu8MacAddr) +@brief Request the current MAC address of the device (the working mac address). + (the function is Blocking until response received) +@param [out] pu8MacAddr + Output MAC address buffer of 6 bytes size. +@return The function shall return M2M_SUCCESS for success and a negative value otherwise. +@sa m2m_wifi_get_otp_mac_address +@pre m2m_wifi_init required to call any WIFI/socket function +*/ +int8_t m2m_wifi_get_mac_address(uint8_t *pu8MacAddr) +{ + int8_t ret = M2M_SUCCESS; + ret = hif_chip_wake(); + if (ret == M2M_SUCCESS) + { + ret = nmi_get_mac_address(pu8MacAddr); + hif_chip_sleep(); + } + + return ret; +} + +/*! +@fn int8_t m2m_wifi_req_scan_result(uint8_t index); +@brief Reads the AP information from the Scan Result list with the given index, + the response received in wifi_cb M2M_WIFI_RESP_SCAN_RESULT, + the response pointer should be casted with tstrM2mWifiscanResult structure +@param [in] index + Index for the requested result, the index range start from 0 till number of AP's found +@sa tstrM2mWifiscanResult,m2m_wifi_get_num_ap_found,m2m_wifi_request_scan +@return The function shall return @ref M2M_SUCCESS for success and a negative value otherwise +@pre m2m_wifi_request_scan need to be called first, then m2m_wifi_get_num_ap_found + to get the number of AP's found +@warning Function used only in STA mode only. the scan result updated only if scan request called, + else it will be cashed in firmware for the host scan request result, + which mean if large delay occur between the scan request and the scan result request, + the result will not be up-to-date +*/ + +int8_t m2m_wifi_req_scan_result(uint8_t index) +{ + int8_t ret = M2M_SUCCESS; + tstrM2mReqScanResult strReqScanRlt; + strReqScanRlt.u8Index = index; + ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_SCAN_RESULT, (uint8_t*)&strReqScanRlt, sizeof(tstrM2mReqScanResult), NULL, 0, 0); + return ret; +} + +/*! +@fn uint8_t m2m_wifi_get_num_ap_found(void); +@brief Reads the number of AP's found in the last Scan Request, + The function read the number of AP's from global variable which updated in the + wifi_cb in M2M_WIFI_RESP_SCAN_DONE. +@sa m2m_wifi_request_scan +@return Return the number of AP's found in the last Scan Request. +@pre m2m_wifi_request_scan need to be called first +@warning That function need to be called in the wifi_cb in M2M_WIFI_RESP_SCAN_DONE, + calling that function in any other place will return undefined/undated numbers. + Function used only in STA mode only. +*/ +uint8_t m2m_wifi_get_num_ap_found(void) +{ + return gu8ChNum; +} + +/*! +@fn uint8_t m2m_wifi_get_sleep_mode(void); +@brief Get the current Power save mode. +@return The current operating power saving mode. +@sa tenuPowerSaveModes , m2m_wifi_set_sleep_mode +*/ +uint8_t m2m_wifi_get_sleep_mode(void) +{ + return hif_get_sleep_mode(); +} + +/*! +@fn int8_t m2m_wifi_set_sleep_mode(uint8_t PsTyp, uint8_t BcastEn); +@brief Set the power saving mode for the WINC3400. +@param [in] PsTyp + Desired power saving mode. Supported types are defined in tenuPowerSaveModes. +@param [in] BcastEn + Broadcast reception enable flag. + If it is 1, the WINC3400 must be awake each DTIM Beacon for receiving Broadcast traffic. + If it is 0, the WINC3400 will not wakeup at the DTIM Beacon, but its wakeup depends only + on the the configured Listen Interval. +@return The function SHALL return 0 for success and a negative value otherwise. +@sa tenuPowerSaveModes +@warning The function called once after initialization. +*/ +int8_t m2m_wifi_set_sleep_mode(uint8_t PsTyp, uint8_t BcastEn) +{ + int8_t ret = M2M_SUCCESS; + tstrM2mPsType strPs; + strPs.u8PsType = PsTyp; + strPs.u8BcastEn = BcastEn; + ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_SLEEP, (uint8_t*)&strPs, sizeof(tstrM2mPsType), NULL, 0, 0); + M2M_INFO("POWER SAVE %d\r\n", PsTyp); + hif_set_sleep_mode(PsTyp); + return ret; +} + +/*! +@fn int8_t m2m_wifi_request_sleep(void) +@brief Request from WINC3400 device to Sleep for specific time in the M2M_PS_MANUAL Power save mode (only). +@param [in] u32SlpReqTime + Request Sleep in ms +@return The function SHALL return M2M_SUCCESS for success and a negative value otherwise. +@sa tenuPowerSaveModes , m2m_wifi_set_sleep_mode +@warning This API is currently unsupported on the WINC3400 +*/ +int8_t m2m_wifi_request_sleep(uint32_t u32SlpReqTime) +{ + int8_t ret = M2M_SUCCESS; + uint8_t psType; + psType = hif_get_sleep_mode(); + if (psType == M2M_PS_MANUAL) + { + tstrM2mSlpReqTime strPs; + strPs.u32SleepTime = u32SlpReqTime; + ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_DOZE, (uint8_t*)&strPs, sizeof(tstrM2mSlpReqTime), NULL, 0, 0); + } + return ret; +} + +/*! +@fn int8_t m2m_wifi_set_device_name(uint8_t *pu8DeviceName, uint8_t u8DeviceNameLength); +@brief Set the WINC3400 device name which is used as P2P device name. +@param [in] pu8DeviceName + Buffer holding the device name. +@param [in] u8DeviceNameLength + Length of the device name. +@return The function SHALL return M2M_SUCCESS for success and a negative value otherwise. +@warning The Function called once after initialization. +*/ +int8_t m2m_wifi_set_device_name(uint8_t *pu8DeviceName, uint8_t u8DeviceNameLength) +{ + tstrM2MDeviceNameConfig strDeviceName; + if (u8DeviceNameLength >= M2M_DEVICE_NAME_MAX) + { + u8DeviceNameLength = M2M_DEVICE_NAME_MAX; + } + //pu8DeviceName[u8DeviceNameLength] = '\0'; + u8DeviceNameLength ++; + memcpy(strDeviceName.au8DeviceName, pu8DeviceName, u8DeviceNameLength); + return hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_SET_DEVICE_NAME, + (uint8_t*)&strDeviceName, sizeof(tstrM2MDeviceNameConfig), NULL, 0, 0); +} + +/*! +@fn int8_t m2m_wifi_configure_sntp(uint8_t *pu8NTPServerName, uint8_t u8NTPServerNameLength, tenuSNTPUseDHCP useDHCP); +@brief Configures what NTP server the SNTP client should use. +@param [in] pu8NTPServerName + Buffer holding the NTP server name. If the first character is an asterisk (*) then it will be treated as a server pool, where the asterisk will + be replaced with an incrementing value from 0 to 3 each time a server fails (example: *.pool.ntp.org). +@param [in] u8NTPServerNameLength + Length of the NTP server name. Should not exceed the maximum NTP server name length of @ref M2M_NTP_MAX_SERVER_NAME_LENGTH +@param [in] useDHCP + Should the NTP server provided by the DHCP server be used. +@return The function SHALL return M2M_SUCCESS for success and a negative value otherwise. +*/ +int8_t m2m_wifi_configure_sntp(uint8_t *pu8NTPServerName, uint8_t u8NTPServerNameLength, tenuSNTPUseDHCP useDHCP) +{ + tstrM2MSNTPConfig strSNTPConfig; + if (u8NTPServerNameLength > M2M_NTP_MAX_SERVER_NAME_LENGTH) + return M2M_ERR_FAIL; + + memcpy((uint8_t*)strSNTPConfig.acNTPServer, pu8NTPServerName, u8NTPServerNameLength); + strSNTPConfig.acNTPServer[u8NTPServerNameLength] = '\0'; + + strSNTPConfig.enuUseDHCP = useDHCP; + + return hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_CONFIG_SNTP, + (uint8_t*)&strSNTPConfig, sizeof(tstrM2MSNTPConfig), NULL, 0, 0); +} + +/*! +@fn \ + uint32_t m2m_wifi_get_chipId(void) + +@brief + Get the WINC Chip ID. + +@return + The function SHALL return chipID >0 or 0 for failure. +*/ +uint32_t m2m_wifi_get_chipId(void) +{ + return nmi_get_chipid(); +} + +/*! +@fn int8_t m2m_wifi_get_firmware_version(tstrM2mRev* pstrRev) + +@brief + Synchronous API to obtain the firmware version currently running on the WINC IC + +@param [out] pstrRev + pointer holds address of structure "tstrM2mRev" that contains the firmware version parameters + +@return + The function SHALL return @ref M2M_SUCCESS for success and a negative value otherwise. +*/ +int8_t m2m_wifi_get_firmware_version(tstrM2mRev *pstrRev) +{ + int8_t ret = M2M_SUCCESS; + ret = hif_chip_wake(); + if (ret == M2M_SUCCESS) + { + ret = nm_get_firmware_full_info(pstrRev); + hif_chip_sleep(); + } + return ret; +} + +/*! +@fn int8_t m2m_wifi_check_ota_rb(void); + +@brief + Synchronous API to check presence and compatibility of the WINC image that is stored in the inactive flash partition. + This is the image that would run on the WINC IC if @ref m2m_ota_switch_firmware or @ref m2m_ota_rollback were called, + followed by a reset of the WINC IC. + +@return + The function SHALL return @ref M2M_SUCCESS for compatible image and a negative value otherwise. +*/ +int8_t m2m_wifi_check_ota_rb(void) +{ + int8_t ret = M2M_SUCCESS; + uint16_t ota_hif_info = 0; + + ret = nm_get_hif_info(NULL, &ota_hif_info); + if (ret == M2M_SUCCESS) + { + ret = hif_check_compatibility(ota_hif_info); + } + return ret; +} + +/*! +@fn \ + int8_t m2m_ota_get_firmware_version(tstrM2mRev *pstrRev); + +@brief + Synchronous API to obtain the firmware version of the WINC image that is stored in the inactive flash partition. + This is the image that would run on the WINC IC if @ref m2m_ota_switch_firmware or @ref m2m_ota_rollback were called, + followed by a reset of the WINC IC. + +@param [out] pstrRev + pointer holds address of structure "tstrM2mRev" that contains the ota fw version parameters + +@return + The function SHALL return @ref M2M_SUCCESS for success and a negative value otherwise. +*/ +int8_t m2m_ota_get_firmware_version(tstrM2mRev *pstrRev) +{ + int8_t ret = M2M_SUCCESS; + ret = hif_chip_wake(); + if (ret == M2M_SUCCESS) + { + ret = nm_get_ota_firmware_info(pstrRev); + hif_chip_sleep(); + } + return ret; +} + +int8_t m2m_wifi_start_provision_mode(tstrM2MAPConfig *pstrM2MAPConfig, char *pcHttpServerDomainName, uint8_t bEnableHttpRedirect) +{ + tstrM2MAPModeConfig strM2MAPModeConfig; + + memcpy((uint8_t*)&strM2MAPModeConfig.strApConfig, (uint8_t*)pstrM2MAPConfig, sizeof(tstrM2MAPConfig)); + + memcpy(strM2MAPModeConfig.strApConfigExt.au8DefRouterIP, pstrM2MAPConfig->au8DHCPServerIP, 4); + memcpy(strM2MAPModeConfig.strApConfigExt.au8DNSServerIP, pstrM2MAPConfig->au8DHCPServerIP, 4); + strM2MAPModeConfig.strApConfigExt.au8SubnetMask[0] = 0; + + return m2m_wifi_start_provision_mode_ext(&strM2MAPModeConfig, pcHttpServerDomainName, bEnableHttpRedirect); +} + +int8_t m2m_wifi_start_provision_mode_ext(tstrM2MAPModeConfig *pstrAPModeConfig, char *pcHttpServerDomainName, uint8_t bEnableHttpRedirect) +{ + int8_t s8Ret = M2M_ERR_FAIL; + + if ((pstrAPModeConfig != NULL)) + { + tstrM2MProvisionModeConfig strProvConfig; + if (M2M_SUCCESS == m2m_validate_ap_parameters(pstrAPModeConfig)) + { + memcpy((uint8_t*)&strProvConfig.strApConfig, (uint8_t*)&pstrAPModeConfig->strApConfig, sizeof(tstrM2MAPConfig)); + memcpy((uint8_t*)&strProvConfig.strApConfigExt, (uint8_t*)&pstrAPModeConfig->strApConfigExt, sizeof(tstrM2MAPConfigExt)); + if ((strlen((const char*)pcHttpServerDomainName) <= 0) || (NULL == pcHttpServerDomainName)) + { + M2M_ERR("INVALID DOMAIN NAME\r\n"); + goto ERR1; + } + memcpy((uint8_t*)strProvConfig.acHttpServerDomainName, (uint8_t*)pcHttpServerDomainName, 64); + strProvConfig.u8EnableRedirect = bEnableHttpRedirect; + + s8Ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_START_PROVISION_MODE | M2M_REQ_DATA_PKT, + (uint8_t*)&strProvConfig, sizeof(tstrM2MProvisionModeConfig), NULL, 0, 0); + + if (s8Ret == M2M_ERR_SEND) + { + // Send again using legacy HIF message. We can use the same structure here as + // the firmware only uses to the HIF message to determine whether or not the + // structure contains a tstrM2MAPConfigExt structure. + s8Ret = hif_send( + M2M_REQ_GROUP_WIFI, + M2M_WIFI_REQ_START_PROVISION_MODE_LEGACY | M2M_REQ_DATA_PKT, + (uint8_t*)&strProvConfig, + sizeof(tstrM2MProvisionModeConfig), + NULL, + 0, + 0 + ); + } + } + else + { + /*goto ERR1;*/ + } + } +ERR1: + return s8Ret; +} + +int8_t m2m_wifi_stop_provision_mode(void) +{ + return hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_STOP_PROVISION_MODE, NULL, 0, NULL, 0, 0); +} + +int8_t m2m_wifi_get_connection_info(void) +{ + return hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_GET_CONN_INFO, NULL, 0, NULL, 0, 0); +} + +int8_t m2m_wifi_set_system_time(uint32_t u32UTCSeconds) +{ + return hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_SET_SYS_TIME, (uint8_t*)&u32UTCSeconds, sizeof(tstrSystemTime), NULL, 0, 0); +} + +/*! + * @fn int8_t m2m_wifi_get_system_time(void); + * @see m2m_wifi_enable_sntp + tstrSystemTime + * @note get the system time from the sntp client + * using the API \ref m2m_wifi_get_system_time. + * @return The function returns @ref M2M_SUCCESS for successful operations and a negative value otherwise. + */ +int8_t m2m_wifi_get_system_time(void) +{ + return hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_GET_SYS_TIME, NULL, 0, NULL, 0, 0); +} + +int8_t m2m_wifi_enable_sntp(uint8_t bEnable) +{ + uint8_t u8Req; + + u8Req = bEnable ? M2M_WIFI_REQ_ENABLE_SNTP_CLIENT : M2M_WIFI_REQ_DISABLE_SNTP_CLIENT; + return hif_send(M2M_REQ_GROUP_WIFI, u8Req, NULL, 0, NULL, 0, 0); +} + +/*! +@fn int8_t m2m_wifi_set_power_profile(uint8_t u8PwrMode); +@brief Change the power profile mode\n + Not implemented in WINC3400 firmware. +@param [in] u8PwrMode + Change the WINC power profile to different mode + PWR_LOW1/PWR_LOW2/PWR_HIGH/PWR_AUTO (tenuM2mPwrMode) +@return The function SHALL return @ref M2M_SUCCESS for success and a negative value otherwise. +@sa tenuM2mPwrMode +@pre m2m_wifi_init +@warning must be called after the initializations and before any connection request and can't be changed in run time, +*/ +int8_t m2m_wifi_set_power_profile(uint8_t u8PwrMode) +{ + int8_t ret = M2M_SUCCESS; + tstrM2mPwrMode strM2mPwrMode; + strM2mPwrMode.u8PwrMode = u8PwrMode; + ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_SET_POWER_PROFILE, (uint8_t*)&strM2mPwrMode, sizeof(tstrM2mPwrMode), NULL, 0, 0); + return ret; +} + +/*! +@fn int8_t m2m_wifi_set_tx_power(uint8_t u8TxPwrLevel); +@brief set the TX power tenuM2mTxPwrLevel +@param [in] u8TxPwrLevel + change the TX power tenuM2mTxPwrLevel +@return The function SHALL return @ref M2M_SUCCESS for success and a negative value otherwise. +@sa tenuM2mTxPwrLevel +@pre m2m_wifi_init +@warning +*/ +int8_t m2m_wifi_set_tx_power(uint8_t u8TxPwrLevel) +{ + int8_t ret = M2M_SUCCESS; + tstrM2mTxPwrLevel strM2mTxPwrLevel; + strM2mTxPwrLevel.u8TxPwrLevel = u8TxPwrLevel; + ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_SET_TX_POWER, (uint8_t*)&strM2mTxPwrLevel, sizeof(tstrM2mTxPwrLevel), NULL, 0, 0); + return ret; +} + +/*! +@fn int8_t m2m_wifi_enable_firmware_logs(uint8_t u8Enable); +@brief Enable or Disable logs in run time (Disable Firmware logs will + enhance the firmware start-up time and performance) +@param [in] u8Enable + Set 1 to enable the logs 0 for disable +@return The function SHALL return @ref M2M_SUCCESS for success and a negative value otherwise. +@sa __DISABLE_FIRMWARE_LOGS__ (build option to disable logs from initializations) +@pre m2m_wifi_init +@warning +*/ +int8_t m2m_wifi_enable_firmware_logs(uint8_t u8Enable) +{ + int8_t ret = M2M_SUCCESS; + tstrM2mEnableLogs strM2mEnableLogs; + strM2mEnableLogs.u8Enable = u8Enable; + ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_SET_ENABLE_LOGS, (uint8_t*)&strM2mEnableLogs, sizeof(tstrM2mEnableLogs), NULL, 0, 0); + return ret; +} + +/*! +@fn int8_t m2m_wifi_set_battery_voltage(uint16_t u16BattVoltx100); +@brief Set the battery voltage to update the firmware calculations. \n + Not implemented in WINC3400 firmware. +@param [in] u16BattVoltx100 + battery voltage multiplied by 100 +@return The function SHALL return @ref M2M_SUCCESS for success and a negative value otherwise. +@sa __DISABLE_FIRMWARE_LOGS__ (build option to disable logs from initializations) +@pre m2m_wifi_init +@warning +*/ +int8_t m2m_wifi_set_battery_voltage(uint16_t u16BattVoltx100) +{ + int8_t ret = M2M_SUCCESS; + tstrM2mBatteryVoltage strM2mBattVol = {0}; + strM2mBattVol.u16BattVolt = u16BattVoltx100; + ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_SET_BATTERY_VOLTAGE, (uint8_t*)&strM2mBattVol, sizeof(tstrM2mBatteryVoltage), NULL, 0, 0); + return ret; +} + +/*! +@fn int8_t m2m_wifi_prng_get_random_bytes(uint8_t * pu8PrngBuff,uint16_t u16PrngSize) +@brief Get random bytes using the PRNG bytes. +@param [in] u16PrngSize + Size of the required random bytes to be generated. +@param [in] pu8PrngBuff + Pointer to user allocated buffer. +@return The function SHALL return M2M_SUCCESS for success and a negative value otherwise. +*/ +int8_t m2m_wifi_prng_get_random_bytes(uint8_t *pu8PrngBuff, uint16_t u16PrngSize) +{ + int8_t ret = M2M_ERR_FAIL; + tstrPrng strRng = {0}; + if ((u16PrngSize < (M2M_BUFFER_MAX_SIZE - sizeof(tstrPrng)))&&(pu8PrngBuff != NULL)) + { + strRng.u16PrngSize = u16PrngSize; + strRng.pu8RngBuff = pu8PrngBuff; + ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_GET_PRNG|M2M_REQ_DATA_PKT, (uint8_t*)&strRng, sizeof(tstrPrng), NULL, 0, 0); + } + else + { + M2M_ERR("PRNG Buffer exceeded maximum size %d or NULL Buffer\r\n", u16PrngSize); + } + return ret; +} + +/*! +@fn \ + int8_t m2m_wifi_enable_mac_mcast(uint8_t* pu8MulticastMacAddress, uint8_t u8AddRemove) + +@brief + Add MAC filter to receive Multicast packets. + +@param [in] pu8MulticastMacAddress + Pointer to the MAC address. +@param [in] u8AddRemove + Flag to Add/Remove MAC address. +@return + The function SHALL return 0 for success and a negative value otherwise. +*/ +int8_t m2m_wifi_enable_mac_mcast(uint8_t *pu8MulticastMacAddress, uint8_t u8AddRemove) +{ + int8_t s8ret = M2M_ERR_FAIL; + tstrM2MMulticastMac strMulticastMac; + + if (pu8MulticastMacAddress != NULL) + { + strMulticastMac.u8AddRemove = u8AddRemove; + memcpy(strMulticastMac.au8macaddress, pu8MulticastMacAddress, M2M_MAC_ADDRES_LEN); + M2M_DBG("mac multicast: %x:%x:%x:%x:%x:%x\r\n", strMulticastMac.au8macaddress[0], strMulticastMac.au8macaddress[1], strMulticastMac.au8macaddress[2], strMulticastMac.au8macaddress[3], strMulticastMac.au8macaddress[4], strMulticastMac.au8macaddress[5]); + s8ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_SET_MAC_MCAST, (uint8_t*)&strMulticastMac, sizeof(tstrM2MMulticastMac), NULL, 0, 0); + } + + return s8ret; +} + +/*! +@fn \ + int8_t m2m_wifi_set_receive_buffer(void* pvBuffer,uint16_t u16BufferLen); + +@brief + set the Ethernet receive buffer, should be called in the receive call back. + +@param [in] pvBuffer + Pointer to the Ethernet receive buffer. +@param [in] u16BufferLen + Length of the buffer. + +@return + The function SHALL return 0 for success and a negative value otherwise. +*/ +int8_t m2m_wifi_set_receive_buffer(void *pvBuffer, uint16_t u16BufferLen) +{ + int8_t s8ret = M2M_SUCCESS; + if (pvBuffer != NULL) + { + gau8ethRcvBuf = pvBuffer; + gu16ethRcvBufSize= u16BufferLen; + } + else + { + s8ret = M2M_ERR_FAIL; + M2M_ERR("Buffer NULL pointer\r\n"); + } + return s8ret; +} + +int8_t m2m_wifi_enable_roaming(uint8_t bEnableDhcp) +{ + tstrM2mWiFiRoaming strWiFiRoaming; + strWiFiRoaming.u8EnableRoaming = 1; + if (0 == bEnableDhcp || 1 == bEnableDhcp) + { + strWiFiRoaming.u8EnableDhcp = bEnableDhcp; + return hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_ROAMING, + (uint8_t*)&strWiFiRoaming, sizeof(tstrM2mWiFiRoaming), NULL, 0, 0); + } + else + { + return M2M_ERR_INVALID_ARG; + } +} + +int8_t m2m_wifi_disable_roaming(void) +{ + tstrM2mWiFiRoaming strWiFiRoaming; + strWiFiRoaming.u8EnableRoaming = 0; + return hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_ROAMING, (uint8_t*)&strWiFiRoaming, sizeof(tstrM2mWiFiRoaming), NULL, 0, 0); +} + +/*! +@fn \ + int8_t m2m_wifi_ble_api_send(const uint8_t* const msg, const uint32_t len); + +@brief + Send an encapsulated Atmel BLE API message + +@param [in] msg + Pointer to the Atmel BLE API message raw bytes +@param [in] len + Length of the msg + +@return + The function SHALL return 0 for success and a negative value otherwise. +*/ +int8_t m2m_wifi_ble_api_send(uint8_t *msg, uint32_t len) +{ + tstrM2mBleApiMsg bleTx; + bleTx.u16Len = len; + return hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_BLE_API_SEND | M2M_REQ_DATA_PKT, + (uint8_t*)&bleTx, sizeof(tstrM2mBleApiMsg), msg, len, sizeof(tstrM2mBleApiMsg)); +} + +//DOM-IGNORE-END diff --git a/ChaletLora.X/Source/winc3400_142/driver/source/nmasic.c b/ChaletLora.X/Source/winc3400_142/driver/source/nmasic.c new file mode 100644 index 0000000..5ea608b --- /dev/null +++ b/ChaletLora.X/Source/winc3400_142/driver/source/nmasic.c @@ -0,0 +1,706 @@ +/******************************************************************************* + File Name: + nmasic.c + + Summary: + This module contains WINC3400 ASIC specific internal APIs. + + Description: + This module contains WINC3400 ASIC specific internal APIs. + *******************************************************************************/ + +//DOM-IGNORE-BEGIN +/******************************************************************************* +* Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries. +* +* Subject to your compliance with these terms, you may use Microchip software +* and any derivatives exclusively with Microchip products. It is your +* responsibility to comply with third party license terms applicable to your +* use of third party software (including open source software) that may +* accompany Microchip software. +* +* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER +* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED +* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A +* PARTICULAR PURPOSE. +* +* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, +* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND +* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS +* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE +* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN +* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, +* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. +*******************************************************************************/ + +#include "nm_common.h" +#include "nmbus.h" +#include "nm_bsp.h" +#include "nmasic.h" +#include "m2m_types.h" + +#define NMI_GLB_RESET_0 (NMI_PERIPH_REG_BASE + 0x400) +#define NMI_INTR_REG_BASE (NMI_PERIPH_REG_BASE + 0xa00) +#define NMI_PIN_MUX_0 (NMI_PERIPH_REG_BASE + 0x408) +#define NMI_INTR_ENABLE (NMI_INTR_REG_BASE) +#define GET_UINT32(X,Y) (X[0+Y] + ((uint32_t)X[1+Y]<<8) + ((uint32_t)X[2+Y]<<16) +((uint32_t)X[3+Y]<<24)) + +#define CORT_HOST_COMM (0x14) +#define HOST_CORT_COMM (0x0e) +#define WAKE_CLK_REG (0x1) +#define CLOCKS_EN_REG (0x13) + +#define TIMEOUT (2000) +#define WAKEUP_TRIALS (4) + +int8_t chip_apply_conf(uint32_t u32Conf) +{ + int8_t ret = M2M_SUCCESS; + uint32_t val32 = u32Conf; + +#ifdef __ENABLE_PMU__ + val32 |= rHAVE_USE_PMU_BIT; +#endif +#ifdef __ENABLE_SLEEP_CLK_SRC_RTC__ + val32 |= rHAVE_SLEEP_CLK_SRC_RTC_BIT; +#elif defined __ENABLE_SLEEP_CLK_SRC_XO__ + val32 |= rHAVE_SLEEP_CLK_SRC_XO_BIT; +#endif +#ifdef __ENABLE_EXT_PA_INV_TX_RX__ + val32 |= rHAVE_EXT_PA_INV_TX_RX; +#endif +#ifdef __ENABLE_LEGACY_RF_SETTINGS__ + val32 |= rHAVE_LEGACY_RF_SETTINGS; +#endif +#ifdef __DISABLE_FIRMWARE_LOGS__ + val32 |= rHAVE_LOGS_DISABLED_BIT; +#endif + do { + nm_write_reg(rNMI_GP_REG_1, val32); + if(val32 != 0) { + uint32_t reg = 0; + ret = nm_read_reg_with_ret(rNMI_GP_REG_1, ®); + if(ret == M2M_SUCCESS) { + if(reg == val32) + break; + } + } else { + break; + } + } while(1); + + return M2M_SUCCESS; +} + +void chip_idle(void) +{ + uint32_t reg =0; + nm_read_reg_with_ret(WAKE_CLK_REG, ®); + if(reg&NBIT1) + { + reg &=~NBIT1; + nm_write_reg(WAKE_CLK_REG, reg); + } +} + +void enable_rf_blocks(void) +{ + nm_write_reg(0x6, 0xdb); + nm_write_reg(0x7, 0x6); + nm_sleep(10); + nm_write_reg(0x1480, 0); + nm_write_reg(0x1484, 0); + nm_sleep(10); + + nm_write_reg(0x6, 0x0); + nm_write_reg(0x7, 0x0); +} + +int8_t enable_interrupts(void) +{ + uint32_t reg = 0; + int8_t ret = M2M_SUCCESS; + /** + interrupt pin mux select + **/ + ret = nm_read_reg_with_ret(NMI_PIN_MUX_0, ®); + if (M2M_SUCCESS != ret) { + return M2M_ERR_BUS_FAIL; + } + reg |= ((uint32_t) 1 << 8); + ret = nm_write_reg(NMI_PIN_MUX_0, reg); + if (M2M_SUCCESS != ret) { + return M2M_ERR_BUS_FAIL; + } + /** + interrupt enable + **/ + ret = nm_read_reg_with_ret(NMI_INTR_ENABLE, ®); + if (M2M_SUCCESS != ret) { + return M2M_ERR_BUS_FAIL; + } + reg |= ((uint32_t) 1 << 16); + ret = nm_write_reg(NMI_INTR_ENABLE, reg); + if (M2M_SUCCESS != ret) { + return M2M_ERR_BUS_FAIL; + } + return M2M_SUCCESS; +} + +int8_t cpu_start(void) { + uint32_t reg = 0; + int8_t ret; + + /** + reset regs + */ + ret = nm_write_reg(BOOTROM_REG, 0); + ret += nm_write_reg(NMI_STATE_REG, 0); + ret += nm_write_reg(NMI_REV_REG, 0); + + /** + Go... + **/ + ret += nm_read_reg_with_ret(0x1118, ®); + if (M2M_SUCCESS != ret) { + ret = M2M_ERR_BUS_FAIL; + M2M_ERR("[nmi start]: fail read reg 0x1118 ...\r\n"); + } + reg |= NBIT0; + ret += nm_write_reg(0x1118, reg); + ret = nm_write_reg(0x150014, 0x1); //DTODO WHY IS THIS HERE?? + ret += nm_read_reg_with_ret(NMI_GLB_RESET_0, ®); + if ((reg & (1ul << 10)) == (1ul << 10)) { + reg &= ~(1ul << 10); + ret += nm_write_reg(NMI_GLB_RESET_0, reg); + } + + reg |= (1ul << 10); + ret += nm_write_reg(NMI_GLB_RESET_0, reg); + nm_sleep(1); /* Removing this can cause a bus error. */ + return ret; +} + +uint32_t nmi_get_chipid(void) +{ + static uint32_t chipid = 0; + + if (chipid == 0) { + uint32_t rfrevid; + + if ((nm_read_reg_with_ret(0x1000, &chipid)) != M2M_SUCCESS) { + chipid = 0; + return 0; + } + if ((nm_read_reg_with_ret(0x13f4, &rfrevid)) != M2M_SUCCESS) { + chipid = 0; + return 0; + } + + if (chipid == 0x1002a0) { + if (rfrevid == 0x1) { /* 1002A0 */ + } else /* if (rfrevid == 0x2) */ { /* 1002A1 */ + chipid = 0x1002a1; + } + } else if (chipid == 0x1002b0) { + if (rfrevid == 3) { /* 1002B0 */ + } else if (rfrevid == 4) { /* 1002B1 */ + chipid = 0x1002b1; + } else /* if(rfrevid == 5) */ { /* 1002B2 */ + chipid = 0x1002b2; + } + } else if(chipid == 0x1000f0) { + /* For 3400, the WiFi chip ID register reads 0x1000f0. + * Therefore using BT chip ID register here which should read 0x3000D0 + */ +#define rBT_CHIP_ID_REG (0x3b0000) + if((nm_read_reg_with_ret(rBT_CHIP_ID_REG, &chipid)) != M2M_SUCCESS) { + chipid = 0; + return 0; + } + if(chipid == 0x3000d0) { + if(rfrevid == 6) { + chipid = 0x3000d1; + } + else if(rfrevid == 2) { + chipid = 0x3000d2; + } + } + } +//#define PROBE_FLASH +#ifdef PROBE_FLASH + if(chipid) { + UWORD32 flashid; + + flashid = probe_spi_flash(); + if((chipid & 0xf00000) == 0x300000) { + if(flashid == 0x1440ef) { + chipid &= ~(0x0f0000); + chipid |= 0x040000; + } + } else { + if(flashid == 0x1230ef) { + chipid &= ~(0x0f0000); + chipid |= 0x050000; + } + if(flashid == 0xc21320c2) { + chipid &= ~(0x0f0000); + chipid |= 0x050000; + } + } + } +#else + /*M2M is by default have SPI flash*/ + if((chipid & 0xf00000) == 0x300000) { + chipid &= ~(0x0f0000); + chipid |= 0x040000; + } else { + chipid &= ~(0x0f0000); + chipid |= 0x050000; + } +#endif /* PROBE_FLASH */ + } + return chipid; +} + +uint32_t nmi_get_rfrevid(void) +{ + uint32_t rfrevid; + if((nm_read_reg_with_ret(0x13f4, &rfrevid)) != M2M_SUCCESS) { + rfrevid = 0; + } + return rfrevid; +} + +void restore_pmu_settings_after_global_reset(void) +{ + /* + * Must restore PMU register value after + * global reset if PMU toggle is done at + * least once since the last hard reset. + */ + if(REV(nmi_get_chipid()) >= REV_2B0) { + nm_write_reg(0x1e48, 0xb78469ce); + } +} + +void nmi_update_pll(void) +{ + uint32_t pll; + + pll = nm_read_reg(0x1428); + pll &= ~0x1ul; + nm_write_reg(0x1428, pll); + pll |= 0x1ul; + nm_write_reg(0x1428, pll); + +} +void nmi_set_sys_clk_src_to_xo(void) +{ + uint32_t val32; + + /* Switch system clock source to XO. This will take effect after nmi_update_pll(). */ + val32 = nm_read_reg(0x141c); + val32 |= (1 << 2); + nm_write_reg(0x141c, val32); + + /* Do PLL update */ + nmi_update_pll(); +} + +int8_t chip_sleep(void) +{ + uint32_t reg; + int8_t ret = M2M_SUCCESS; + + while(1) + { + ret = nm_read_reg_with_ret(CORT_HOST_COMM, ®); + if(ret != M2M_SUCCESS) goto ERR1; + if((reg & NBIT0) == 0) break; + } + + /* Clear bit 1 */ + ret = nm_read_reg_with_ret(WAKE_CLK_REG, ®); + if(ret != M2M_SUCCESS)goto ERR1; + if(reg & NBIT1) + { + reg &=~NBIT1; + ret = nm_write_reg(WAKE_CLK_REG, reg); + if(ret != M2M_SUCCESS)goto ERR1; + } + + ret = nm_read_reg_with_ret(HOST_CORT_COMM, ®); + if(ret != M2M_SUCCESS)goto ERR1; + if(reg & NBIT0) + { + reg &= ~NBIT0; + ret = nm_write_reg(HOST_CORT_COMM, reg); + if(ret != M2M_SUCCESS)goto ERR1; + } + +ERR1: + return ret; +} + +int8_t chip_wake(void) +{ + int8_t ret = M2M_SUCCESS; + uint32_t reg = 0, clk_status_reg = 0, trials = 0; + + nm_read_reg_with_ret(CLOCKS_EN_REG, &clk_status_reg); + + ret = nm_read_reg_with_ret(HOST_CORT_COMM, ®); + if(ret != M2M_SUCCESS)goto _WAKE_EXIT; + + if(!(reg & NBIT0)) + { + /*USE bit 0 to indicate host wakeup*/ + ret = nm_write_reg(HOST_CORT_COMM, reg|NBIT0); + if(ret != M2M_SUCCESS)goto _WAKE_EXIT; + } + + ret = nm_read_reg_with_ret(WAKE_CLK_REG, ®); + if(ret != M2M_SUCCESS) goto _WAKE_EXIT; + + /* Set bit 1 */ + if(!(reg & NBIT1)) + { + ret = nm_write_reg(WAKE_CLK_REG, reg | NBIT1); + if(ret != M2M_SUCCESS) goto _WAKE_EXIT; + } + + do + { + ret = nm_read_reg_with_ret(CLOCKS_EN_REG, &clk_status_reg); + if(ret != M2M_SUCCESS) { + M2M_ERR("Bus error (5).%d %" PRIx32 "\n", ret, clk_status_reg); + goto _WAKE_EXIT; + } + if (clk_status_reg & NBIT2) { + break; + } + nm_sleep(2); + trials++; + if (trials > WAKEUP_TRIALS) + { + M2M_ERR("Failed to wakeup the chip\n"); + ret = M2M_ERR_TIME_OUT; + goto _WAKE_EXIT; + } + } while(1); + /* Workaround sometimes spi fail to read clock regs after reading/writing clockless registers */ + //nm_bus_reset(); +_WAKE_EXIT: + return ret; +} + +int8_t chip_reset_and_cpu_halt(void) +{ + int8_t ret = M2M_SUCCESS; + uint32_t reg = 0; + + ret = chip_wake(); + if (ret != M2M_SUCCESS) { + return ret; + } + chip_reset(); + ret = nm_read_reg_with_ret(0x1118, ®); + if (M2M_SUCCESS != ret) { + ret = M2M_ERR_BUS_FAIL; + M2M_ERR("[nmi start]: fail read reg 0x1118 ...\r\n"); + } + reg |= (1 << 0); + ret = nm_write_reg(0x1118, reg); + ret += nm_read_reg_with_ret(NMI_GLB_RESET_0, ®); + if ((reg & (1ul << 10)) == (1ul << 10)) { + reg &= ~(1ul << 10); + ret += nm_write_reg(NMI_GLB_RESET_0, reg); + ret += nm_read_reg_with_ret(NMI_GLB_RESET_0, ®); + } + nm_write_reg(BOOTROM_REG, 0); + nm_write_reg(NMI_STATE_REG, 0); + nm_write_reg(NMI_REV_REG, 0); + nm_write_reg(NMI_PIN_MUX_0, 0x11111000); + return ret; +} +int8_t chip_reset(void) +{ + int8_t ret = M2M_SUCCESS; + + ret += nm_write_reg(NMI_GLB_RESET_0, 0); + nm_sleep(50); + return ret; +} + +int8_t wait_for_bootrom(uint8_t arg) +{ + int8_t ret = M2M_SUCCESS; + uint32_t reg = 0, cnt = 0; + uint32_t u32GpReg1 = 0; + + reg = 0; + while(1) { + reg = nm_read_reg(0x1014); /* wait for efuse loading done */ + if (reg & 0x80000000) { + break; + } + nm_sleep(1); /* Removing this can cause a bus error. */ + } + reg = nm_read_reg(M2M_WAIT_FOR_HOST_REG); + reg &= 0x1; + + /* check if waiting for the host will be skipped or not */ + if(reg == 0) + { + while(reg != M2M_FINISH_BOOT_ROM) + { + nm_sleep(1); + reg = nm_read_reg(BOOTROM_REG); + + if (++cnt > TIMEOUT) + { + M2M_DBG("failed to load firmware from flash.\r\n"); + ret = M2M_ERR_INIT; + goto ERR2; + } + } + } + + if (M2M_WIFI_MODE_CONFIG == arg) { + nm_write_reg(NMI_REV_REG, M2M_ATE_FW_START_VALUE); + } else if (M2M_WIFI_MODE_ETHERNET == arg) { + u32GpReg1 = rHAVE_ETHERNET_MODE_BIT; + } else { + /*bypass this step*/ + } + + if (REV(nmi_get_chipid()) == REV_3A0) + { + chip_apply_conf(u32GpReg1 | rHAVE_USE_PMU_BIT); + } + else + { + chip_apply_conf(u32GpReg1); + } + + nm_write_reg(BOOTROM_REG, M2M_START_FIRMWARE); + +#ifdef __ROM_TEST__ + rom_test(); +#endif /* __ROM_TEST__ */ + +ERR2: + return ret; +} + +int8_t wait_for_firmware_start(uint8_t arg) +{ + int8_t ret = M2M_SUCCESS; + uint32_t reg = 0, cnt = 0; + volatile uint32_t regAddress = NMI_STATE_REG; + volatile uint32_t checkValue = M2M_FINISH_INIT_STATE; + + if(M2M_WIFI_MODE_CONFIG == arg) { + regAddress = NMI_REV_REG; + checkValue = M2M_ATE_FW_START_VALUE;//M2M_ATE_FW_IS_UP_VALUE; + } else { + /*bypass this step*/ + } + + while (checkValue != reg) + { + nm_sleep(2); /* Removing this can cause a bus error. */ + M2M_DBG("%x %x %x\r\n", (unsigned int)nm_read_reg(0x108c), (unsigned int)nm_read_reg(0x108c), (unsigned int)nm_read_reg(0x14A0)); + if (nm_read_reg_with_ret(regAddress, ®) != M2M_SUCCESS) + { + // ensure reg != checkValue + reg = !checkValue; + } + if (++cnt > TIMEOUT) + { + M2M_DBG("Time out for wait firmware Run\r\n"); + ret = M2M_ERR_INIT; + goto ERR; + } + } + if (M2M_FINISH_INIT_STATE == checkValue) + { + nm_write_reg(NMI_STATE_REG, 0); + } +ERR: + return ret; +} + +int8_t chip_deinit(void) +{ + uint32_t reg = 0; + int8_t ret; + uint8_t timeout = 10; + + /** + stop the firmware, need a re-download + **/ + ret = nm_read_reg_with_ret(NMI_GLB_RESET_0, ®); + if (ret != M2M_SUCCESS) { + M2M_ERR("failed to de-initialize\r\n"); + } + reg &= ~(1 << 10); + ret = nm_write_reg(NMI_GLB_RESET_0, reg); + + if (ret != M2M_SUCCESS) { + M2M_ERR("Error while writing reg\r\n"); + return ret; + } + + do { + ret = nm_read_reg_with_ret(NMI_GLB_RESET_0, ®); + if (ret != M2M_SUCCESS) { + M2M_ERR("Error while reading reg\r\n"); + return ret; + } + /*Workaround to ensure that the chip is actually reset*/ + if ((reg & (1 << 10))) { + M2M_DBG("Bit 10 not reset retry %d\r\n", timeout); + reg &= ~(1 << 10); + ret = nm_write_reg(NMI_GLB_RESET_0, reg); + timeout--; + } else { + break; + } + } while (timeout); + + return ret; +} + +int8_t set_gpio_dir(uint8_t gpio, uint8_t dir) +{ + uint32_t val32; + int8_t ret; + + ret = nm_read_reg_with_ret(0x20108, &val32); + if(ret != M2M_SUCCESS) goto _EXIT; + + if(dir) { + val32 |= (1ul << gpio); + } else { + val32 &= ~(1ul << gpio); + } + + ret = nm_write_reg(0x20108, val32); + +_EXIT: + return ret; +} +int8_t set_gpio_val(uint8_t gpio, uint8_t val) +{ + uint32_t val32; + int8_t ret; + + ret = nm_read_reg_with_ret(0x20100, &val32); + if(ret != M2M_SUCCESS) goto _EXIT; + + if(val) { + val32 |= (1ul << gpio); + } else { + val32 &= ~(1ul << gpio); + } + + ret = nm_write_reg(0x20100, val32); + +_EXIT: + return ret; +} + +int8_t get_gpio_val(uint8_t gpio, uint8_t* val) +{ + uint32_t val32; + int8_t ret; + + ret = nm_read_reg_with_ret(0x20104, &val32); + if(ret != M2M_SUCCESS) goto _EXIT; + + *val = (uint8_t)((val32 >> gpio) & 0x01); + +_EXIT: + return ret; +} + +int8_t pullup_ctrl(uint32_t pinmask, uint8_t enable) +{ + int8_t s8Ret; + uint32_t val32; + s8Ret = nm_read_reg_with_ret(0x142c, &val32); + if (s8Ret != M2M_SUCCESS) { + M2M_ERR("[pullup_ctrl]: failed to read\r\n"); + goto _EXIT; + } + if (enable) { + val32 &= ~pinmask; + } else { + val32 |= pinmask; + } + s8Ret = nm_write_reg(0x142c, val32); + if (s8Ret != M2M_SUCCESS) { + M2M_ERR("[pullup_ctrl]: failed to write\r\n"); + goto _EXIT; + } +_EXIT: + return s8Ret; +} + +int8_t nmi_get_otp_mac_address(uint8_t *pu8MacAddr, uint8_t *pu8IsValid) +{ + int8_t ret; + uint32_t u32RegValue; + uint8_t mac[6]; + tstrGpRegs strgp = {0}; + + ret = nm_read_reg_with_ret(rNMI_GP_REG_0, &u32RegValue); + if (ret != M2M_SUCCESS) goto _EXIT_ERR; + + ret = nm_read_block(u32RegValue|0x30000,(uint8_t*)&strgp,sizeof(tstrGpRegs)); + if (ret != M2M_SUCCESS) goto _EXIT_ERR; + u32RegValue = strgp.u32Mac_efuse_mib; + + if (!EFUSED_MAC(u32RegValue)) { + M2M_DBG("Default MAC\r\n"); + memset(pu8MacAddr, 0, 6); + goto _EXIT_ERR; + } + + M2M_DBG("OTP MAC\r\n"); + u32RegValue >>=16; + ret = nm_read_block(u32RegValue|0x30000, mac, 6); + memcpy(pu8MacAddr, mac, 6); + if (pu8IsValid) *pu8IsValid = 1; + return ret; + +_EXIT_ERR: + if (pu8IsValid) *pu8IsValid = 0; + return ret; +} + +int8_t nmi_get_mac_address(uint8_t *pu8MacAddr) +{ + int8_t ret; + uint32_t u32RegValue; + uint8_t mac[6]; + tstrGpRegs strgp = {0}; + + ret = nm_read_reg_with_ret(rNMI_GP_REG_0, &u32RegValue); + if (ret != M2M_SUCCESS) goto _EXIT_ERR; + + ret = nm_read_block(u32RegValue|0x30000, (uint8_t*)&strgp, sizeof(tstrGpRegs)); + if (ret != M2M_SUCCESS) goto _EXIT_ERR; + u32RegValue = strgp.u32Mac_efuse_mib; + + u32RegValue &=0x0000ffff; + ret = nm_read_block(u32RegValue|0x30000, mac, 6); + memcpy(pu8MacAddr, mac, 6); + +_EXIT_ERR: + return ret; +} + +//DOM-IGNORE-END diff --git a/ChaletLora.X/Source/winc3400_142/driver/source/nmbus.c b/ChaletLora.X/Source/winc3400_142/driver/source/nmbus.c new file mode 100644 index 0000000..60bcada --- /dev/null +++ b/ChaletLora.X/Source/winc3400_142/driver/source/nmbus.c @@ -0,0 +1,248 @@ +/******************************************************************************* + This module contains WINC3400 bus APIs implementation. + + File Name: + nmbus.c + + Summary: + This module contains WINC3400 bus APIs implementation. + + Description: + This module contains WINC3400 bus APIs implementation. +*******************************************************************************/ + +//DOM-IGNORE-BEGIN +/******************************************************************************* +* Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries. +* +* Subject to your compliance with these terms, you may use Microchip software +* and any derivatives exclusively with Microchip products. It is your +* responsibility to comply with third party license terms applicable to your +* use of third party software (including open source software) that may +* accompany Microchip software. +* +* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER +* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED +* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A +* PARTICULAR PURPOSE. +* +* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, +* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND +* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS +* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE +* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN +* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, +* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. +*******************************************************************************/ +#include "nmbus.h" +#include "nmspi.h" + +#define MAX_TRX_CFG_SZ 8 +#define NM_BUS_MAX_TRX_SZ 256 + +/** +* @struct tstrNmBusCapabilities +* @brief Structure holding bus capabilities information +* @sa NM_BUS_TYPE_I2C, NM_BUS_TYPE_SPI +*/ +typedef struct +{ + uint16_t u16MaxTrxSz; /*!< Maximum transfer size. Must be >= 16 bytes*/ +} tstrNmBusCapabilities; + +tstrNmBusCapabilities egstrNmBusCapabilities = +{ + NM_BUS_MAX_TRX_SZ +}; + +/* +* @fn nm_bus_init +* @brief Initialize the bus wrapper +* @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure +*/ +static int8_t nm_bus_init(void *pvinit) +{ + nm_reset(); + nm_sleep(1); + return M2M_SUCCESS; +} + +/* +* @fn nm_bus_deinit +* @brief De-initialize the bus wrapper +*/ +static int8_t nm_bus_deinit(void) +{ + return M2M_SUCCESS; +} + +/** + * @fn nm_bus_iface_init + * @brief Initialize bus interface + * @return @ref M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure + */ +int8_t nm_bus_iface_init(void *pvInitVal) +{ + int8_t ret = M2M_SUCCESS; + ret = nm_bus_init(pvInitVal); + + return ret; +} + +/** + * @fn nm_bus_iface_deinit + * @brief Deinitialize bus interface + * @return @ref M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure + */ +int8_t nm_bus_iface_deinit(void) +{ + int8_t ret = M2M_SUCCESS; + ret = nm_bus_deinit(); + + return ret; +} + +/** + * @fn nm_bus_reset + * @brief reset bus interface + * @return @ref M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure + * @version 1.0 + */ +int8_t nm_bus_reset(void) +{ + return nm_spi_reset(); +} + +/** + * @fn nm_bus_iface_reconfigure + * @brief reconfigure bus interface + * @return @ref M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure + */ +int8_t nm_bus_iface_reconfigure(void *ptr) +{ + int8_t ret = M2M_SUCCESS; + return ret; +} +/* + * @fn nm_read_reg + * @brief Read register + * @param[in] u32Addr + * Register address + * @return Register value + */ +uint32_t nm_read_reg(uint32_t u32Addr) +{ + return nm_spi_read_reg(u32Addr); +} + +/* + * @fn nm_read_reg_with_ret + * @brief Read register with error code return + * @param[in] u32Addr + * Register address + * @param[out] pu32RetVal + * Pointer to u32 variable used to return the read value + * @return @ref M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure +*/ +int8_t nm_read_reg_with_ret(uint32_t u32Addr, uint32_t* pu32RetVal) +{ + return nm_spi_read_reg_with_ret(u32Addr, pu32RetVal); +} + +/* + * @fn nm_write_reg + * @brief write register + * @param[in] u32Addr + * Register address + * @param[in] u32Val + * Value to be written to the register + * @return @ref M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure + */ +int8_t nm_write_reg(uint32_t u32Addr, uint32_t u32Val) +{ + return nm_spi_write_reg(u32Addr, u32Val); +} + +static inline int8_t p_nm_read_block(uint32_t u32Addr, uint8_t *puBuf, uint16_t u16Sz) +{ + return nm_spi_read_block(u32Addr, puBuf, u16Sz); +} +/* + * @fn nm_read_block + * @brief Read block of data + * @param[in] u32Addr + * Start address + * @param[out] puBuf + * Pointer to a buffer used to return the read data + * @param[in] u32Sz + * Number of bytes to read. The buffer size must be >= u32Sz + * @return @ref M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure + */ +int8_t nm_read_block(uint32_t u32Addr, uint8_t *puBuf, uint32_t u32Sz) +{ + uint16_t u16MaxTrxSz = egstrNmBusCapabilities.u16MaxTrxSz - MAX_TRX_CFG_SZ; + uint32_t off = 0; + int8_t s8Ret = M2M_SUCCESS; + + for(;;) + { + if(u32Sz <= u16MaxTrxSz) + { + s8Ret += p_nm_read_block(u32Addr, &puBuf[off], (uint16_t)u32Sz); + break; + } + else + { + s8Ret += p_nm_read_block(u32Addr, &puBuf[off], u16MaxTrxSz); + if(M2M_SUCCESS != s8Ret) break; + u32Sz -= u16MaxTrxSz; + off += u16MaxTrxSz; + u32Addr += u16MaxTrxSz; + } + } + + return s8Ret; +} + +static inline int8_t p_nm_write_block(uint32_t u32Addr, uint8_t *puBuf, uint16_t u16Sz) +{ + return nm_spi_write_block(u32Addr, puBuf, u16Sz); +} +/** + * @fn nm_write_block + * @brief Write block of data + * @param[in] u32Addr + * Start address + * @param[in] puBuf + * Pointer to the buffer holding the data to be written + * @param[in] u32Sz + * Number of bytes to write. The buffer size must be >= u32Sz + * @return @ref M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure + */ +int8_t nm_write_block(uint32_t u32Addr, uint8_t *puBuf, uint32_t u32Sz) +{ + uint16_t u16MaxTrxSz = egstrNmBusCapabilities.u16MaxTrxSz - MAX_TRX_CFG_SZ; + uint32_t off = 0; + int8_t s8Ret = M2M_SUCCESS; + + for(;;) + { + if(u32Sz <= u16MaxTrxSz) + { + s8Ret += p_nm_write_block(u32Addr, &puBuf[off], (uint16_t)u32Sz); + break; + } + else + { + s8Ret += p_nm_write_block(u32Addr, &puBuf[off], u16MaxTrxSz); + if(M2M_SUCCESS != s8Ret) break; + u32Sz -= u16MaxTrxSz; + off += u16MaxTrxSz; + u32Addr += u16MaxTrxSz; + } + } + + return s8Ret; +} + +//DOM-IGNORE-END diff --git a/ChaletLora.X/Source/winc3400_142/driver/source/nmdrv.c b/ChaletLora.X/Source/winc3400_142/driver/source/nmdrv.c new file mode 100644 index 0000000..421638a --- /dev/null +++ b/ChaletLora.X/Source/winc3400_142/driver/source/nmdrv.c @@ -0,0 +1,410 @@ +/******************************************************************************* + File Name: + nmdrv.c + + Summary: + This module contains WINC3400 M2M driver APIs implementation. + + Description: + This module contains WINC3400 M2M driver APIs implementation. + *******************************************************************************/ + +//DOM-IGNORE-BEGIN +/******************************************************************************* +* Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries. +* +* Subject to your compliance with these terms, you may use Microchip software +* and any derivatives exclusively with Microchip products. It is your +* responsibility to comply with third party license terms applicable to your +* use of third party software (including open source software) that may +* accompany Microchip software. +* +* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER +* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED +* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A +* PARTICULAR PURPOSE. +* +* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, +* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND +* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS +* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE +* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN +* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, +* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. +*******************************************************************************/ + +#include "nm_common.h" +#include "nmbus.h" +#include "nm_bsp.h" +#include "nmdrv.h" +#include "nmasic.h" +#include "m2m_types.h" + +#include "nmspi.h" + +static tenuNmState genuNmState = NM_STATE_DEINIT; +/** + * @fn nm_get_hif_info(uint16_t *pu16FwHifInfo, uint16_t *pu16OtaHifInfo); + * @brief Get Hif info of images in both partitions (Firmware and Ota). + * @param[out] pu16FwHifInfo + * Pointer holding Hif info of image in the active partition. + * @param[out] pu16OtaHifInfo + * Pointer holding Hif info of image in the inactive partition. + * @return @ref M2M_SUCCESS in case of success and Negative error code in case of failure + */ +int8_t nm_get_hif_info(uint16_t *pu16FwHifInfo, uint16_t *pu16OtaHifInfo) +{ + int8_t ret = M2M_SUCCESS; + uint32_t reg = 0; + + ret = nm_read_reg_with_ret(NMI_REV_REG, ®); + if(ret == M2M_SUCCESS) + { + if(pu16FwHifInfo != NULL) + { + *pu16FwHifInfo = (uint16_t)reg; + } + if(pu16OtaHifInfo) + { + *pu16OtaHifInfo = (uint16_t)(reg>>16); + } + } + return ret; +} + +/** + * @fn nm_get_firmware_full_info(tstrM2mRev* M2mRev) + * @brief Get Firmware version info + * @param[out] M2mRev + * Pointer holds address of structure @ref tstrM2mRev that contains the firmware version parameters + */ +int8_t nm_get_firmware_full_info(tstrM2mRev *pstrRev) +{ + uint16_t fw_hif_info = 0; + uint32_t reg = 0; + int8_t ret = M2M_SUCCESS; + tstrGpRegs strgp = {0}; + + memset((uint8_t*)pstrRev, 0, sizeof(tstrM2mRev)); + nm_get_hif_info(&fw_hif_info, NULL); + + M2M_INFO("Fw HIF: %04x\r\n", fw_hif_info); + if(M2M_GET_HIF_BLOCK(fw_hif_info) == M2M_HIF_BLOCK_VALUE) + { + ret = nm_read_reg_with_ret(rNMI_GP_REG_0, ®); + if(ret == M2M_SUCCESS) + { + if(reg != 0) + { + ret = nm_read_block(reg|0x30000, (uint8_t*)&strgp, sizeof(tstrGpRegs)); + if(ret == M2M_SUCCESS) + { + reg = strgp.u32Firmware_Ota_rev; + reg &= 0x0000ffff; + if(reg != 0) + { + ret = nm_read_block(reg|0x30000, (uint8_t*)pstrRev, sizeof(tstrM2mRev)); + if(ret == M2M_SUCCESS) + { + M2M_INFO("Firmware HIF (%u) : %u.%u\r\n", M2M_GET_HIF_BLOCK(pstrRev->u16FirmwareHifInfo), M2M_GET_HIF_MAJOR(pstrRev->u16FirmwareHifInfo), M2M_GET_HIF_MINOR(pstrRev->u16FirmwareHifInfo)); + M2M_INFO("Firmware ver : %u.%u.%u\r\n", pstrRev->u8FirmwareMajor, pstrRev->u8FirmwareMinor, pstrRev->u8FirmwarePatch); + M2M_INFO("Firmware Build %s Time %s\r\n", pstrRev->BuildDate, pstrRev->BuildTime); + + /* Check Hif info is consistent */ + if(fw_hif_info != pstrRev->u16FirmwareHifInfo) + { + ret = M2M_ERR_FAIL; + M2M_ERR("Inconsistent Firmware Version\r\n"); + } + } + } + else + { + ret = M2M_ERR_FAIL; + } + } + } + else + { + ret = M2M_ERR_FAIL; + } + } + } + else + { + ret = M2M_ERR_FAIL; + } + if(ret != M2M_SUCCESS) + { + M2M_ERR("Unknown Firmware Version\r\n"); + } + return ret; +} +/** + * @fn nm_get_ota_firmware_info(tstrM2mRev* pstrRev) + * @brief Get Firmware version info + * @param[out] M2mRev + * Pointer holds address of structure @ref tstrM2mRev that contains the firmware version parameters + */ +int8_t nm_get_ota_firmware_info(tstrM2mRev *pstrRev) +{ + uint16_t ota_hif_info = 0; + uint32_t reg = 0; + int8_t ret = M2M_SUCCESS; + tstrGpRegs strgp = {0}; + + memset((uint8_t*)pstrRev, 0, sizeof(tstrM2mRev)); + nm_get_hif_info(NULL, &ota_hif_info); + + M2M_INFO("OTA HIF: %04x\r\n", ota_hif_info); + if(M2M_GET_HIF_BLOCK(ota_hif_info) == M2M_HIF_BLOCK_VALUE) + { + ret = nm_read_reg_with_ret(rNMI_GP_REG_0, ®); + if(ret == M2M_SUCCESS) + { + if(reg != 0) + { + ret = nm_read_block(reg|0x30000, (uint8_t*)&strgp, sizeof(tstrGpRegs)); + if(ret == M2M_SUCCESS) + { + reg = strgp.u32Firmware_Ota_rev; + reg >>= 16; + if(reg != 0) + { + ret = nm_read_block(reg|0x30000, (uint8_t*)pstrRev, sizeof(tstrM2mRev)); + if(ret == M2M_SUCCESS) + { + M2M_INFO("OTA HIF (%u) : %u.%u\r\n", M2M_GET_HIF_BLOCK(pstrRev->u16FirmwareHifInfo), M2M_GET_HIF_MAJOR(pstrRev->u16FirmwareHifInfo), M2M_GET_HIF_MINOR(pstrRev->u16FirmwareHifInfo)); + M2M_INFO("OTA ver : %u.%u.%u\r\n", pstrRev->u8FirmwareMajor, pstrRev->u8FirmwareMinor, pstrRev->u8FirmwarePatch); + M2M_INFO("OTA Build %s Time %s\r\n", pstrRev->BuildDate, pstrRev->BuildTime); + + /* Check Hif info is consistent */ + if(ota_hif_info != pstrRev->u16FirmwareHifInfo) + { + ret = M2M_ERR_FAIL; + M2M_ERR("Inconsistent OTA Version\r\n"); + } + } + } + else + { + ret = M2M_ERR_FAIL; + } + } + } + else + { + ret = M2M_ERR_FAIL; + } + } + } + else + { + ret = M2M_ERR_FAIL; + } + if(ret != M2M_SUCCESS) + { + M2M_INFO("No valid OTA image\r\n"); + } + return ret; +} + +/** + * @fn nm_drv_init_download_mode + * @brief Initialize NMC1000 driver in download mode + * @return @ref M2M_SUCCESS in case of success and Negative error code in case of failure + */ +int8_t nm_drv_init_download_mode(void) +{ + int8_t ret = M2M_SUCCESS; + + ret = nm_bus_iface_init(NULL); + if (M2M_SUCCESS != ret) { + M2M_ERR("[nmi start]: fail init bus\r\n"); + goto ERR1; + } + + nm_spi_lock_init(); + + /* Must do this after global reset to set SPI data packet size. */ + nm_spi_init(); + + M2M_INFO("Chip ID %lx\r\n", nmi_get_chipid()); + + /*disable all interrupt in ROM (to disable uart) in 2b0 chip*/ + nm_write_reg(0x20300, 0); + + genuNmState = NM_STATE_INIT; +ERR1: + return ret; +} + +int8_t nm_drv_init_hold(void) +{ + int8_t ret = M2M_SUCCESS; + + nm_spi_lock_init(); + + ret = nm_bus_iface_init(NULL); + if (M2M_SUCCESS != ret) { + M2M_ERR("[nmi start]: fail init bus\r\n"); + goto ERR1; + } + +#ifdef BUS_ONLY + return; +#endif + +#ifdef NO_HW_CHIP_EN + ret = chip_wake(); + nm_sleep(10); + if (M2M_SUCCESS != ret) { + M2M_ERR("[nmi start]: fail chip_wakeup\r\n"); + goto ERR2; + } + + /** + Go... + **/ + ret = chip_reset(); + if (M2M_SUCCESS != ret) { + goto ERR2; + } +#endif + M2M_INFO("Chip ID %lx\r\n", nmi_get_chipid()); + /* Must do this after global reset to set SPI data packet size. */ + nm_spi_init(); + /*return power save to default value*/ + chip_idle(); + + genuNmState = NM_STATE_INIT; + + return ret; +#ifdef NO_HW_CHIP_EN +ERR2: + nm_bus_iface_deinit(); +#endif +ERR1: + return ret; +} + +int8_t nm_drv_init_start(void * arg) +{ + int8_t ret = M2M_SUCCESS; + uint8_t u8Mode = M2M_WIFI_MODE_NORMAL; + + if(NULL != arg) + { + u8Mode = *((uint8_t *)arg); + if(u8Mode < M2M_WIFI_MODE_NORMAL || u8Mode >= M2M_WIFI_MODE_MAX) + u8Mode = M2M_WIFI_MODE_NORMAL; + } + + ret = cpu_start(); + if (M2M_SUCCESS != ret) { + goto ERR2; + } + ret = wait_for_bootrom(u8Mode); + if (M2M_SUCCESS != ret) { + goto ERR2; + } + + ret = wait_for_firmware_start(u8Mode); + if (M2M_SUCCESS != ret) { + goto ERR2; + } + + if(M2M_WIFI_MODE_CONFIG == u8Mode) { + goto ERR1; + } else { + /*continue running*/ + } + + ret = enable_interrupts(); + if (M2M_SUCCESS != ret) { + M2M_ERR("failed to enable interrupts..\r\n"); + goto ERR2; + } + + genuNmState = NM_STATE_START; + + return ret; +ERR2: + nm_bus_iface_deinit(); + nm_spi_deinit(); +ERR1: + return ret; +} + +/** + * @fn nm_drv_init + * @brief Initialize NMC1000 driver + * @param[in] arg + * Generic argument passed on to nm_drv_init_start + * @return @ref M2M_SUCCESS in case of success and Negative error code in case of failure + */ +int8_t nm_drv_init(void* arg) +{ + int8_t ret = M2M_SUCCESS; + + ret = nm_drv_init_hold(); + + if(ret == M2M_SUCCESS) + ret = nm_drv_init_start(arg); + + return ret; +} + +/** + * @fn nm_drv_deinit + * @brief Deinitialize NMC1000 driver + */ +int8_t nm_drv_deinit(void* arg) +{ + int8_t ret; + + ret = chip_deinit(); + if (M2M_SUCCESS != ret) { + M2M_ERR("[nmi stop]: chip_deinit fail\r\n"); + goto ERR1; + } + + ret = nm_bus_iface_deinit(); + if (M2M_SUCCESS != ret) { + M2M_ERR("[nmi stop]: fail init bus\r\n"); + goto ERR1; + } + /* Must do this after global reset to set SPI data packet size. */ + nm_spi_deinit(); + +ERR1: + genuNmState = NM_STATE_DEINIT; + + return ret; +} + + +/** + * @fn nm_cpu_start(void) + * @brief Start CPU from the WINC module + * @return @ref M2M_SUCCESS in case of success and Negative error code in case of failure + */ +int8_t nm_cpu_start(void) +{ + int8_t ret; + + ret = cpu_start(); + return ret; +} + +/** + * @fn nm_get_state(void) + * @brief Get the current state of the WINC module + * @return The current state of the WINC module + */ +tenuNmState nm_get_state(void) +{ + return genuNmState; +} + +//DOM-IGNORE-END \ No newline at end of file diff --git a/ChaletLora.X/Source/winc3400_142/driver/source/nmspi.c b/ChaletLora.X/Source/winc3400_142/driver/source/nmspi.c new file mode 100644 index 0000000..2a7de63 --- /dev/null +++ b/ChaletLora.X/Source/winc3400_142/driver/source/nmspi.c @@ -0,0 +1,960 @@ +/******************************************************************************* + This module contains WINC3400 SPI protocol bus APIs implementation. + + File Name: + nmspi.c + + Summary: + This module contains WINC3400 SPI protocol bus APIs implementation. + + Description: + This module contains WINC3400 SPI protocol bus APIs implementation. + *******************************************************************************/ + +//DOM-IGNORE-BEGIN +/******************************************************************************* +* Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries. +* +* Subject to your compliance with these terms, you may use Microchip software +* and any derivatives exclusively with Microchip products. It is your +* responsibility to comply with third party license terms applicable to your +* use of third party software (including open source software) that may +* accompany Microchip software. +* +* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER +* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED +* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A +* PARTICULAR PURPOSE. +* +* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, +* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND +* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS +* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE +* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN +* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, +* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. +*******************************************************************************/ + +//#include "osal/osal.h" +#include "nm_common.h" + +#include "nmspi.h" +//#include "wdrv_winc_common.h" +//#include "wdrv_winc_spi.h" + +#define NMI_PERIPH_REG_BASE 0x1000 +#define NMI_INTR_REG_BASE (NMI_PERIPH_REG_BASE+0xa00) +#define NMI_CHIPID (NMI_PERIPH_REG_BASE) +#define NMI_PIN_MUX_0 (NMI_PERIPH_REG_BASE + 0x408) +#define NMI_INTR_ENABLE (NMI_INTR_REG_BASE) + +#define NMI_SPI_REG_BASE 0xe800 +#define NMI_SPI_CTL (NMI_SPI_REG_BASE) +#define NMI_SPI_MASTER_DMA_ADDR (NMI_SPI_REG_BASE+0x4) +#define NMI_SPI_MASTER_DMA_COUNT (NMI_SPI_REG_BASE+0x8) +#define NMI_SPI_SLAVE_DMA_ADDR (NMI_SPI_REG_BASE+0xc) +#define NMI_SPI_SLAVE_DMA_COUNT (NMI_SPI_REG_BASE+0x10) +#define NMI_SPI_TX_MODE (NMI_SPI_REG_BASE+0x20) +#define NMI_SPI_PROTOCOL_CONFIG (NMI_SPI_REG_BASE+0x24) +#define NMI_SPI_INTR_CTL (NMI_SPI_REG_BASE+0x2c) +#define NMI_SPI_MISC_CTRL (NMI_SPI_REG_BASE+0x48) + +#define NMI_SPI_PROTOCOL_OFFSET (NMI_SPI_PROTOCOL_CONFIG-NMI_SPI_REG_BASE) + +#define SPI_BASE NMI_SPI_REG_BASE + +//#define CMD_DMA_WRITE 0xc1 +//#define CMD_DMA_READ 0xc2 +#define CMD_INTERNAL_WRITE 0xc3 +#define CMD_INTERNAL_READ 0xc4 +//#define CMD_TERMINATE 0xc5 +//#define CMD_REPEAT 0xc6 +#define CMD_DMA_EXT_WRITE 0xc7 +#define CMD_DMA_EXT_READ 0xc8 +#define CMD_SINGLE_WRITE 0xc9 +#define CMD_SINGLE_READ 0xca +#define CMD_RESET 0xcf + +#define N_OK 0 +#define N_FAIL -1 +#define N_RESET -2 +#define N_RETRY -3 + +#define SPI_RESP_RETRY_COUNT (10) +#define SPI_RETRY_COUNT (10) +#define DATA_PKT_SZ_256 256 +#define DATA_PKT_SZ_512 512 +#define DATA_PKT_SZ_1K 1024 +#define DATA_PKT_SZ_4K (4 * 1024) +#define DATA_PKT_SZ_8K (8 * 1024) +#define DATA_PKT_SZ DATA_PKT_SZ_8K + +static uint8_t gu8Crc_off = 0; + +//static OSAL_MUTEX_HANDLE_TYPE s_spiLock = 0; + +static inline int8_t spi_read(uint8_t *b, uint16_t sz) +{ + //JFM WINC TODO + // if (true == WDRV_WINC_SPIReceive((unsigned char *const) b, sz)) + return N_OK; + + return N_FAIL; +} + +static inline int8_t spi_write(uint8_t *b, uint16_t sz) +{ + //JFM WINC TODO + // if (true == WDRV_WINC_SPISend((unsigned char *const) b, sz)) + return N_OK; + + return N_FAIL; +} + +/******************************************** + + Crc7 + +********************************************/ + +static const uint8_t crc7_syndrome_table[256] = { + 0x00, 0x09, 0x12, 0x1b, 0x24, 0x2d, 0x36, 0x3f, + 0x48, 0x41, 0x5a, 0x53, 0x6c, 0x65, 0x7e, 0x77, + 0x19, 0x10, 0x0b, 0x02, 0x3d, 0x34, 0x2f, 0x26, + 0x51, 0x58, 0x43, 0x4a, 0x75, 0x7c, 0x67, 0x6e, + 0x32, 0x3b, 0x20, 0x29, 0x16, 0x1f, 0x04, 0x0d, + 0x7a, 0x73, 0x68, 0x61, 0x5e, 0x57, 0x4c, 0x45, + 0x2b, 0x22, 0x39, 0x30, 0x0f, 0x06, 0x1d, 0x14, + 0x63, 0x6a, 0x71, 0x78, 0x47, 0x4e, 0x55, 0x5c, + 0x64, 0x6d, 0x76, 0x7f, 0x40, 0x49, 0x52, 0x5b, + 0x2c, 0x25, 0x3e, 0x37, 0x08, 0x01, 0x1a, 0x13, + 0x7d, 0x74, 0x6f, 0x66, 0x59, 0x50, 0x4b, 0x42, + 0x35, 0x3c, 0x27, 0x2e, 0x11, 0x18, 0x03, 0x0a, + 0x56, 0x5f, 0x44, 0x4d, 0x72, 0x7b, 0x60, 0x69, + 0x1e, 0x17, 0x0c, 0x05, 0x3a, 0x33, 0x28, 0x21, + 0x4f, 0x46, 0x5d, 0x54, 0x6b, 0x62, 0x79, 0x70, + 0x07, 0x0e, 0x15, 0x1c, 0x23, 0x2a, 0x31, 0x38, + 0x41, 0x48, 0x53, 0x5a, 0x65, 0x6c, 0x77, 0x7e, + 0x09, 0x00, 0x1b, 0x12, 0x2d, 0x24, 0x3f, 0x36, + 0x58, 0x51, 0x4a, 0x43, 0x7c, 0x75, 0x6e, 0x67, + 0x10, 0x19, 0x02, 0x0b, 0x34, 0x3d, 0x26, 0x2f, + 0x73, 0x7a, 0x61, 0x68, 0x57, 0x5e, 0x45, 0x4c, + 0x3b, 0x32, 0x29, 0x20, 0x1f, 0x16, 0x0d, 0x04, + 0x6a, 0x63, 0x78, 0x71, 0x4e, 0x47, 0x5c, 0x55, + 0x22, 0x2b, 0x30, 0x39, 0x06, 0x0f, 0x14, 0x1d, + 0x25, 0x2c, 0x37, 0x3e, 0x01, 0x08, 0x13, 0x1a, + 0x6d, 0x64, 0x7f, 0x76, 0x49, 0x40, 0x5b, 0x52, + 0x3c, 0x35, 0x2e, 0x27, 0x18, 0x11, 0x0a, 0x03, + 0x74, 0x7d, 0x66, 0x6f, 0x50, 0x59, 0x42, 0x4b, + 0x17, 0x1e, 0x05, 0x0c, 0x33, 0x3a, 0x21, 0x28, + 0x5f, 0x56, 0x4d, 0x44, 0x7b, 0x72, 0x69, 0x60, + 0x0e, 0x07, 0x1c, 0x15, 0x2a, 0x23, 0x38, 0x31, + 0x46, 0x4f, 0x54, 0x5d, 0x62, 0x6b, 0x70, 0x79 +}; + +static uint8_t crc7_byte(uint8_t crc, uint8_t data) +{ + return crc7_syndrome_table[(crc << 1) ^ data]; +} + +static uint8_t crc7(uint8_t crc, const uint8_t *buffer, uint32_t len) +{ + while (len--) + crc = crc7_byte(crc, *buffer++); + return crc; +} + +/******************************************** + + Spi protocol Function + +********************************************/ + +static int8_t spi_cmd(uint8_t cmd, uint32_t adr, uint32_t u32data, uint32_t sz, uint8_t clockless) +{ + uint8_t bc[9]; + uint8_t len = 5; + + bc[0] = cmd; + switch (cmd) + { + case CMD_SINGLE_READ: /* single word (4 bytes) read */ + bc[1] = (uint8_t)(adr >> 16); + bc[2] = (uint8_t)(adr >> 8); + bc[3] = (uint8_t)adr; + len = 5; + break; + + case CMD_INTERNAL_READ: /* internal register read */ + bc[1] = (uint8_t)(adr >> 8); + if(clockless) + bc[1] |= (1 << 7); + bc[2] = (uint8_t)adr; + bc[3] = 0x00; + len = 5; + break; +#ifdef CMD_TERMINATE + case CMD_TERMINATE: /* termination */ + bc[1] = 0x00; + bc[2] = 0x00; + bc[3] = 0x00; + len = 5; + break; +#endif +#ifdef CMD_REPEAT + case CMD_REPEAT: /* repeat */ + bc[1] = 0x00; + bc[2] = 0x00; + bc[3] = 0x00; + len = 5; + break; +#endif + case CMD_RESET: /* reset */ + bc[1] = 0xff; + bc[2] = 0xff; + bc[3] = 0xff; + len = 5; + break; +#if defined(CMD_DMA_WRITE) || defined(CMD_DMA_READ) + case CMD_DMA_WRITE: /* dma write */ + case CMD_DMA_READ: /* dma read */ + bc[1] = (uint8_t)(adr >> 16); + bc[2] = (uint8_t)(adr >> 8); + bc[3] = (uint8_t)adr; + bc[4] = (uint8_t)(sz >> 8); + bc[5] = (uint8_t)(sz); + len = 7; + break; +#endif + case CMD_DMA_EXT_WRITE: /* dma extended write */ + case CMD_DMA_EXT_READ: /* dma extended read */ + bc[1] = (uint8_t)(adr >> 16); + bc[2] = (uint8_t)(adr >> 8); + bc[3] = (uint8_t)adr; + bc[4] = (uint8_t)(sz >> 16); + bc[5] = (uint8_t)(sz >> 8); + bc[6] = (uint8_t)(sz); + len = 8; + break; + + case CMD_INTERNAL_WRITE: /* internal register write */ + bc[1] = (uint8_t)(adr >> 8); + if(clockless) bc[1] |= (1 << 7); + bc[2] = (uint8_t)(adr); + bc[3] = (uint8_t)(u32data >> 24); + bc[4] = (uint8_t)(u32data >> 16); + bc[5] = (uint8_t)(u32data >> 8); + bc[6] = (uint8_t)(u32data); + len = 8; + break; + + case CMD_SINGLE_WRITE: /* single word write */ + bc[1] = (uint8_t)(adr >> 16); + bc[2] = (uint8_t)(adr >> 8); + bc[3] = (uint8_t)(adr); + bc[4] = (uint8_t)(u32data >> 24); + bc[5] = (uint8_t)(u32data >> 16); + bc[6] = (uint8_t)(u32data >> 8); + bc[7] = (uint8_t)(u32data); + len = 9; + break; + + default: + return N_FAIL; + } + + if (!gu8Crc_off) + { + bc[len-1] = (crc7(0x7f, (const uint8_t *)&bc[0], len-1)) << 1; + } + else + { + len -= 1; + } + + if (N_OK != spi_write(bc, len)) + { + M2M_ERR("[spi_cmd]: Failed cmd write, bus error...\r\n"); + return N_FAIL; + } + + return N_OK; +} + +static int8_t spi_cmd_rsp(uint8_t cmd) +{ + uint8_t rsp; + int8_t s8RetryCnt; + + /** + Command/Control response + **/ + if ((cmd == CMD_RESET) +#ifdef CMD_TERMINATE + || (cmd == CMD_TERMINATE) +#endif +#ifdef CMD_REPEAT + || (cmd == CMD_REPEAT) +#endif + ) + { + if (N_OK != spi_read(&rsp, 1)) + return N_FAIL; + } + + /* wait for response */ + s8RetryCnt = SPI_RESP_RETRY_COUNT; + do + { + if (N_OK != spi_read(&rsp, 1)) + { + M2M_ERR("[spi_cmd_rsp]: Failed cmd response read, bus error...\r\n"); + return N_FAIL; + } + } + while((rsp != cmd) && (s8RetryCnt-- > 0)); + + if (s8RetryCnt < 0) + { + M2M_ERR("[spi_cmd_rsp]: Failed cmd response read\n"); + return N_FAIL; + } + /** + State response + **/ + /* wait for response */ + s8RetryCnt = SPI_RESP_RETRY_COUNT; + do + { + if (N_OK != spi_read(&rsp, 1)) + { + M2M_ERR("[spi_cmd_rsp]: Failed cmd response read, bus error...\r\n"); + return N_FAIL; + } + } + while((rsp != 0x00) && (s8RetryCnt-- > 0)); + + if (s8RetryCnt < 0) + { + M2M_ERR("[spi_cmd_rsp]: Failed cmd response read\n"); + return N_FAIL; + } + + return N_OK; +} + +static void spi_reset(void) +{ + nm_sleep(1); + spi_cmd(CMD_RESET, 0, 0, 0, 0); + spi_cmd_rsp(CMD_RESET); + nm_sleep(1); +} + +/******************************************** + + Spi Internal Read/Write Function + +********************************************/ + +static int8_t spi_data_read(uint8_t *b, uint16_t sz,uint8_t clockless) +{ + int16_t retry, ix, nbytes; + int8_t result = N_OK; + uint8_t crc[2]; + uint8_t rsp; + + /** + Data + **/ + ix = 0; + do { + if (sz <= DATA_PKT_SZ) + nbytes = sz; + else + nbytes = DATA_PKT_SZ; + + /** + Data Response header + **/ + retry = SPI_RESP_RETRY_COUNT; + do + { + if (N_OK != spi_read(&rsp, 1)) + { + M2M_ERR("[spi_data_read]: Failed data response read, bus error...\r\n"); + result = N_FAIL; + break; + } + if ((rsp & 0xf0) == 0xf0) + break; + } + while (retry--); + + if (result == N_FAIL) + break; + + if (retry <= 0) + { + M2M_ERR("[spi_data_read]: Failed data response read...(%02x)\r\n", rsp); + result = N_FAIL; + break; + } + + /** + Read bytes + **/ + if (N_OK != spi_read(&b[ix], nbytes)) + { + M2M_ERR("[spi_data_read]: Failed data block read, bus error...\r\n"); + result = N_FAIL; + break; + } + if(!clockless) + { + /** + Read Crc + **/ + if (!gu8Crc_off) + { + if (N_OK != spi_read(crc, 2)) + { + M2M_ERR("[spi_data_read]: Failed data block CRC read, bus error...\r\n"); + result = N_FAIL; + break; + } + } + } + ix += nbytes; + sz -= nbytes; + + } while (sz); + + return result; +} + +static int8_t spi_data_write(uint8_t *b, uint16_t sz) +{ + int16_t ix = 0; + uint16_t nbytes; + int8_t result = N_OK; + uint8_t cmd, order, crc[2] = {0}; + //uint8_t rsp; + + /** + Data + **/ + do + { + if (sz <= DATA_PKT_SZ) + nbytes = sz; + else + nbytes = DATA_PKT_SZ; + + /** + Write command + **/ + cmd = 0xf0; + if (ix == 0) + { + if (sz <= DATA_PKT_SZ) + order = 0x3; + else + order = 0x1; + } + else + { + if (sz <= DATA_PKT_SZ) + order = 0x3; + else + order = 0x2; + } + + cmd |= order; + if (N_OK != spi_write(&cmd, 1)) + { + M2M_ERR("[spi_data_write]: Failed data block cmd write, bus error...\r\n"); + result = N_FAIL; + break; + } + + /** + Write data + **/ + if (N_OK != spi_write(&b[ix], nbytes)) + { + M2M_ERR("[spi_data_write]: Failed data block write, bus error...\r\n"); + result = N_FAIL; + break; + } + + /** + Write Crc + **/ + if (!gu8Crc_off) + { + if (N_OK != spi_write(crc, 2)) + { + M2M_ERR("[spi_data_write]: Failed data block CRC write, bus error...\r\n"); + result = N_FAIL; + break; + } + } + + ix += nbytes; + sz -= nbytes; + } + while (sz); + + return result; +} + +/******************************************** + + Spi interfaces + +********************************************/ + +static int8_t spi_write_reg(uint32_t u32Addr, uint32_t u32Val) +{ + uint8_t cmd = CMD_SINGLE_WRITE; + uint8_t clockless = 0; + + if (u32Addr <= 0x30) + { + /** + NMC1000 clockless registers. + **/ + cmd = CMD_INTERNAL_WRITE; + clockless = 1; + } + + if (spi_cmd(cmd, u32Addr, u32Val, 4, clockless) != N_OK) + { + M2M_ERR("[spi_write_reg]: Failed cmd, write reg (%08" PRIx32 ")...\r\n", u32Addr); + return N_FAIL; + } + + if (spi_cmd_rsp(cmd) != N_OK) + { + M2M_ERR("[spi_write_reg]: Failed cmd response, write reg (%08" PRIx32 ")...\r\n", u32Addr); + return N_FAIL; + } + + return N_OK; +} + +static int8_t spi_write_block(uint32_t u32Addr, uint8_t *puBuf, uint16_t u16Sz) +{ + uint8_t len; + uint8_t rsp[3]; + + /** + Command + **/ + if (spi_cmd(CMD_DMA_EXT_WRITE, u32Addr, 0, u16Sz, 0) != N_OK) + { + M2M_ERR("[spi_write_block]: Failed cmd, write block (%08" PRIx32 ")...\r\n", u32Addr); + return N_FAIL; + } + + if (spi_cmd_rsp(CMD_DMA_EXT_WRITE) != N_OK) + { + M2M_ERR("[spi_write_block]: Failed cmd response, write block (%08" PRIx32 ")...\r\n", u32Addr); + return N_FAIL; + } + + /** + Data + **/ + if (spi_data_write(puBuf, u16Sz) != N_OK) + { + M2M_ERR("[spi_write_block]: Failed block data write...\r\n"); + return N_FAIL; + } + /** + Data RESP + **/ + if (!gu8Crc_off) + len = 2; + else + len = 3; + + if (N_OK != spi_read(&rsp[0], len)) + { + M2M_ERR("[spi_write_block]: Failed bus error...\r\n"); + return N_FAIL; + } + + if((rsp[len-1] != 0) || (rsp[len-2] != 0xC3)) + { + M2M_ERR("[spi_write_block]: Failed data response read, %x %x %x\r\n", rsp[0], rsp[1], rsp[2]); + return N_FAIL; + } + + return N_OK; +} + +static int8_t spi_read_reg(uint32_t u32Addr, uint32_t* pu32RetVal) +{ + uint8_t cmd = CMD_SINGLE_READ; + uint8_t tmp[4]; + uint8_t clockless = 0; + + if (u32Addr <= 0xff) + { + /** + NMC1000 clockless registers. + **/ + cmd = CMD_INTERNAL_READ; + clockless = 1; + } + + if (spi_cmd(cmd, u32Addr, 0, 4, clockless) != N_OK) + { + M2M_ERR("[spi_read_reg]: Failed cmd, read reg (%08" PRIx32 ")...\r\n", u32Addr); + return N_FAIL; + } + + if (spi_cmd_rsp(cmd) != N_OK) + { + M2M_ERR("[spi_read_reg]: Failed cmd response, read reg (%08" PRIx32 ")...\r\n", u32Addr); + return N_FAIL; + } + + /* to avoid endianess issues */ + if (spi_data_read(&tmp[0], 4, clockless) != N_OK) + { + M2M_ERR("[spi_read_reg]: Failed data read...\r\n"); + return N_FAIL; + } + + *pu32RetVal = ((uint32_t)tmp[0]) | + ((uint32_t)tmp[1] << 8) | + ((uint32_t)tmp[2] << 16) | + ((uint32_t)tmp[3] << 24); + + return N_OK; +} + +static int8_t spi_read_block(uint32_t u32Addr, uint8_t *puBuf, uint16_t u16Sz) +{ + /** + Command + **/ + if (spi_cmd(CMD_DMA_EXT_READ, u32Addr, 0, u16Sz, 0) != N_OK) + { + M2M_ERR("[spi_read_block]: Failed cmd, read block (%08" PRIx32 ")...\r\n", u32Addr); + return N_FAIL; + } + + if (spi_cmd_rsp(CMD_DMA_EXT_READ) != N_OK) + { + M2M_ERR("[spi_read_block]: Failed cmd response, read block (%08" PRIx32 ")...\r\n", u32Addr); + return N_FAIL; + } + + /** + Data + **/ + if (spi_data_read(puBuf, u16Sz, 0) != N_OK) + { + M2M_ERR("[spi_read_block]: Failed block data read...\r\n"); + return N_FAIL; + } + + return N_OK; +} + +static void spi_init_pkt_sz(void) +{ + uint32_t val32; + + /* Make sure SPI max. packet size fits the defined DATA_PKT_SZ. */ + val32 = nm_spi_read_reg(SPI_BASE+0x24); + val32 &= ~(0x7 << 4); + switch(DATA_PKT_SZ) + { + case 256: val32 |= (0 << 4); break; + case 512: val32 |= (1 << 4); break; + case 1024: val32 |= (2 << 4); break; + case 2048: val32 |= (3 << 4); break; + case 4096: val32 |= (4 << 4); break; + case 8192: val32 |= (5 << 4); break; + } + nm_spi_write_reg(SPI_BASE+0x24, val32); +} + +/******************************************** + + Bus interfaces + +********************************************/ + +int8_t nm_spi_reset(void) +{ +// if (OSAL_RESULT_TRUE != OSAL_MUTEX_Lock(&s_spiLock, OSAL_WAIT_FOREVER)) +// return M2M_ERR_BUS_FAIL; + + spi_cmd(CMD_RESET, 0, 0, 0, 0); + spi_cmd_rsp(CMD_RESET); + + // OSAL_MUTEX_Unlock(&s_spiLock); + + return M2M_SUCCESS; +} + +void nm_spi_lock_init(void) +{ +// OSAL_MUTEX_Create(&s_spiLock); +} + +/* +* @fn nm_spi_init +* @brief Initialize the SPI +* @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure +*/ +int8_t nm_spi_init(void) +{ + uint32_t chipid; + uint32_t reg = 0; + + /** + configure protocol + **/ + gu8Crc_off = 0; + + if (nm_spi_read_reg_with_ret(NMI_SPI_PROTOCOL_CONFIG, ®) != M2M_SUCCESS) + { + /* Read failed. Try with CRC off. This might happen when module + is removed but chip isn't reset*/ + gu8Crc_off = 1; + M2M_ERR("[nm_spi_init]: Failed internal read protocol with CRC on, retrying with CRC off...\r\n"); + + if (nm_spi_read_reg_with_ret(NMI_SPI_PROTOCOL_CONFIG, ®) != M2M_SUCCESS) + { + // Reaad failed with both CRC on and off, something went bad + M2M_ERR("[nm_spi_init]: Failed internal read protocol...\r\n"); + + return M2M_ERR_BUS_FAIL; + } + } + if(gu8Crc_off == 0) + { + reg &= ~0xc; /* disable CRC checking */ + reg &= ~0x70; + reg |= (0x5 << 4); + + if (nm_spi_write_reg(NMI_SPI_PROTOCOL_CONFIG, reg) != M2M_SUCCESS) + { + M2M_ERR("[nm_spi_init]: Failed internal write protocol reg...\r\n"); + + return M2M_ERR_BUS_FAIL; + } + + gu8Crc_off = 1; + } + + /** + make sure can read back chip id correctly + **/ + if (nm_spi_read_reg_with_ret(0x1000, &chipid) != M2M_SUCCESS) + { + M2M_ERR("[nm_spi_init]: Fail cmd read chip id...\r\n"); + return M2M_ERR_BUS_FAIL; + } + + M2M_DBG("[nm_spi_init]: chipid (%08x)\r\n", (unsigned int)chipid); + spi_init_pkt_sz(); + + return M2M_SUCCESS; +} + +/* +* @fn nm_spi_deinit +* @brief DeInitialize the SPI +* @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure +*/ +int8_t nm_spi_deinit(void) +{ + gu8Crc_off = 0; +// OSAL_MUTEX_Delete(&s_spiLock); + return M2M_SUCCESS; +} + +/* +* @fn nm_spi_read_reg +* @brief Read register +* @param [in] u32Addr +* Register address +* @return Register value +*/ +uint32_t nm_spi_read_reg(uint32_t u32Addr) +{ + uint32_t u32Val; + + nm_spi_read_reg_with_ret(u32Addr, &u32Val); + + return u32Val; +} + +/* +* @fn nm_spi_read_reg_with_ret +* @brief Read register with error code return +* @param [in] u32Addr +* Register address +* @param [out] pu32RetVal +* Pointer to u32 variable used to return the read value +* @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure +*/ +int8_t nm_spi_read_reg_with_ret(uint32_t u32Addr, uint32_t* pu32RetVal) +{ + uint8_t retry = SPI_RETRY_COUNT; + +// if (OSAL_RESULT_TRUE != OSAL_MUTEX_Lock(&s_spiLock, OSAL_WAIT_FOREVER)) +// return M2M_ERR_BUS_FAIL; + + while(retry--) + { + if (spi_read_reg(u32Addr, pu32RetVal) == N_OK) + { +// OSAL_MUTEX_Unlock(&s_spiLock); + + return M2M_SUCCESS; + } + + M2M_ERR("Reset and retry %d %" PRIx32 "\r\n", retry, u32Addr); + spi_reset(); + } + +// OSAL_MUTEX_Unlock(&s_spiLock); + + return M2M_ERR_BUS_FAIL; +} + +/* +* @fn nm_spi_write_reg +* @brief write register +* @param [in] u32Addr +* Register address +* @param [in] u32Val +* Value to be written to the register +* @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure +*/ +int8_t nm_spi_write_reg(uint32_t u32Addr, uint32_t u32Val) +{ + uint8_t retry = SPI_RETRY_COUNT; + +// if (OSAL_RESULT_TRUE != OSAL_MUTEX_Lock(&s_spiLock, OSAL_WAIT_FOREVER)) + // return M2M_ERR_BUS_FAIL; + + while(retry--) + { + if (spi_write_reg(u32Addr, u32Val) == N_OK) + { +// OSAL_MUTEX_Unlock(&s_spiLock); + + return M2M_SUCCESS; + } + + M2M_ERR("Reset and retry %d %" PRIx32 " %" PRIx32 "\r\n", retry, u32Addr, u32Val); + spi_reset(); + } + +// OSAL_MUTEX_Unlock(&s_spiLock); + + return M2M_ERR_BUS_FAIL; +} + +/* +* @fn nm_spi_read_block +* @brief Read block of data +* @param [in] u32Addr +* Start address +* @param [out] puBuf +* Pointer to a buffer used to return the read data +* @param [in] u16Sz +* Number of bytes to read. The buffer size must be >= u16Sz +* @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure +*/ +int8_t nm_spi_read_block(uint32_t u32Addr, uint8_t *puBuf, uint16_t u16Sz) +{ + uint8_t retry = SPI_RETRY_COUNT; + uint8_t tmpBuf[2] = {0,0}; + uint8_t *puTmpBuf; + +// if (OSAL_RESULT_TRUE != OSAL_MUTEX_Lock(&s_spiLock, OSAL_WAIT_FOREVER)) + // return M2M_ERR_BUS_FAIL; + + if (u16Sz == 1) + { + u16Sz = 2; + puTmpBuf = tmpBuf; + } + else + { + puTmpBuf = puBuf; + } + + while(retry--) + { + if (spi_read_block(u32Addr, puTmpBuf, u16Sz) == N_OK) + { +// OSAL_MUTEX_Unlock(&s_spiLock); + + if (puTmpBuf == tmpBuf) + *puBuf = *tmpBuf; + + return M2M_SUCCESS; + } + + M2M_ERR("Reset and retry %d %" PRIx32 " %d\r\n", retry, u32Addr, u16Sz); + spi_reset(); + } + +// OSAL_MUTEX_Unlock(&s_spiLock); + + return M2M_ERR_BUS_FAIL; +} + +/* +* @fn nm_spi_write_block +* @brief Write block of data +* @param [in] u32Addr +* Start address +* @param [in] puBuf +* Pointer to the buffer holding the data to be written +* @param [in] u16Sz +* Number of bytes to write. The buffer size must be >= u16Sz +* @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure +*/ +int8_t nm_spi_write_block(uint32_t u32Addr, uint8_t *puBuf, uint16_t u16Sz) +{ + uint8_t retry = SPI_RETRY_COUNT; + +// if (OSAL_RESULT_TRUE != OSAL_MUTEX_Lock(&s_spiLock, OSAL_WAIT_FOREVER)) +// return M2M_ERR_BUS_FAIL; + + //Workaround hardware problem with single byte transfers over SPI bus + if (u16Sz == 1) + u16Sz = 2; + + while(retry--) + { + if (spi_write_block(u32Addr, puBuf, u16Sz) == N_OK) + { +// OSAL_MUTEX_Unlock(&s_spiLock); + + return M2M_SUCCESS; + } + + M2M_ERR("Reset and retry %d %" PRIx32 " %d\r\n", retry, u32Addr, u16Sz); + spi_reset(); + } + +// OSAL_MUTEX_Unlock(&s_spiLock); + + return M2M_ERR_BUS_FAIL; +} + +//DOM-IGNORE-END diff --git a/ChaletLora.X/Source/winc3400_142/socket/include/m2m_socket_host_if.h b/ChaletLora.X/Source/winc3400_142/socket/include/m2m_socket_host_if.h new file mode 100644 index 0000000..8ca7251 --- /dev/null +++ b/ChaletLora.X/Source/winc3400_142/socket/include/m2m_socket_host_if.h @@ -0,0 +1,347 @@ +/******************************************************************************* + BSD compatible socket interface internal types. + + File Name: + m2m_socket_host_if.h + + Summary: + + Description: + +*******************************************************************************/ + +//DOM-IGNORE-BEGIN +/******************************************************************************* +* Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries. +* +* Subject to your compliance with these terms, you may use Microchip software +* and any derivatives exclusively with Microchip products. It is your +* responsibility to comply with third party license terms applicable to your +* use of third party software (including open source software) that may +* accompany Microchip software. +* +* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER +* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED +* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A +* PARTICULAR PURPOSE. +* +* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, +* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND +* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS +* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE +* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN +* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, +* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. +*******************************************************************************/ + +#ifndef __M2M_SOCKET_HOST_IF_H__ +#define __M2M_SOCKET_HOST_IF_H__ + + +#ifdef __cplusplus +extern "C" { +#endif + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +INCLUDES +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + +#include "socket.h" + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +MACROS +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + +#define SSL_MAX_OPT_LEN HOSTNAME_MAX_SIZE + +#define ALPN_LIST_MIN_SIZE 4 +#define ALPN_LIST_MAX_SIZE 32 +/*!< + Maximum length of ALPN list that can be specified by the application. + The list is in the following format: + @verbatim + 0 1 2 3 ... (bytes) + +-------+-------+-------+ ... +-------+ ... +-------+ ... + | Length L (BE) | len1 | name1... | len2 | name2... | len3 | name3... + +-------+-------+-------+ ... +-------+ ... +-------+ ... + Length fields do not include themselves. + @endverbatim +*/ + +#define SOCKET_CMD_INVALID 0x00 +/*!< + Invalid Socket command value. +*/ + +#define PING_ERR_SUCCESS 0 +#define PING_ERR_DEST_UNREACH 1 +#define PING_ERR_TIMEOUT 2 + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +DATA TYPES +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + + +/*! +* @brief +*/ +typedef struct { + uint16_t u16Family; + uint16_t u16Port; + uint32_t u32IPAddr; +} tstrSockAddr; + + +//typedef int8_t SOCKET; +typedef tstrSockAddr tstrUIPSockAddr; + + + +/*! +@struct \ + tstrDnsReply + +@brief + DNS Reply, contains hostName and HostIP. +*/ +typedef struct { + char acHostName[HOSTNAME_MAX_SIZE]; + uint32_t u32HostIP; +} tstrDnsReply; + + +/*! +@brief +*/ +typedef struct { + tstrSockAddr strAddr; + SOCKET sock; + uint8_t u8Void; + uint16_t u16SessionID; +} tstrBindCmd; + + +/*! +@brief +*/ +typedef struct { + SOCKET sock; + int8_t s8Status; + uint16_t u16SessionID; +} tstrBindReply; + + +/*! +* @brief +*/ +typedef struct { + SOCKET sock; + uint8_t u8BackLog; + uint16_t u16SessionID; +} tstrListenCmd; + + +/*! +@struct \ + tstrSocketRecvMsg + +@brief Socket recv status. + + It is passed to the APPSocketEventHandler with SOCKET_MSG_RECV or SOCKET_MSG_RECVFROM message type + in a response to a user call to the recv or recvfrom. + If the received data from the remote peer is larger than the USER Buffer size (given at recv call), the data is + delivered to the user in a number of consecutive chunks according to the USER Buffer size. +*/ +typedef struct { + SOCKET sock; + int8_t s8Status; + uint16_t u16SessionID; +} tstrListenReply; + + +/*! +* @brief +*/ +typedef struct { + tstrSockAddr strAddr; + SOCKET sListenSock; + SOCKET sConnectedSock; + uint16_t u16Void; +} tstrAcceptReply; + + +/*! +* @brief +*/ +typedef struct { + tstrSockAddr strAddr; + SOCKET sock; + uint8_t u8SslFlags; + uint16_t u16SessionID; +} tstrConnectCmd; + + +/*! +@struct \ + tstrConnectReply + +@brief + Connect Reply, contains sock number and error value +*/ +typedef struct { + SOCKET sock; + int8_t s8Error; + /*!< + 0 for successful connection, in which case u16AppDataOffset is valid. + Negative for failed connection, in which case u8ErrorType and u8ErrorDetail may give more info. + */ + union { + uint16_t u16AppDataOffset; + /*!< + In further packet send requests the host interface should put the user application + data at this offset in the allocated shared data packet. + */ + struct { + uint8_t u8ErrSource; + /*!< + 0: No detail + 1: TLS Alert received from peer + 2: TLS Alert generated locally + */ + uint8_t u8ErrCode; + /*!< + For TLS Alerts, this is the Alert ID. + */ + }; + }; +} tstrConnectReply; + + +/*! +@struct \ + tstrConnectAlpnReply + +@brief + Connect Reply, contains sock number, error value and index of negotiated application protocol. +*/ +typedef struct{ + tstrConnectReply strConnReply; + uint8_t u8AppProtocolIdx; + /*!< + 1-based index of application-layer protocol negotiated during TLS handshake. + */ + uint8_t __PAD24__[3]; +}tstrConnectAlpnReply; + + +/*! +@brief +*/ +typedef struct{ + SOCKET sock; + uint8_t u8Void; + uint16_t u16DataSize; + tstrSockAddr strAddr; + uint16_t u16SessionID; + uint16_t u16Void; +} tstrSendCmd; + + +/*! +@struct \ + tstrSendReply + +@brief + Send Reply, contains socket number and number of sent bytes. +*/ +typedef struct { + SOCKET sock; + uint8_t u8Void; + int16_t s16SentBytes; + uint16_t u16SessionID; + uint16_t u16Void; +} tstrSendReply; + + +/*! +* @brief +*/ +typedef struct { + uint32_t u32Timeoutmsec; + SOCKET sock; + uint8_t u8Void; + uint16_t u16SessionID; + uint16_t u16BufLen; +} tstrRecvCmd; + + +/*! +@struct \ + tstrRecvReply +@brief +*/ +typedef struct { + tstrSockAddr strRemoteAddr; + int16_t s16RecvStatus; + uint16_t u16DataOffset; + SOCKET sock; + uint8_t u8Void; + uint16_t u16SessionID; +} tstrRecvReply; + + +/*! +* @brief +*/ +typedef struct { + uint32_t u32OptionValue; + SOCKET sock; + uint8_t u8Option; + uint16_t u16SessionID; +} tstrSetSocketOptCmd; + + +typedef struct { + SOCKET sslSock; + uint8_t __PAD24__[3]; +} tstrSSLSocketCreateCmd; + + +/*! +* @brief +*/ +typedef struct { + SOCKET sock; + uint8_t u8Option; + uint16_t u16SessionID; + uint32_t u32OptLen; + uint8_t au8OptVal[SSL_MAX_OPT_LEN]; +} tstrSSLSetSockOptCmd; + + +/*! +*/ +typedef struct { + uint32_t u32DestIPAddr; + uint32_t u32CmdPrivate; + uint16_t u16PingCount; + uint8_t u8TTL; + uint8_t __PAD8__; +} tstrPingCmd; + + +typedef struct { + uint32_t u32IPAddr; + uint32_t u32CmdPrivate; + uint32_t u32RTT; + uint16_t u16Success; + uint16_t u16Fail; + uint8_t u8ErrorCode; + uint8_t __PAD24__[3]; +} tstrPingReply; + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* __M2M_SOCKET_HOST_IF_H__ */ diff --git a/ChaletLora.X/Source/winc3400_142/socket/include/netinet_in.h b/ChaletLora.X/Source/winc3400_142/socket/include/netinet_in.h new file mode 100644 index 0000000..b4b4e2f --- /dev/null +++ b/ChaletLora.X/Source/winc3400_142/socket/include/netinet_in.h @@ -0,0 +1,95 @@ +/******************************************************************************* + Company: + Microchip Technology Inc. + + File Name: + netinet_in.h + + Summary: + + Description: + *******************************************************************************/ + +// DOM-IGNORE-BEGIN +/******************************************************************************* +* Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries. +* +* Subject to your compliance with these terms, you may use Microchip software +* and any derivatives exclusively with Microchip products. It is your +* responsibility to comply with third party license terms applicable to your +* use of third party software (including open source software) that may +* accompany Microchip software. +* +* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER +* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED +* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A +* PARTICULAR PURPOSE. +* +* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, +* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND +* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS +* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE +* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN +* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, +* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. +*******************************************************************************/ +// DOM-IGNORE-END + +#ifndef _NETINET_IN_H +#define _NETINET_IN_H + +#include +#include + +// DOM-IGNORE-BEGIN +#ifdef __cplusplus // Provide C++ Compatibility + extern "C" { +#endif +// DOM-IGNORE-END + +typedef uint32_t in_addr_t; + +struct in_addr { + /*!< + Network Byte Order representation of the IPv4 address. For example, + the address "192.168.0.10" is represented as 0x0A00A8C0. + */ + in_addr_t s_addr; +}; + +struct sockaddr_in{ + uint16_t sin_family; + /*!< + Specifies the address family(AF). + Members of AF_INET address family are IPv4 addresses. + Hence,the only supported value for this is AF_INET. + */ + uint16_t sin_port; + /*!< + Port number of the socket. + Network sockets are identified by a pair of IP addresses and port number. + Must be set in the Network Byte Order format , @ref _htons (e.g. _htons(80)). + Can NOT have zero value. + */ + struct in_addr sin_addr; + /*!< + IP Address of the socket. + The IP address is of type @ref in_addr structure. + Can be set to "0" to accept any IP address for server operation. + */ + uint8_t sin_zero[8]; + /*!< + Padding to make structure the same size as @ref sockaddr. + */ +}; + +const char *inet_ntop(int af, const void *src, char *dst, size_t size); +in_addr_t inet_addr(const char *cp); + +// DOM-IGNORE-BEGIN +#ifdef __cplusplus +} +#endif +// DOM-IGNORE-END + +#endif /* _NETINET_IN_H */ diff --git a/ChaletLora.X/Source/winc3400_142/socket/include/socket.h b/ChaletLora.X/Source/winc3400_142/socket/include/socket.h new file mode 100644 index 0000000..38d97bc --- /dev/null +++ b/ChaletLora.X/Source/winc3400_142/socket/include/socket.h @@ -0,0 +1,2074 @@ +/******************************************************************************* + File Name: + socket.h + + Summary: + WINC3400 BSD Compatible Socket Interface + + Description: + WINC3400 BSD Compatible Socket Interface + *******************************************************************************/ + +//DOM-IGNORE-BEGIN +/******************************************************************************* +* Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries. +* +* Subject to your compliance with these terms, you may use Microchip software +* and any derivatives exclusively with Microchip products. It is your +* responsibility to comply with third party license terms applicable to your +* use of third party software (including open source software) that may +* accompany Microchip software. +* +* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER +* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED +* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A +* PARTICULAR PURPOSE. +* +* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, +* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND +* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS +* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE +* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN +* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, +* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. +*******************************************************************************/ +//DOM-IGNORE-END + +#ifndef __SOCKET_H__ +#define __SOCKET_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/** @defgroup SOCKETAPI Socket + * BSD compatible socket interface between the host layer and the network + * protocol stacks in the firmware. + * These functions are used by the host application to send or receive + * packets and to do other socket operations. + */ + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +INCLUDES +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + +#include "m2m_types.h" +#include "netinet_in.h" + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +MACROS +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ +/**@defgroup SOCKETDEF Defines + * @ingroup SOCKETAPI + * The following list of macros are used to define constants used throughout the socket layer, and SSL Socket options. + */ + +/**@defgroup IPDefines TCP/IP Defines + * @ingroup SOCKETDEF + * The following list of macros are used to define constants used throughout the socket layer. + * @{ + */ +/* + * HOSTNAME_MAX_SIZE is defined here and also in host_drv/socket/include/m2m_socket_host_if.h + * The two definitions must match. +*/ +#define HOSTNAME_MAX_SIZE 100 +/*!< + Maximum allowed size for a host domain name passed to the function @ref gethostbyname. + command value. Used with the @ref setsockopt function. +*/ + +#define SOCKET_BUFFER_MAX_LENGTH 1400 +/*!< + Maximum allowed size for a socket data buffer. Used with @ref send socket + function to ensure that the buffer sent is within the allowed range. +*/ + +#define AF_INET 2 +/*!< + The AF_INET is the address family used for IPv4. An IPv4 transport address is specified with the @ref sockaddr_in structure. + (It is the only supported type for the current implementation.) +*/ + +#define SOCK_STREAM 1 +/*!< + One of the IPv4 supported socket types for reliable connection-oriented stream connection. + Passed to the @ref socket function for the socket creation operation. +*/ + +#define SOCK_DGRAM 2 +/*!< + One of the IPv4 supported socket types for unreliable connectionless datagram connection. + Passed to the @ref socket function for the socket creation operation. +*/ + +#define SOCK_RAW 3 +/*!< + Socket type used for sending and receiving raw frames. + Raw socket expects and returns IP header. + Please note that SO_BROADCAST socket option is not currently supported when using RAW socket. + Similarly to SOCK_DGRAM it is oriented towards unreliable connectionless communication. + Passed to the @ref socket function for the socket creation operation. +*/ + +#define SOCKET_FLAGS_SSL 0x01 +/*!< + This flag may be set in the u8Config parameter of @ref socket (when the + u8Type parameter is @ref SOCK_STREAM), to create a TLS socket.\n + Note that the number of TLS sockets is limited to 2.\n + This flag is kept for legacy purposes. It is recommended that applications + use @ref SOCKET_CONFIG_SSL_ON instead. +*/ + +#define SOCKET_FLAGS_IPPROTO_RAW 0x02 +/*!< + This flag may be set in the u8Config parameter of @ref socket (when the + u8Type parameter is @ref SOCK_RAW), to allow the Application to use a + socket of type raw to send/receive frames.\n + This flag is kept for legacy purposes. It is recommended that applications + use @ref SOCKET_CONFIG_IPPROTO_RAW instead. +*/ + +#define SOCKET_CONFIG_DEFAULT 0 +/*!< + This value may be passed in the u8Config parameter of @ref socket to create + a socket with default configuration. +*/ + +#define SOCKET_CONFIG_SSL_OFF 0 +/*!< + This value may be passed in the u8Config parameter of @ref socket (when the + u8Type parameter is @ref SOCK_STREAM), to create a socket not capable of + TLS. +*/ +#define SOCKET_CONFIG_SSL_ON 1 +/*!< + This value may be passed in the u8Config parameter of @ref socket (when the + u8Type parameter is @ref SOCK_STREAM), to create a TLS socket.\n + Note that the number of TLS sockets is limited to 2. +*/ +#define SOCKET_CONFIG_SSL_DELAY 2 +/*!< + This value may be passed in the u8Config parameter of @ref socket (when the + u8Type parameter is @ref SOCK_STREAM), to create a TCP socket which has the + potential to upgrade to a TLS socket later (by calling @ref secure).\n + Note that the total number of TLS sockets and potential TLS sockets is + limited to 2. +*/ + +#define SOCKET_CONFIG_IPPROTO_RAW 2 +/*!< + This value may be passed in the u8Config parameter of @ref socket (when the u8Type parameter is + @ref SOCK_RAW), to allow the Application to use a socket of type raw to send/receive frames. + This assumes that the application will fill the IP and protocol (UDP, TCP, ICMP) headers. + Typically, when sending ICMP frames via RAW socket, there are two options that can be used: + - IPPROTO_RAW - Raw IP packets, implies that IP_HDRINCL is enabled and therefore + the host application should fill in the corresponding protocol header checksum. + - IPPROTO_ICMP - ICMP packets, the WINC would fill in the ICMP header checksum (not supported). + @warning + Please note that only SOCKET_CONFIG_IPPROTO_RAW is currently supported. + Raw sockets can be used to send TCP/UDP/ICMP packets, however, the current implementation only + supports receiving Raw ICMP frames, which also requires @ref SO_ICMP_FILTER to be set appropriately. +*/ + +#define TCP_SOCK_MAX (7) +/*!< + Maximum number of simultaneous TCP sockets. +*/ + +#define UDP_SOCK_MAX 4 +/*!< + Maximum number of simultaneous UDP sockets. +*/ + +#define RAW_SOCK_MAX 1 +/*!< + Maximum number of simultaneous RAW sockets. +*/ + +#define MAX_SOCKET (TCP_SOCK_MAX + UDP_SOCK_MAX + RAW_SOCK_MAX) +/*!< + Maximum number of simultaneous sockets. +*/ + +#define SOL_SOCKET 1 +/*!< + Socket option. + Used with the @ref setsockopt function +*/ + +#define SOL_SSL_SOCKET 2 +/*!< + SSL Socket option level. + Used with the @ref setsockopt function +*/ + +#define SOL_RAW 255 +/*!< + Raw Socket option level. + Used with the @ref setsockopt function +*/ + +#define SO_SET_UDP_SEND_CALLBACK 0x00 +/*!< + Socket option used by the application to enable/disable + the use of UDP send callbacks.\n + Used with the @ref setsockopt function.\n + The option value should be cast to int type.\n + 0: disable UDP send callbacks.\n + 1: enable UDP send callbacks.\n + Default setting is enable. + + @warning @ref connect and @ref bind cause this setting to + be lost, so the application should only set this option + after calling @ref connect or @ref bind. +*/ + +#define IP_ADD_MEMBERSHIP 0x01 +/*!< + Set Socket Option Add Membership command value. + Used with the @ref setsockopt function. +*/ + +#define IP_DROP_MEMBERSHIP 0x02 +/*!< + Set Socket Option Drop Membership command value. + Used with the @ref setsockopt function. +*/ + +#define SO_TCP_KEEPALIVE 0x04 +/*!< + Socket option to enable or disable TCP keep-alive.\n + Used with the @ref setsockopt function.\n + The option value should be cast to int type.\n + 0: disable TCP keep-alive.\n + 1: enable TCP keep-alive.\n + Default setting is disable. + + @warning @ref connect and @ref bind cause this setting to + be lost, so the application should only set this option + after calling @ref connect or @ref bind. +*/ + +#define SO_TCP_KEEPIDLE 0x05 +/*!< + Socket option to set the time period after which the socket will trigger keep-alive transmissions.\n + Used with the @ref setsockopt function.\n + The option value should be cast to int type.\n + Option value is the time period in units of 500ms. Maximum 2^32 - 1. + Default setting is 120 (60 seconds). + + @warning @ref connect and @ref bind cause this setting to + be lost, so the application should only set this option + after calling @ref connect or @ref bind. +*/ + +#define SO_TCP_KEEPINTVL 0x06 +/*!< + Socket option to set the time period between keep-alive retransmissions.\n + Used with the @ref setsockopt function.\n + The option value should be cast to int type.\n + Option value is the time period in units of 500ms. Maximum 255. + Default setting is 1 (0.5 seconds). + + @warning @ref connect and @ref bind cause this setting to + be lost, so the application should only set this option + after calling @ref connect or @ref bind. +*/ + +#define SO_TCP_KEEPCNT 0x07 +/*!< + Socket option to set the number of keep-alive retransmissions to be carried out before declaring that the remote end is not available.\n + Used with the @ref setsockopt function.\n + The option value should be cast to int type.\n + Maximum 255. + Default setting is 20. + + @warning @ref connect and @ref bind cause this setting to + be lost, so the application should only set this option + after calling @ref connect or @ref bind. +*/ + +#define SO_ICMP_FILTER 0x01 +/*!< + Socket option to set the ICMP filter for raw sockets when receiving.\n + Current implementation allows for a filter none (0) or filter all (anything else). + Filter none means that all ICMP frames will be delivered to the host via raw socket. + Filter all means that all ICMP frames will NOT be delivered to host and handled internally by the WINC (please note that fragmentation is not supported by the WINC). + Used with the @ref setsockopt function.\n + The option value should be cast to int type.\n + Default setting is 0 (filter none). + + @warning @ref bind and @ref shutdown will cause this setting to be lost, + so the application should only set this option after @ref bind. +*/ + +#define RAW_SOCK_ID (TCP_SOCK_MAX + UDP_SOCK_MAX) +/*!< + Socket number to be used for RAW socket. +*/ + +/**@}*/ //IPDefines + + +/**@addtogroup TLSDefines + * @{ + */ +#define ALPN_LIST_MAX_APP_LENGTH 30 +/*!< + Maximum length of ALPN list that can be specified by the application. + This length includes separators (spaces) and terminator (NUL). +*/ +/**@}*/ // TLSDefines + +/** + * @defgroup TLSDefines TLS Defines + * @ingroup SOCKETDEF + * @ingroup SSLAPI +*/ + +/**@defgroup SSLSocketOptions TLS Socket Options + * @ingroup TLSDefines + * The following list of macros are used to define SSL Socket options. + * @{ + * @sa setsockopt + */ + +#define SO_SSL_BYPASS_X509_VERIF 0x01 +/*!< + Allow an opened SSL socket to bypass the X509 certificate verification + process. + It is recommended NOT to use this socket option in production software + applications. It is supported for debugging and testing purposes.\n + The option value should be casted to int type.\n + 0: do not bypass the X509 certificate verification process (default, + recommended).\n + 1: bypass the X509 certificate verification process.\n + + This option only takes effect if it is set after calling @ref socket and + before calling @ref connect or @ref secure. +*/ + +#define SO_SSL_SNI 0x02 +/*!< + Set the Server Name Indicator (SNI) for an SSL socket. The SNI is a NULL- + terminated string containing the server name associated with the + connection. Its size must not exceed @ref HOSTNAME_MAX_SIZE. If the SNI is + not a null string, then TLS Client Hello messages will include the SNI + extension.\n + + This option only takes effect if it is set after calling @ref socket and + before calling @ref connect or @ref secure. +*/ + +#define SO_SSL_ENABLE_SESSION_CACHING 0x03 +/*!< + This option allow the TLS to cache the session information for fast TLS + session establishment in future connections using the TLS Protocol session + resume features.\n + The option value should be casted to int type.\n + 0: disable TLS session caching (default).\n + 1: enable TLS session caching.\n + Note that TLS session caching is always enabled in TLS Server Mode and this + option is ignored.\n + + This option only takes effect if it is set after calling @ref socket and + before calling @ref connect or @ref secure. +*/ + +#define SO_SSL_ENABLE_CERTNAME_VALIDATION 0x04 +/*!< + Enable internal validation of server name against the server's + certificate subject common name. If there is no server name + provided (via the @ref SO_SSL_SNI option), setting this option + does nothing.\n + The option value should be casted to int type.\n + 0: disable server certificate name validation (default).\n + 1: enable server certificate name validation (recommended).\n + + This option only takes effect if it is set after calling @ref socket and + before calling @ref connect or @ref secure. +*/ + +#define SO_SSL_ALPN 0x05 +/*!< + Set the list to use for Application-Layer Protocol Negotiation + for an SSL socket.\n + This option is intended for internal use and should not be + used by the application. Applications should use the API @ref + set_alpn_list. +*/ + +#define SO_SSL_ENABLE_SNI_VALIDATION 0x04 +/*!< + Legacy name for @ref SO_SSL_ENABLE_CERTNAME_VALIDATION.\n +*/ +/**@}*/ //SSLSocketOptions + +/**@defgroup SSLCipherSuiteID TLS Cipher Suite IDs + * @ingroup TLSDefines + * The following list of macros defined the list of supported TLS Cipher suites. + * Each MACRO defines a single Cipher suite. + * @sa m2m_ssl_set_active_ciphersuites + * @{ + */ + +#define SSL_CIPHER_RSA_WITH_AES_128_CBC_SHA NBIT0 +#define SSL_CIPHER_RSA_WITH_AES_128_CBC_SHA256 NBIT1 +#define SSL_CIPHER_DHE_RSA_WITH_AES_128_CBC_SHA NBIT2 +#define SSL_CIPHER_DHE_RSA_WITH_AES_128_CBC_SHA256 NBIT3 +#define SSL_CIPHER_RSA_WITH_AES_128_GCM_SHA256 NBIT4 +#define SSL_CIPHER_DHE_RSA_WITH_AES_128_GCM_SHA256 NBIT5 +#define SSL_CIPHER_RSA_WITH_AES_256_CBC_SHA NBIT6 +#define SSL_CIPHER_RSA_WITH_AES_256_CBC_SHA256 NBIT7 +#define SSL_CIPHER_DHE_RSA_WITH_AES_256_CBC_SHA NBIT8 +#define SSL_CIPHER_DHE_RSA_WITH_AES_256_CBC_SHA256 NBIT9 +#define SSL_CIPHER_ECDHE_RSA_WITH_AES_128_CBC_SHA NBIT10 +#define SSL_CIPHER_ECDHE_RSA_WITH_AES_256_CBC_SHA NBIT11 +#define SSL_CIPHER_ECDHE_RSA_WITH_AES_128_CBC_SHA256 NBIT12 +#define SSL_CIPHER_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 NBIT13 +#define SSL_CIPHER_ECDHE_RSA_WITH_AES_128_GCM_SHA256 NBIT14 +#define SSL_CIPHER_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 NBIT15 + +#define SSL_ECC_ONLY_CIPHERS \ +(\ + SSL_CIPHER_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 | \ + SSL_CIPHER_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 \ +) +/*!< + All supported ciphers which use ECDSA. +*/ + +#define SSL_DEFAULT_CIPHERS \ +(\ + SSL_CIPHER_RSA_WITH_AES_128_CBC_SHA | \ + SSL_CIPHER_RSA_WITH_AES_128_CBC_SHA256 | \ + SSL_CIPHER_DHE_RSA_WITH_AES_128_CBC_SHA | \ + SSL_CIPHER_DHE_RSA_WITH_AES_128_CBC_SHA256 | \ + SSL_CIPHER_RSA_WITH_AES_128_GCM_SHA256 | \ + SSL_CIPHER_DHE_RSA_WITH_AES_128_GCM_SHA256 | \ + SSL_CIPHER_RSA_WITH_AES_256_CBC_SHA | \ + SSL_CIPHER_RSA_WITH_AES_256_CBC_SHA256 | \ + SSL_CIPHER_DHE_RSA_WITH_AES_256_CBC_SHA | \ + SSL_CIPHER_DHE_RSA_WITH_AES_256_CBC_SHA256 \ +) +/*!< + All supported Non-ECC Ciphers. This is the default active group after startup. + Note that firmware may support only a subset of these. +*/ +/**@}*/ // SSLCipherSuiteID + +/**@defgroup SOCKETBYTEORDER Byte Order + * @ingroup SOCKETAPI + * The following list of macros are used to convert between host representation and network byte order. + * @{ + */ + +#ifdef _NM_BSP_BIG_END +#define _htonl(m) (m) +/*!< + Convert a 4-byte integer from the Host representation to Network byte order (Host is BE). +*/ +#define _htons(A) (A) +/*!< + Convert a 2-byte integer (short) from Host representation to Network byte order (Host is BE). +*/ +#else +#define _htonl(m) \ + (uint32_t)(((uint32_t)(m << 24)) | ((uint32_t)((m & 0x0000FF00) << 8)) | ((uint32_t)((m & 0x00FF0000) >> 8)) | ((uint32_t)(((uint32_t)m) >> 24))) +/*!< + Convert a 4-byte integer from Host representation to Network byte order (Host is LE). +*/ + +#define _htons(A) (uint16_t)((((uint16_t) (A)) << 8) | (((uint16_t) (A)) >> 8)) +/*!< + Convert a 2-byte integer (short) from Host representation to Network byte order (Host is LE). +*/ +#endif + + +#define _ntohl _htonl +/*!< + Convert a 4-byte integer from Network byte order to Host representation. +*/ + + +#define _ntohs _htons +/*!< + Convert a 2-byte integer from Network byte order to Host representation. +*/ +/**@}*/ // SOCKETBYTEORDER + +/************** +Socket Errors +**************/ +/**@defgroup SocketErrorCode Error Codes + * @ingroup SOCKETAPI + * The following list of macros are used to define the possible error codes. + * Errors are listed in numerical order with the error macro name. + * @{ + */ +#define SOCK_ERR_NO_ERROR 0 +/*!< + Successful socket operation. This code is also used with event @ref SOCKET_MSG_RECV if a socket connection is closed. + In that case, the application should call @ref shutdown(). +*/ + +#define SOCK_ERR_INVALID_ADDRESS -1 +/*!< + Socket address is invalid. The socket operation cannot be completed successfully without specifying a valid address + For example: bind is called without specifying a port number +*/ + +#define SOCK_ERR_ADDR_ALREADY_IN_USE -2 +/*!< + Socket operation cannot bind on the given address. Only one IP address per socket, and one socket per IP address is permitted - + any attempt for a new socket to bind with an IP address already bound to another open socket will return the following error code. +*/ + +#define SOCK_ERR_MAX_TCP_SOCK -3 +/*!< + Exceeded the maximum number of TCP sockets. A maximum number of TCP sockets opened simultaneously is defined through TCP_SOCK_MAX. + It is not permitted to exceed that number at socket creation. Identifies that @ref socket operation failed. +*/ + +#define SOCK_ERR_MAX_UDP_SOCK -4 +/*!< + Exceeded the maximum number of UDP sockets. A maximum number of UDP sockets opened simultaneously is defined through UDP_SOCK_MAX. + It is not permitted to exceed that number at socket creation. Identifies that @ref socket operation failed +*/ + +#define SOCK_ERR_INVALID_ARG -6 +/*!< + An invalid argument is passed to a socket function. Identifies that @ref socket operation failed +*/ + +#define SOCK_ERR_MAX_LISTEN_SOCK -7 +/*!< + Exceeded the maximum number of TCP passive listening sockets. + Identifies that @ref listen operation failed. +*/ + +#define SOCK_ERR_INVALID -9 +/*!< + The requested socket operation is not valid in the current socket state. + For example: @ref accept is called on a TCP socket before @ref bind or @ref listen. +*/ + +#define SOCK_ERR_ADDR_IS_REQUIRED -11 +/*!< + Destination address is required. Failure to provide the socket address required for the socket operation to be completed. + The @ref sendto function requires a destination address to send data. +*/ + +#define SOCK_ERR_CONN_ABORTED -12 +/*!< + The socket is closed (reset) by the peer. If this error is received, the application should call @ref shutdown(). +*/ + +#define SOCK_ERR_TIMEOUT -13 +/*!< + The socket pending operation has timed out. The socket remains open. +*/ + +#define SOCK_ERR_BUFFER_FULL -14 +/*!< + No buffer space available to be used for the requested socket operation. +*/ +/**@}*/ // SocketErrorCode + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +DATA TYPES +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + +/** @defgroup SocketEnums Enumeration/Typedefs + * @ingroup SOCKETAPI + * Specific Enumeration-typedefs used for socket operations + * @{ + */ +/*! +@typedef \ + SOCKET + +@brief + Definition for socket handler data type. + Socket ID,used with all socket operations to uniquely identify the socket handler. + The ID is uniquely assigned at socket creation when calling @ref socket operation. +*/ +typedef int8_t SOCKET; + +/*! +@enum \ + tenuSockErrSource + +@brief + Source of socket error (local, remote or unknown). + +@see tstrSockErr +*/ + +typedef enum { + SOCKET_ERR_UNKNOWN = 0, + /*!< + No detail available (also used when there is no error). + */ + SOCKET_ERR_TLS_REMOTE, + /*!< + TLS Error Alert received from peer. + */ + SOCKET_ERR_TLS_LOCAL + /*!< + TLS Error Alert generated locally. + */ +} tenuSockErrSource; + +/*! +@struct \ + sockaddr + +@brief + Generic socket address structure. + +@see + sockaddr_in +*/ +struct sockaddr { + uint16_t sa_family; + /*!< + Socket address family. + */ + uint8_t sa_data[14]; + /*!< + Maximum size of all the different socket address structures. + */ +}; + +/*! +@struct \ + tstrSockErr + +@brief + Detail about socket failures. Used with @ref get_error_detail. +*/ +typedef struct { + tenuSockErrSource enuErrSource; + /*!< + Source of socket error (local, remote or unknown). + */ + uint8_t u8ErrCode; + /*!< + TLS Alert code as defined in + https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-6. + */ +} tstrSockErr; +/**@}*/ //SocketEnums + +/**@defgroup AsyncCallback Asynchronous Events + * @ingroup SocketEnums + * Specific Enumeration used for asynchronous operations + * @{ */ +/*! +@enum \ + tenuSocketCallbackMsgType + +@brief + Asynchronous APIs make use of callback functions in-order to return back the results once the corresponding socket operation is completed. + Hence resuming the normal execution of the application code while the socket operation returns the results. + Callback functions expect event messages to be passed in, in-order to identify the operation they're returning the results for. + The following enum identifies the type of events that are received in the callback function. + + Application Use: + In order for application developers to handle the pending events from the network controller through the callback functions, + a function call must be made to the function @ref m2m_wifi_handle_events at least once for each socket operation. + +@see bind +@see listen +@see accept +@see connect +@see send +@see recv +*/ + +typedef enum { + SOCKET_MSG_BIND = 1, + /*!< + Bind socket event. + */ + SOCKET_MSG_LISTEN, + /*!< + Listen socket event. + */ + SOCKET_MSG_DNS_RESOLVE, + /*!< + DNS Resolution event. + */ + SOCKET_MSG_ACCEPT, + /*!< + Accept socket event. + */ + SOCKET_MSG_CONNECT, + /*!< + Connect socket event. + */ + SOCKET_MSG_RECV, + /*!< + Receive socket event. + */ + SOCKET_MSG_SEND, + /*!< + Send socket event. + */ + SOCKET_MSG_SENDTO, + /*!< + Sendto socket event. + */ + SOCKET_MSG_RECVFROM, + /*!< + Recvfrom socket event. + */ + SOCKET_MSG_SECURE +/*!< + Existing socket made secure event. +*/ +} tenuSocketCallbackMsgType; + +/*! +@struct \ + tstrSocketBindMsg + +@brief Socket bind status. + + An asynchronous call to the @ref bind socket operation, returns information through this structure in response. + This structure together with the event @ref SOCKET_MSG_BIND are passed in parameters to the callback function. +@see + bind +*/ +typedef struct { + int8_t status; + /*!< + The result of the bind operation. + Holding a value of ZERO for a successful bind or otherwise a negative + error code corresponding to @ref SocketErrorCode. + */ +} tstrSocketBindMsg; + +/*! +@struct \ + tstrSocketListenMsg + +@brief Socket listen status. + + Socket listen information is returned through this structure in response to the asynchronous call to the @ref listen function. + This structure together with the event @ref SOCKET_MSG_LISTEN are passed-in parameters to the callback function. +@see + listen +*/ +typedef struct { + int8_t status; + /*!< + Holding a value of ZERO for a successful listen or otherwise a negative + error code corresponding to @ref SocketErrorCode. + */ +} tstrSocketListenMsg; + +/*! +@struct \ + tstrSocketAcceptMsg + +@brief Socket accept status. + + Socket accept information is returned through this structure in response to the asynchronous call to the @ref accept function. + This structure together with the event @ref SOCKET_MSG_ACCEPT are passed-in parameters to the callback function. +*/ +typedef struct { + SOCKET sock; + /*!< + On a successful @ref accept operation, the return information is the socket ID for the accepted connection with the remote peer. + Otherwise a negative error code is returned to indicate failure of the accept operation. + */ + struct sockaddr_in strAddr; + /*!< + Socket address structure for the remote peer. + */ +} tstrSocketAcceptMsg; + +/*! +@struct \ + tstrSocketConnectMsg + +@brief Socket connect status. + + Socket connect information is returned through this structure in response to an asynchronous call to the @ref connect socket function + or the @ref secure socket function. + This structure and the event @ref SOCKET_MSG_CONNECT or @ref SOCKET_MSG_SECURE are passed in parameters to the callback function. + If the application receives this structure with a negative value in s8Error, the application should call @ref close(). +*/ +typedef struct { + SOCKET sock; + /*!< + Socket ID referring to the socket passed to the @ref connect or @ref secure function call. + */ + int8_t s8Error; + /*!< + Connect error code:\n + - ZERO for a successful connect or successful secure. \n + - Otherwise a negative error code corresponding to the type of error. + */ +} tstrSocketConnectMsg; + +/*! +@struct \ + tstrSocketRecvMsg + +@brief Socket recv status. + + Socket receive information is returned through this structure in response to the asynchronous call to the @ref recv or @ref recvfrom socket functions. + This structure, together with the events @ref SOCKET_MSG_RECV or @ref SOCKET_MSG_RECVFROM, is passed-in parameters to the callback function. +@remark + After receiving this structure, the application should issue a new call to @ref recv or @ref recvfrom in order to receive subsequent data. + In the case of @ref SOCKET_MSG_RECVFROM (UDP), any further data in the same datagram is dropped, then subsequent datagrams are buffered on the WINC until the application provides a buffer via a new call to @ref recvfrom. + In the case of @ref SOCKET_MSG_RECV (TCP), all subsequent data is buffered on the WINC until the application provides a buffer via a new call to @ref recv. + A negative or zero buffer size indicates an error with the following code: + @ref SOCK_ERR_NO_ERROR : Socket connection closed. The application should now call @ref shutdown(). + @ref SOCK_ERR_CONN_ABORTED : Socket connection aborted. The application should now call @ref shutdown(). + @ref SOCK_ERR_TIMEOUT : Socket receive timed out. The socket connection remains open. +*/ +typedef struct { + uint8_t *pu8Buffer; + /*!< + Pointer to the USER buffer (passed to @ref recv and @ref recvfrom function) containing the received data chunk. + */ + int16_t s16BufferSize; + /*!< + The received data chunk size. + Holds a negative value if there is a receive error or ZERO on success upon reception of close socket message. + */ + uint16_t u16RemainingSize; + /*!< + This field is used internally by the driver. In normal operation, this field will be 0 when the application receives this structure. + */ + struct sockaddr_in strRemoteAddr; + /*!< + Socket address structure for the remote peer. It is valid for @ref SOCKET_MSG_RECVFROM event. + */ +} tstrSocketRecvMsg; +/**@}*/ // AsyncCallback + +/**@defgroup SocketCallbacks Callbacks + * @ingroup SOCKETAPI + * @{ + */ +/*! +@typedef \ + tpfAppSocketCb + +@brief + The main socket application callback function. Applications register their main socket application callback through this function by calling @ref registerSocketCallback. + In response to events received, the following callback function is called to handle the corresponding asynchronous function called. Example: @ref bind, @ref connect,...etc. + +@param [in] sock + Socket ID for the callback. + + The socket callback function is called whenever a new event is received in response + to socket operations. + +@param [in] u8Msg + Socket event type. Possible values are: + - @ref SOCKET_MSG_BIND + - @ref SOCKET_MSG_LISTEN + - @ref SOCKET_MSG_ACCEPT + - @ref SOCKET_MSG_CONNECT + - @ref SOCKET_MSG_RECV + - @ref SOCKET_MSG_SEND + - @ref SOCKET_MSG_SENDTO + - @ref SOCKET_MSG_RECVFROM + - @ref SOCKET_MSG_SECURE + +@param [in] pvMsg + Pointer to message structure. Existing types are: + - tstrSocketBindMsg + - tstrSocketListenMsg + - tstrSocketAcceptMsg + - tstrSocketConnectMsg + - tstrSocketRecvMsg + +@see + tenuSocketCallbackMsgType + tstrSocketRecvMsg + tstrSocketConnectMsg + tstrSocketAcceptMsg + tstrSocketListenMsg + tstrSocketBindMsg +*/ +typedef void (*tpfAppSocketCb)(SOCKET sock, uint8_t u8Msg, void *pvMsg); + +/*! +@typedef \ + tpfAppResolveCb + +@brief + DNS resolution callback function. + Applications requiring DNS resolution should register their callback through this function by calling @ref registerSocketCallback. + The following callback is triggered in response to an asynchronous call to the @ref gethostbyname function (DNS Resolution callback). + +@param [in] pu8DomainName + Domain name of the host. + +@param [in] u32ServerIP + Server IPv4 address encoded in Network byte order format. If it is Zero, then the DNS resolution failed. +*/ +typedef void (*tpfAppResolveCb)(uint8_t *pu8DomainName, uint32_t u32ServerIP); + +/*! +@typedef \ + tpfPingCb + +@brief PING Callback + + The function delivers the ping statistics for the sent ping triggered by calling + @ref m2m_ping_req. + +@param [in] u32IPAddr + Destination IP. + +@param [in] u32RTT + Round Trip Time. + +@param [in] u8ErrorCode + Ping error code. It may be one of: + - PING_ERR_SUCCESS + - PING_ERR_DEST_UNREACH + - PING_ERR_TIMEOUT +*/ +typedef void (*tpfPingCb)(uint32_t u32IPAddr, uint32_t u32RTT, uint8_t u8ErrorCode); +/**@}*/ //SocketCallbacks + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +FUNCTION PROTOTYPES +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ +/**@defgroup SocketAPI Functions + * @ingroup SOCKETAPI + * The function performs the necessary initializations for the socket library through the following steps: + * - A check made by the global variable gbSocketInit, ensuring that initialization for sockets is performed only once, + * in-order to prevent resetting the socket instances already created in the global socket array (gastrSockets). + * - Zero initializations to the global socket array (gastrSockets), which holds the list of TCP sockets. + * - Registers the socket (Host Interface)hif callback function through the call to the hif_register_cb function. + * This facilitates handling all of the socket related functions received through interrupts from the firmware. + * @{ + */ +/*! + * @fn void socketInit(void); + The function performs the necessary initialization for the socket library through the following steps: + - A check made by the global variable gbSocketInit, ensuring that initialization for sockets is performed only once, + in-order to prevent resetting the socket instances already created in the global socket array (gastrSockets). + - Zero initializations to the global socket array (gastrSockets), which holds the list of TCP sockets. + - Registers the socket (Host Interface)hif callback function through the call to the hif_register_cb function. + This facilitates handling all of the socket related functions received through interrupts from the firmware. + +@return void + +@remarks + This initialization function must be invoked before any socket operation is performed. + No error codes from this initialization function since the socket array is statically allocated based on the maximum number of + sockets @ref MAX_SOCKET which is tuned to the systems capability. +\section SocketExample1 Example +This example demonstrates the use of the socketInit for socket initialization for an mqtt chat application. + \code + tstrWifiInitParam param; + tstrNetworkId strNetworkId; + tstrAuthPsk strAuthPsk; + int8_t ret; + char topic[strlen(MAIN_CHAT_TOPIC) + MAIN_CHAT_USER_NAME_SIZE + 1]; + + //Initialize the board. + system_init(); + + //Initialize the UART console. + configure_console(); + + // Initialize the BSP. + nm_bsp_init(); + + ---------- + + // Initialize socket interface. + socketInit(); + registerSocketCallback(socket_event_handler, socket_resolve_handler); + + // Connect to router. + strNetworkId.pu8Bssid = NULL; + strNetworkId.pu8Ssid = MAIN_WLAN_SSID; + strNetworkId.u8SsidLen = sizeof(MAIN_WLAN_SSID); + strNetworkId.u8Channel = M2M_WIFI_CH_ALL; + + strAuthPsk.pu8Psk = NULL; + strAuthPsk.pu8Passphrase = MAIN_WLAN_PSK; + strAuthPsk.u8PassphraseLen = (uint8_t)strlen((char*)MAIN_WLAN_PSK); + + m2m_wifi_connect_psk(WIFI_CRED_SAVE_ENCRYPTED, &strNetworkId, &strAuthPsk); +\endcode +*/ +void socketInit(void); + +/*! +@fn \ + void socketDeinit(void); + +@brief Socket Layer De-initialization + + The function performs the necessary cleanup for the socket library static data + It must be invoked only after all desired socket operations have been performed on any active sockets. +*/ +void socketDeinit(void); + +/*! +@fn \ + uint8_t IsSocketReady(void); + +@see socketInit + socketDeinit +@return If the socket has been initialized and is ready. + Should return 1 after @ref socketInit and 0 after @ref socketDeinit +*/ +uint8_t IsSocketReady(void); + +/*! +@fn \ + void registerSocketCallback(tpfAppSocketCb socket_cb, tpfAppResolveCb resolve_cb); + Register two callback functions one for asynchronous socket events and the other one for DNS callback registering function. + The registered callback functions are used to retrieve information in response to the asynchronous socket functions called. +@param [in] socket_cb + Assignment of callback function to the global callback @ref tpfAppSocketCb gpfAppSocketCb. Delivers + socket messages to the host application. In response to the asynchronous function calls, such as @ref bind + @ref listen @ref accept @ref connect + +@param [in] resolve_cb + Assignment of callback function to the global callback @ref tpfAppResolveCb gpfAppResolveCb. + Used for DNS resolving functionality. The DNS resolving technique is determined by the application + registering the callback. + NULL is assigned when DNS resolution is not required. + +@return void +@remarks + If any of the socket functionality is not to be used, NULL is passed in as a parameter. + It must be invoked after @ref socketInit and before other socket layer operations. + +\section SocketExample2 Example + This example demonstrates the use of the registerSocketCallback to register a socket callback function with DNS resolution CB set to null + for a simple UDP server example. + \code + tstrWifiInitParam param; + tstrNetworkId strNetworkId; + tstrAuthPsk strAuthPsk; + int8_t ret; + struct sockaddr_in addr; + + // Initialize the board + system_init(); + + //Initialize the UART console. + configure_console(); + + // Initialize the BSP. + nm_bsp_init(); + + // Initialize socket address structure. + addr.sin_family = AF_INET; + addr.sin_port = _htons(MAIN_WIFI_M2M_SERVER_PORT); + addr.sin_addr.s_addr = _htonl(MAIN_WIFI_M2M_SERVER_IP); + + // Initialize Wi-Fi parameters structure. + memset((uint8_t *)¶m, 0, sizeof(tstrWifiInitParam)); + + // Initialize Wi-Fi driver with data and status callbacks. + param.pfAppWifiCb = wifi_cb; + ret = m2m_wifi_init(¶m); + if (M2M_SUCCESS != ret) { + printf("main: m2m_wifi_init call error!(%d)\r\n", ret); + while (1) { + } + } + + // Initialize socket module + socketInit(); + registerSocketCallback(socket_cb, NULL); + + // Connect to router. + strNetworkId.pu8Bssid = NULL; + strNetworkId.pu8Ssid = MAIN_WLAN_SSID; + strNetworkId.u8SsidLen = sizeof(MAIN_WLAN_SSID); + strNetworkId.u8Channel = M2M_WIFI_CH_ALL; + + strAuthPsk.pu8Psk = NULL; + strAuthPsk.pu8Passphrase = MAIN_WLAN_PSK; + strAuthPsk.u8PassphraseLen = (uint8_t)strlen((char*)MAIN_WLAN_PSK); + + m2m_wifi_connect_psk(WIFI_CRED_SAVE_ENCRYPTED, &strNetworkId, &strAuthPsk); +\endcode +*/ +//void registerSocketCallback(tpfAppSocketCb socket_cb, tpfAppResolveCb resolve_cb); +void registerSocketEventCallback(tpfAppSocketCb socket_cb); +void registerSocketResolveCallback(tpfAppResolveCb resolve_cb); + +/*! +@fn \ + SOCKET socket(uint16_t u16Domain, uint8_t u8Type, uint8_t u8Config); + Synchronous socket allocation function based on the specified socket type. Created sockets are non-blocking and their possible types are either TCP or a UDP sockets. + The maximum allowed number of TCP sockets is @ref TCP_SOCK_MAX sockets while the maximum number of UDP sockets that can be created simultaneously is @ref UDP_SOCK_MAX sockets. + +@param [in] u16Domain + Socket family. The only allowed value is AF_INET (IPv4.0) for TCP/UDP sockets. + +@param [in] u8Type + Socket type. Allowed values are: + - [SOCK_STREAM](@ref SOCK_STREAM) + - [SOCK_DGRAM](@ref SOCK_DGRAM) + - [SOCK_RAW](@ref SOCK_RAW) + +@param[in] u8Config + Used to specify the socket configuration. The interpretation of + this parameter depends on the setting of u8Type.\n + - When u8Type is [SOCK_STREAM](@ref SOCK_STREAM) the following configuration values + are defined:\n + - [SOCKET_CONFIG_SSL_OFF](@ref SOCKET_CONFIG_SSL_OFF): The socket is not secured by + TLS.\n + - [SOCKET_CONFIG_SSL_ON](@ref SOCKET_CONFIG_SSL_ON): The socket is secured by TLS.\n + - [SOCKET_CONFIG_SSL_DELAY](@ref SOCKET_CONFIG_SSL_DELAY): The socket is not secured + by TLS, but may be secured later, by calling @ref secure.\n + - When u8Type is [SOCK_RAW](@ref SOCK_RAW), the following configuration values + are defined:\n + - [SOCKET_CONFIG_IPPROTO_RAW](@ref SOCKET_CONFIG_IPPROTO_RAW): The socket is to be + used for raw ICMP frames.\n + - For all values of u8Type, the following configuration values + are defined:\n + - [SOCKET_CONFIG_DEFAULT](@ref SOCKET_CONFIG_DEFAULT): The default configuration.\n + All other configuration values are reserved and should not be + used. + +@pre + The @ref socketInit function must be called once at the beginning of the application to initialize the socket handler. + before any call to the @ref socket function can be made. + +@see + connect + secure + bind + listen + accept + recv + recvfrom + send + sendto + shutdown + setsockopt + getsockopt + +@return + On successful socket creation, a non-blocking socket type is created and a socket ID is returned + In case of failure the function returns a negative value, identifying one of the socket error codes defined. + For example: @ref SOCK_ERR_INVALID for invalid argument or + @ref SOCK_ERR_MAX_TCP_SOCK if the number of TCP allocated sockets exceeds the number of available sockets. + +@remarks + The socket function must be called before any other related socket functions "e.g. send, recv, shutdown ..etc" +\section SocketExample3 Example + This example demonstrates the use of the socket function to allocate the socket, returning the socket handler to be used for other +socket operations. Socket creation is dependent on the socket type. +\subsection sub1 UDP example +@code + SOCKET UdpServerSocket = -1; + + UdpServerSocket = socket(AF_INET, SOCK_DGRAM, SOCKET_CONFIG_DEFAULT); + +@endcode +\subsection sub2 TCP example +@code + static SOCKET tcp_client_socket = -1; + + tcp_client_socket = socket(AF_INET, SOCK_STREAM, SOCKET_CONFIG_DEFAULT)); +@endcode +*/ +SOCKET socket(uint16_t u16Domain, uint8_t u8Type, uint8_t u8Config); +/*! +\fn \ + int8_t bind(SOCKET sock, struct sockaddr *pstrAddr, uint8_t u8AddrLen); + Asynchronous bind function associates the provided address and local port to the socket. + The function can be used with both TCP and UDP sockets. It is mandatory to call the @ref bind function before starting any UDP or TCP server operation. + Upon socket bind completion, the application will receive a @ref SOCKET_MSG_BIND message in the socket callback. +@param [in] sock + Socket ID, must hold a non negative value. + A negative value will return a socket error @ref SOCK_ERR_INVALID_ARG. Indicating that an invalid argument is passed in. + +@param [in] pstrAddr + Pointer to socket address structure "sockaddr_in" + [sockaddr_in](@ref sockaddr_in) + + +@param [in] u8AddrLen + Size of the given socket address structure in bytes. + +@pre + The socket function must be called to allocate a socket before passing the socket ID to the bind function. + +@see socket +@see connect +@see listen +@see accept +@see recv +@see recvfrom +@see send +@see sendto + +@return + The function returns ZERO for successful operations and a negative value otherwise. + The possible error values are: + - [SOCK_ERR_NO_ERROR](@ref SOCK_ERR_NO_ERROR) + Indicating that the operation was successful. + + - [SOCK_ERR_INVALID_ARG](@ref SOCK_ERR_INVALID_ARG) + Indicating passing invalid arguments such as negative socket ID or NULL socket address structure. + + - [SOCK_ERR_INVALID](@ref SOCK_ERR_INVALID) + Indicate socket bind failure. +\section SocketExample4 Example + This example demonstrates the call of the bind socket operation after a successful socket operation. +@code + struct sockaddr_in addr; + SOCKET udpServerSocket =-1; + int ret = -1; + + if(udpServerSocket == -1) + { + udpServerSocket = socket(AF_INET,SOCK_DGRAM,SOCKET_CONFIG_DEFAULT); + if(udpServerSocket >= 0) + { + addr.sin_family = AF_INET; + addr.sin_port = _htons(UDP_SERVER_PORT); + addr.sin_addr.s_addr = 0; + ret = bind(udpServerSocket,(struct sockaddr*)&addr,sizeof(addr)); + + if(ret == 0) + printf("Bind success!\n"); + else + { + printf("Bind Failed. Error code = %d\n",ret); + shutdown(udpServerSocket); + } + else + { + printf("UDP Server Socket Creation Failed\n"); + return; + } + } +@endcode +*/ +int8_t bind(SOCKET sock, struct sockaddr *pstrAddr, uint8_t u8AddrLen); +/*! +@fn \ + int8_t listen(SOCKET sock, uint8_t backlog); + + After successfully binding a socket to an IP address and port on the system, start listening passively for incoming connections. + The socket must be bound on a local port or the listen operation fails. + Upon the call to the asynchronous listen function, response is received through the event @ref SOCKET_MSG_LISTEN + in the socket callback. + + A successful listen means the TCP server operation is active. If a connection is accepted, then the application socket callback function is + notified with the new connected socket through the event @ref SOCKET_MSG_ACCEPT. Hence there is no need to call the @ref accept function + after calling @ref listen. + + After a connection is accepted, the user is then required to call @ref recv to receive any packets transmitted by the remote host or to receive notification of socket connection + termination. +@param [in] sock + Socket ID, must hold a non negative value. + A negative value will return a socket error @ref SOCK_ERR_INVALID_ARG. Indicating that an invalid argument is passed in. + +@param [in] backlog + Not used by the current implementation. + +@pre + The bind function must be called to assign the port number and IP address to the socket before the listen operation. + +@see bind +@see accept +@see recv +@see recvfrom +@see send +@see sendto + +@return + The function returns ZERO for successful operations and a negative value otherwise. + The possible error values are: + - [SOCK_ERR_NO_ERROR](@ref SOCK_ERR_NO_ERROR) + Indicating that the operation was successful. + + - [SOCK_ERR_INVALID_ARG](@ref SOCK_ERR_INVALID_ARG) + Indicating invalid arguments were passed, such as negative socket ID. + + - [SOCK_ERR_INVALID](@ref SOCK_ERR_INVALID) + Indicate socket listen failure. +\section SocketExample5 Example +This example demonstrates the call of the listen socket operation after a successful socket operation. +@code + static void TCP_Socketcallback(SOCKET sock, uint8_t u8Msg, void * pvMsg) + { + int ret =-1; + + switch(u8Msg) + { + case SOCKET_MSG_BIND: + { + tstrSocketBindMsg *pstrBind = (tstrSocketBindMsg*)pvMsg; + if(pstrBind != NULL) + { + if(pstrBind->status == 0) + { + ret = listen(sock, 0); + + if(ret <0) + printf("Listen failure! Error = %d\n",ret); + } + else + { + M2M_ERR("bind Failure!\n"); + shutdown(sock); + } + } + } + break; + + case SOCKET_MSG_LISTEN: + { + + tstrSocketListenMsg *pstrListen = (tstrSocketListenMsg*)pvMsg; + if(pstrListen != NULL) + { + if(pstrListen->status == 0) + { + ret = accept(sock,NULL,0); + } + else + { + M2M_ERR("listen Failure!\n"); + shutdown(sock); + } + } + } + break; + + case SOCKET_MSG_ACCEPT: + { + tstrSocketAcceptMsg *pstrAccept = (tstrSocketAcceptMsg*)pvMsg; + + if(pstrAccept->sock >= 0) + { + TcpNotificationSocket = pstrAccept->sock; + recv(pstrAccept->sock,gau8RxBuffer,sizeof(gau8RxBuffer),TEST_RECV_TIMEOUT); + } + else + { + M2M_ERR("accept failure\n"); + } + } + break; + + default: + break; + } + } + +@endcode +*/ +int8_t listen(SOCKET sock, uint8_t backlog); +/*! +@fn \ + int8_t accept(SOCKET sock, struct sockaddr *addr, uint8_t *addrlen); + The function has no current implementation. An empty declaration is used to prevent errors when legacy application code is used. + As it has no effect, it can be safely removed from any application using it. +@param [in] sock + Socket ID, must hold a non negative value. + A negative value will return a socket error @ref SOCK_ERR_INVALID_ARG. Indicating that an invalid argument is passed in. +@param [in] addr + Not used in the current implementation. + +@param [in] addrlen + Not used in the current implementation. + +@return + The function returns ZERO for successful operations and a negative value otherwise. + The possible error values are: + - [SOCK_ERR_NO_ERROR](@ref SOCK_ERR_NO_ERROR) + Indicating that the operation was successful. + + - [SOCK_ERR_INVALID_ARG](@ref SOCK_ERR_INVALID_ARG) + Indicating passing invalid arguments such as negative socket ID. +*/ +int8_t accept(SOCKET sock, struct sockaddr *addr, uint8_t *addrlen); +/*! +@fn \ + int8_t connect(SOCKET sock, struct sockaddr *pstrAddr, uint8_t u8AddrLen); + Establishes a TCP connection with a remote server. + The asynchronous connect function must be called after receiving a valid socket ID from the @ref socket function. + The application socket callback function is notified of the result of the connection attempt through the event @ref SOCKET_MSG_CONNECT, + along with a structure @ref tstrSocketConnectMsg. + If socket connection fails, the application should call @ref shutdown(). + A successful connect means the TCP session is active. The application is then required to make a call to the @ref recv function + to receive any packets transmitted by the remote server, unless the application is interrupted by a notification of socket connection + termination. +@param [in] sock + Socket ID, must hold a non negative value. + A negative value will return a socket error @ref SOCK_ERR_INVALID_ARG. Indicating that an invalid argument is passed in. + +@param [in] pstrAddr + Address of the remote server. + +@param [in] u8AddrLen + Size of the given socket address structure in bytes. + Not currently used, implemented for BSD compatibility only. +@pre + The socket function must be called to allocate a TCP socket before passing the socket ID to the bind function. + If the socket is not bound, you do NOT have to call bind before the "connect" function. + +@see + socket + recv + send + shutdown + +@return + The function returns ZERO for successful operations and a negative value otherwise. + The possible error values are: + - [SOCK_ERR_NO_ERROR](@ref SOCK_ERR_NO_ERROR) + Indicating that the operation was successful. + + - [SOCK_ERR_INVALID_ARG](@ref SOCK_ERR_INVALID_ARG) + Indicating passing invalid arguments such as negative socket ID or NULL socket address structure. + + - [SOCK_ERR_INVALID](@ref SOCK_ERR_INVALID) + Indicate socket connect failure. +\section SocketExample6 Example + The example demonstrates a TCP application, showing how the asynchronous call to the connect function is made through the main function and how the + callback function handles the @ref SOCKET_MSG_CONNECT event. +\subsection sub3 Main Function +@code + struct sockaddr_in Serv_Addr; + SOCKET TcpClientSocket =-1; + int ret = -1 + + TcpClientSocket = socket(AF_INET,SOCK_STREAM,SOCKET_CONFIG_DEFAULT); + Serv_Addr.sin_family = AF_INET; + Serv_Addr.sin_port = _htons(1234); + Serv_Addr.sin_addr.s_addr = inet_addr(SERVER); + printf("Connected to server via socket %u\n",TcpClientSocket); + + do + { + ret = connect(TcpClientSocket,(sockaddr_in*)&Serv_Addr,sizeof(Serv_Addr)); + if(ret != 0) + { + printf("Connection Error\n"); + } + else + { + printf("Connection successful.\n"); + break; + } + }while(1) +@endcode +\subsection sub4 Socket Callback +@code + if(u8Msg == SOCKET_MSG_CONNECT) + { + tstrSocketConnectMsg *pstrConnect = (tstrSocketConnectMsg*)pvMsg; + if(pstrConnect->s8Error == 0) + { + uint8_t acBuffer[GROWL_MSG_SIZE]; + uint16_t u16MsgSize; + + printf("Connect success!\n"); + + u16MsgSize = FormatMsg(u8ClientID, acBuffer); + send(sock, acBuffer, u16MsgSize, 0); + recv(pstrNotification->Socket, (void*)au8Msg,GROWL_DESCRIPTION_MAX_LENGTH, GROWL_RX_TIMEOUT); + u8Retry = GROWL_CONNECT_RETRY; + } + else + { + M2M_DBG("Connection Failed, Error: %d\n",pstrConnect->s8Error"); + shutdown(pstrNotification->Socket); + } + } +@endcode +*/ +int8_t connect(SOCKET sock, struct sockaddr *pstrAddr, uint8_t u8AddrLen); +/*! +@fn \ + int8_t secure(SOCKET sock); + + Converts an (insecure) TCP connection with a remote server into a secure TLS-over-TCP connection. + It may be called after both of the following:\n + - a TCP socket has been created by the @ref socket function, with u8Config parameter set to + @ref SOCKET_CONFIG_SSL_DELAY.\n + - a successful connection has been made on the socket via the @ref connect function. + This is an asynchronous API; the application socket callback function is notified of the result + of the attempt to make the connection secure through the event @ref SOCKET_MSG_SECURE, along + with a structure @ref tstrSocketConnectMsg. + If the attempt to make the connection secure fails, the application should call @ref close(). + +@param[in] sock + Socket ID, corresponding to a connected TCP socket. + +@pre + @ref socket and @ref connect must be called to connect a TCP socket before passing the socket ID to this function. + Value @ref SOCKET_CONFIG_SSL_DELAY must have been set in the u8Config parameter that was passed to @ref socket. + +@see + socket + connect + +@return + The function returns SOCK_ERR_NO_ERROR for successful operations and a negative error value otherwise. + The possible error values are: + - @ref SOCK_ERR_INVALID_ARG + Indicating passing invalid arguments such as negative socket ID. + + - @ref SOCK_ERR_INVALID + Indicating failure to process the request. +*/ +int8_t secure(SOCKET sock); +/*! +@fn \ + int16_t recv(SOCKET sock, void *pvRecvBuf, uint16_t u16BufLen, uint32_t u32Timeoutmsec); + + An asynchronous receive function, used to retrieve data from a TCP stream. + Before calling the recv function, a successful socket connection status must have been received through any of the two socket events + [SOCKET_MSG_CONNECT] or [SOCKET_MSG_ACCEPT], from the socket callback. Hence, indicating that the socket is already connected to a remote + host. + The application receives the required data in response to this asynchronous call through the reception of the event @ref SOCKET_MSG_RECV in the + socket callback. + + Receiving the SOCKET_MSG_RECV message in the callback with zero or negative buffer length indicates the following: + - @ref SOCK_ERR_NO_ERROR : Socket connection closed. The application should now call @ref shutdown(). + - @ref SOCK_ERR_CONN_ABORTED : Socket connection aborted. The application should now call @ref shutdown(). + - @ref SOCK_ERR_TIMEOUT : Socket receive timed out. The socket connection remains open. + +@param [in] sock + Socket ID, must hold a non negative value. + A negative value will return a socket error @ref SOCK_ERR_INVALID_ARG. Indicating that an invalid argument is passed in. + + +@param [in] pvRecvBuf + Pointer to a buffer that will hold the received data. The buffer is used + in the recv callback to deliver the received data to the caller. The buffer must + be resident in memory (heap or global buffer). + +@param [in] u16BufLen + The buffer size in bytes. + +@param [in] u32Timeoutmsec + Timeout for the recv function in milli-seconds. If the value is set to ZERO, the timeout + will be set to infinite (the recv function waits forever). If the timeout period is + elapsed with no data received, the socket will get a timeout error. +@pre + - The socket function must be called to allocate a TCP socket before passing the socket ID to the recv function. + - The socket in a connected state is expected to receive data through the socket interface. + +@see socket +@see connect +@see bind +@see listen +@see recvfrom +@see shutdown + +@return + The function returns ZERO for successful operations and a negative value otherwise. + The possible error values are: + - [SOCK_ERR_NO_ERROR](@ref SOCK_ERR_NO_ERROR) + Indicating that the operation was successful. + + - [SOCK_ERR_INVALID_ARG](@ref SOCK_ERR_INVALID_ARG) + Indicating passing invalid arguments such as negative socket ID or NULL receive buffer. + + - [SOCK_ERR_BUFFER_FULL](@ref SOCK_ERR_BUFFER_FULL) + Indicate socket receive failure. +\section SocketExample7 Example + The example demonstrates a code snippet for the calling of the recv function in the socket callback upon notification of the accept or connect events, and the parsing of the + received data when the SOCKET_MSG_RECV event is received. +@code + + switch(u8Msg) + { + + case SOCKET_MSG_ACCEPT: + { + tstrSocketAcceptMsg *pstrAccept = (tstrSocketAcceptMsg*)pvMsg; + + if(pstrAccept->sock >= 0) + { + recv(pstrAccept->sock,gau8RxBuffer,sizeof(gau8RxBuffer),TEST_RECV_TIMEOUT); + } + else + { + M2M_ERR("accept\n"); + } + } + break; + + + case SOCKET_MSG_RECV: + { + tstrSocketRecvMsg *pstrRx = (tstrSocketRecvMsg*)pvMsg; + + if(pstrRx->s16BufferSize > 0) + { + + recv(sock,gau8RxBuffer,sizeof(gau8RxBuffer),TEST_RECV_TIMEOUT); + } + else + { + printf("Socket recv Error: %d\n",pstrRx->s16BufferSize); + shutdown(sock); + } + } + break; + + default: + break; + } +@endcode +*/ +int16_t recv(SOCKET sock, void *pvRecvBuf, uint16_t u16BufLen, uint32_t u32Timeoutmsec); +/*! +@fn \ + int16_t recvfrom(SOCKET sock, void *pvRecvBuf, uint16_t u16BufLen, uint32_t u32TimeoutSeconds); + Receives data from a UDP Socket. + + The asynchronous recvfrom function is used to retrieve data from a UDP socket. The socket must already be bound to + a local port before a call to the recvfrom function is made (i.e message @ref SOCKET_MSG_BIND is received + with successful status in the socket callback). + + Upon calling the recvfrom function with a successful return code, the application is expected to receive a notification + in the socket callback whenever a message is received through the @ref SOCKET_MSG_RECVFROM event. + + Receiving the SOCKET_MSG_RECVFROM message in the callback with zero, indicates that the socket is closed. + Whereby a negative buffer length indicates one of the socket error codes such as socket timeout error @ref SOCK_ERR_TIMEOUT + + The recvfrom callback can also be used to show the IP address of the remote host that sent the frame by + using the "strRemoteAddr" element in the @ref tstrSocketRecvMsg structure. (refer to the code example) +@param [in] sock + Socket ID, must hold a non negative value. + A negative value will return a socket error @ref SOCK_ERR_INVALID_ARG. Indicating that an invalid argument is passed in. + +@param [in] pvRecvBuf + Pointer to a buffer that will hold the received data. The buffer shall be used + in the recv callback to deliver the received data to the caller. The buffer must + be resident in memory (heap or global buffer). + +@param [in] u16BufLen + The buffer size in bytes. + +@param [in] u32TimeoutSeconds + Timeout for the recv function in milli-seconds. If the value is set to ZERO, the timeout + will be set to infinite (the recv function waits forever). + +@pre + - The socket function must be called to allocate a TCP socket before passing the socket ID to the recv function. + - The socket corresponding to the socket ID must be successfully bound to a local port through the call to a @ref bind function. + +@see + socket + bind + shutdown + +@return + The function returns ZERO for successful operations and a negative value otherwise. + The possible error values are: + - [SOCK_ERR_NO_ERROR](@ref SOCK_ERR_NO_ERROR) + Indicating that the operation was successful. + + - [SOCK_ERR_INVALID_ARG](@ref SOCK_ERR_INVALID_ARG) + Indicating passing invalid arguments such as negative socket ID or NULL Receive buffer. + + - [SOCK_ERR_BUFFER_FULL](@ref SOCK_ERR_BUFFER_FULL) + Indicate socket receive failure. +\section SocketExample8 Example + The example demonstrates a code snippet for the calling of the recvfrom function in the socket callback upon notification of a successful bind event, and the parsing of the + received data when the SOCKET_MSG_RECVFROM event is received. +@code + switch(u8Msg) + { + + case SOCKET_MSG_BIND: + { + tstrSocketBindMsg *pstrBind = (tstrSocketBindMsg*)pvMsg; + + if(pstrBind != NULL) + { + if(pstrBind->status == 0) + { + recvfrom(sock, gau8SocketTestBuffer, TEST_BUFFER_SIZE, 0); + } + else + { + M2M_ERR("bind\n"); + } + } + } + break; + + + case SOCKET_MSG_RECVFROM: + { + tstrSocketRecvMsg *pstrRx = (tstrSocketRecvMsg*)pvMsg; + + if(pstrRx->s16BufferSize > 0) + { + //get the remote host address and port number + uint16_t u16port = pstrRx->strRemoteAddr.sin_port; + uint32_t strRemoteHostAddr = pstrRx->strRemoteAddr.sin_addr.s_addr; + + printf("Received frame with size = %d.\tHost address=%x, Port number = %d\n\n",pstrRx->s16BufferSize,strRemoteHostAddr, u16port); + + ret = recvfrom(sock,gau8SocketTestBuffer,sizeof(gau8SocketTestBuffer),TEST_RECV_TIMEOUT); + } + else + { + printf("Socket recv Error: %d\n",pstrRx->s16BufferSize); + ret = shutdown(sock); + } + } + break; + + default: + break; + } +@endcode +*/ +int16_t recvfrom(SOCKET sock, void *pvRecvBuf, uint16_t u16BufLen, uint32_t u32Timeoutmsec); +/*! +@fn \ + int16_t send(SOCKET sock, void *pvSendBuffer, uint16_t u16SendLength, uint16_t u16Flags); + Asynchronous sending function, used to send data on a TCP/UDP socket. + + Called by the application code when there is outgoing data available required to be sent on a specific socket handler. + The only difference between this function and the similar @ref sendto function, is the type of socket the data is sent on and the parameters passed in. + @ref send function is most commonly called for sockets in a connected state. + After the data is sent, the socket callback function registered using registerSocketCallback(), is expected to receive an event of type + @ref SOCKET_MSG_SEND holding information containing the number of data bytes sent. +@param [in] sock + Socket ID, must hold a non negative value. + A negative value will return a socket error @ref SOCK_ERR_INVALID_ARG. Indicating that an invalid argument is passed in. + +@param [in] pvSendBuffer + Pointer to a buffer holding data to be transmitted. + +@param [in] u16SendLength + The buffer size in bytes. + +@param [in] u16Flags + Not used in the current implementation. + +@pre + Sockets must be initialized using socketInit. \n + + For TCP Socket:\n + Must use a successfully connected Socket (so that the intended recipient address is known ahead of sending the data). + Hence this function is expected to be called after a successful socket connect operation(in client case or accept in the + the server case).\n + + For UDP Socket:\n + UDP sockets most commonly use @ref sendto function, where the destination address is defined. However, in-order to send outgoing data + using the @ref send function, at least one successful call must be made to the @ref sendto function before consecutive calls to the @ref send function, + to ensure that the destination address is saved in the firmware. + +@see + socketInit + recv + sendto + socket + connect + accept + sendto + +@warning + u16SendLength must not exceed @ref SOCKET_BUFFER_MAX_LENGTH. \n + Use a valid socket identifier through the prior call to the @ref socket function. + Must use a valid buffer pointer. + Successful completion of a call to send() does not guarantee delivery of the message, + A negative return value indicates only locally-detected errors + * @return The function shall return @ref SOCK_ERR_NO_ERROR for successful operation and a negative value otherwise. + */ +int16_t send(SOCKET sock, void *pvSendBuffer, uint16_t u16SendLength, uint16_t u16Flags); + +/*! + * @fn int16_t sendto(SOCKET sock, void *pvSendBuffer, uint16_t u16SendLength, uint16_t flags, struct sockaddr *pstrDestAddr, uint8_t u8AddrLen); + * @brief Asynchronous sending function, used to send data on a UDP socket. + * Called by the application code when there is data required to be sent on a UDP socket. + * @details The application code is expected to receive data from a successfully bound socket node. + * The only difference between this function and the similar @ref send function, is the type of socket the data is received on. + * This function works only with UDP sockets. + * After the data is sent, the socket callback function registered using @ref registerSocketCallback(), is expected to receive an + * event of type @ref SOCKET_MSG_SENDTO. + * @param[in] sock + * Socket ID, must hold a non negative value. + * A negative value will return a socket error @ref SOCK_ERR_INVALID_ARG. Indicating that an invalid argument is passed in. + * @param[in] pvSendBuffer + * Pointer to a buffer holding data to be transmitted. + * A NULL value will return a socket error @ref SOCK_ERR_INVALID_ARG. Indicating that an invalid argument is passed in. + * @param[in] u16SendLength + * The buffer size in bytes. It must not exceed @ref SOCKET_BUFFER_MAX_LENGTH. + * @param[in] flags + * Not used in the current implementation + * @param[in] pstrDestAddr + * The destination address. + * @param[in] u8AddrLen + * Destination address length in bytes. + * Not used in the current implementation, only included for BSD compatibility. + * @pre Sockets must be initialized using @ref socketInit. + * @see + * socketInit + * recvfrom + * sendto + * socket + * connect + * accept + * send + * @warning + * u16SendLength must not exceed @ref SOCKET_BUFFER_MAX_LENGTH.\n + * Use a valid socket (returned from socket). + * A valid buffer pointer must be used (not NULL).\n + * Successful completion of a call to sendto does not guarantee delivery of the message, + * A negative return value indicates only locally-detected errors. + * @return The function returns @ref SOCK_ERR_NO_ERROR for successful operation and a negative value (indicating the error) otherwise. +*/ +int16_t sendto(SOCKET sock, void *pvSendBuffer, uint16_t u16SendLength, uint16_t flags, struct sockaddr *pstrDestAddr, uint8_t u8AddrLen); + +/*! + * @fn int8_t shutdown(SOCKET sock); + * @brief Synchronous shutdown function, releases all the socket assigned resources. + * @param[in] sock + * Socket ID, must hold a non negative value. + * A negative value will return a socket error @ref SOCK_ERR_INVALID_ARG. Indicating that an invalid argument is passed in. + * @pre + * Sockets must be initialized through the call of the socketInit function. + * @ref shutdown is called only for valid socket identifiers created through the @ref socket function. + * @warning If @ref shutdown is called while there are still pending messages (sent or received ) they will be discarded. + * @see socketInit, socket + * @return The function returned @ref SOCK_ERR_NO_ERROR for successful operation and a negative value (indicating the error) otherwise. + */ +int8_t shutdown(SOCKET sock); + +/*! + * @fn int8_t gethostbyname(const char *pcHostName); + * Asynchronous DNS resolving function. This function uses DNS to resolve a domain name to the corresponding IP address. + * A call to this function will cause a DNS request to be sent and the response will be delivered to the DNS callback + * function registered using registerSocketCallback. + * @param[in] pcHostName + * NULL terminated string containing the domain name for the remote host. + * Its size must not exceed [HOSTNAME_MAX_SIZE](@ref HOSTNAME_MAX_SIZE). + * @see registerSocketCallback + * @warning + * Successful completion of a call to gethostbyname() does not guarantee success of the DNS request, + * a negative return value indicates only locally-detected errors + * @return + * - @ref SOCK_ERR_NO_ERROR + * - @ref SOCK_ERR_INVALID_ARG + */ +int8_t gethostbyname(const char *pcHostName); + +/*! +@fn \ + int8_t setsockopt(SOCKET socket, uint8_t u8Level, uint8_t option_name, + const void *option_value, uint16_t u16OptionLen); + + The setsockopt() function shall set the option specified by the option_name + argument, at the protocol level specified by the level argument, to the value + pointed to by the option_value argument for the socket specified by the socket argument. + +@param[in] socket + Socket handler. + +@param[in] u8Level + Protocol level.\n + Supported protocol levels are @ref SOL_SOCKET, @ref SOL_SSL_SOCKET and @ref SOL_RAW. + +@param[in] option_name + Option to be set.\n + For protocol level @ref SOL_SOCKET, the supported option names are:\n + @ref SO_SET_UDP_SEND_CALLBACK\n + @ref SO_TCP_KEEPALIVE\n + @ref SO_TCP_KEEPIDLE\n + @ref SO_TCP_KEEPINTVL\n + @ref SO_TCP_KEEPCNT\n + For protocol level @ref SOL_SSL_SOCKET, the supported option names are:\n + @ref SO_SSL_BYPASS_X509_VERIF\n + @ref SO_SSL_SNI\n + @ref SO_SSL_ENABLE_SESSION_CACHING\n + @ref SO_SSL_ENABLE_CERTNAME_VALIDATION\n + @ref SO_SSL_ALPN\n + For protocol level @ref SOL_RAW, the supported option names are:\n + @ref SO_ICMP_FILTER\n + +@param[in] option_value + Pointer to user provided value. + +@param[in] u16OptionLen + Length of the option value in bytes. Refer to each option documentation for the required length. + +@return + The function shall return \ref SOCK_ERR_NO_ERROR for successful operation + and a negative value (indicating the error) otherwise. +*/ +int8_t setsockopt(SOCKET socket, uint8_t u8Level, uint8_t option_name, + const void *option_value, uint16_t u16OptionLen); + +/*! + * @fn int8_t getsockopt(SOCKET sock, uint8_t u8Level, uint8_t u8OptName, const void *pvOptValue, uint8_t *pu8OptLen); + * @brief Get socket options retrieves. + * This Function isn't implemented yet but this is the form that will be released later. + * @param[in] sock + * Socket Identifier. + * @param[in] u8Level + * The protocol level of the option. + * @param[in] u8OptName + * The u8OptName argument specifies a single option to get. + * @param[out] pvOptValue + * The pvOptValue argument contains pointer to a buffer containing the option value. + * @param[out] pu8OptLen + * Option value buffer length. + * @return The function shall return ZERO for successful operation and a negative value otherwise. + */ +int8_t getsockopt(SOCKET sock, uint8_t u8Level, uint8_t u8OptName, const void *pvOptValue, uint8_t *pu8OptLen); + +/*! + * @fn int8_t m2m_ping_req(uint32_t u32DstIP, uint8_t u8TTL, tpfPingCb fpPingCb); + * @brief The function request to send ping request to the given IP Address. + * @param[in] u32DstIP + * Target Destination IP Address for the ping request. It must be represented in Network byte order. + * The function @ref nmi_inet_addr could be used to translate the dotted decimal notation IP + * to its Network bytes order integer representative. + * @param[in] u8TTL + * IP TTL value for the ping request. If set to ZERO, the default value SHALL be used. + * @param[in] fpPingCb + * Callback will be called to deliver the ping statistics. + + * @warning This API should only be used to request one ping at a time; calling this API invalidates callbacks + * for previous ping requests. + * @see nmi_inet_addr + * @return The function returns @ref M2M_SUCCESS for successful operations and a negative value otherwise. + */ +int8_t m2m_ping_req(uint32_t u32DstIP, uint8_t u8TTL, tpfPingCb fpPingCb); + +/*! + * @fn int8_t set_alpn_list(SOCKET sock, const char *pcProtocolList); + * + * This function sets the protocol list used for application-layer protocol negotiation (ALPN). + * If used, it must be called after creating a SSL socket (using @ref socket) and before + * connecting/binding (using @ref connect or @ref bind) or securing (using @ref secure). + * + * @param[in] sock + * Socket ID obtained by a call to @ref socket. This is the SSL socket to which + * the ALPN list applies. + * + * @param[in] pcProtocolList + * Pointer to the list of protocols available in the application. \n + * The entries in the list must: \n + * - be separated with ' ' (space). \n + * - not contain ' ' (space) or '\0' (NUL). \n + * - be non-zero length. \n + * . + * The list itself must: \n + * - be terminated with '\0' (NUL). \n + * - be no longer than @ref ALPN_LIST_MAX_APP_LENGTH, including separators (spaces) and terminator (NUL). \n + * - contain at least one entry. + * + * @return The function returns @ref M2M_SUCCESS for successful operations and a negative value otherwise. + * + * \section SocketExample9 Example + * The example demonstrates an application using @ref set_alpn_list and @ref get_alpn_index to negotiate secure HTTP/2 + * (with fallback option of HTTP/1.1). + + * \subsection sub5 Main Function + * @code + * SOCKET TcpClientSocket = socket(AF_INET, SOCK_STREAM, SOCKET_CONFIG_SSL_ON); + * if (TcpClientSocket >= 0) + * { + * struct sockaddr_in Serv_Addr = { + * .sin_family = AF_INET, + * .sin_port = _htons(1234), + * .sin_addr.s_addr = inet_addr(SERVER) + * }; + * set_alpn_list(TcpClientSocket, "h2 http/1.1"); + * connect(TcpClientSocket, &Serv_Addr, sizeof(Serv_Addr)); + * } + * @endcode + * \subsection sub6 Socket Callback + * @code + * if(u8Msg == SOCKET_MSG_CONNECT) + * { + * tstrSocketConnectMsg *pstrConnect = (tstrSocketConnectMsg*)pvMsg; + * if(pstrConnect->s8Error == 0) + * { + * uint8_t alpn_index = get_alpn_index(pstrConnect->sock); + * switch (alpn_index) + * { + * case 1: + * printf("Negotiated HTTP/2\n"); + * break; + * case 2: + * printf("Negotiated HTTP/1.1\n"); + * break; + * case 0: + * printf("Protocol negotiation did not occur\n"); + * break; + * } + * } + * } + * @endcode +*/ +int8_t set_alpn_list(SOCKET sock, const char *pcProtocolList); +/*! + * @fn int8_t get_alpn_index(SOCKET sock); + * + * This function gets the index of the protocol negotiated via ALPN. + * It should be called when a SSL socket connection succeeds, in order to determine which + * application-layer protocol must be used. + * + * @param[in] sock + * Socket ID obtained by a call to @ref socket. This is the SSL socket to which + * the ALPN applies. + * + * @return The function returns:\n + * - >0: 1-based index of negotiated protocol with respect to the list previously provided to @ref set_alpn_list.\n + * - 0: No negotiation occurred (eg TLS peer did not support ALPN).\n + * - <0: Invalid parameters (socket is not in use, or not an SSL socket).\n + * + * @see @ref SocketExample9 +*/ +int8_t get_alpn_index(SOCKET sock); + +/*! + *@fn int8_t get_error_detail(SOCKET sock, tstrSockErr *pstrErr); + * + * This function gets detail about a socket failure. The application can call this when notified + * of a socket failure via @ref SOCKET_MSG_CONNECT or @ref SOCKET_MSG_RECV. + * If used, it must be called before @ref shutdown. + + * @param[in] sock + * Socket ID obtained by a call to @ref socket. + * + * @param[out] pstrErr + * Pointer to structure to be populated with the details of the socket failure. + * + * @return The function returns @ref SOCK_ERR_NO_ERROR if the request is successful. In this case pstrErr + * has been populated. + * The function returns a negative value if the request is not successful. In this case pstrErr + * has not been populated. +*/ +int8_t get_error_detail(SOCKET sock, tstrSockErr *pstrErr); +/**@}*/ // SocketAPI + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* __SOCKET_H__ */ diff --git a/ChaletLora.X/Source/winc3400_142/socket/source/inet_addr.c b/ChaletLora.X/Source/winc3400_142/socket/source/inet_addr.c new file mode 100644 index 0000000..5c0b682 --- /dev/null +++ b/ChaletLora.X/Source/winc3400_142/socket/source/inet_addr.c @@ -0,0 +1,119 @@ +/******************************************************************************* + WINC3400 Wireless Driver + + File Name: + inet_addr.c + + Summary: + Implementation of standard inet_addr function. + + Description: + Implementation of standard inet_addr function. + *******************************************************************************/ + +//DOM-IGNORE-BEGIN +/******************************************************************************* +* Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries. +* +* Subject to your compliance with these terms, you may use Microchip software +* and any derivatives exclusively with Microchip products. It is your +* responsibility to comply with third party license terms applicable to your +* use of third party software (including open source software) that may +* accompany Microchip software. +* +* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER +* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED +* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A +* PARTICULAR PURPOSE. +* +* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, +* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND +* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS +* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE +* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN +* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, +* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. +*******************************************************************************/ + +#include +#include "socket.h" + +in_addr_t inet_addr(const char *cp) +{ + uint8_t i,l; + uint16_t t; + uint32_t ip; + char c; + + ip = 0; + + for (i=0; i<4; i++) + { + t = 0; + ip >>= 8; + + // Count non-delimiter or terminator characters + + for (l=0; l<4; l++) + { + c = cp[l]; + + if (('.' == c) || ('\0' == c)) + { + break; + } + } + + // There must be 1 to 3 characters + + if ((0 == l) || (4 == l)) + { + return 0; + } + + c = *cp++; + + // First digit can't be '0' unless it's the only one + + if ((l > 1) && (c == '0')) + { + return 0; + } + + while(l--) + { + // Each digit must be decimal + + if ((c < '0') || (c > '9')) + { + return 0; + } + + t = (t * 10) + (c - '0'); + + c = *cp++; + } + + // Total accumulated number must be less than 256 + + if (t > 255) + { + return 0; + } + + // Pack number into 32 bit IP address representation + + ip |= ((uint32_t)t << 24); + + // First three numbers must terminate with '.', last one with '\0's + + if ((('\0' == c) && (i != 3)) || (('\0' != c) && (i == 3))) + { + return 0; + } + } + + return ip; +} + +//DOM-IGNORE-END diff --git a/ChaletLora.X/Source/winc3400_142/socket/source/inet_ntop.c b/ChaletLora.X/Source/winc3400_142/socket/source/inet_ntop.c new file mode 100644 index 0000000..bdc190a --- /dev/null +++ b/ChaletLora.X/Source/winc3400_142/socket/source/inet_ntop.c @@ -0,0 +1,104 @@ +/******************************************************************************* + WINC3400 Wireless Driver + + File Name: + inet_ntop.c + + Summary: + Implementation of standard inet_ntop function. + + Description: + Implementation of standard inet_ntop function. + *******************************************************************************/ + +//DOM-IGNORE-BEGIN +/******************************************************************************* +* Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries. +* +* Subject to your compliance with these terms, you may use Microchip software +* and any derivatives exclusively with Microchip products. It is your +* responsibility to comply with third party license terms applicable to your +* use of third party software (including open source software) that may +* accompany Microchip software. +* +* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER +* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED +* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A +* PARTICULAR PURPOSE. +* +* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, +* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND +* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS +* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE +* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN +* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, +* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. +*******************************************************************************/ + +#include +#include +#include +#include "socket.h" + +const char *inet_ntop(int af, const void *src, char *dst, size_t size) +{ + uint8_t i, v, t, c, n; + char *rp = dst; + uint32_t ip = ((struct in_addr*)src)->s_addr; + + if ((NULL == src) || (NULL == dst) || (size < 16)) + { + return NULL; + } + + for (i=0; i<4; i++) + { + t = ip; + v = 100; + + // Check for zero + + if (t > 0) + { + n = 0; + + do + { + c = '0'; + while (t >= v) + { + c++; + t -= v; + } + v /= 10; + + if (('0' != c) || (n > 0)) + { + *dst++ = c; + + n++; + } + } + while (v > 0); + } + else + { + *dst++ = '0'; + } + + if (3 == i) + { + *dst++ = '\0'; + } + else + { + *dst++ = '.'; + } + + ip >>= 8; + } + + return rp; +} + +//DOM-IGNORE-END diff --git a/ChaletLora.X/Source/winc3400_142/socket/source/socket.c b/ChaletLora.X/Source/winc3400_142/socket/source/socket.c new file mode 100644 index 0000000..10e8e33 --- /dev/null +++ b/ChaletLora.X/Source/winc3400_142/socket/source/socket.c @@ -0,0 +1,1261 @@ +/******************************************************************************* + File Name: + socket.c + + Summary: + WINC3400 BSD Compatible Socket Interface + + Description: + WINC3400 BSD Compatible Socket Interface + *******************************************************************************/ + +//DOM-IGNORE-BEGIN +/******************************************************************************* +* Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries. +* +* Subject to your compliance with these terms, you may use Microchip software +* and any derivatives exclusively with Microchip products. It is your +* responsibility to comply with third party license terms applicable to your +* use of third party software (including open source software) that may +* accompany Microchip software. +* +* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER +* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED +* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A +* PARTICULAR PURPOSE. +* +* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, +* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND +* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS +* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE +* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN +* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, +* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. +*******************************************************************************/ + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +INCLUDES +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + +#include "nm_bsp.h" +#include "socket.h" +#include "m2m_hif.h" +#include "m2m_socket_host_if.h" + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +MACROS +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + +#define TLS_RECORD_HEADER_LENGTH (5) +#define ETHERNET_HEADER_OFFSET (34) +#define ETHERNET_HEADER_LENGTH (14) +#define TCP_IP_HEADER_LENGTH (40) +#define UDP_IP_HEADER_LENGTH (28) + +#define IP_PACKET_OFFSET (ETHERNET_HEADER_LENGTH + ETHERNET_HEADER_OFFSET - M2M_HIF_HDR_OFFSET) + +#define TCP_TX_PACKET_OFFSET (IP_PACKET_OFFSET + TCP_IP_HEADER_LENGTH) +#define UDP_TX_PACKET_OFFSET (IP_PACKET_OFFSET + UDP_IP_HEADER_LENGTH) +#define SSL_TX_PACKET_OFFSET (TCP_TX_PACKET_OFFSET + TLS_RECORD_HEADER_LENGTH) + +#define SOCKET_REQUEST(reqID, reqArgs, reqSize, reqPayload, reqPayloadSize, reqPayloadOffset) \ + hif_send(M2M_REQ_GROUP_IP, reqID, reqArgs, reqSize, reqPayload, reqPayloadSize, reqPayloadOffset) + + +#define SSL_FLAGS_ACTIVE NBIT0 +#define SSL_FLAGS_BYPASS_X509 NBIT1 +#define SSL_FLAGS_CACHE_SESSION NBIT4 +#define SSL_FLAGS_CHECK_CERTNAME NBIT6 +#define SSL_FLAGS_DELAY NBIT7 + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +PRIVATE DATA TYPES +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + + +/*! +* @brief +*/ +typedef struct { + SOCKET sock; + uint8_t u8Dummy; + uint16_t u16SessionID; +} tstrCloseCmd; + + +/*! +* @brief +*/ +typedef struct { + uint8_t *pu8UserBuffer; + uint16_t u16UserBufferSize; + uint16_t u16SessionID; + uint16_t u16DataOffset; + uint8_t bIsUsed; + uint8_t u8SSLFlags; + uint8_t bIsRecvPending; + uint8_t u8AlpnStatus; + uint8_t u8ErrSource; + uint8_t u8ErrCode; +} tstrSocket; + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +GLOBALS +*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + +volatile tstrSocket gastrSockets[MAX_SOCKET]; +volatile uint16_t gu16SessionID = 0; + +volatile tpfAppSocketCb gpfAppSocketCb; +volatile tpfAppResolveCb gpfAppResolveCb; +volatile uint8_t gbSocketInit = 0; + +static tpfPingCb gfpPingCb = NULL; +static uint32_t gu32PingId = 0; + +/********************************************************************* +Function + Socket_ReadSocketData + +Description + Callback function used by the WINC3400 driver to deliver messages + for socket layer. + +Return + None. +*********************************************************************/ +static void Socket_ReadSocketData(SOCKET sock, tstrSocketRecvMsg *pstrRecv, uint8_t u8SocketMsg, + uint32_t u32StartAddress, uint16_t u16ReadCount) +{ + uint32_t u32Address = u32StartAddress; + uint16_t u16Read; + int16_t s16Diff; + + pstrRecv->u16RemainingSize = u16ReadCount; + if((u16ReadCount > 0) && (gastrSockets[sock].pu8UserBuffer != NULL) && (gastrSockets[sock].u16UserBufferSize > 0) && (gastrSockets[sock].bIsUsed == 1)) + { + u16Read = u16ReadCount; + s16Diff = u16Read - gastrSockets[sock].u16UserBufferSize; + if(s16Diff > 0) + { + /* We don't expect to be here. Firmware only sends data to the driver according to the application's buffer size. + * But it is worth keeping this check, eg in case the application calls recv again with a smaller buffer size, or in case of HIF hacking. */ + u16Read = gastrSockets[sock].u16UserBufferSize; + } + + if(hif_receive(u32Address, gastrSockets[sock].pu8UserBuffer, u16Read, 1) == M2M_SUCCESS) + { + pstrRecv->pu8Buffer = gastrSockets[sock].pu8UserBuffer; + pstrRecv->s16BufferSize = u16Read; + pstrRecv->u16RemainingSize -= u16Read; + + gastrSockets[sock].u16UserBufferSize = 0; + gastrSockets[sock].pu8UserBuffer = NULL; + + if(gpfAppSocketCb) + gpfAppSocketCb(sock, u8SocketMsg, pstrRecv); + } + else + { + M2M_ERR("Current <%d>\r\n", u16ReadCount); + } + } +} +/********************************************************************* +Function + m2m_ip_cb + +Description + Callback function used by the NMC1000 driver to deliver messages + for socket layer. + +Return + None. +*********************************************************************/ +static void m2m_ip_cb(uint8_t u8OpCode, uint16_t u16BufferSize, uint32_t u32Address) +{ + if(u8OpCode == SOCKET_CMD_BIND) + { + tstrBindReply strBindReply; + tstrSocketBindMsg strBind; + + if(hif_receive(u32Address, (uint8_t*)&strBindReply, sizeof(tstrBindReply), 0) == M2M_SUCCESS) + { + strBind.status = strBindReply.s8Status; + if(gpfAppSocketCb) + gpfAppSocketCb(strBindReply.sock, SOCKET_MSG_BIND, &strBind); + } + } + else if(u8OpCode == SOCKET_CMD_LISTEN) + { + tstrListenReply strListenReply; + tstrSocketListenMsg strListen; + if(hif_receive(u32Address, (uint8_t*)&strListenReply, sizeof(tstrListenReply), 0) == M2M_SUCCESS) + { + strListen.status = strListenReply.s8Status; + if(gpfAppSocketCb) + gpfAppSocketCb(strListenReply.sock, SOCKET_MSG_LISTEN, &strListen); + } + } + else if(u8OpCode == SOCKET_CMD_ACCEPT) + { + tstrAcceptReply strAcceptReply; + tstrSocketAcceptMsg strAccept; + if(hif_receive(u32Address, (uint8_t*)&strAcceptReply, sizeof(tstrAcceptReply), 0) == M2M_SUCCESS) + { + if((strAcceptReply.sConnectedSock >= 0) && (strAcceptReply.sConnectedSock < MAX_SOCKET)) + { + gastrSockets[strAcceptReply.sConnectedSock].u8SSLFlags = 0; + gastrSockets[strAcceptReply.sConnectedSock].bIsUsed = 1; + + /* The session ID is used to distinguish different socket connections + by comparing the assigned session ID to the one reported by the firmware*/ + ++gu16SessionID; + if(gu16SessionID == 0) + ++gu16SessionID; + + gastrSockets[strAcceptReply.sConnectedSock].u16SessionID = gu16SessionID; + M2M_DBG("Socket %d session ID = %d\r\n", strAcceptReply.sConnectedSock, gu16SessionID); + } + strAccept.sock = strAcceptReply.sConnectedSock; + strAccept.strAddr.sin_family = AF_INET; + strAccept.strAddr.sin_port = strAcceptReply.strAddr.u16Port; + strAccept.strAddr.sin_addr.s_addr = strAcceptReply.strAddr.u32IPAddr; + if(gpfAppSocketCb) + gpfAppSocketCb(strAcceptReply.sListenSock, SOCKET_MSG_ACCEPT, &strAccept); + } + } + else if((u8OpCode == SOCKET_CMD_CONNECT) || (u8OpCode == SOCKET_CMD_SSL_CONNECT) || (u8OpCode == SOCKET_CMD_SSL_CONNECT_ALPN)) + { + /* Note that for successful connections the fw always sends SOCKET_CMD_CONNECT, even for SSL connections. */ + tstrConnectAlpnReply strConnectAlpnReply = {{0}}; + tstrSocketConnectMsg strConnMsg; + uint16_t u16HifSz = sizeof(tstrConnectAlpnReply); + if(u8OpCode != SOCKET_CMD_SSL_CONNECT_ALPN) + u16HifSz = sizeof(tstrConnectReply); + if(hif_receive(u32Address, (uint8_t*)&strConnectAlpnReply, u16HifSz, 0) == M2M_SUCCESS) + { + if((strConnectAlpnReply.strConnReply.sock >= 0) && (strConnectAlpnReply.strConnReply.sock < MAX_SOCKET)) + { + uint8_t u8Msg = SOCKET_MSG_CONNECT; + + strConnMsg.sock = strConnectAlpnReply.strConnReply.sock; + strConnMsg.s8Error = strConnectAlpnReply.strConnReply.s8Error; + + /* If the SOCKET_CMD_SSL_CONNECT op code is received and the socket was already connected, then the + callback corresponds to an attempt to make the socket secure. */ + if(0 != gastrSockets[strConnMsg.sock].u16DataOffset) + { + u8Msg = SOCKET_MSG_SECURE; + } + if(strConnectAlpnReply.strConnReply.s8Error == SOCK_ERR_NO_ERROR) + { + gastrSockets[strConnMsg.sock].u16DataOffset = strConnectAlpnReply.strConnReply.u16AppDataOffset - M2M_HIF_HDR_OFFSET; + gastrSockets[strConnMsg.sock].u8AlpnStatus = strConnectAlpnReply.u8AppProtocolIdx; + } + else + { + gastrSockets[strConnMsg.sock].u8ErrSource = strConnectAlpnReply.strConnReply.u8ErrSource; + gastrSockets[strConnMsg.sock].u8ErrCode = strConnectAlpnReply.strConnReply.u8ErrCode; + } + if(gpfAppSocketCb) + gpfAppSocketCb(strConnMsg.sock, u8Msg, &strConnMsg); + } + } + } + else if(u8OpCode == SOCKET_CMD_DNS_RESOLVE) + { + tstrDnsReply strDnsReply; + if(hif_receive(u32Address, (uint8_t*)&strDnsReply, sizeof(tstrDnsReply), 0) == M2M_SUCCESS) + { + strDnsReply.u32HostIP = strDnsReply.u32HostIP; + if(gpfAppResolveCb) + gpfAppResolveCb((uint8_t*)strDnsReply.acHostName, strDnsReply.u32HostIP); + } + } + else if((u8OpCode == SOCKET_CMD_RECV) || (u8OpCode == SOCKET_CMD_RECVFROM) || (u8OpCode == SOCKET_CMD_SSL_RECV)) + { + SOCKET sock; + int16_t s16RecvStatus; + tstrRecvReply strRecvReply; + uint16_t u16ReadSize; + tstrSocketRecvMsg strRecvMsg; + uint8_t u8CallbackMsgID = SOCKET_MSG_RECV; + uint16_t u16DataOffset; + + if(u8OpCode == SOCKET_CMD_RECVFROM) + u8CallbackMsgID = SOCKET_MSG_RECVFROM; + + /* Read RECV REPLY data structure. + */ + u16ReadSize = sizeof(tstrRecvReply); + if(hif_receive(u32Address, (uint8_t*)&strRecvReply, u16ReadSize, 0) == M2M_SUCCESS) + { + if((strRecvReply.sock >= 0) && (strRecvReply.sock < MAX_SOCKET)) + { + uint16_t u16SessionID = 0; + + sock = strRecvReply.sock; + u16SessionID = strRecvReply.u16SessionID; + M2M_DBG("recv callback session ID = %d\r\n", u16SessionID); + + /* Reset the Socket RX Pending Flag. + */ + gastrSockets[sock].bIsRecvPending = 0; + + s16RecvStatus = NM_BSP_B_L_16(strRecvReply.s16RecvStatus); + u16DataOffset = NM_BSP_B_L_16(strRecvReply.u16DataOffset); + strRecvMsg.strRemoteAddr.sin_port = strRecvReply.strRemoteAddr.u16Port; + strRecvMsg.strRemoteAddr.sin_addr.s_addr = strRecvReply.strRemoteAddr.u32IPAddr; + + if(u16SessionID == gastrSockets[sock].u16SessionID) + { + if((s16RecvStatus > 0) && (s16RecvStatus < u16BufferSize)) + { + /* Skip incoming bytes until reaching the Start of Application Data. + */ + u32Address += u16DataOffset; + + /* Read the Application data and deliver it to the application callback in + the given application buffer. Firmware only sends data up to + the size of the application buffer. For TCP, a new call to recv is needed + in order to retrieve any outstanding data from firmware. + */ + u16ReadSize = (uint16_t)s16RecvStatus; + Socket_ReadSocketData(sock, &strRecvMsg, u8CallbackMsgID, u32Address, u16ReadSize); + } + else + { + /* Don't tidy up here. Application must call shutdown(). + */ + strRecvMsg.s16BufferSize = s16RecvStatus; + strRecvMsg.pu8Buffer = NULL; + if(gpfAppSocketCb) + gpfAppSocketCb(sock, u8CallbackMsgID, &strRecvMsg); + } + } + else + { + M2M_DBG("Discard recv callback %d %d\r\n", u16SessionID, gastrSockets[sock].u16SessionID); + if(u16ReadSize < u16BufferSize) + hif_receive(0, NULL, 0, 1); + } + } + } + } + else if((u8OpCode == SOCKET_CMD_SEND) || (u8OpCode == SOCKET_CMD_SENDTO) || (u8OpCode == SOCKET_CMD_SSL_SEND)) + { + SOCKET sock; + int16_t s16Rcvd; + tstrSendReply strReply; + uint8_t u8CallbackMsgID = SOCKET_MSG_SEND; + + if(u8OpCode == SOCKET_CMD_SENDTO) + u8CallbackMsgID = SOCKET_MSG_SENDTO; + + if(hif_receive(u32Address, (uint8_t*)&strReply, sizeof(tstrSendReply), 0) == M2M_SUCCESS) + { + if((strReply.sock >=0) && (strReply.sock < MAX_SOCKET)) + { + uint16_t u16SessionID = 0; + + sock = strReply.sock; + u16SessionID = strReply.u16SessionID; + M2M_DBG("send callback session ID = %d\r\n", u16SessionID); + + s16Rcvd = NM_BSP_B_L_16(strReply.s16SentBytes); + + if(u16SessionID == gastrSockets[sock].u16SessionID) + { + if(gpfAppSocketCb) + gpfAppSocketCb(sock, u8CallbackMsgID, &s16Rcvd); + } + else + { + M2M_DBG("Discard send callback %d %d\r\n", u16SessionID, gastrSockets[sock].u16SessionID); + } + } + } + } + else if(u8OpCode == SOCKET_CMD_PING) + { + tstrPingReply strPingReply; + if(hif_receive(u32Address, (uint8_t*)&strPingReply, sizeof(tstrPingReply), 1) == M2M_SUCCESS) + { + if((gu32PingId == strPingReply.u32CmdPrivate) && (gfpPingCb != NULL)) + { + gfpPingCb(strPingReply.u32IPAddr, strPingReply.u32RTT, strPingReply.u8ErrorCode); + } + } + } +} +/********************************************************************* +Function + socketInit + +Description + +Return + None. +*********************************************************************/ +void socketInit(void) +{ + if(gbSocketInit==0) + { + memset((uint8_t*)gastrSockets, 0, MAX_SOCKET * sizeof(tstrSocket)); + hif_register_cb(M2M_REQ_GROUP_IP, m2m_ip_cb); + gbSocketInit=1; + gu16SessionID = 0; + } +} + +/********************************************************************* +Function + socketDeinit + +Description + +Return + None. +*********************************************************************/ +void socketDeinit(void) +{ + memset((uint8_t*)gastrSockets, 0, MAX_SOCKET * sizeof(tstrSocket)); + hif_register_cb(M2M_REQ_GROUP_IP, NULL); + gpfAppSocketCb = NULL; + gpfAppResolveCb = NULL; + gbSocketInit = 0; +} + + +/********************************************************************* +Function + registerSocketCallback + +Description + +Return + None. +*********************************************************************/ +void registerSocketCallback(tpfAppSocketCb pfAppSocketCb, tpfAppResolveCb pfAppResolveCb) +{ + gpfAppSocketCb = pfAppSocketCb; + gpfAppResolveCb = pfAppResolveCb; +} +void registerSocketEventCallback(tpfAppSocketCb pfAppSocketCb) +{ + gpfAppSocketCb = pfAppSocketCb; +} +void registerSocketResolveCallback(tpfAppResolveCb pfAppResolveCb) +{ + gpfAppResolveCb = pfAppResolveCb; +} + +/********************************************************************* +Function + socket + +Description + Creates a socket. + +Return + - Negative value for error. + - ZERO or positive value as a socket ID if successful. +*********************************************************************/ +SOCKET socket(uint16_t u16Domain, uint8_t u8Type, uint8_t u8Config) +{ + SOCKET sock = -1; + uint8_t u8SockID; + uint8_t u8Count; + volatile tstrSocket *pstrSock = NULL; + static volatile uint8_t u8NextTcpSock = 0; + static volatile uint8_t u8NextUdpSock = 0; + + /* The only supported family is the AF_INET for UDP and TCP transport layer protocols. */ + if(u16Domain == AF_INET) + { + if(u8Type == SOCK_STREAM) + { + for(u8Count = 0; u8Count < TCP_SOCK_MAX; u8Count ++) + { + u8SockID = u8NextTcpSock; + pstrSock = &gastrSockets[u8NextTcpSock]; + u8NextTcpSock = (u8NextTcpSock + 1) % TCP_SOCK_MAX; + if(!pstrSock->bIsUsed) + { + sock = (SOCKET)u8SockID; + memset((uint8_t*)pstrSock, 0, sizeof(tstrSocket)); + /* In the current implementation, SSL flags only have meaning for the TCP socket case. */ + if(u8Config != SOCKET_CONFIG_SSL_OFF) + { + tstrSSLSocketCreateCmd strSSLCreate; + + strSSLCreate.sslSock = sock; + SOCKET_REQUEST(SOCKET_CMD_SSL_CREATE, (uint8_t*)&strSSLCreate, sizeof(tstrSSLSocketCreateCmd), 0, 0, 0); + + pstrSock->u8SSLFlags = SSL_FLAGS_ACTIVE; + if(u8Config == SOCKET_CONFIG_SSL_DELAY) + pstrSock->u8SSLFlags |= SSL_FLAGS_DELAY; + } + break; + } + } + } + else if(u8Type == SOCK_DGRAM) + { + volatile tstrSocket *pastrUDPSockets = &gastrSockets[TCP_SOCK_MAX]; + for(u8Count = 0; u8Count < UDP_SOCK_MAX; u8Count ++) + { + u8SockID = u8NextUdpSock; + pstrSock = &pastrUDPSockets[u8NextUdpSock]; + u8NextUdpSock = (u8NextUdpSock + 1) % UDP_SOCK_MAX; + if(!pstrSock->bIsUsed) + { + sock = (SOCKET)(u8SockID + TCP_SOCK_MAX); + memset((uint8_t*)pstrSock, 0, sizeof(tstrSocket)); + break; + } + } + } + else if(u8Type == SOCK_RAW) + { + /* Only raw IP packets are supported */ + if(u8Config == SOCKET_CONFIG_IPPROTO_RAW) + { + pstrSock = &gastrSockets[RAW_SOCK_ID]; + if(!pstrSock->bIsUsed) + { + /* Socket identified by RAW_SOCK_ID is reserved */ + sock = (SOCKET)RAW_SOCK_ID; + memset((uint8_t*)pstrSock, 0, sizeof(tstrSocket)); + } + } + } + + if(sock >= 0) + { + pstrSock->bIsUsed = 1; + + /* The session ID is used to distinguish different socket connections + by comparing the assigned session ID to the one reported by the firmware*/ + ++gu16SessionID; + if(gu16SessionID == 0) + ++gu16SessionID; + + pstrSock->u16SessionID = gu16SessionID; + M2M_INFO("Socket %d session ID = %d\r\n", sock, gu16SessionID); + } + } + return sock; +} +/********************************************************************* +Function + bind + +Description + Request to bind a socket on a local address. + +Return +*********************************************************************/ +int8_t bind(SOCKET sock, struct sockaddr *pstrAddr, uint8_t u8AddrLen) +{ + int8_t s8Ret = SOCK_ERR_INVALID_ARG; + if((pstrAddr != NULL) && (sock >= 0) && (sock < MAX_SOCKET) && (gastrSockets[sock].bIsUsed == 1) && (u8AddrLen != 0)) + { + tstrBindCmd strBind; + + /* Build the bind request. */ + strBind.sock = sock; + memcpy((uint8_t *)&strBind.strAddr, (uint8_t *)pstrAddr, sizeof(tstrSockAddr)); + + strBind.strAddr.u16Family = strBind.strAddr.u16Family; + strBind.strAddr.u16Port = strBind.strAddr.u16Port; + strBind.strAddr.u32IPAddr = strBind.strAddr.u32IPAddr; + strBind.u16SessionID = gastrSockets[sock].u16SessionID; + + /* Send the request. */ + s8Ret = SOCKET_REQUEST(SOCKET_CMD_BIND, (uint8_t*)&strBind, sizeof(tstrBindCmd), NULL, 0, 0); + if(s8Ret != SOCK_ERR_NO_ERROR) + { + s8Ret = SOCK_ERR_INVALID; + } + } + return s8Ret; +} +/********************************************************************* +Function + listen + +Description + + +Return +*********************************************************************/ +int8_t listen(SOCKET sock, uint8_t backlog) +{ + int8_t s8Ret = SOCK_ERR_INVALID_ARG; + + if((sock >= 0) && (sock < MAX_SOCKET) && (gastrSockets[sock].bIsUsed == 1)) + { + tstrListenCmd strListen; + + strListen.sock = sock; + strListen.u8BackLog = backlog; + strListen.u16SessionID = gastrSockets[sock].u16SessionID; + + s8Ret = SOCKET_REQUEST(SOCKET_CMD_LISTEN, (uint8_t*)&strListen, sizeof(tstrListenCmd), NULL, 0, 0); + if(s8Ret != SOCK_ERR_NO_ERROR) + { + s8Ret = SOCK_ERR_INVALID; + } + } + return s8Ret; +} +/********************************************************************* +Function + accept + +Description + +Return +*********************************************************************/ +int8_t accept(SOCKET sock, struct sockaddr *addr, uint8_t *addrlen) +{ + int8_t s8Ret = SOCK_ERR_INVALID_ARG; + + if((sock >= 0) && (sock < MAX_SOCKET) && (gastrSockets[sock].bIsUsed == 1)) + { + s8Ret = SOCK_ERR_NO_ERROR; + } + return s8Ret; +} +/********************************************************************* +Function + connect + +Description + Connect to a remote TCP Server. + +Return +*********************************************************************/ +int8_t connect(SOCKET sock, struct sockaddr *pstrAddr, uint8_t u8AddrLen) +{ + int8_t s8Ret = SOCK_ERR_INVALID_ARG; + if((sock >= 0) && (sock < MAX_SOCKET) && (pstrAddr != NULL) && (gastrSockets[sock].bIsUsed == 1) && (u8AddrLen != 0)) + { + tstrConnectCmd strConnect; + uint8_t u8Cmd = SOCKET_CMD_CONNECT; + if((gastrSockets[sock].u8SSLFlags) & SSL_FLAGS_ACTIVE) + { + u8Cmd = SOCKET_CMD_SSL_CONNECT; + strConnect.u8SslFlags = gastrSockets[sock].u8SSLFlags; + } + strConnect.sock = sock; + memcpy((uint8_t *)&strConnect.strAddr, (uint8_t *)pstrAddr, sizeof(tstrSockAddr)); + + strConnect.u16SessionID = gastrSockets[sock].u16SessionID; + s8Ret = SOCKET_REQUEST(u8Cmd, (uint8_t*)&strConnect, sizeof(tstrConnectCmd), NULL, 0, 0); + if(s8Ret != SOCK_ERR_NO_ERROR) + { + s8Ret = SOCK_ERR_INVALID; + } + } + return s8Ret; +} +/********************************************************************* +Function + secure + +Description + Make secure (TLS) an open TCP client connection. + +Return +*********************************************************************/ +int8_t secure(SOCKET sock) +{ + int8_t s8Ret = SOCK_ERR_INVALID_ARG; + if((sock >= 0) && (sock < MAX_SOCKET) && (gastrSockets[sock].bIsUsed == 1)) + { + if( + (gastrSockets[sock].u8SSLFlags & SSL_FLAGS_ACTIVE) + && (gastrSockets[sock].u8SSLFlags & SSL_FLAGS_DELAY) + && (gastrSockets[sock].u16DataOffset != 0) + ) + { + tstrConnectCmd strConnect = {0}; + + gastrSockets[sock].u8SSLFlags &= ~SSL_FLAGS_DELAY; + strConnect.u8SslFlags = gastrSockets[sock].u8SSLFlags; + strConnect.sock = sock; + strConnect.u16SessionID = gastrSockets[sock].u16SessionID; + + s8Ret = SOCKET_REQUEST(SOCKET_CMD_SECURE, (uint8_t*)&strConnect, sizeof(tstrConnectCmd), NULL, 0, 0); + if(s8Ret != SOCK_ERR_NO_ERROR) + { + s8Ret = SOCK_ERR_INVALID; + } + } + } + return s8Ret; +} +/********************************************************************* +Function + send + +Description + +Return +*********************************************************************/ +int16_t send(SOCKET sock, void *pvSendBuffer, uint16_t u16SendLength, uint16_t flags) +{ + int16_t s16Ret = SOCK_ERR_INVALID_ARG; + + if((sock >= 0) && (sock < MAX_SOCKET) && (pvSendBuffer != NULL) && (u16SendLength <= SOCKET_BUFFER_MAX_LENGTH) && (gastrSockets[sock].bIsUsed == 1)) + { + uint16_t u16DataOffset; + tstrSendCmd strSend; + uint8_t u8Cmd; + + u8Cmd = SOCKET_CMD_SEND; + u16DataOffset = TCP_TX_PACKET_OFFSET; + + strSend.sock = sock; + strSend.u16DataSize = NM_BSP_B_L_16(u16SendLength); + strSend.u16SessionID = gastrSockets[sock].u16SessionID; + + if(sock >= TCP_SOCK_MAX) + { + u16DataOffset = UDP_TX_PACKET_OFFSET; + } + if( + (gastrSockets[sock].u8SSLFlags & SSL_FLAGS_ACTIVE) + && (!(gastrSockets[sock].u8SSLFlags & SSL_FLAGS_DELAY)) + ) + { + u8Cmd = SOCKET_CMD_SSL_SEND; + u16DataOffset = gastrSockets[sock].u16DataOffset; + } + + s16Ret = SOCKET_REQUEST(u8Cmd|M2M_REQ_DATA_PKT, (uint8_t*)&strSend, sizeof(tstrSendCmd), pvSendBuffer, u16SendLength, u16DataOffset); + if(s16Ret != SOCK_ERR_NO_ERROR) + { + s16Ret = SOCK_ERR_BUFFER_FULL; + } + } + return s16Ret; +} +/********************************************************************* +Function + sendto + +Description + +Return +*********************************************************************/ +int16_t sendto(SOCKET sock, void *pvSendBuffer, uint16_t u16SendLength, uint16_t flags, struct sockaddr *pstrDestAddr, uint8_t u8AddrLen) +{ + int16_t s16Ret = SOCK_ERR_INVALID_ARG; + uint16_t u16MaxLength = (sock == RAW_SOCK_ID) ? (M2M_HIF_MAX_PACKET_SIZE - M2M_HIF_HDR_OFFSET - sizeof(tstrSendCmd)) : SOCKET_BUFFER_MAX_LENGTH; + + if((sock >= 0) && (sock < MAX_SOCKET) && (pvSendBuffer != NULL) && (u16SendLength <= u16MaxLength) && (gastrSockets[sock].bIsUsed == 1)) + { + tstrSendCmd strSendTo; + + memset((uint8_t*)&strSendTo, 0, sizeof(tstrSendCmd)); + + strSendTo.sock = sock; + strSendTo.u16DataSize = NM_BSP_B_L_16(u16SendLength); + strSendTo.u16SessionID = gastrSockets[sock].u16SessionID; + + if(pstrDestAddr != NULL) + { + struct sockaddr_in *pstrAddr; + pstrAddr = (void*)pstrDestAddr; + + strSendTo.strAddr.u16Family = pstrAddr->sin_family; + strSendTo.strAddr.u16Port = pstrAddr->sin_port; + strSendTo.strAddr.u32IPAddr = pstrAddr->sin_addr.s_addr; + } + s16Ret = SOCKET_REQUEST(SOCKET_CMD_SENDTO|M2M_REQ_DATA_PKT, (uint8_t*)&strSendTo, sizeof(tstrSendCmd), + pvSendBuffer, u16SendLength, (sock == RAW_SOCK_ID) ? IP_PACKET_OFFSET: UDP_TX_PACKET_OFFSET); + + if(s16Ret != SOCK_ERR_NO_ERROR) + { + s16Ret = SOCK_ERR_BUFFER_FULL; + } + } + return s16Ret; +} +/********************************************************************* +Function + recv + +Description + +Return + +*********************************************************************/ +int16_t recv(SOCKET sock, void *pvRecvBuf, uint16_t u16BufLen, uint32_t u32Timeoutmsec) +{ + int16_t s16Ret = SOCK_ERR_INVALID_ARG; + + if((sock >= 0) && (sock < MAX_SOCKET) && (pvRecvBuf != NULL) && (u16BufLen != 0) && (gastrSockets[sock].bIsUsed == 1)) + { + s16Ret = SOCK_ERR_NO_ERROR; + gastrSockets[sock].pu8UserBuffer = (uint8_t*)pvRecvBuf; + gastrSockets[sock].u16UserBufferSize = u16BufLen; + + if(!gastrSockets[sock].bIsRecvPending) + { + tstrRecvCmd strRecv; + uint8_t u8Cmd = SOCKET_CMD_RECV; + + gastrSockets[sock].bIsRecvPending = 1; + if( + (gastrSockets[sock].u8SSLFlags & SSL_FLAGS_ACTIVE) + && (!(gastrSockets[sock].u8SSLFlags & SSL_FLAGS_DELAY)) + ) + { + u8Cmd = SOCKET_CMD_SSL_RECV; + } + + /* Check the timeout value. */ + if(u32Timeoutmsec == 0) + strRecv.u32Timeoutmsec = 0xFFFFFFFF; + else + strRecv.u32Timeoutmsec = NM_BSP_B_L_32(u32Timeoutmsec); + strRecv.sock = sock; + strRecv.u16SessionID = gastrSockets[sock].u16SessionID; + strRecv.u16BufLen = u16BufLen; + + s16Ret = SOCKET_REQUEST(u8Cmd, (uint8_t*)&strRecv, sizeof(tstrRecvCmd), NULL, 0, 0); + if(s16Ret != SOCK_ERR_NO_ERROR) + { + s16Ret = SOCK_ERR_BUFFER_FULL; + } + } + } + return s16Ret; +} +/********************************************************************* +Function + shutdown + +Description + +Return + None. +*********************************************************************/ +int8_t shutdown(SOCKET sock) +{ + int8_t s8Ret = SOCK_ERR_INVALID_ARG; + + M2M_INFO("Sock to delete <%d> (used %u)\r\n", sock, gastrSockets[sock].bIsUsed); + + if((sock >= 0) && (sock < MAX_SOCKET) && (gastrSockets[sock].bIsUsed == 1)) + { + uint8_t u8Cmd = SOCKET_CMD_CLOSE; + tstrCloseCmd strclose; + strclose.sock = sock; + strclose.u16SessionID = gastrSockets[sock].u16SessionID; + + if(gastrSockets[sock].u8SSLFlags & SSL_FLAGS_ACTIVE) + { + u8Cmd = SOCKET_CMD_SSL_CLOSE; + } + s8Ret = SOCKET_REQUEST(u8Cmd, (uint8_t*)&strclose, sizeof(tstrCloseCmd), NULL, 0, 0); + if(s8Ret != SOCK_ERR_NO_ERROR) + { + s8Ret = SOCK_ERR_INVALID; + } + memset((uint8_t*)&gastrSockets[sock], 0, sizeof(tstrSocket)); + } + return s8Ret; +} +/********************************************************************* +Function + recvfrom + +Description + +Return + +*********************************************************************/ +int16_t recvfrom(SOCKET sock, void *pvRecvBuf, uint16_t u16BufLen, uint32_t u32Timeoutmsec) +{ + int16_t s16Ret = SOCK_ERR_NO_ERROR; + if((sock >= 0) && (sock < MAX_SOCKET) && (pvRecvBuf != NULL) && (u16BufLen != 0) && (gastrSockets[sock].bIsUsed == 1)) + { + if(gastrSockets[sock].bIsUsed) + { + s16Ret = SOCK_ERR_NO_ERROR; + gastrSockets[sock].pu8UserBuffer = (uint8_t*)pvRecvBuf; + gastrSockets[sock].u16UserBufferSize = u16BufLen; + + if(!gastrSockets[sock].bIsRecvPending) + { + tstrRecvCmd strRecv; + + gastrSockets[sock].bIsRecvPending = 1; + + /* Check the timeout value. */ + if(u32Timeoutmsec == 0) + strRecv.u32Timeoutmsec = 0xFFFFFFFF; + else + strRecv.u32Timeoutmsec = NM_BSP_B_L_32(u32Timeoutmsec); + strRecv.sock = sock; + strRecv.u16SessionID = gastrSockets[sock].u16SessionID; + strRecv.u16BufLen = u16BufLen; + + s16Ret = SOCKET_REQUEST(SOCKET_CMD_RECVFROM, (uint8_t*)&strRecv, sizeof(tstrRecvCmd), NULL, 0, 0); + if(s16Ret != SOCK_ERR_NO_ERROR) + { + s16Ret = SOCK_ERR_BUFFER_FULL; + } + } + } + } + else + { + s16Ret = SOCK_ERR_INVALID_ARG; + } + return s16Ret; +} +/********************************************************************* +Function + gethostbyname + +Description + +Return + None. +*********************************************************************/ +int8_t gethostbyname(const char *pcHostName) +{ + int8_t s8Err = SOCK_ERR_INVALID_ARG; + uint8_t u8HostNameSize = (uint8_t)strlen(pcHostName); + if(u8HostNameSize <= HOSTNAME_MAX_SIZE) + { + s8Err = SOCKET_REQUEST(SOCKET_CMD_DNS_RESOLVE|M2M_REQ_DATA_PKT, (uint8_t*)pcHostName, u8HostNameSize + 1, NULL, 0, 0); + if(s8Err != SOCK_ERR_NO_ERROR) + { + s8Err = SOCK_ERR_INVALID; + } + } + return s8Err; +} + +/********************************************************************* +Function + rawSetSockOpt + +Description + +Return + None. +*********************************************************************/ +static int8_t rawSetSockOpt(SOCKET sock, uint8_t u8Opt, const void *pvOptVal, uint16_t u16OptLen) +{ + int8_t s8Ret = SOCK_ERR_INVALID_ARG; + if(RAW_SOCK_ID == sock) + { + if(u16OptLen == sizeof(uint32_t)) + { + uint8_t u8Cmd = SOCKET_CMD_RAW_SET_SOCK_OPT; + tstrSetSocketOptCmd strSetSockOpt; + strSetSockOpt.u8Option = u8Opt; + strSetSockOpt.sock = sock; + strSetSockOpt.u32OptionValue = *(uint32_t*)pvOptVal; + strSetSockOpt.u16SessionID = gastrSockets[sock].u16SessionID; + + s8Ret = SOCKET_REQUEST(u8Cmd, (uint8_t*)&strSetSockOpt, sizeof(tstrSetSocketOptCmd), NULL, 0, 0); + if(s8Ret != SOCK_ERR_NO_ERROR) + { + s8Ret = SOCK_ERR_INVALID; + } + } + } + + return s8Ret; +} +/********************************************************************* +Function + sslSetSockOpt + +Description + +Return + None. +*********************************************************************/ +static int8_t sslSetSockOpt(SOCKET sock, uint8_t u8Opt, const void *pvOptVal, uint16_t u16OptLen) +{ + int8_t s8Ret = SOCK_ERR_INVALID_ARG; + if(sock < TCP_SOCK_MAX) + { + if(gastrSockets[sock].u8SSLFlags & SSL_FLAGS_ACTIVE) + { + uint8_t sslFlag = 0; + + s8Ret = SOCK_ERR_NO_ERROR; + if(u16OptLen == sizeof(int)) + { + if(u8Opt == SO_SSL_BYPASS_X509_VERIF) + { + sslFlag = SSL_FLAGS_BYPASS_X509; + } + else if(u8Opt == SO_SSL_ENABLE_SESSION_CACHING) + { + sslFlag = SSL_FLAGS_CACHE_SESSION; + } + else if(u8Opt == SO_SSL_ENABLE_CERTNAME_VALIDATION) + { + sslFlag = SSL_FLAGS_CHECK_CERTNAME; + } + } + if(sslFlag) + { + int optVal = *((int*)pvOptVal); + if(optVal) + { + gastrSockets[sock].u8SSLFlags |= sslFlag; + } + else + { + gastrSockets[sock].u8SSLFlags &= ~sslFlag; + } + } + else if( + ((u8Opt == SO_SSL_SNI) && (u16OptLen < HOSTNAME_MAX_SIZE)) + || ((u8Opt == SO_SSL_ALPN) && (u16OptLen <= ALPN_LIST_MAX_SIZE)) + ) + { + tstrSSLSetSockOptCmd strCmd = {0}; + + strCmd.sock = sock; + strCmd.u16SessionID = gastrSockets[sock].u16SessionID; + strCmd.u8Option = u8Opt; + strCmd.u32OptLen = u16OptLen; + memcpy(strCmd.au8OptVal, (uint8_t*)pvOptVal, u16OptLen); + + s8Ret = SOCKET_REQUEST(SOCKET_CMD_SSL_SET_SOCK_OPT, (uint8_t*)&strCmd, sizeof(tstrSSLSetSockOptCmd), 0, 0, 0); + if(s8Ret == M2M_ERR_MEM_ALLOC) + { + s8Ret = SOCKET_REQUEST(SOCKET_CMD_SSL_SET_SOCK_OPT | M2M_REQ_DATA_PKT, + (uint8_t*)&strCmd, sizeof(tstrSSLSetSockOptCmd), 0, 0, 0); + } + } + else + { + M2M_ERR("Unknown SSL Socket Option %d\r\n",u8Opt); + s8Ret = SOCK_ERR_INVALID_ARG; + } + } + else + { + M2M_ERR("Not SSL Socket\r\n"); + } + } + return s8Ret; +} +/********************************************************************* +Function + setsockopt + +Description + +Return + None. +*********************************************************************/ +int8_t setsockopt(SOCKET sock, uint8_t u8Level, uint8_t option_name, + const void *option_value, uint16_t u16OptionLen) +{ + int8_t s8Ret = SOCK_ERR_INVALID_ARG; + if((sock >= 0) && (sock < MAX_SOCKET) && (option_value != NULL) && (gastrSockets[sock].bIsUsed == 1)) + { + if(u8Level == SOL_RAW) + { + s8Ret = rawSetSockOpt(sock, option_name, option_value, u16OptionLen); + } + else if(u8Level == SOL_SSL_SOCKET) + { + s8Ret = sslSetSockOpt(sock, option_name, option_value, u16OptionLen); + } + else if(u8Level == SOL_SOCKET) + { + if(u16OptionLen == sizeof(uint32_t)) + { + uint8_t u8Cmd = SOCKET_CMD_SET_SOCKET_OPTION; + tstrSetSocketOptCmd strSetSockOpt; + strSetSockOpt.u8Option=option_name; + strSetSockOpt.sock = sock; + strSetSockOpt.u32OptionValue = *(uint32_t*)option_value; + strSetSockOpt.u16SessionID = gastrSockets[sock].u16SessionID; + + s8Ret = SOCKET_REQUEST(u8Cmd, (uint8_t*)&strSetSockOpt, sizeof(tstrSetSocketOptCmd), NULL, 0, 0); + if(s8Ret != SOCK_ERR_NO_ERROR) + { + s8Ret = SOCK_ERR_INVALID; + } + } + } + } + return s8Ret; +} +/********************************************************************* +Function + getsockopt + +Description + +Return + None. +*********************************************************************/ +int8_t getsockopt(SOCKET sock, uint8_t u8Level, uint8_t u8OptName, const void *pvOptValue, uint8_t* pu8OptLen) +{ + // This is not implemented so return a value that will cause failure should this be used. + return SOCK_ERR_INVALID_ARG; +} +/********************************************************************* +Function + m2m_ping_req + +Description + Send Ping request. + +Return +*********************************************************************/ +int8_t m2m_ping_req(uint32_t u32DstIP, uint8_t u8TTL, tpfPingCb fpPingCb) +{ + int8_t s8Ret = M2M_ERR_INVALID_ARG; + + if((u32DstIP != 0) && (fpPingCb != NULL)) + { + tstrPingCmd strPingCmd; + + strPingCmd.u16PingCount = 1; + strPingCmd.u32DestIPAddr = u32DstIP; + strPingCmd.u32CmdPrivate = ++gu32PingId; + strPingCmd.u8TTL = u8TTL; + + gfpPingCb = fpPingCb; + s8Ret = SOCKET_REQUEST(SOCKET_CMD_PING, (uint8_t*)&strPingCmd, sizeof(tstrPingCmd), NULL, 0, 0); + } + return s8Ret; +} +/********************************************************************* +Function + set_alpn_protocol_list + +Description + This function sets the protocol list used for application-layer protocol negotiation (ALPN). + If used, it must be called after creating a SSL socket (using @ref socket) and before + connecting/binding (using @ref connect or @ref bind). + +Return + The function returns @ref M2M_SUCCESS for successful operations and a negative value otherwise. +*********************************************************************/ +int8_t set_alpn_list(SOCKET sock, const char *pcProtocolList) +{ + int8_t s8Ret = SOCK_ERR_INVALID_ARG; + + if ((sock >= 0) && (sock < TCP_SOCK_MAX) && (pcProtocolList != NULL)) + { + uint8_t u8Length = strlen(pcProtocolList); + if ((u8Length > 0) && (u8Length < ALPN_LIST_MAX_APP_LENGTH)) + { + /* + ALPN socket option requires Alpn list in this format: + 0 1 2 3 ... (bytes) + +-------+-------+-------+ ... +-------+ ... +-------+ ... + | Length L (BE) | len1 | name1... | len2 | name2... | len3 | name3... + +-------+-------+-------+ ... +-------+ ... +-------+ ... + Length fields do not include themselves. + */ + uint8_t au8AlpnList[ALPN_LIST_MAX_SIZE] = {0}; + uint8_t *pu8Ptr = &au8AlpnList[3] + u8Length; + uint8_t u8Len = 0; + + memcpy(&au8AlpnList[3], pcProtocolList, u8Length); + u8Length++; + au8AlpnList[1] = u8Length; + au8AlpnList[2] = ' '; + + /* Convert space characters into length fields. */ + while (u8Length--) + { + if (*--pu8Ptr == ' ') + { + if (u8Len == 0) goto ERR; + *pu8Ptr = u8Len; + u8Len = 0; + } + else u8Len++; + } + s8Ret = setsockopt(sock, SOL_SSL_SOCKET, SO_SSL_ALPN, au8AlpnList, sizeof(au8AlpnList)); + } + } +ERR: + return s8Ret; +} +/********************************************************************* +Function + get_alpn_protocol_index + +Description + This function gets the protocol list used for application-layer protocol negotiation (ALPN). + If used, it must be called after creating a SSL socket (using @ref socket) and before + connecting/binding (using @ref connect or @ref bind). + +Return + The function returns the index of the selected application-layer protocol. + Special values: + 0: no negotiation has occurred. + <0: error. +*********************************************************************/ +int8_t get_alpn_index(SOCKET sock) +{ + if (sock >= TCP_SOCK_MAX || sock < 0) + return SOCK_ERR_INVALID_ARG; + if (!(gastrSockets[sock].u8SSLFlags & SSL_FLAGS_ACTIVE) || !gastrSockets[sock].bIsUsed) + return SOCK_ERR_INVALID_ARG; + return gastrSockets[sock].u8AlpnStatus; +} + +/********************************************************************* +Function + IsSocketReady + +Description + +Return + None. +*********************************************************************/ +uint8_t IsSocketReady(void) +{ + return gbSocketInit; +} + +/********************************************************************* +Function + get_error_detail + +Description + This function gets detail about a socket failure. + The application can call this when notified of a socket failure via + @ref SOCKET_MSG_CONNECT or @ref SOCKET_MSG_RECV. + If used, it must be called before @ref shutdown. + +Return + The function returns @ref SOCK_ERR_NO_ERROR if the request is successful + and a negative value otherwise. +*********************************************************************/ +int8_t get_error_detail(SOCKET sock, tstrSockErr *pstrErr) +{ + if ((sock >= TCP_SOCK_MAX) || (sock < 0) || (pstrErr == NULL)) + return SOCK_ERR_INVALID_ARG; + if (!gastrSockets[sock].bIsUsed) + return SOCK_ERR_INVALID_ARG; + pstrErr->enuErrSource = gastrSockets[sock].u8ErrSource; + pstrErr->u8ErrCode = gastrSockets[sock].u8ErrCode; + return SOCK_ERR_NO_ERROR; +} diff --git a/ChaletLora.X/Source/winc3400_142/spi_flash/include/spi_flash.h b/ChaletLora.X/Source/winc3400_142/spi_flash/include/spi_flash.h new file mode 100644 index 0000000..0c879f5 --- /dev/null +++ b/ChaletLora.X/Source/winc3400_142/spi_flash/include/spi_flash.h @@ -0,0 +1,192 @@ +/******************************************************************************* + WINC3400 SPI Flash Interface + + File Name: + spi_flash.h + + Summary: + WINC3400 SPI Flash Interface + + Description: + WINC3400 SPI Flash Interface + *******************************************************************************/ + +//DOM-IGNORE-BEGIN +/******************************************************************************* +* Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries. +* +* Subject to your compliance with these terms, you may use Microchip software +* and any derivatives exclusively with Microchip products. It is your +* responsibility to comply with third party license terms applicable to your +* use of third party software (including open source software) that may +* accompany Microchip software. +* +* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER +* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED +* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A +* PARTICULAR PURPOSE. +* +* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, +* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND +* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS +* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE +* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN +* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, +* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. +*******************************************************************************/ + +/** @defgroup SPIFLASHAPI SPI FLASH + */ + +#ifndef __SPI_FLASH_H__ +#define __SPI_FLASH_H__ + +#include "nm_common.h" +#include "nmbus.h" +#include "spi_flash_map.h" + +/** @defgroup SPIFLASHFUNCTIONS Functions + * @ingroup SPIFLASHAPI + */ + +/**@{*/ +/*! + * @fn uint32_t spi_flash_get_size(void); + * @brief Returns with \ref uint32_t value which is total flash size\n + * @note Returned value in Mb (Mega Bit). + * @return SPI flash size in case of success and a ZERO value in case of failure. + */ +uint32_t spi_flash_get_size(void); + +/*! + * @fn int8_t spi_flash_read(uint8_t *, uint32_t, uint32_t); + * @brief Read a specified portion of data from SPI Flash.\n + * @param [out] pu8Buf + * Pointer to data buffer which will be filled with data in case of successful operation. + * @param [in] u32Addr + * Address (Offset) to read from at the SPI flash. + * @param [in] u32Sz + * Total size of data to be read in bytes + * @warning + * - Address (offset) plus size of data must not exceed flash size.\n + * - No firmware is required for reading from SPI flash.\n + * - In case of there is a running firmware, it is required to pause your firmware first + * before any trial to access SPI flash to avoid any racing between host and running firmware on bus using + * @ref m2m_wifi_download_mode + * @note + * - It is blocking function\n + * @sa m2m_wifi_download_mode, spi_flash_get_size + * @return The function returns @ref M2M_SUCCESS for successful operations and a negative value otherwise. + */ +int8_t spi_flash_read(uint8_t *pu8Buf, uint32_t u32Addr, uint32_t u32Sz); + +/*! + * @fn int8_t spi_flash_write(uint8_t *, uint32_t, uint32_t); + * @brief Write a specified portion of data to SPI Flash.\n + * @param [in] pu8Buf + * Pointer to data buffer which contains the data to be written. + * @param [in] u32Offset + * Address (Offset) to write at the SPI flash. + * @param [in] u32Sz + * Total number of size of data bytes + * @note + * - It is blocking function\n + * - It is user's responsibility to verify that data has been written successfully + * by reading data again and comparing it with the original. + * @warning + * - Address (offset) plus size of data must not exceed flash size.\n + * - No firmware is required for writing to SPI flash.\n + * - In case of there is a running firmware, it is required to pause your firmware first + * before any trial to access SPI flash to avoid any racing between host and running firmware on bus using + * @ref m2m_wifi_download_mode. + * - Before writing to any section, it is required to erase that section first. + * @sa m2m_wifi_download_mode, spi_flash_get_size, spi_flash_erase + * @return The function returns @ref M2M_SUCCESS for successful operations and a negative value otherwise. + */ +int8_t spi_flash_write(uint8_t* pu8Buf, uint32_t u32Offset, uint32_t u32Sz); + +/*! + * @fn int8_t spi_flash_erase(uint32_t, uint32_t); + * @brief Erase a specified portion of SPI Flash.\n + * @param [in] u32Offset + * Address (Offset) to erase from the SPI flash. + * @param [in] u32Sz + * Total number of bytes required to be erased. + * @note It is blocking function \n + * @warning + * - Address (offset) plus size of data must not exceed flash size.\n + * - No firmware is required for writing to SPI flash.\n + * - In case of there is a running firmware, it is required to pause your firmware first + * before any trial to access SPI flash to avoid any racing between host and running firmware on bus using + * @ref m2m_wifi_download_mode + * @sa m2m_wifi_download_mode, spi_flash_get_size + * @return The function returns @ref M2M_SUCCESS for successful operations and a negative value otherwise. + * \section SPIFLASHExample Example + * @code{.c} + * #include "spi_flash.h" + * + * #define DATA_TO_REPLACE "THIS IS A NEW SECTOR IN FLASH" + * + * int main() + * { + * uint8_t au8FlashContent[FLASH_SECTOR_SZ] = {0}; + * uint32_t u32FlashTotalSize = 0; + * uint32_t u32FlashOffset = 0; + * + * ret = m2m_wifi_download_mode(); + * if(M2M_SUCCESS != ret) + * { + * printf("Unable to enter download mode\r\n"); + * } + * else + * { + * u32FlashTotalSize = spi_flash_get_size(); + * } + * + * while((u32FlashTotalSize > u32FlashOffset) && (M2M_SUCCESS == ret)) + * { + * ret = spi_flash_read(au8FlashContent, u32FlashOffset, FLASH_SECTOR_SZ); + * if(M2M_SUCCESS != ret) + * { + * printf("Unable to read SPI sector\r\n"); + * break; + * } + * memcpy(au8FlashContent, DATA_TO_REPLACE, strlen(DATA_TO_REPLACE)); + * + * ret = spi_flash_erase(u32FlashOffset, FLASH_SECTOR_SZ); + * if(M2M_SUCCESS != ret) + * { + * printf("Unable to erase SPI sector\r\n"); + * break; + * } + * + * ret = spi_flash_write(au8FlashContent, u32FlashOffset, FLASH_SECTOR_SZ); + * if(M2M_SUCCESS != ret) + * { + * printf("Unable to write SPI sector\r\n"); + * break; + * } + * u32FlashOffset += FLASH_SECTOR_SZ; + * } + * + * if(M2M_SUCCESS == ret) + * { + * printf("Successful operations\r\n"); + * } + * else + * { + * printf("Failed operations\r\n"); + * } + * + * while(1); + * return M2M_SUCCESS; + * } + * @endcode + */ + +int8_t spi_flash_erase(uint32_t u32Offset, uint32_t u32Sz); + +/**@} + */ + +#endif //__SPI_FLASH_H__ \ No newline at end of file diff --git a/ChaletLora.X/Source/winc3400_142/spi_flash/include/spi_flash_map.h b/ChaletLora.X/Source/winc3400_142/spi_flash/include/spi_flash_map.h new file mode 100644 index 0000000..ed2e961 --- /dev/null +++ b/ChaletLora.X/Source/winc3400_142/spi_flash/include/spi_flash_map.h @@ -0,0 +1,264 @@ +/******************************************************************************* + WINC3400 SPI Flash Map + + File Name: + spi_flash_map.h + + Summary: + WINC3400 SPI Flash Map + + Description: + WINC3400 SPI Flash Map + *******************************************************************************/ + +//DOM-IGNORE-BEGIN +/******************************************************************************* +* Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries. +* +* Subject to your compliance with these terms, you may use Microchip software +* and any derivatives exclusively with Microchip products. It is your +* responsibility to comply with third party license terms applicable to your +* use of third party software (including open source software) that may +* accompany Microchip software. +* +* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER +* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED +* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A +* PARTICULAR PURPOSE. +* +* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, +* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND +* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS +* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE +* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN +* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, +* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. +*******************************************************************************/ + + +#ifndef __SPI_FLASH_MAP_H__ +#define __SPI_FLASH_MAP_H__ + +//#define DOWNLOAD_ROLLBACK +//#define OTA_GEN +#define _PROGRAM_POWER_SAVE_ +#define BT_IMAGE_PRESENT + +/* =======*=======*=======*=======*======= + * General Sizes for Flash Memory + * =======*=======*=======*=======*======= + */ + +#define FLASH_START_ADDR (0UL) +/*! M2M_OTA_IMAGE2_OFFSET) +#error "Exceed Flash Size" +#endif /* ((FLASH_COMMON_SZ + OTA_IMAGE_SIZE) > M2M_OTA_IMAGE2_OFFSET) */ +#if ((M2M_OTA_IMAGE2_OFFSET + OTA_IMAGE_SIZE) > FLASH_8M_TOTAL_SZ) +#error "OTA Exceed Flash Size" +#endif /* ((M2M_OTA_IMAGE2_OFFSET + OTA_IMAGE_SIZE) > FLASH_8M_TOTAL_SZ) */ + +#endif /* __SPI_FLASH_MAP_H__ */ diff --git a/ChaletLora.X/Source/winc3400_142/spi_flash/source/spi_flash.c b/ChaletLora.X/Source/winc3400_142/spi_flash/source/spi_flash.c new file mode 100644 index 0000000..1563f90 --- /dev/null +++ b/ChaletLora.X/Source/winc3400_142/spi_flash/source/spi_flash.c @@ -0,0 +1,599 @@ +/******************************************************************************* + WINC3400 SPI Flash Interface + + File Name: + spi_flash.c + + Summary: + WINC3400 SPI Flash Interface + + Description: + WINC3400 SPI Flash Interface + *******************************************************************************/ + +//DOM-IGNORE-BEGIN +/******************************************************************************* +* Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries. +* +* Subject to your compliance with these terms, you may use Microchip software +* and any derivatives exclusively with Microchip products. It is your +* responsibility to comply with third party license terms applicable to your +* use of third party software (including open source software) that may +* accompany Microchip software. +* +* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER +* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED +* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A +* PARTICULAR PURPOSE. +* +* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, +* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND +* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS +* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE +* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN +* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, +* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. +*******************************************************************************/ + +#include "spi_flash.h" +#include "spi_flash_map.h" +#include "nm_common.h" +#define DUMMY_REGISTER (0x1084) + +#define TIMEOUT (-1) /*MS*/ + +#define MAX_PROG_CNT 2 +#define MAX_RETRY 4 +#define MAX_ATTEMPT_STATUS_READ 50000 + +#define HOST_SHARE_MEM_BASE (0xd0000UL) +#define CORTUS_SHARE_MEM_BASE (0x60000000UL) +#define NMI_SPI_FLASH_ADDR (0x111c) +/*********************************************************** +SPI Flash DMA +***********************************************************/ +#define GET_UINT32(X,Y) (X[0+Y] + ((uint32_t)X[1+Y]<<8) + ((uint32_t)X[2+Y]<<16) +((uint32_t)X[3+Y]<<24)) +#define SPI_FLASH_BASE (0x10200) +#define SPI_FLASH_MODE (SPI_FLASH_BASE + 0x00) +#define SPI_FLASH_CMD_CNT (SPI_FLASH_BASE + 0x04) +#define SPI_FLASH_DATA_CNT (SPI_FLASH_BASE + 0x08) +#define SPI_FLASH_BUF1 (SPI_FLASH_BASE + 0x0c) +#define SPI_FLASH_BUF2 (SPI_FLASH_BASE + 0x10) +#define SPI_FLASH_BUF_DIR (SPI_FLASH_BASE + 0x14) +#define SPI_FLASH_TR_DONE (SPI_FLASH_BASE + 0x18) +#define SPI_FLASH_DMA_ADDR (SPI_FLASH_BASE + 0x1c) +#define SPI_FLASH_MSB_CTL (SPI_FLASH_BASE + 0x20) +#define SPI_FLASH_TX_CTL (SPI_FLASH_BASE + 0x24) + +/*********************************************/ +/* STATIC FUNCTIONS */ +/*********************************************/ + +/** +* @fn spi_flash_read_status_reg +* @brief Read status register +* @param[OUT] val + value of status reg +* @return Status of execution +* @note Compatible with MX25L6465E +*/ +static int8_t spi_flash_read_status_reg(uint8_t *val) +{ + int8_t ret = M2M_SUCCESS; + uint8_t cmd[1]; + uint32_t reg; + + cmd[0] = 0x05; + + ret += nm_write_reg(SPI_FLASH_DATA_CNT, 4); + ret += nm_write_reg(SPI_FLASH_BUF1, cmd[0]); + ret += nm_write_reg(SPI_FLASH_BUF_DIR, 0x01); + ret += nm_write_reg(SPI_FLASH_DMA_ADDR, DUMMY_REGISTER); + ret += nm_write_reg(SPI_FLASH_CMD_CNT, 1 | (1<<7)); + do + { + ret += nm_read_reg_with_ret(SPI_FLASH_TR_DONE, (uint32_t *)®); + if(M2M_SUCCESS != ret) break; + } + while(reg != 1); + + reg = (M2M_SUCCESS == ret)?(nm_read_reg(DUMMY_REGISTER)):(0); + *val = reg & 0xff; + return ret; +} + +/** +* @fn spi_flash_load_to_cortus_mem +* @brief Load data from SPI flash into cortus memory +* @param[IN] u32MemAdr +* Cortus load address. It must be set to its AHB access address +* @param[IN] u32FlashAdr +* Address to read from at the SPI flash +* @param[IN] u32Sz +* Data size +* @return Status of execution +* @note Compatible with MX25L6465E and should be working with other types +*/ +static int8_t spi_flash_load_to_cortus_mem(uint32_t u32MemAdr, uint32_t u32FlashAdr, uint32_t u32Sz) +{ + uint8_t cmd[5]; + uint32_t val = 0; + int8_t ret = M2M_SUCCESS; + + cmd[0] = 0x0b; + cmd[1] = (uint8_t)(u32FlashAdr >> 16); + cmd[2] = (uint8_t)(u32FlashAdr >> 8); + cmd[3] = (uint8_t)(u32FlashAdr); + cmd[4] = 0xA5; + + ret += nm_write_reg(SPI_FLASH_DATA_CNT, u32Sz); + ret += nm_write_reg(SPI_FLASH_BUF1, cmd[0]|(cmd[1]<<8)|(cmd[2]<<16)|(cmd[3]<<24)); + ret += nm_write_reg(SPI_FLASH_BUF2, cmd[4]); + ret += nm_write_reg(SPI_FLASH_BUF_DIR, 0x1f); + ret += nm_write_reg(SPI_FLASH_DMA_ADDR, u32MemAdr); + ret += nm_write_reg(SPI_FLASH_CMD_CNT, 5 | (1<<7)); + do + { + ret += nm_read_reg_with_ret(SPI_FLASH_TR_DONE, (uint32_t *)&val); + if(M2M_SUCCESS != ret) break; + } + while(val != 1); + + return ret; +} + +/** +* @fn spi_flash_sector_erase +* @brief Erase sector (4KB) +* @param[IN] u32FlashAdr +* Any memory address within the sector +* @return Status of execution +* @note Compatible with MX25L6465E and should be working with other types +*/ +static int8_t spi_flash_sector_erase(uint32_t u32FlashAdr) +{ + uint8_t cmd[4]; + uint32_t val = 0; + int8_t ret = M2M_SUCCESS; + + cmd[0] = 0x20; + cmd[1] = (uint8_t)(u32FlashAdr >> 16); + cmd[2] = (uint8_t)(u32FlashAdr >> 8); + cmd[3] = (uint8_t)(u32FlashAdr); + + ret += nm_write_reg(SPI_FLASH_DATA_CNT, 0); + ret += nm_write_reg(SPI_FLASH_BUF1, cmd[0]|(cmd[1]<<8)|(cmd[2]<<16)|(cmd[3]<<24)); + ret += nm_write_reg(SPI_FLASH_BUF_DIR, 0x0f); + ret += nm_write_reg(SPI_FLASH_DMA_ADDR, 0); + ret += nm_write_reg(SPI_FLASH_CMD_CNT, 4 | (1<<7)); + do + { + ret += nm_read_reg_with_ret(SPI_FLASH_TR_DONE, (uint32_t *)&val); + if(M2M_SUCCESS != ret) break; + } + while(val != 1); + + return ret; +} + +/** +* @fn spi_flash_write_enable +* @brief Send write enable command to SPI flash +* @return Status of execution +* @note Compatible with MX25L6465E and should be working with other types +*/ +static int8_t spi_flash_write_enable(void) +{ + uint8_t cmd[1]; + uint32_t val = 0; + int8_t ret = M2M_SUCCESS; + + cmd[0] = 0x06; + + ret += nm_write_reg(SPI_FLASH_DATA_CNT, 0); + ret += nm_write_reg(SPI_FLASH_BUF1, cmd[0]); + ret += nm_write_reg(SPI_FLASH_BUF_DIR, 0x01); + ret += nm_write_reg(SPI_FLASH_DMA_ADDR, 0); + ret += nm_write_reg(SPI_FLASH_CMD_CNT, 1 | (1<<7)); + do + { + ret += nm_read_reg_with_ret(SPI_FLASH_TR_DONE, (uint32_t *)&val); + if(M2M_SUCCESS != ret) break; + } + while(val != 1); + + return ret; +} + +/** +* @fn spi_flash_write_disable +* @brief Send write disable command to SPI flash +* @note Compatible with MX25L6465E and should be working with other types +*/ +static int8_t spi_flash_write_disable(void) +{ + uint8_t cmd[1]; + uint32_t val = 0; + int8_t ret = M2M_SUCCESS; + cmd[0] = 0x04; + + ret += nm_write_reg(SPI_FLASH_DATA_CNT, 0); + ret += nm_write_reg(SPI_FLASH_BUF1, cmd[0]); + ret += nm_write_reg(SPI_FLASH_BUF_DIR, 0x01); + ret += nm_write_reg(SPI_FLASH_DMA_ADDR, 0); + ret += nm_write_reg(SPI_FLASH_CMD_CNT, 1 | (1<<7)); + do + { + ret += nm_read_reg_with_ret(SPI_FLASH_TR_DONE, (uint32_t *)&val); + if(M2M_SUCCESS != ret) break; + } + while(val != 1); + + return ret; +} + +/** +* @fn spi_flash_page_program +* @brief Write data (less than page size) from cortus memory to SPI flash +* @param[IN] u32MemAdr +* Cortus data address. It must be set to its AHB access address +* @param[IN] u32FlashAdr +* Address to write to at the SPI flash +* @param[IN] u32Sz +* Data size +* @note Compatible with MX25L6465E and should be working with other types +*/ +static int8_t spi_flash_page_program(uint32_t u32MemAdr, uint32_t u32FlashAdr, uint32_t u32Sz) +{ + uint8_t cmd[4]; + uint32_t val = 0; + int8_t ret = M2M_SUCCESS; + + cmd[0] = 0x02; + cmd[1] = (uint8_t)(u32FlashAdr >> 16); + cmd[2] = (uint8_t)(u32FlashAdr >> 8); + cmd[3] = (uint8_t)(u32FlashAdr); + + ret += nm_write_reg(SPI_FLASH_DATA_CNT, 0); + ret += nm_write_reg(SPI_FLASH_BUF1, cmd[0]|(cmd[1]<<8)|(cmd[2]<<16)|(cmd[3]<<24)); + ret += nm_write_reg(SPI_FLASH_BUF_DIR, 0x0f); + ret += nm_write_reg(SPI_FLASH_DMA_ADDR, u32MemAdr); + ret += nm_write_reg(SPI_FLASH_CMD_CNT, 4 | (1<<7) | ((u32Sz & 0xfffff) << 8)); + do + { + ret += nm_read_reg_with_ret(SPI_FLASH_TR_DONE, (uint32_t *)&val); + if(M2M_SUCCESS != ret) break; + } + while(val != 1); + + return ret; +} + +/** +* @fn spi_flash_read_internal +* @brief Read from data from SPI flash +* @param[OUT] pu8Buf +* Pointer to data buffer +* @param[IN] u32Addr +* Address to read from at the SPI flash +* @param[IN] u32Sz +* Data size +* @note Data size must be < 64KB (limitation imposed by the bus wrapper) +*/ +static int8_t spi_flash_read_internal(uint8_t *pu8Buf, uint32_t u32Addr, uint32_t u32Sz) +{ + int8_t ret = M2M_SUCCESS; + /* read size must be < 64KB */ + ret = spi_flash_load_to_cortus_mem(HOST_SHARE_MEM_BASE, u32Addr, u32Sz); + if(M2M_SUCCESS != ret) goto ERR; + ret = nm_read_block(HOST_SHARE_MEM_BASE, pu8Buf, u32Sz); +ERR: + return ret; +} + +/** +* @fn spi_flash_pp +* @brief Program data of size less than a page (256 bytes) at the SPI flash +* @param[IN] u32Offset +* Address to write to at the SPI flash +* @param[IN] pu8Buf +* Pointer to data buffer +* @param[IN] u32Sz +* Data size +* @return Status of execution +*/ +static int8_t spi_flash_pp(uint32_t u32Offset, uint8_t *pu8Buf, uint16_t u16Sz) +{ + int8_t ret = M2M_SUCCESS; + uint8_t tmp; + int i; + int p; + int x; + + /* use shared packet memory as temp mem */ + ret += nm_write_block(HOST_SHARE_MEM_BASE, pu8Buf, u16Sz); + + for(i = 0, p = 0; (i < MAX_RETRY) && (p < MAX_PROG_CNT); i++) + { + if((spi_flash_write_enable() == M2M_SUCCESS) && + (spi_flash_page_program(HOST_SHARE_MEM_BASE, u32Offset, u16Sz) == M2M_SUCCESS)) + { + x = 0; + while(++x < MAX_ATTEMPT_STATUS_READ) + { + if(spi_flash_read_status_reg(&tmp) != M2M_SUCCESS) + { + break; + } + if(!(tmp & 0x01)) + { + p++; + break; + } + } + } + } + ret = spi_flash_write_disable(); + + if(p < MAX_PROG_CNT) + { + ret = M2M_ERR_FAIL; + } + + return ret; +} + +/** +* @fn spi_flash_rdid +* @brief Read SPI Flash ID +* @return SPI FLash ID +*/ +static uint32_t spi_flash_rdid(void) +{ + unsigned char cmd[1]; + uint32_t reg = 0; + uint32_t cnt = 0; + int8_t ret = M2M_SUCCESS; + + cmd[0] = 0x9f; + + ret += nm_write_reg(SPI_FLASH_DATA_CNT, 4); + ret += nm_write_reg(SPI_FLASH_BUF1, cmd[0]); + ret += nm_write_reg(SPI_FLASH_BUF_DIR, 0x1); + ret += nm_write_reg(SPI_FLASH_DMA_ADDR, DUMMY_REGISTER); + ret += nm_write_reg(SPI_FLASH_CMD_CNT, 1 | (1<<7)); + do + { + ret += nm_read_reg_with_ret(SPI_FLASH_TR_DONE, (uint32_t *)®); + if(M2M_SUCCESS != ret) break; + if(++cnt > 500) + { + ret = M2M_ERR_INIT; + break; + } + } + while(reg != 1); + reg = (M2M_SUCCESS == ret)?(nm_read_reg(DUMMY_REGISTER)):(0); + M2M_PRINT("Flash ID %x \n", (unsigned int)reg); + return reg; +} + +/** +* @fn spi_flash_unlock +* @brief Unlock SPI Flash +*/ +#if 0 +static void spi_flash_unlock(void) +{ + uint8_t tmp; + tmp = spi_flash_read_security_reg(); + spi_flash_clear_security_flags(); + if(tmp & 0x80) + { + spi_flash_write_enable(); + spi_flash_gang_unblock(); + } +} +#endif + +/*********************************************/ +/* GLOBAL FUNCTIONS */ +/*********************************************/ + +/** +* @fn spi_flash_read +* @brief Read from data from SPI flash +* @param[OUT] pu8Buf +* Pointer to data buffer +* @param[IN] u32offset +* Address to read from at the SPI flash +* @param[IN] u32Sz +* Data size +* @return Status of execution +* @note Data size is limited by the SPI flash size only +*/ +int8_t spi_flash_read(uint8_t *pu8Buf, uint32_t u32offset, uint32_t u32Sz) +{ + int8_t ret = M2M_SUCCESS; + if(u32Sz > FLASH_BLOCK_SIZE) + { + do + { + ret = spi_flash_read_internal(pu8Buf, u32offset, FLASH_BLOCK_SIZE); + if(M2M_SUCCESS != ret) goto ERR; + u32Sz -= FLASH_BLOCK_SIZE; + u32offset += FLASH_BLOCK_SIZE; + pu8Buf += FLASH_BLOCK_SIZE; + } while(u32Sz > FLASH_BLOCK_SIZE); + } + + ret = spi_flash_read_internal(pu8Buf, u32offset, u32Sz); + +ERR: + return ret; +} + +/** +* @fn spi_flash_write +* @brief Program SPI flash +* @param[IN] pu8Buf +* Pointer to data buffer +* @param[IN] u32Offset +* Address to write to at the SPI flash +* @param[IN] u32Sz +* Data size +* @return Status of execution +*/ +int8_t spi_flash_write(uint8_t *pu8Buf, uint32_t u32Offset, uint32_t u32Sz) +{ +#ifdef PROFILING + uint32_t t1 = 0; + uint32_t percent =0; + uint32_t tpercent =0; +#endif + int8_t ret = M2M_SUCCESS; + uint32_t u32wsz; + uint32_t u32off; + uint32_t u32Blksz; + u32Blksz = FLASH_PAGE_SZ; + u32off = u32Offset % u32Blksz; +#ifdef PROFILING + tpercent = (u32Sz/u32Blksz)+((u32Sz%u32Blksz)>0); + t1 = nm_bsp_get_tick(); + M2M_PRINT(">Start programming...\r\n"); +#endif + if(u32Sz<=0) + { + M2M_ERR("Data size = %d", (int)u32Sz); + ret = M2M_ERR_FAIL; + goto ERR; + } + + if(u32off) /*first part of data in the address page*/ + { + u32wsz = u32Blksz - u32off; + if(spi_flash_pp(u32Offset, pu8Buf, (uint16_t)BSP_MIN(u32Sz, u32wsz))!=M2M_SUCCESS) + { + ret = M2M_ERR_FAIL; + goto ERR; + } + if(u32Sz < u32wsz) goto EXIT; + pu8Buf += u32wsz; + u32Offset += u32wsz; + u32Sz -= u32wsz; + } + while(u32Sz > 0) + { + u32wsz = BSP_MIN(u32Sz, u32Blksz); + + /*write complete page or the remaining data*/ + if(spi_flash_pp(u32Offset, pu8Buf, (uint16_t)u32wsz)!=M2M_SUCCESS) + { + ret = M2M_ERR_FAIL; + goto ERR; + } + pu8Buf += u32wsz; + u32Offset += u32wsz; + u32Sz -= u32wsz; +#ifdef PROFILING + percent++; + printf("\r>Complete Percentage = %d%%.\r", ((percent*100)/tpercent)); +#endif + } +EXIT: +#ifdef PROFILING + M2M_PRINT("\rDone\t\t\t\t\t\t"); + M2M_PRINT("\n#Programming time = %f sec\n\r", (nm_bsp_get_tick() - t1)/1000.0); +#endif +ERR: + return ret; +} + +/** +* @fn spi_flash_erase +* @brief Erase from data from SPI flash +* @param[IN] u32Offset +* Address to write to at the SPI flash +* @param[IN] u32Sz +* Data size +* @return Status of execution +* @note Data size is limited by the SPI flash size only +*/ +int8_t spi_flash_erase(uint32_t u32Offset, uint32_t u32Sz) +{ + uint32_t i = 0; + uint8_t tmp = 0; + int p; + int j; + int x; + +#ifdef PROFILING + uint32_t t; + t = nm_bsp_get_tick(); +#endif + M2M_DBG("\r\n>Start erasing...\r\n"); + for(i = u32Offset; i < (u32Sz +u32Offset); i += (16*FLASH_PAGE_SZ)) + { + for(j = 0, p = 0; (j < MAX_RETRY) && (p < MAX_PROG_CNT); j++) + { + if((spi_flash_write_enable() == M2M_SUCCESS) && + (spi_flash_read_status_reg(&tmp) == M2M_SUCCESS) && + (spi_flash_sector_erase(i) == M2M_SUCCESS) && + (spi_flash_read_status_reg(&tmp) == M2M_SUCCESS)) + { + x = 0; + while(++x < MAX_ATTEMPT_STATUS_READ) + { + if(spi_flash_read_status_reg(&tmp) != M2M_SUCCESS) + { + break; + } + if(!(tmp & 0x01)) + { + p++; + break; + } + } + } + } + if(p != MAX_PROG_CNT) + { + return M2M_ERR_FAIL; + } + } + M2M_DBG("Done\r\n"); +#ifdef PROFILING + M2M_PRINT("#Erase time = %f sec\n", (nm_bsp_get_tick()-t)/1000.0); +#endif + return M2M_SUCCESS; +} + +/** +* @fn spi_flash_get_size +* @brief Get size of SPI Flash +* @return Size of Flash +*/ +uint32_t spi_flash_get_size(void) +{ + uint32_t u32FlashId = 0, u32FlashPwr = 0; + static uint32_t gu32InternalFlashSize= 0; + + if(!gu32InternalFlashSize) + { + u32FlashId = spi_flash_rdid();//spi_flash_probe(); + if(u32FlashId != 0xffffffff) + { + /*flash size is the third byte from the FLASH RDID*/ + u32FlashPwr = ((u32FlashId>>16)&0xff) - 0x11; /*2MBIT is the min*/ + /*That number power 2 to get the flash size*/ + gu32InternalFlashSize = 1<Source/BoardCfg_Pinguino.h Source/BoardCfg_ChaletduinoV2.h - + - Source/winc3400/bsp/include/nm_bsp.h - Source/winc3400/bsp/include/nm_bsp_internal.h - - - - - - - Source/winc3400/bus_wrapper/include/nm_bus_wrapper.h - - + Source/winc3400_142/bsp/include/nm_bsp.h - Source/winc3400/common/include/nm_common.h - Source/winc3400/common/include/nm_debug.h + Source/winc3400_142/common/include/nm_common.h + Source/winc3400_142/common/include/nm_debug.h - Source/winc3400/driver/include/ecc_types.h - Source/winc3400/driver/include/m2m_ate_mode.h - Source/winc3400/driver/include/m2m_crypto.h - Source/winc3400/driver/include/m2m_flash.h - Source/winc3400/driver/include/m2m_ota.h - Source/winc3400/driver/include/m2m_periph.h - Source/winc3400/driver/include/m2m_ssl.h - Source/winc3400/driver/include/m2m_types.h - Source/winc3400/driver/include/m2m_wifi.h + Source/winc3400_142/driver/include/ecc_types.h + Source/winc3400_142/driver/include/m2m_flash.h + Source/winc3400_142/driver/include/m2m_hif.h + Source/winc3400_142/driver/include/m2m_ota.h + Source/winc3400_142/driver/include/m2m_periph.h + Source/winc3400_142/driver/include/m2m_ssl.h + Source/winc3400_142/driver/include/m2m_types.h + Source/winc3400_142/driver/include/m2m_wifi.h + Source/winc3400_142/driver/include/nmasic.h + Source/winc3400_142/driver/include/nmbus.h + Source/winc3400_142/driver/include/nmdrv.h + Source/winc3400_142/driver/include/nmspi.h - Source/winc3400/driver/source/m2m_hif.h - Source/winc3400/driver/source/nmasic.h - Source/winc3400/driver/source/nmbus.h - Source/winc3400/driver/source/nmdrv.h - Source/winc3400/driver/source/nmflash.h - Source/winc3400/driver/source/nmi2c.h - Source/winc3400/driver/source/nmspi.h - Source/winc3400/driver/source/nmuart.h - - Source/winc3400/programmer/programmer.h - - Source/winc3400/socket/include/m2m_socket_host_if.h - Source/winc3400/socket/include/socket.h + Source/winc3400_142/socket/include/m2m_socket_host_if.h + Source/winc3400_142/socket/include/netinet_in.h + Source/winc3400_142/socket/include/socket.h - Source/winc3400/socket/source/socket_internal.h - Source/winc3400/spi_flash/include/spi_flash.h - Source/winc3400/spi_flash/include/spi_flash_map.h + Source/winc3400_142/spi_flash/include/spi_flash.h + Source/winc3400_142/spi_flash/include/spi_flash_map.h - Source/winc3400/winc_init.h Source/define.h Source/DigitalIO.h @@ -127,66 +110,52 @@ Source/ChaletduinoBoard.c Source/ChaletduinoV2Board.c - + - - Source/winc3400/bsp/source/nm_bsp.c - - - - - - - Source/winc3400/bus_wrapper/source/nm_bus_wrapper.c - - Source/winc3400/common/source/nm_common.c + Source/winc3400_142/common/source/nm_common.c - Source/winc3400/driver/source/m2m_ate_mode.c - Source/winc3400/driver/source/m2m_crypto.c - Source/winc3400/driver/source/m2m_flash.c - Source/winc3400/driver/source/m2m_hif.c - Source/winc3400/driver/source/m2m_ota.c - Source/winc3400/driver/source/m2m_periph.c - Source/winc3400/driver/source/m2m_ssl.c - Source/winc3400/driver/source/m2m_wifi.c - Source/winc3400/driver/source/nmasic.c - Source/winc3400/driver/source/nmbus.c - Source/winc3400/driver/source/nmdrv.c - Source/winc3400/driver/source/nmflash.c - Source/winc3400/driver/source/nmi2c.c - Source/winc3400/driver/source/nmspi.c - Source/winc3400/driver/source/nmuart.c + Source/winc3400_142/driver/source/m2m_flash.c + Source/winc3400_142/driver/source/m2m_hif.c + Source/winc3400_142/driver/source/m2m_ota.c + Source/winc3400_142/driver/source/m2m_periph.c + Source/winc3400_142/driver/source/m2m_ssl.c + Source/winc3400_142/driver/source/m2m_wifi.c + Source/winc3400_142/driver/source/nmasic.c + Source/winc3400_142/driver/source/nmbus.c + Source/winc3400_142/driver/source/nmdrv.c + Source/winc3400_142/driver/source/nmspi.c - - - Source/winc3400/socket/source/socket.c + Source/winc3400_142/socket/source/inet_addr.c + Source/winc3400_142/socket/source/inet_ntop.c + Source/winc3400_142/socket/source/socket.c - Source/winc3400/spi_flash/source/spi_flash.c + Source/winc3400_142/spi_flash/source/spi_flash.c - Source/winc3400/winc_init.c Source/DigitalIO.c Source/InternalUart.c @@ -928,42 +897,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1128,7 +1061,8 @@ - + @@ -1186,7 +1120,6 @@ - @@ -1410,41 +1343,7 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + diff --git a/ChaletLora.X/nbproject/private/private.xml b/ChaletLora.X/nbproject/private/private.xml index 3510694..371fb63 100644 --- a/ChaletLora.X/nbproject/private/private.xml +++ b/ChaletLora.X/nbproject/private/private.xml @@ -16,16 +16,6 @@ - - file:/D:/Main/PicDev/Projets/ChaletLora/ChaletLora.X/Source/main.c - file:/D:/Main/PicDev/Projets/ChaletLora/ChaletLora.X/Source/BootloaderInterface.c - file:/D:/Main/PicDev/Projets/ChaletLora/ChaletLora.X/Source/WiFiCtrl.c - file:/D:/Main/PicDev/Projets/ChaletLora/ChaletLora.X/Source/winc3400/socket/source/socket.c - file:/D:/Main/PicDev/Projets/ChaletLora/ChaletLora.X/Source/LoraNetworkInterface.c - file:/D:/Main/PicDev/Projets/ChaletLora/ChaletLora.X/Source/main.h - file:/D:/Main/PicDev/Projets/ChaletLora/ChaletLora.X/Source/WiFiCtrl.h - file:/D:/Main/PicDev/Projets/ChaletLora/ChaletLora.X/Source/Terminal.c - file:/D:/Main/PicDev/Projets/ChaletLora/ChaletLora.X/Source/versionbuild.h - +