sint8 m2m_wifi_enable_ap(CONST tstrM2MAPConfig* pstrM2MAPConfig) { sint8 ret = M2M_SUCCESS; #if defined(M2M_WILC1000) && defined(COMPUTE_PMK_IN_HOST) if(pstrM2MAPConfig->u8SecType == M2M_WIFI_SEC_WPA_PSK) { tstrM2MAPConfig strTempM2MAPConfig; m2m_memcpy((uint8 *)&strTempM2MAPConfig, (uint8 *)pstrM2MAPConfig, sizeof(tstrM2MAPConfig)); strTempM2MAPConfig.u8IsPMKUsed = 1; pbkdf2_sha1((uint8 *)pstrM2MAPConfig->au8PSK,m2m_strlen((uint8 *)pstrM2MAPConfig->au8PSK), (uint8 *)pstrM2MAPConfig->au8SSID,m2m_strlen((uint8 *)pstrM2MAPConfig->au8SSID),strTempM2MAPConfig.au8PMK); pstrM2MAPConfig = &strTempM2MAPConfig; } #elif defined(M2M_WILC1000) && !defined(COMPUTE_PMK_IN_HOST) if(pstrM2MAPConfig->u8SecType == M2M_WIFI_SEC_WPA_PSK) { tstrM2MAPConfig strTempM2MAPConfig; strTempM2MAPConfig.u8IsPMKUsed = 0; m2m_memcpy((uint8*)&strTempM2MAPConfig,(uint8*)pstrM2MAPConfig,sizeof(tstrM2MAPConfig)); pstrM2MAPConfig = &strTempM2MAPConfig; } #endif ret = hif_send(M2M_REQ_GRP_WIFI, M2M_WIFI_REQ_ENABLE_AP, (uint8 *)pstrM2MAPConfig, sizeof(tstrM2MAPConfig), NULL, 0, 0); return ret; }
static sint8 nm_i2c_write_special(uint8 *wb1, uint16 sz1, uint8 *wb2, uint16 sz2) { static uint8 tmp[NM_BUS_MAX_TRX_SZ]; m2m_memcpy(tmp, wb1, sz1); m2m_memcpy(&tmp[sz1], wb2, sz2); return nm_i2c_write(tmp, sz1+sz2); }
sint8 m2m_wifi_start_provision_mode(tstrM2MAPConfig *pstrAPConfig, char *pcHttpServerDomainName, uint8 bEnableHttpRedirect) { sint8 s8Ret = M2M_ERR_FAIL; if((pstrAPConfig != NULL)) { tstrM2MProvisionModeConfig strProvConfig; if(M2M_SUCCESS == m2m_validate_ap_parameters(pstrAPConfig)) { m2m_memcpy((uint8*)&strProvConfig.strApConfig, (uint8*)pstrAPConfig, sizeof(tstrM2MAPConfig)); if((m2m_strlen((uint8 *)pcHttpServerDomainName) <= 0) || (NULL == pcHttpServerDomainName)) { M2M_ERR("INVALID DOMAIN NAME\n"); goto ERR1; } m2m_memcpy((uint8*)strProvConfig.acHttpServerDomainName, (uint8*)pcHttpServerDomainName, 64); strProvConfig.u8EnableRedirect = bEnableHttpRedirect; /* Stop Scan if it is ongoing. */ gu8scanInProgress = 0; s8Ret = hif_send(M2M_REQ_GRP_WIFI, M2M_WIFI_REQ_START_PROVISION_MODE | M2M_REQ_DATA_PKT, (uint8*)&strProvConfig, sizeof(tstrM2MProvisionModeConfig), NULL, 0, 0); } else { /*goto ERR1;*/ } } ERR1: return s8Ret; }
sint8 nmi_get_mac_address(uint8 *pu8MacAddr) { sint8 ret; uint32 u32RegValue; uint8 mac[6]; tstrGpRegs strgp = {0}; ret = nm_read_reg_with_ret(rNMI_GP_REG_2, &u32RegValue); if(ret != M2M_SUCCESS) goto _EXIT_ERR; #ifdef ARDUINO if (u32RegValue) { ret = nm_read_block(u32RegValue|0x30000,(uint8*)&strgp,sizeof(tstrGpRegs)); if(ret != M2M_SUCCESS) goto _EXIT_ERR; u32RegValue = strgp.u32Mac_efuse_mib; } else { // firmware version 19.3.0 ret = nm_read_reg_with_ret(rNMI_GP_REG_0, &u32RegValue); if(ret != M2M_SUCCESS) goto _EXIT_ERR; } #else ret = nm_read_block(u32RegValue|0x30000,(uint8*)&strgp,sizeof(tstrGpRegs)); if(ret != M2M_SUCCESS) goto _EXIT_ERR; u32RegValue = strgp.u32Mac_efuse_mib; #endif u32RegValue &=0x0000ffff; ret = nm_read_block(u32RegValue|0x30000, mac, 6); m2m_memcpy(pu8MacAddr, mac, 6); return ret; _EXIT_ERR: return ret; }
/********************************************************************* Function connectSocket Description Connect to a remote TCP Server. Return Author Ahmed Ezzat Version 1.0 Date 5 June 2012 *********************************************************************/ sint8 connectSocket(SOCKET sock, struct sockaddr *pstrAddr, uint8 u8AddrLen) { sint8 s8Ret = SOCK_ERR_INVALID_ARG; if((sock >= 0) && (pstrAddr != NULL) && (gastrSockets[sock].bIsUsed == 1) && (u8AddrLen != 0)) { tstrConnectCmd strConnect; uint8 u8Cmd = SOCKET_CMD_CONNECT; if((gastrSockets[sock].u8SSLFlags) & SSL_FLAGS_ACTIVE) { u8Cmd = SOCKET_CMD_SSL_CONNECT; strConnect.u8SslFlags = gastrSockets[sock].u8SSLFlags; } strConnect.sock = sock; m2m_memcpy((uint8 *)&strConnect.strAddr, (uint8 *)pstrAddr, sizeof(tstrSockAddr)); strConnect.strAddr.u16Family = strConnect.strAddr.u16Family; strConnect.strAddr.u16Port = strConnect.strAddr.u16Port; strConnect.strAddr.u32IPAddr = strConnect.strAddr.u32IPAddr; strConnect.u16SessionID = gastrSockets[sock].u16SessionID; s8Ret = SOCKET_REQUEST(u8Cmd, (uint8*)&strConnect,sizeof(tstrConnectCmd), NULL, 0, 0); if(s8Ret != SOCK_ERR_NO_ERROR) { s8Ret = SOCK_ERR_INVALID; } } return s8Ret; }
sint8 nmi_get_otp_mac_address(uint8 *pu8MacAddr, uint8 * pu8IsValid) { sint8 ret; uint32 u32RegValue; uint8 mac[6]; tstrGpRegs strgp = {0}; ret = nm_read_reg_with_ret(rNMI_GP_REG_2, &u32RegValue); if(ret != M2M_SUCCESS) goto _EXIT_ERR; ret = nm_read_block(u32RegValue|0x30000,(uint8*)&strgp,sizeof(tstrGpRegs)); if(ret != M2M_SUCCESS) goto _EXIT_ERR; u32RegValue = strgp.u32Mac_efuse_mib; if(!EFUSED_MAC(u32RegValue)) { M2M_DBG("Default MAC\n"); m2m_memset(pu8MacAddr, 0, 6); goto _EXIT_ERR; } M2M_DBG("OTP MAC\n"); u32RegValue >>=16; ret = nm_read_block(u32RegValue|0x30000, mac, 6); m2m_memcpy(pu8MacAddr,mac,6); if(pu8IsValid) *pu8IsValid = 1; return ret; _EXIT_ERR: if(pu8IsValid) *pu8IsValid = 0; return ret; }
/********************************************************************* Function connect Description Connect to a remote TCP Server. Return Author Ahmed Ezzat Version 1.0 Date 5 June 2012 *********************************************************************/ sint8 connect(SOCKET sock, struct sockaddr *pstrAddr, uint8 u8AddrLen) { sint8 s8Ret = SOCK_ERR_INVALID_ARG; if((sock >= 0) && (pstrAddr != NULL)) { tstrConnectCmd strConnect; uint8 u8Cmd = SOCKET_CMD_CONNECT; if(gastrSockets[sock].bIsSSLSock) { u8Cmd = SOCKET_CMD_SSL_CONNECT; } strConnect.sock = sock; m2m_memcpy((uint8 *)&strConnect.strAddr, (uint8 *)pstrAddr, sizeof(tstrSockAddr)); //strConnect.strAddr = *((tstrSockAddr*)pstrAddr); strConnect.strAddr.u16Family = strConnect.strAddr.u16Family; strConnect.strAddr.u16Port = strConnect.strAddr.u16Port; strConnect.strAddr.u32IPAddr = strConnect.strAddr.u32IPAddr; s8Ret = SOCKET_REQUEST(u8Cmd, (uint8*)&strConnect,sizeof(tstrConnectCmd), NULL, 0, 0); if(s8Ret != SOCK_ERR_NO_ERROR) { s8Ret = SOCK_ERR_INVALID; } } return s8Ret; }
/********************************************************************* Function bind Description Request to bind a socket on a local address. Return Author Ahmed Ezzat Version 1.0 Date 5 June 2012 *********************************************************************/ sint8 bind(SOCKET sock, struct sockaddr *pstrAddr, uint8 u8AddrLen) { sint8 s8Ret = SOCK_ERR_INVALID_ARG; if((pstrAddr != NULL) && (sock >= 0) && (gastrSockets[sock].bIsUsed == 1) && (u8AddrLen != 0)) { tstrBindCmd strBind; /* Build the bind request. */ strBind.sock = sock; m2m_memcpy((uint8 *)&strBind.strAddr, (uint8 *)pstrAddr, sizeof(tstrSockAddr)); //strBind.strAddr = *((tstrSockAddr*)pstrAddr); 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*)&strBind,sizeof(tstrBindCmd) , NULL , 0, 0); if(s8Ret != SOCK_ERR_NO_ERROR) { s8Ret = SOCK_ERR_INVALID; } } return s8Ret; }
sint8 m2m_wifi_set_mac_address(uint8 au8MacAddress[6]) { tstrM2mSetMacAddress strTmp; m2m_memcpy((uint8*) strTmp.au8Mac, (uint8*) au8MacAddress, 6); return hif_send(M2M_REQ_GRP_WIFI, M2M_WIFI_REQ_SET_MAC_ADDRESS, (uint8*) &strTmp, sizeof(tstrM2mSetMacAddress), NULL, 0,0); }
sint8 m2m_wifi_request_scan_ssid(uint8 ch,char* pcssid) { sint8 s8Ret = M2M_ERR_SCAN_IN_PROGRESS; if(!gu8scanInProgress) { tstrM2MScan strtmp; m2m_memset((uint8*)&strtmp,0,sizeof(tstrM2MScan)); strtmp.u8ChNum = ch; if(pcssid) { uint8 len = m2m_strlen((uint8 *)pcssid); m2m_memcpy(strtmp.au8SSID,(uint8 *)pcssid,len+1); } s8Ret = hif_send(M2M_REQ_GRP_WIFI, M2M_WIFI_REQ_SCAN, (uint8*)&strtmp, sizeof(tstrM2MScan),NULL, 0,0); if(s8Ret == M2M_SUCCESS) { gu8scanInProgress = 1; } else { M2M_ERR("SCAN Failed Ret = %d\n",s8Ret); } } else { M2M_ERR("SCAN In Progress\n"); } return s8Ret; }
sint8 nmi_get_otp_mac_address(uint8 *pu8MacAddr, uint8 * pu8IsValid) { sint8 ret; uint32 u32RegValue; uint8 mac[6]; ret = nm_read_reg_with_ret(rNMI_GP_REG_0, &u32RegValue); if(ret != M2M_SUCCESS) goto _EXIT_ERR; if(!EFUSED_MAC(u32RegValue)) { M2M_DBG("Default MAC\n"); m2m_memset(pu8MacAddr, 0, 6); goto _EXIT_ERR; } M2M_DBG("OTP MAC\n"); u32RegValue >>=16; nm_read_block(u32RegValue|0x30000, mac, 6); m2m_memcpy(pu8MacAddr,mac,6); if(pu8IsValid) *pu8IsValid = 1; return ret; _EXIT_ERR: if(pu8IsValid) *pu8IsValid = 0; return ret; }
/********************************************************************* Function setsockopt Description Return None. Author Abdelrahman Diab Version 1.0 Date 9 September 2014 *********************************************************************/ sint8 sslSetSockOpt(SOCKET sock, uint8 u8Opt, const void *pvOptVal, uint16 u16OptLen) { sint8 s8Ret = SOCK_ERR_INVALID_ARG; if(sock < TCP_SOCK_MAX) { if(gastrSockets[sock].u8SSLFlags & SSL_FLAGS_ACTIVE) { if(u8Opt == SO_SSL_BYPASS_X509_VERIF) { int optVal = *((int*)pvOptVal); if(optVal) { gastrSockets[sock].u8SSLFlags |= SSL_FLAGS_BYPASS_X509; } else { gastrSockets[sock].u8SSLFlags &= ~SSL_FLAGS_BYPASS_X509; } s8Ret = SOCK_ERR_NO_ERROR; } else if(u8Opt == SO_SSL_SNI) { if(u16OptLen < HOSTNAME_MAX_SIZE) { uint8 *pu8SNI = (uint8*)pvOptVal; tstrSSLSetSockOptCmd strCmd; strCmd.sock = sock; strCmd.u16SessionID = gastrSockets[sock].u16SessionID; strCmd.u8Option = u8Opt; strCmd.u32OptLen = u16OptLen; m2m_memcpy(strCmd.au8OptVal, pu8SNI, HOSTNAME_MAX_SIZE); if(SOCKET_REQUEST(SOCKET_CMD_SSL_SET_SOCK_OPT, (uint8*)&strCmd, sizeof(tstrSSLSetSockOptCmd), 0, 0, 0) == M2M_ERR_MEM_ALLOC) { s8Ret = SOCKET_REQUEST(SOCKET_CMD_SSL_SET_SOCK_OPT | M2M_REQ_DATA_PKT, (uint8*)&strCmd, sizeof(tstrSSLSetSockOptCmd), 0, 0, 0); } s8Ret = SOCK_ERR_NO_ERROR; } else { M2M_ERR("SNI Exceeds Max Length\n"); } } else { M2M_ERR("Unknown SSL Socket Option %d\n",u8Opt); } } else { M2M_ERR("Not SSL Socket\n"); } } return s8Ret; }
/* * @fn hif_receive * @brief Host interface interrupt serviece routine * @param[IN] u32Addr * Receive start address * @param[OUT] pu8Buf * Pointer to receive buffer. Allocated by the caller * @param[IN] u16Sz * Receive buffer size * @return M2M_SUCCESS in case of success and -ve error code in case of failure * @author M. Abdelmawla * @date 15 July 2012 * @return 1 in case of interrupt received else 0 will be returned * @version 1.0 */ sint8 hif_receive(uint32 u32Addr, uint8 *pu8Buf, uint16 u16Sz, uint8 isDone) { if (pu8Buf != NULL) { m2m_memcpy(pu8Buf, (uint8*) u32Addr, u16Sz); } return M2M_SUCCESS; }
sint8 m2m_wifi_ap_add_black_list(uint8 bAddNewEntry,uint8* mac_addr) { uint8 u8BlackListRequest[7]; u8BlackListRequest[0] = bAddNewEntry; m2m_memcpy(&u8BlackListRequest[1],mac_addr,6); sint8 ret = M2M_SUCCESS; ret = hif_send(M2M_REQ_GRP_WIFI, M2M_WIFI_REQ_AP_BLACK_LIST,u8BlackListRequest, sizeof(u8BlackListRequest), NULL, 0, 0); return ret; }
sint8 m2m_wifi_start_provision_mode(tstrM2MAPConfig *pstrAPConfig, char *pcHttpServerDomainName, uint8 bEnableHttpRedirect) { sint8 s8Ret = M2M_ERR_FAIL; if((pstrAPConfig != NULL)) { tstrM2MProvisionModeConfig strProvConfig; m2m_memcpy((uint8*)&strProvConfig.strApConfig, (uint8*)pstrAPConfig, sizeof(tstrM2MAPConfig)); m2m_memcpy((uint8*)strProvConfig.acHttpServerDomainName, (uint8*)pcHttpServerDomainName, 64); strProvConfig.u8EnableRedirect = bEnableHttpRedirect; /* Stop Scan if it is ongoing. */ gu8scanInProgress = 0; s8Ret = hif_send(M2M_REQ_GRP_WIFI, M2M_WIFI_REQ_START_PROVISION_MODE | M2M_REQ_DATA_PKT, (uint8*)&strProvConfig, sizeof(tstrM2MProvisionModeConfig), NULL, 0, 0); } return s8Ret; }
sint8 m2m_wifi_set_device_name(uint8 *pu8DeviceName, uint8 u8DeviceNameLength) { tstrM2MDeviceNameConfig strDeviceName; if(u8DeviceNameLength >= M2M_DEVICE_NAME_MAX) { u8DeviceNameLength = M2M_DEVICE_NAME_MAX; } //pu8DeviceName[u8DeviceNameLength] = '\0'; u8DeviceNameLength ++; m2m_memcpy(strDeviceName.au8DeviceName, pu8DeviceName, u8DeviceNameLength); return hif_send(M2M_REQ_GRP_WIFI, M2M_WIFI_REQ_SET_DEVICE_NAME, (uint8*)&strDeviceName, sizeof(tstrM2MDeviceNameConfig), NULL, 0,0); }
sint8 m2m_wifi_wps(uint8 u8TriggerType,const char *pcPinNumber) { tstrM2MWPSConnect strtmp; /* Stop Scan if it is ongoing. */ gu8scanInProgress = 0; strtmp.u8TriggerType = u8TriggerType; /*If WPS is using PIN METHOD*/ if (u8TriggerType == WPS_PIN_TRIGGER) m2m_memcpy ((uint8*)strtmp.acPinNumber,(uint8*) pcPinNumber,8); return hif_send(M2M_REQ_GRP_WIFI, M2M_WIFI_REQ_WPS, (uint8*)&strtmp,sizeof(tstrM2MWPSConnect), NULL, 0,0); }
sint8 m2m_crypto_rsa_sign_verify(uint8 *pu8N, uint16 u16NSize, uint8 *pu8E, uint16 u16ESize, uint8 *pu8SignedMsgHash, uint16 u16HashLength, uint8 *pu8RsaSignature) { sint8 ret = M2M_ERR_FAIL; if((!gstrCryptoCtxt.u8CryptoBusy) && (pu8N != NULL) && (pu8E != NULL) && (pu8RsaSignature != NULL) && (pu8SignedMsgHash != NULL) && (u16NSize != 0) && (u16ESize != 0) && (u16HashLength != 0) && (pu8RsaSignature != NULL) ) { tstrRsaPayload strRsa = {0}; m2m_memcpy(strRsa.au8N,pu8N,u16NSize); m2m_memcpy(strRsa.au8E,pu8E,u16ESize); m2m_memcpy(strRsa.au8Hash,pu8SignedMsgHash,u16HashLength); strRsa.u16Esz = u16ESize; strRsa.u16Hsz = u16HashLength; strRsa.u16Nsz = u16NSize; ret = hif_send(M2M_REQ_GROUP_CRYPTO,M2M_CRYPTO_REQ_RSA_SIGN_VERIFY|M2M_REQ_DATA_PKT,(uint8*)&strRsa,sizeof(tstrRsaPayload),NULL,0,0); } return ret; }
NMI_API sint8 m2m_wifi_enable_mac_mcast(uint8* pu8MulticastMacAddress, uint8 u8AddRemove) { sint8 s8ret = M2M_ERR_FAIL; tstrM2MMulticastMac strMulticastMac; if(pu8MulticastMacAddress != NULL ) { strMulticastMac.u8AddRemove = u8AddRemove; m2m_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_GRP_WIFI, M2M_WIFI_REQ_SET_MAC_MCAST, (uint8 *)&strMulticastMac,sizeof(tstrM2MMulticastMac),NULL,0,0); } return s8ret; }
sint8 nmi_get_mac_address(uint8 *pu8MacAddr) { sint8 ret; uint32 u32RegValue; uint8 mac[6]; ret = nm_read_reg_with_ret(rNMI_GP_REG_0, &u32RegValue); if(ret != M2M_SUCCESS) goto _EXIT_ERR; u32RegValue &=0x0000ffff; nm_read_block(u32RegValue|0x30000, mac, 6); m2m_memcpy(pu8MacAddr, mac, 6); return ret; _EXIT_ERR: return ret; }
NMI_API void Socket_ReadSocketData(SOCKET sock, tstrSocketRecvMsg *pstrRecv,uint8 u8SocketMsg, uint32 u32StartAddress,uint16 u16ReadCount) { if((u16ReadCount > 0) && (gastrSockets[sock].pu8UserBuffer != NULL) && (gastrSockets[sock].u16UserBufferSize > 0) && (gastrSockets[sock].bIsUsed == 1)) { u32Address = u32StartAddress; uint16 u16Read; sint16 s16Diff; uint8 u8SetRxDone; m2m_memcpy((uint8 *)&msg_xfer, (uint8 *)pstrRecv, sizeof(tstrSocketRecvMsg)); msg_xfer.u16RemainingSize = u16ReadCount; //do //{ u8SetRxDone = 1; u16Read = u16ReadCount; s16Diff = u16Read - gastrSockets[sock].u16UserBufferSize; if(s16Diff > 0) { u8SetRxDone = 0; u16Read = gastrSockets[sock].u16UserBufferSize; hif_small_xfer = 1; sock_xfer = sock; type_xfer = u8SocketMsg; } if(hif_receive(u32Address, gastrSockets[sock].pu8UserBuffer, u16Read, u8SetRxDone) == M2M_SUCCESS) { msg_xfer.pu8Buffer = gastrSockets[sock].pu8UserBuffer; msg_xfer.s16BufferSize = u16Read; msg_xfer.u16RemainingSize -= u16Read; if (gpfAppSocketCb) gpfAppSocketCb(sock,u8SocketMsg, &msg_xfer); u32Address += u16Read; } else { M2M_INFO("(ERRR)Current <%d>\n", u16ReadCount); //break; } //}while(u16ReadCount != 0); } }
/********************************************************************* Function nmi_inet_addr Description Return Unsigned 32-bit integer representing the IP address in Network byte order. Author Ahmed Ezzat Version 1.0 Date 4 June 2012 *********************************************************************/ uint32 nmi_inet_addr(char *pcIpAddr) { uint8 tmp; uint32 u32IP = 0; uint8 au8IP[4]; uint8 c; uint8 i, j; tmp = 0; for(i = 0; i < 4; ++i) { j = 0; do { c = *pcIpAddr; ++j; if(j > 4) { return 0; } if(c == '.' || c == 0) { au8IP[i] = tmp; tmp = 0; } else if(c >= '0' && c <= '9') { tmp = (tmp * 10) + (c - '0'); } else { return 0; } ++pcIpAddr; } while(c != '.' && c != 0); } m2m_memcpy((uint8*)&u32IP, au8IP, 4); return u32IP; }
uint8_t WiFiClass::beginProvision(char *ssid, char *url, uint8_t channel) { tstrM2MAPConfig strM2MAPConfig; if (!_init) { init(); } // Enter Provision mode: memset(&strM2MAPConfig, 0x00, sizeof(tstrM2MAPConfig)); strcpy((char *)&strM2MAPConfig.au8SSID, ssid); strM2MAPConfig.u8ListenChannel = channel; strM2MAPConfig.u8SecType = M2M_WIFI_SEC_OPEN; strM2MAPConfig.u8SsidHide = SSID_MODE_VISIBLE; strM2MAPConfig.au8DHCPServerIP[0] = 0xC0; /* 192 */ strM2MAPConfig.au8DHCPServerIP[1] = 0xA8; /* 168 */ strM2MAPConfig.au8DHCPServerIP[2] = 0x01; /* 1 */ strM2MAPConfig.au8DHCPServerIP[3] = 0x01; /* 1 */ if (m2m_wifi_start_provision_mode((tstrM2MAPConfig *)&strM2MAPConfig, url, 1) < 0) { _status = WL_CONNECT_FAILED; return _status; } _status = WL_CONNECTED; _mode = WL_PROV_MODE; memset(_ssid, 0, M2M_MAX_SSID_LEN); memcpy(_ssid, ssid, strlen(ssid)); m2m_memcpy((uint8 *)&_localip, (uint8 *)&strM2MAPConfig.au8DHCPServerIP[0], 4); _submask = 0x00FFFFFF; _gateway = _localip; // WiFi led ON (rev A then rev B). m2m_periph_gpio_set_val(M2M_PERIPH_GPIO15, 0); m2m_periph_gpio_set_val(M2M_PERIPH_GPIO4, 0); return _status; }
sint8 m2m_wifi_download_cert(uint8* pCertData,uint32 u32CertSize) { sint8 s8Ret = -1; char pChunk[32]; uint32 u32ChunkSize = 32,u32TempSize,u32OrigCertSize = u32CertSize; if(u32CertSize > M2M_ENTERPRISE_CERT_MAX_LENGTH_IN_BYTES) { M2M_ERR("too large Cert. file, Max is %d\n",M2M_ENTERPRISE_CERT_MAX_LENGTH_IN_BYTES); return s8Ret; } while(u32CertSize > 0) { if(u32CertSize < u32ChunkSize) u32ChunkSize = u32CertSize; u32TempSize = (u32OrigCertSize&0xffff)|(u32ChunkSize<<16); m2m_memcpy((uint8 *)pChunk, &pCertData[u32OrigCertSize-u32CertSize], u32ChunkSize); s8Ret = hif_send(M2M_REQ_GRP_WIFI, M2M_WIFI_REQ_CERT_ADD_CHUNK, (uint8*)&u32TempSize, sizeof(u32CertSize), (uint8 *)pChunk, u32ChunkSize,sizeof(u32CertSize)); u32CertSize -= u32ChunkSize; } return hif_send(M2M_REQ_GRP_WIFI, M2M_WIFI_REQ_CERT_DOWNLOAD_DONE, NULL, 0, NULL, 0, 0); }
sint8 nmi_get_mac_address(uint8 *pu8MacAddr) { sint8 ret; uint32 u32RegValue; uint8 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*)&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); m2m_memcpy(pu8MacAddr, mac, 6); return ret; _EXIT_ERR: return ret; }
sint8 m2m_wifi_connect(char *pcSsid, uint8 u8SsidLen, uint8 u8SecType, void *pvAuthInfo, uint16 u16Ch) { sint8 ret = M2M_SUCCESS; tstrM2mWifiConnect strConnect; tstrM2MWifiSecInfo *pstrAuthInfo; if(u8SecType != M2M_WIFI_SEC_OPEN) { if((pvAuthInfo == NULL)||(m2m_strlen(pvAuthInfo)<=0)||(m2m_strlen(pvAuthInfo)>=M2M_MAX_PSK_LEN)) { M2M_ERR("PSK LEN INVALID\n"); ret = M2M_ERR_FAIL; goto ERR1; } } if((u8SsidLen<=0)||(u8SsidLen>=M2M_MAX_SSID_LEN)) { M2M_ERR("SSID LEN INVALID\n"); ret = M2M_ERR_FAIL; goto ERR1; } if(u16Ch>M2M_WIFI_CH_14) { if(u16Ch!=M2M_WIFI_CH_ALL) { M2M_ERR("CH INVALID\n"); ret = M2M_ERR_FAIL; goto ERR1; } } m2m_memcpy(strConnect.au8SSID, (uint8*)pcSsid, u8SsidLen); strConnect.au8SSID[u8SsidLen] = 0; strConnect.u16Ch = NM_BSP_B_L_16(u16Ch); pstrAuthInfo = &strConnect.strSec; pstrAuthInfo->u8SecType = u8SecType; if(u8SecType == M2M_WIFI_SEC_WEP) { tstrM2mWifiWepParams * pstrWepParams = (tstrM2mWifiWepParams*)pvAuthInfo; tstrM2mWifiWepParams *pstrWep = &pstrAuthInfo->uniAuth.strWepInfo; pstrWep->u8KeyIndx =pstrWepParams->u8KeyIndx-1; if(pstrWep->u8KeyIndx >= WEP_KEY_MAX_INDEX) { M2M_ERR("Invalid Wep key index %d\n", pstrWep->u8KeyIndx); ret = M2M_ERR_FAIL; goto ERR1; } pstrWep->u8KeySz = pstrWepParams->u8KeySz-1; if ((pstrWep->u8KeySz != WEP_40_KEY_STRING_SIZE)&& (pstrWep->u8KeySz != WEP_104_KEY_STRING_SIZE)) { M2M_ERR("Invalid Wep key length %d\n", pstrWep->u8KeySz); ret = M2M_ERR_FAIL; goto ERR1; } m2m_memcpy((uint8*)pstrWep->au8WepKey,(uint8*)pstrWepParams->au8WepKey, pstrWepParams->u8KeySz); pstrWep->au8WepKey[pstrWepParams->u8KeySz] = 0; } else if(u8SecType == M2M_WIFI_SEC_WPA_PSK) { uint8 u8KeyLen = m2m_strlen((uint8*)pvAuthInfo) + 1; m2m_memcpy(pstrAuthInfo->uniAuth.au8PSK, (uint8*)pvAuthInfo, u8KeyLen); } else if(u8SecType == M2M_WIFI_SEC_802_1X) { m2m_memcpy((uint8*)&pstrAuthInfo->uniAuth.strCred1x, (uint8*)pvAuthInfo, sizeof(tstr1xAuthCredentials)); } else if(u8SecType == M2M_WIFI_SEC_OPEN) { } else { M2M_ERR("undefined sec type\n"); ret = M2M_ERR_FAIL; goto ERR1; } ret = hif_send(M2M_REQ_GRP_WIFI, M2M_WIFI_REQ_CONNECT, (uint8*)&strConnect, sizeof(tstrM2mWifiConnect),NULL, 0,0); ERR1: return ret; }
/** * @fn m2m_wifi_cb(uint8 u8OpCode, uint16 u16DataSize, uint32 u32Addr, uint8 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. * @author * @date * @version 1.0 */ static void m2m_wifi_cb(uint8 u8OpCode, uint16 u16DataSize, uint32 u32Addr) { uint8 rx_buf[8]; if (u8OpCode == M2M_WIFI_RESP_CON_STATE_CHANGED) { tstrM2mWifiStateChanged strState; if (hif_receive(u32Addr, (uint8*) &strState,sizeof(tstrM2mWifiStateChanged), 0) == M2M_SUCCESS) { if (gpfAppWifiCb) gpfAppWifiCb(M2M_WIFI_RESP_CON_STATE_CHANGED, &strState); } } else if(u8OpCode == M2M_WIFI_RESP_CONN_INFO) { tstrM2MConnInfo strConnInfo; if(hif_receive(u32Addr, (uint8*)&strConnInfo, sizeof(tstrM2MConnInfo), 1) == M2M_SUCCESS) { if(gpfAppWifiCb) gpfAppWifiCb(M2M_WIFI_RESP_CONN_INFO, &strConnInfo); } } else if (u8OpCode == M2M_WIFI_RESP_MEMORY_RECOVER) { #if 0 if (hif_receive(u32Addr, rx_buf, 4, 1) == M2M_SUCCESS) { tstrM2mWifiStateChanged strState; m2m_memcpy((uint8*) &strState, rx_buf,sizeof(tstrM2mWifiStateChanged)); if (app_wifi_recover_cb) app_wifi_recover_cb(strState.u8CurrState); } #endif } else if (u8OpCode == M2M_WIFI_REQ_DHCP_CONF) { tstrM2MIPConfig strIpConfig; if (hif_receive(u32Addr, (uint8 *)&strIpConfig, sizeof(tstrM2MIPConfig), 0) == M2M_SUCCESS) { if (gpfAppWifiCb) gpfAppWifiCb(M2M_WIFI_REQ_DHCP_CONF, (uint8 *)&strIpConfig.u32StaticIP); } } else if (u8OpCode == M2M_WIFI_REQ_WPS) { tstrM2MWPSInfo strWps; m2m_memset((uint8*)&strWps,0,sizeof(tstrM2MWPSInfo)); if(hif_receive(u32Addr, (uint8*)&strWps, sizeof(tstrM2MWPSInfo), 0) == M2M_SUCCESS) { if (gpfAppWifiCb) gpfAppWifiCb(M2M_WIFI_REQ_WPS, &strWps); } } else if (u8OpCode == M2M_WIFI_RESP_IP_CONFLICT) { tstrM2MIPConfig strIpConfig; if(hif_receive(u32Addr, (uint8 *)&strIpConfig, sizeof(tstrM2MIPConfig), 0) == M2M_SUCCESS) { M2M_DBG("Conflicted IP\"%u.%u.%u.%u\"\n", ((uint8 *)&strIpConfig.u32StaticIP)[0], ((uint8 *)&strIpConfig.u32StaticIP)[1], ((uint8 *)&strIpConfig.u32StaticIP)[2], ((uint8 *)&strIpConfig.u32StaticIP)[3]); if (gpfAppWifiCb) gpfAppWifiCb(M2M_WIFI_RESP_IP_CONFLICT, NULL); } } else if (u8OpCode == M2M_WIFI_RESP_SCAN_DONE) { tstrM2mScanDone strState; gu8scanInProgress = 0; if(hif_receive(u32Addr, (uint8*)&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*)&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*)&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*)&strResp, sizeof(tstrM2MDefaultConnResp), 1) == M2M_SUCCESS) { if(gpfAppWifiCb) gpfAppWifiCb(M2M_WIFI_RESP_DEFAULT_CONNECT, &strResp); } } #ifdef ETH_MODE else if(u8OpCode == M2M_WIFI_RESP_ETHERNET_RX_PACKET) { if(hif_receive(u32Addr, rx_buf ,sizeof(tstrM2mIpRsvdPkt), 0) == M2M_SUCCESS) { tstrM2mIpRsvdPkt * pstrM2MIpRxPkt = (tstrM2mIpRsvdPkt*)rx_buf; tstrM2mIpCtrlBuf strM2mIpCtrlBuf; uint16 u16Offset = pstrM2MIpRxPkt->u16PktOffset; strM2mIpCtrlBuf.u16RemainigDataSize = pstrM2MIpRxPkt->u16PktSz; if((gpfAppEthCb) &&(gau8ethRcvBuf)&& (gu16ethRcvBufSize > 0)) { while (strM2mIpCtrlBuf.u16RemainigDataSize > 0) { if(strM2mIpCtrlBuf.u16RemainigDataSize > gu16ethRcvBufSize) { strM2mIpCtrlBuf.u16DataSize = gu16ethRcvBufSize ; } else { strM2mIpCtrlBuf.u16DataSize = strM2mIpCtrlBuf.u16RemainigDataSize; } if(hif_receive(u32Addr+u16Offset, gau8ethRcvBuf, strM2mIpCtrlBuf.u16DataSize, 0) == M2M_SUCCESS) { strM2mIpCtrlBuf.u16RemainigDataSize -= strM2mIpCtrlBuf.u16DataSize; u16Offset += strM2mIpCtrlBuf.u16DataSize; gpfAppEthCb(M2M_WIFI_RESP_ETHERNET_RX_PACKET, gau8ethRcvBuf, &(strM2mIpCtrlBuf)); } else { break; } } } } } #endif #ifdef CONF_MGMT else if(u8OpCode == M2M_WIFI_RESP_WIFI_RX_PACKET) { tstrM2MWifiRxPacketInfo strRxPacketInfo; if(hif_receive(u32Addr, (uint8*)&strRxPacketInfo, sizeof(tstrM2MWifiRxPacketInfo), 0) == M2M_SUCCESS) { u16DataSize -= sizeof(tstrM2MWifiRxPacketInfo); if(gstrMgmtCtrl.pu8Buf != NULL) { if(u16DataSize > (gstrMgmtCtrl.u16Sz + gstrMgmtCtrl.u16Offset)) { u16DataSize = gstrMgmtCtrl.u16Sz; } u32Addr += sizeof(tstrM2MWifiRxPacketInfo) + gstrMgmtCtrl.u16Offset; if(hif_receive(u32Addr , gstrMgmtCtrl.pu8Buf, u16DataSize, 1) != M2M_SUCCESS) { u16DataSize = 0; } } if(gpfAppMonCb) gpfAppMonCb(&strRxPacketInfo, gstrMgmtCtrl.pu8Buf,u16DataSize); } } #endif else { M2M_ERR("REQ Not defined %d\n",u8OpCode); } }
sint8 m2m_crypto_sha256_hash_update(tstrM2mSha256Ctxt *pstrSha256Ctxt, uint8 *pu8Data, uint16 u16DataLength) { sint8 s8Ret = M2M_ERR_FAIL; tstrSHA256HashCtxt *pstrSHA256 = (tstrSHA256HashCtxt*)pstrSha256Ctxt; if(pstrSHA256 != NULL) { uint32 u32ReadAddr; uint32 u32WriteAddr = SHARED_MEM_BASE; uint32 u32Addr = u32WriteAddr; uint32 u32ResidualBytes; uint32 u32NBlocks; uint32 u32Offset; uint32 u32CurrentBlock = 0; uint8 u8IsDone = 0; /* Get the remaining bytes from the previous update (if the length is not block aligned). */ u32ResidualBytes = pstrSHA256->u32TotalLength % SHA_BLOCK_SIZE; /* Update the total data length. */ pstrSHA256->u32TotalLength += u16DataLength; if(u32ResidualBytes != 0) { if((u32ResidualBytes + u16DataLength) >= SHA_BLOCK_SIZE) { u32Offset = SHA_BLOCK_SIZE - u32ResidualBytes; m2m_memcpy(&pstrSHA256->au8CurrentBlock[u32ResidualBytes], pu8Data, u32Offset); pu8Data += u32Offset; u16DataLength -= u32Offset; nm_write_block(u32Addr, pstrSHA256->au8CurrentBlock, SHA_BLOCK_SIZE); u32Addr += SHA_BLOCK_SIZE; u32CurrentBlock = 1; } else { m2m_memcpy(&pstrSHA256->au8CurrentBlock[u32ResidualBytes], pu8Data, u16DataLength); u16DataLength = 0; } } /* Get the number of HASH BLOCKs and the residual bytes. */ u32NBlocks = u16DataLength / SHA_BLOCK_SIZE; u32ResidualBytes = u16DataLength % SHA_BLOCK_SIZE; if(u32NBlocks != 0) { nm_write_block(u32Addr, pu8Data, (uint16)(u32NBlocks * SHA_BLOCK_SIZE)); pu8Data += (u32NBlocks * SHA_BLOCK_SIZE); } u32NBlocks += u32CurrentBlock; if(u32NBlocks != 0) { uint32 u32RegVal = 0; nm_write_reg(SHA256_CTRL, u32RegVal); u32RegVal |= SHA256_CTRL_FORCE_SHA256_QUIT_MASK; nm_write_reg(SHA256_CTRL, u32RegVal); if(pstrSHA256->u8InitHashFlag) { pstrSHA256->u8InitHashFlag = 0; u32RegVal |= SHA256_CTRL_INIT_SHA256_STATE_MASK; } u32ReadAddr = u32WriteAddr + (u32NBlocks * SHA_BLOCK_SIZE); nm_write_reg(SHA256_DATA_LENGTH, (u32NBlocks * SHA_BLOCK_SIZE)); nm_write_reg(SHA256_START_RD_ADDR, u32WriteAddr); nm_write_reg(SHA256_START_WR_ADDR, u32ReadAddr); u32RegVal |= SHA256_CTRL_START_CALC_MASK; u32RegVal &= ~(0x7 << 8); u32RegVal |= (2 << 8); nm_write_reg(SHA256_CTRL, u32RegVal); /* 5. Wait for done_intr */ while(!u8IsDone) { u32RegVal = nm_read_reg(SHA256_DONE_INTR_STS); u8IsDone = u32RegVal & NBIT0; } } if(u32ResidualBytes != 0) { m2m_memcpy(pstrSHA256->au8CurrentBlock, pu8Data, u32ResidualBytes); } s8Ret = M2M_SUCCESS; } return s8Ret; }
/** * @fn hif_send * @brief Send packet using host interface * @param[IN] u8Gid * Group ID * @param[IN] u8Opcode * Operation ID * @param[IN] pu8Buff * Packet buffer. Allocated by the caller * @param[IN] Packet buffer size (including the HIF header) * @return M2M_SUCCESS in case of success and -ve error code in case of failure * @version 1.0 */ sint8 hif_send(uint8 u8Gid,uint8 u8Opcode,uint8 *pu8CtrlBuf,uint16 u16CtrlBufSize, uint8 *pu8DataBuf,uint16 u16DataSize, uint16 u16DataOffset) { sint8 s8Ret = M2M_ERR_MEM_ALLOC; uint8 *pu8M2MRequest,*pu8Buffer = NULL; uint8 u8Data = 0; u8Data = u8Opcode & (NBIT7); u8Opcode = u8Opcode & (~NBIT7); if (u8Data) { if (app_get_num_free_packet_buffers() > APP_M2M_IP_PACKET_LIMIT) { pu8Buffer = app_m2m_alloc_spacket(); /* This size is fixed */ } else { //M2M_ERR("Reach limits for IP TX PACKETS\n"); } } else { pu8Buffer = app_m2m_alloc_cpacket(u16CtrlBufSize + M2M_HIF_HDR_OFFSET + u16DataOffset + u16DataSize); } if(pu8Buffer != NULL) { tstrHifHdr *pstrHifHeader = (tstrHifHdr*) pu8Buffer; pstrHifHeader->u8Gid = u8Gid; pstrHifHeader->u8Opcode = u8Opcode; pstrHifHeader->u16Length = 0; pu8M2MRequest = pu8Buffer + M2M_HIF_HDR_OFFSET; if ((pu8DataBuf != NULL) && (u16DataSize != 0)) { pstrHifHeader->u16Length += u16DataOffset + u16DataSize; m2m_memcpy(pu8M2MRequest + u16DataOffset, pu8DataBuf, u16DataSize); } else { pstrHifHeader->u16Length += u16CtrlBufSize; } if ((pu8CtrlBuf != NULL) && (u16CtrlBufSize != 0)) { m2m_memcpy(pu8M2MRequest, pu8CtrlBuf, u16CtrlBufSize); } if (app_post_req != NULL) { app_post_req(u8Gid, u8Opcode, pu8Buffer); } else { M2M_ERR("NULL CB\n"); } s8Ret = M2M_SUCCESS; } else { //M2M_ERR("Out Of Memory\n"); } return s8Ret; }
sint8 m2m_crypto_rsa_sign_gen(uint8 *pu8N, uint16 u16NSize, uint8 *pu8d, uint16 u16dSize, uint8 *pu8SignedMsgHash, uint16 u16HashLength, uint8 *pu8RsaSignature) { sint8 s8Ret = M2M_RSA_SIGN_FAIL; if((pu8N != NULL) && (pu8d != NULL) && (pu8RsaSignature != NULL) && (pu8SignedMsgHash != NULL)) { uint16 u16TLength, u16TEncodingLength; uint8 *pu8T; uint8 au8EM[512]; /* Selection of correct T Encoding based on the hash size. */ if(u16HashLength == MD5_DIGEST_SIZE) { pu8T = (uint8*)au8TEncodingMD5; u16TEncodingLength = sizeof(au8TEncodingMD5); } else if(u16HashLength == SHA1_DIGEST_SIZE) { pu8T = (uint8*)au8TEncodingSHA1; u16TEncodingLength = sizeof(au8TEncodingSHA1); } else { pu8T = (uint8*)au8TEncodingSHA2; u16TEncodingLength = sizeof(au8TEncodingSHA2); } u16TLength = u16TEncodingLength + 1 + u16HashLength; /* If emLen < tLen + 11. */ if(u16NSize >= (u16TLength + 11)) { uint16 u16PSLength = 0; uint16 u16Offset = 0; /* The calculated EM must match the following pattern. *======*======*======*======*======* * 0x00 || 0x01 || PS || 0x00 || T * *======*======*======*======*======* Where PS is all 0xFF T is defined based on the hash algorithm. */ au8EM[u16Offset ++] = 0; au8EM[u16Offset ++] = 1; u16PSLength = u16NSize - u16TLength - 3; m2m_memset(&au8EM[u16Offset], 0xFF, u16PSLength); u16Offset += u16PSLength; au8EM[u16Offset ++] = 0; m2m_memcpy(&au8EM[u16Offset], pu8T, u16TEncodingLength); u16Offset += u16TEncodingLength; au8EM[u16Offset ++] = u16HashLength; m2m_memcpy(&au8EM[u16Offset], pu8SignedMsgHash, u16HashLength); /* RSA Signature Generation */ BigInt_ModExp(au8EM, u16NSize, pu8d, u16dSize, pu8N, u16NSize, pu8RsaSignature, u16NSize); s8Ret = M2M_RSA_SIGN_OK; } } return s8Ret; }