/********************************************************************* Function send Description Return Author Ahmed Ezzat Version 1.0 Date 5 June 2012 *********************************************************************/ sint16 send(SOCKET sock, void *pvSendBuffer, uint16 u16SendLength, uint16 flags) { sint16 s16Ret = SOCK_ERR_INVALID_ARG; if((sock >= 0) && (pvSendBuffer != NULL) && (u16SendLength <= SOCKET_BUFFER_MAX_LENGTH) && (gastrSockets[sock].bIsUsed == 1)) { uint16 u16DataOffset; tstrSendCmd strSend; uint8 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) { u8Cmd = SOCKET_CMD_SSL_SEND; u16DataOffset = SSL_TX_PACKET_OFFSET; } s16Ret = SOCKET_REQUEST(u8Cmd|M2M_REQ_DATA_PKT, (uint8*)&strSend, sizeof(tstrSendCmd), pvSendBuffer, u16SendLength, u16DataOffset); if(s16Ret != SOCK_ERR_NO_ERROR) { s16Ret = SOCK_ERR_BUFFER_FULL; } } return s16Ret; }
/********************************************************************* Function sendto Description Return Author Ahmed Ezzat Version 1.0 Date 4 June 2012 *********************************************************************/ sint16 sendto(SOCKET sock, void *pvSendBuffer, uint16 u16SendLength, uint16 flags, struct sockaddr *pstrDestAddr, uint8 u8AddrLen) { sint16 s16Ret = SOCK_ERR_INVALID_ARG; if((sock >= 0) && (pvSendBuffer != NULL) && (u16SendLength <= SOCKET_BUFFER_MAX_LENGTH)) { if(gastrSockets[sock].bIsUsed) { tstrSendCmd strSendTo; m2m_memset((uint8*)&strSendTo, 0, sizeof(tstrSendCmd)); strSendTo.sock = sock; strSendTo.u16DataSize = NM_BSP_B_L_16(u16SendLength); 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*)&strSendTo, sizeof(tstrSendCmd), pvSendBuffer, u16SendLength, UDP_TX_PACKET_OFFSET); if(s16Ret != SOCK_ERR_NO_ERROR) { s16Ret = SOCK_ERR_BUFFER_FULL; } } } return s16Ret; }
/** * @fn hif_isr * @brief Host interface interrupt service routine * @author M. Abdelmawla * @date 15 July 2012 * @return 1 in case of interrupt received else 0 will be returned * @version 1.0 */ static sint8 hif_isr(void) { sint8 ret = M2M_SUCCESS; uint32 reg; volatile tstrHifHdr strHif; ret = nm_read_reg_with_ret(WIFI_HOST_RCV_CTRL_0, ®); if(M2M_SUCCESS == ret) { if(reg & 0x1) /* New interrupt has been received */ { uint16 size; nm_bsp_interrupt_ctrl(0); /*Clearing RX interrupt*/ reg &= ~NBIT0; ret = nm_write_reg(WIFI_HOST_RCV_CTRL_0,reg); if(ret != M2M_SUCCESS)goto ERR1; gstrHifCxt.u8HifRXDone = 1; size = (uint16)((reg >> 2) & 0xfff); if (size > 0) { uint32 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\n"); nm_bsp_interrupt_ctrl(1); goto ERR1; } gstrHifCxt.u32RxAddr = address; gstrHifCxt.u32RxSize = size; ret = nm_read_block(address, (uint8*)&strHif, sizeof(tstrHifHdr)); strHif.u16Length = NM_BSP_B_L_16(strHif.u16Length); if(M2M_SUCCESS != ret) { M2M_ERR("(hif) address bus fail\n"); nm_bsp_interrupt_ctrl(1); goto ERR1; } if(strHif.u16Length != size) { if((size - strHif.u16Length) > 4) { M2M_ERR("(hif) Corrupted packet Size = %u <L = %u, G = %u, OP = %02X>\n", size, strHif.u16Length, strHif.u8Gid, strHif.u8Opcode); nm_bsp_interrupt_ctrl(1); ret = M2M_ERR_BUS_FAIL; goto ERR1; } } 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\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("Scoket callback is not registered\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\n"); } else if(M2M_REQ_GROUP_CRYPTO == strHif.u8Gid) { if(gstrHifCxt.pfCryptoCb) gstrHifCxt.pfCryptoCb(strHif.u8Opcode,strHif.u16Length - M2M_HIF_HDR_OFFSET, address + M2M_HIF_HDR_OFFSET); else M2M_ERR("Crypto callback is not registered\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\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("(hif) invalid group ID\n"); ret = M2M_ERR_BUS_FAIL; goto ERR1; } if(gstrHifCxt.u8HifRXDone) { M2M_ERR("(hif) host app didn't set RX Done <%u><%X>\n", strHif.u8Gid, strHif.u8Opcode); ret = hif_set_rx_done(); if(ret != M2M_SUCCESS) goto ERR1; } } else { M2M_ERR("(hif) Wrong Size\n"); ret = M2M_ERR_RCV; goto ERR1; } } else {
sint8 hif_send(uint8 u8Gid,uint8 u8Opcode,uint8 *pu8CtrlBuf,uint16 u16CtrlBufSize, uint8 *pu8DataBuf,uint16 u16DataSize, uint16 u16DataOffset) { sint8 ret = M2M_ERR_SEND; volatile tstrHifHdr strHif; strHif.u8Opcode = u8Opcode&(~NBIT7); strHif.u8Gid = u8Gid; strHif.u16Length = M2M_HIF_HDR_OFFSET; if(pu8DataBuf != NULL) { strHif.u16Length += u16DataOffset + u16DataSize; } else { strHif.u16Length += u16CtrlBufSize; } ret = hif_chip_wake(); if(ret == M2M_SUCCESS) { volatile uint32 reg, dma_addr = 0; volatile uint16 cnt = 0; //#define OPTIMIZE_BUS /*please define in firmware also*/ #ifndef OPTIMIZE_BUS reg = 0UL; reg |= (uint32)u8Gid; reg |= ((uint32)u8Opcode<<8); reg |= ((uint32)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; #else reg = 0UL; reg |= NBIT1; reg |= ((u8Opcode & NBIT7) ? (NBIT2):(0)); /*Data = 1 or config*/ reg |= (u8Gid == M2M_REQ_GROUP_IP) ? (NBIT3):(0); /*IP = 1 or non IP*/ reg |= ((uint32)strHif.u16Length << 4); /*length of pkt max = 4096*/ ret = nm_write_reg(WIFI_HOST_RCV_CTRL_2, reg); if(M2M_SUCCESS != ret) goto ERR1; #endif dma_addr = 0; for(cnt = 0; cnt < 1000; cnt ++) { ret = nm_read_reg_with_ret(WIFI_HOST_RCV_CTRL_2,(uint32 *)®); 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 >= 500) { if(cnt < 501) { M2M_INFO("Slowing down...\n"); } nm_bsp_sleep(1); } if (!(reg & NBIT1)) { ret = nm_read_reg_with_ret(WIFI_HOST_RCV_CTRL_4,(uint32 *)&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 u32CurrAddr; u32CurrAddr = dma_addr; strHif.u16Length=NM_BSP_B_L_16(strHif.u16Length); ret = nm_write_block(u32CurrAddr, (uint8*)&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 += (u16DataOffset - u16CtrlBufSize); ret = nm_write_block(u32CurrAddr, pu8DataBuf, u16DataSize); if(M2M_SUCCESS != ret) goto ERR1; u32CurrAddr += u16DataSize; } reg = dma_addr << 2; reg |= NBIT1; ret = nm_write_reg(WIFI_HOST_RCV_CTRL_3, reg); if(M2M_SUCCESS != ret) goto ERR1; } else { ret = hif_chip_sleep(); M2M_DBG("Failed to alloc rx size %d\r",ret); ret = M2M_ERR_MEM_ALLOC; goto ERR2; } } else { M2M_ERR("(HIF)Fail to wakup the chip\n"); goto ERR2; } /*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*/ return ret; }
/********************************************************************* Function m2m_ip_cb Description Callback function used by the NMC1000 driver to deliver messages for socket layer. Return None. Author Ahmed Ezzat Version 1.0 Date 17 July 2012 *********************************************************************/ static void m2m_ip_cb(uint8 u8OpCode, uint16 u16BufferSize,uint32 u32Address) { if(u8OpCode == SOCKET_CMD_BIND) { tstrBindReply strBindReply; tstrSocketBindMsg strBind; if(hif_receive(u32Address, (uint8*)&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*)&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*)&strAcceptReply, sizeof(tstrAcceptReply), 0) == M2M_SUCCESS) { if(strAcceptReply.sConnectedSock >= 0) { 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)) { tstrConnectReply strConnectReply; tstrSocketConnectMsg strConnMsg; if(hif_receive(u32Address, (uint8*)&strConnectReply, sizeof(tstrConnectReply), 0) == M2M_SUCCESS) { strConnMsg.sock = strConnectReply.sock; strConnMsg.s8Error = strConnectReply.s8Error; if(gpfAppSocketCb) gpfAppSocketCb(strConnectReply.sock,SOCKET_MSG_CONNECT, &strConnMsg); } } else if(u8OpCode == SOCKET_CMD_DNS_RESOLVE) { tstrDnsReply strDnsReply; if(hif_receive(u32Address, (uint8*)&strDnsReply, sizeof(tstrDnsReply), 0) == M2M_SUCCESS) { strDnsReply.u32HostIP = strDnsReply.u32HostIP; if(gpfAppResolveCb) gpfAppResolveCb((uint8*)strDnsReply.acHostName, strDnsReply.u32HostIP); } } else if((u8OpCode == SOCKET_CMD_RECV) || (u8OpCode == SOCKET_CMD_RECVFROM) || (u8OpCode == SOCKET_CMD_SSL_RECV)) { SOCKET sock; sint16 s16RecvStatus; tstrRecvReply strRecvReply; uint16 u16ReadSize; tstrSocketRecvMsg strRecvMsg; uint8 u8CallbackMsgID = SOCKET_MSG_RECV; uint16 u16DataOffset; if(u8OpCode == SOCKET_CMD_RECVFROM) u8CallbackMsgID = SOCKET_MSG_RECVFROM; /* Read RECV REPLY data structure. */ u16ReadSize = sizeof(tstrRecvReply); if(hif_receive(u32Address, (uint8*)&strRecvReply, u16ReadSize, 0) == M2M_SUCCESS) { uint16 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. If the buffer is smaller than the received data, the data is passed to the application in chunks according to its buffer size. */ u16ReadSize = (uint16)s16RecvStatus; Socket_ReadSocketData(sock, &strRecvMsg, u8CallbackMsgID, u32Address, u16ReadSize); } else { 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; sint16 s16Rcvd; tstrSendReply strReply; uint8 u8CallbackMsgID = SOCKET_MSG_SEND; if(u8OpCode == SOCKET_CMD_SENDTO) u8CallbackMsgID = SOCKET_MSG_SENDTO; if(hif_receive(u32Address, (uint8*)&strReply, sizeof(tstrSendReply), 0) == M2M_SUCCESS) { uint16 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); } } } }
/** * @fn hif_isr * @brief Host interface interrupt service routine * @author M. Abdelmawla * @date 15 July 2012 * @return 1 in case of interrupt received else 0 will be returned * @version 1.0 */ static sint8 hif_isr(void) { sint8 ret = M2M_ERR_BUS_FAIL; uint32 reg; volatile tstrHifHdr strHif; ret = hif_chip_wake(); if(ret == M2M_SUCCESS) { ret = nm_read_reg_with_ret(WIFI_HOST_RCV_CTRL_0, ®); if(M2M_SUCCESS == ret) { if(reg & 0x1) /* New interrupt has been received */ { uint16 size; nm_bsp_interrupt_ctrl(0); /*Clearing RX interrupt*/ reg &= ~(1<<0); ret = nm_write_reg(WIFI_HOST_RCV_CTRL_0,reg); if(ret != M2M_SUCCESS)goto ERR1; gu8HifSizeDone = 0; size = (uint16)((reg >> 2) & 0xfff); if (size > 0) { uint32 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\n"); nm_bsp_interrupt_ctrl(1); goto ERR1; } ret = nm_read_block(address, (uint8*)&strHif, sizeof(tstrHifHdr)); strHif.u16Length = NM_BSP_B_L_16(strHif.u16Length); if(M2M_SUCCESS != ret) { M2M_ERR("(hif) address bus fail\n"); nm_bsp_interrupt_ctrl(1); goto ERR1; } if(strHif.u16Length != size) { if((size - strHif.u16Length) > 4) { M2M_ERR("(hif) Corrupted packet Size = %u <L = %u, G = %u, OP = %02X>\n", size, strHif.u16Length, strHif.u8Gid, strHif.u8Opcode); nm_bsp_interrupt_ctrl(1); ret = M2M_ERR_BUS_FAIL; goto ERR1; } } if(M2M_REQ_GROUP_WIFI == strHif.u8Gid) { if(pfWifiCb) pfWifiCb(strHif.u8Opcode,strHif.u16Length - M2M_HIF_HDR_OFFSET, address + M2M_HIF_HDR_OFFSET); } else if(M2M_REQ_GROUP_IP == strHif.u8Gid) { if(pfIpCb) pfIpCb(strHif.u8Opcode,strHif.u16Length - M2M_HIF_HDR_OFFSET, address + M2M_HIF_HDR_OFFSET); } else if(M2M_REQ_GROUP_OTA == strHif.u8Gid) { if(pfOtaCb) pfOtaCb(strHif.u8Opcode,strHif.u16Length - M2M_HIF_HDR_OFFSET, address + M2M_HIF_HDR_OFFSET); } else if(M2M_REQ_GROUP_CRYPTO == strHif.u8Gid) { if(pfCryptoCb) pfCryptoCb(strHif.u8Opcode,strHif.u16Length - M2M_HIF_HDR_OFFSET, address + M2M_HIF_HDR_OFFSET); } else if(M2M_REQ_GROUP_SIGMA == strHif.u8Gid) { if(pfSigmaCb) pfSigmaCb(strHif.u8Opcode,strHif.u16Length - M2M_HIF_HDR_OFFSET, address + M2M_HIF_HDR_OFFSET); } else { M2M_ERR("(hif) invalid group ID\n"); ret = M2M_ERR_BUS_FAIL; goto ERR1; } #ifndef ENABLE_UNO_BOARD if(!gu8HifSizeDone) { M2M_ERR("(hif) host app didn't set RX Done\n"); ret = hif_set_rx_done(); } #endif } else { ret = M2M_ERR_RCV; M2M_ERR("(hif) Wrong Size\n"); goto ERR1; } } else {
sint8 hif_send(uint8 u8Gid,uint8 u8Opcode,uint8 *pu8CtrlBuf,uint16 u16CtrlBufSize, uint8 *pu8DataBuf,uint16 u16DataSize, uint16 u16DataOffset) { sint8 ret = M2M_ERR_SEND; volatile tstrHifHdr strHif; strHif.u8Opcode = u8Opcode&(~NBIT7); strHif.u8Gid = u8Gid; strHif.u16Length = M2M_HIF_HDR_OFFSET; if(pu8DataBuf != NULL) { strHif.u16Length += u16DataOffset + u16DataSize; } else { strHif.u16Length += u16CtrlBufSize; } ret = hif_chip_wake(); if(ret == M2M_SUCCESS) { volatile uint32 reg, dma_addr = 0; volatile uint16 cnt = 0; reg = 0UL; reg |= (uint32)u8Gid; reg |= ((uint32)u8Opcode<<8); reg |= ((uint32)strHif.u16Length<<16); ret = nm_write_reg(NMI_STATE_REG,reg); if(M2M_SUCCESS != ret) goto ERR1; reg = 0; reg |= (1<<1); ret = nm_write_reg(WIFI_HOST_RCV_CTRL_2, reg); if(M2M_SUCCESS != ret) goto ERR1; dma_addr = 0; //nm_bsp_interrupt_ctrl(0); for(cnt = 0; cnt < 1000; cnt ++) { ret = nm_read_reg_with_ret(WIFI_HOST_RCV_CTRL_2,(uint32 *)®); if(ret != M2M_SUCCESS) break; if (!(reg & 0x2)) { ret = nm_read_reg_with_ret(0x150400,(uint32 *)&dma_addr); if(ret != M2M_SUCCESS) { /*in case of read error clear the dma address and return error*/ dma_addr = 0; } /*in case of success break */ break; } } //nm_bsp_interrupt_ctrl(1); if (dma_addr != 0) { volatile uint32 u32CurrAddr; u32CurrAddr = dma_addr; strHif.u16Length=NM_BSP_B_L_16(strHif.u16Length); ret = nm_write_block(u32CurrAddr, (uint8*)&strHif, M2M_HIF_HDR_OFFSET); #ifdef CONF_WINC_USE_I2C nm_bsp_sleep(1); #endif if(M2M_SUCCESS != ret) goto ERR1; u32CurrAddr += M2M_HIF_HDR_OFFSET; if(pu8CtrlBuf != NULL) { ret = nm_write_block(u32CurrAddr, pu8CtrlBuf, u16CtrlBufSize); #ifdef CONF_WINC_USE_I2C nm_bsp_sleep(1); #endif if(M2M_SUCCESS != ret) goto ERR1; u32CurrAddr += u16CtrlBufSize; } if(pu8DataBuf != NULL) { u32CurrAddr += (u16DataOffset - u16CtrlBufSize); ret = nm_write_block(u32CurrAddr, pu8DataBuf, u16DataSize); #ifdef CONF_WINC_USE_I2C nm_bsp_sleep(1); #endif if(M2M_SUCCESS != ret) goto ERR1; u32CurrAddr += u16DataSize; } reg = dma_addr << 2; reg |= (1 << 1); ret = nm_write_reg(WIFI_HOST_RCV_CTRL_3, reg); if(M2M_SUCCESS != ret) goto ERR1; } else { M2M_DBG("Failed to alloc rx size\r"); ret = M2M_ERR_MEM_ALLOC; goto ERR1; } } else { M2M_ERR("(HIF)Fail to wakup the chip\n"); goto ERR1; } ret = hif_chip_sleep(); ERR1: 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; }
sint8 m2m_wifi_connect_sc(char *pcSsid, uint8 u8SsidLen, uint8 u8SecType, void *pvAuthInfo, uint16 u16Ch, uint8 u8NoSaveCred) { sint8 ret = M2M_SUCCESS; tstrM2mWifiConnect strConnect; tstrM2MWifiSecInfo *pstrAuthInfo; if(u8SecType != M2M_WIFI_SEC_OPEN) { if(pvAuthInfo == NULL) { M2M_ERR("Key is not valid\n"); ret = M2M_ERR_FAIL; goto ERR1; } if((u8SecType == M2M_WIFI_SEC_WPA_PSK) && (m2m_strlen(pvAuthInfo) == (M2M_MAX_PSK_LEN-1))) { uint8 i = 0; uint8* pu8Psk = (uint8*)pvAuthInfo; while(i < (M2M_MAX_PSK_LEN-1)) { if(pu8Psk[i]<'0' || (pu8Psk[i]>'9' && pu8Psk[i] < 'A')|| (pu8Psk[i]>'F' && pu8Psk[i] < 'a') || pu8Psk[i] > 'f') { M2M_ERR("Invalid Key\n"); ret = M2M_ERR_FAIL; goto ERR1; } i++; } } } 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); /* Credentials will be Not be saved if u8NoSaveCred is set */ strConnect.u8NoSaveCred = u8NoSaveCred ? 1:0; 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) { uint16 u16KeyLen = m2m_strlen((uint8*)pvAuthInfo); if((u16KeyLen <= 0)||(u16KeyLen >= M2M_MAX_PSK_LEN)) { M2M_ERR("Incorrect PSK key length\n"); ret = M2M_ERR_FAIL; goto ERR1; } m2m_memcpy(pstrAuthInfo->uniAuth.au8PSK, (uint8*)pvAuthInfo, u16KeyLen + 1); } 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_GROUP_WIFI, M2M_WIFI_REQ_CONNECT, (uint8*)&strConnect, sizeof(tstrM2mWifiConnect),NULL, 0,0); ERR1: return ret; }