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; }
static sint8 m2m_validate_scan_options(tstrM2MScanOption* ptstrM2MScanOption) { sint8 s8Ret = M2M_SUCCESS; /* Check for incoming pointer */ if(ptstrM2MScanOption == NULL) { M2M_ERR("INVALID POINTER\n"); s8Ret = M2M_ERR_FAIL; } /* Check for valid No of slots */ if(ptstrM2MScanOption->u8NumOfSlot < 1) { M2M_ERR("INVALID No of scan slots!\n"); s8Ret = M2M_ERR_FAIL; } /* Check for valid time of slots */ if(ptstrM2MScanOption->u8SlotTime < 1) { M2M_ERR("INVALID scan slot time!\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\n"); s8Ret = M2M_ERR_FAIL; } /* Check for valid RSSI threshold */ if((ptstrM2MScanOption->s8RssiThresh < -99) || (ptstrM2MScanOption->s8RssiThresh >= 0)) { M2M_ERR("INVALID RSSI threshold %d \n",ptstrM2MScanOption->s8RssiThresh); s8Ret = M2M_ERR_FAIL; } return s8Ret; }
/* * @fn nm_drv_deinit * @brief Deinitialize NMC1000 driver * @author M. Abdelmawla * @date 17 July 2012 * @version 1.0 */ sint8 nm_drv_deinit(void * arg) { (void)arg; // Silence "unused" warning sint8 ret; ret = chip_deinit(); if (M2M_SUCCESS != ret) { M2M_ERR("[nmi stop]: chip_deinit fail\n"); goto ERR1; } ret = nm_bus_iface_deinit(); if (M2M_SUCCESS != ret) { M2M_ERR("[nmi stop]: fail init bus\n"); goto ERR1; } #ifdef CONF_WINC_USE_SPI /* Must do this after global reset to set SPI data packet size. */ nm_spi_deinit(); #endif ERR1: return ret; }
sint8 m2m_wifi_request_scan(uint8 ch) { sint8 s8Ret = M2M_ERR_SCAN_IN_PROGRESS; if(!gu8scanInProgress) { tstrM2MScan strtmp; strtmp.u8ChNum = ch; 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; }
/********************************************************************* 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 nm_uart_reconfigure * @brief Reconfigures the UART interface * @param [in] ptr * Pointer to a DWORD containing baudrate at this moment. * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure * @author Viswanathan Murugesan * @date 22 OCT 2014 * @version 1.0 */ sint8 nm_uart_reconfigure(void *ptr) { tstrNmUartDefault strUart; sint8 s8Ret = M2M_SUCCESS; uint8 b[HDR_SZ+1]; /*write reg*/ b[0] = 0xa5; b[1] = 5; b[2] = 0; b[3] = 0; b[4] = 0; b[5] = 0; b[6] = 0; b[7] = 0; b[8] = 0; b[9] = (uint8)((*(unsigned long *)ptr) & 0x000000ff); b[10] = (uint8)(((*(unsigned long *)ptr) & 0x0000ff00)>>8); b[11] = (uint8)(((*(unsigned long *)ptr) & 0x00ff0000)>>16); b[12] = (uint8)(((*(unsigned long *)ptr) & 0xff000000)>>24); b[2] = get_cs(&b[1],HDR_SZ); get_cs(&b[1],HDR_SZ); strUart.pu8Buf = b; strUart.u16Sz = sizeof(b); if(M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_W, &strUart)) { M2M_ERR("write error\n"); s8Ret = M2M_ERR_BUS_FAIL; } else { if(!nm_bus_get_chip_type()) { //check for the ack from the SAMD21 for the packet reception. strUart.u16Sz = 1; if(M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_R, &strUart)) { s8Ret = M2M_ERR_BUS_FAIL; } if(b[0] == 0xAC) { M2M_DBG("Successfully sent the UART reconfigure command\n"); } else { M2M_ERR("write error\n"); s8Ret = M2M_ERR_BUS_FAIL; } } } return s8Ret; }
NMI_API sint8 m2m_ota_test(void) { uint32 page = 0; uint8 buffer[1500]; uint32 u32Sz = 0; sint8 ret = M2M_SUCCESS; FILE *fp =NULL; fp = fopen(M2M_OTA_FILE,"rb"); if(fp) { fseek(fp, 0L, SEEK_END); u32Sz = ftell(fp); fseek(fp, 0L, SEEK_SET); while(u32Sz > 0) { { page = (rand()%1400); if((page<100)||(page>1400)) page = 1400; } if(u32Sz>page) { u32Sz-=page; } else { page = u32Sz; u32Sz = 0; } printf("page %d\n", (int)page); fread(buffer,page,1,fp); ret = hif_send(M2M_REQ_GROUP_OTA,M2M_OTA_REQ_TEST|M2M_REQ_DATA_PKT,NULL,0,(uint8*)&buffer,page,0); if(ret != M2M_SUCCESS) { M2M_ERR("\n"); } nm_bsp_sleep(1); } } else { M2M_ERR("nO err\n"); } return ret; }
/* * @fn nm_i2c_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 * @author M. Abdelmawla * @date 11 July 2012 * @version 1.0 */ sint8 nm_i2c_write_block(uint32 u32Addr, uint8 *pu8Buf, uint16 u16Sz) { uint8 au8Buf[7]; tstrNmI2cSpecial strI2c; sint8 s8Ret = M2M_SUCCESS; au8Buf[0] = 0x12; au8Buf[1] = (uint8)(u32Addr >> 24); au8Buf[2] = (uint8)(u32Addr >> 16); au8Buf[3] = (uint8)(u32Addr >> 8); au8Buf[4] = (uint8)(u32Addr); au8Buf[5] = (uint8)(u16Sz >> 8); au8Buf[6] = (uint8)(u16Sz); strI2c.pu8Buf1 = au8Buf; strI2c.pu8Buf2 = pu8Buf; strI2c.u16Sz1 = sizeof(au8Buf); strI2c.u16Sz2 = u16Sz; if(M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_W_SPECIAL, &strI2c)) { M2M_ERR("write error\n"); s8Ret = M2M_ERR_BUS_FAIL; } return s8Ret; }
sint8 chip_reset_and_cpu_halt(void) { sint8 ret = M2M_SUCCESS; uint32 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 ...\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, ®); } #if 0 reg |= (1ul << 10); ret += nm_write_reg(NMI_GLB_RESET_0, reg); ret += nm_read_reg_with_ret(NMI_GLB_RESET_0, ®); #endif 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; }
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; }
/* * @fn nm_i2c_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 * @author M. Abdelmawla * @date 11 July 2012 * @version 1.0 */ sint8 nm_i2c_write_reg(uint32 u32Addr, uint32 u32Val) { tstrNmI2cDefault strI2c; uint8 b[16]; sint8 s8Ret = M2M_SUCCESS; if(u32Addr < 0xff) { /* clockless i2c */ b[0] = 0x19; b[1] = (uint8)(u32Addr); b[2] = (uint8)(u32Val); strI2c.u16Sz = 3; } else { b[0] = 0x90; b[1] = (uint8)(u32Addr >> 24); b[2] = (uint8)(u32Addr >> 16); b[3] = (uint8)(u32Addr >> 8); b[4] = (uint8)u32Addr; b[5] = 0x04; b[6] = (uint8)u32Val; b[7] = (uint8)(u32Val >> 8); b[8] = (uint8)(u32Val >> 16); b[9] = (uint8)(u32Val >> 24); strI2c.u16Sz = 10; } strI2c.pu8Buf = b; if(M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_W, &strI2c)) { M2M_ERR("write error\n"); s8Ret = M2M_ERR_BUS_FAIL; } return s8Ret; }
sint8 cpu_start(void) { uint32 reg; sint8 ret; /** reset regs */ nm_write_reg(BOOTROM_REG,0); nm_write_reg(NMI_STATE_REG,0); 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 ...\n"); } reg |= (1 << 0); ret = nm_write_reg(0x1118, reg); ret = nm_write_reg(0x150014, 0x1); 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_bsp_sleep(1); /* TODO: Why bus error if this delay is not here. */ 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_ota_cb(uint8 u8OpCode, uint16 u16DataSize, uint32 u32Addr) { sint8 ret = M2M_SUCCESS; if(u8OpCode == M2M_OTA_RESP_NOTIF_UPDATE_INFO) { tstrOtaUpdateInfo strOtaUpdateInfo; m2m_memset((uint8*)&strOtaUpdateInfo,0,sizeof(tstrOtaUpdateInfo)); ret = hif_receive(u32Addr,(uint8*)&strOtaUpdateInfo,sizeof(tstrOtaUpdateInfo),0); if(ret == M2M_SUCCESS) { if(gpfOtaNotifCb) gpfOtaNotifCb(&strOtaUpdateInfo); } } else if (u8OpCode == M2M_OTA_RESP_UPDATE_STATUS) { tstrOtaUpdateStatusResp strOtaUpdateStatusResp; m2m_memset((uint8*)&strOtaUpdateStatusResp,0,sizeof(tstrOtaUpdateStatusResp)); ret = hif_receive(u32Addr, (uint8*) &strOtaUpdateStatusResp,sizeof(tstrOtaUpdateStatusResp), 0); if(ret == M2M_SUCCESS) { if(gpfOtaUpdateCb) gpfOtaUpdateCb(strOtaUpdateStatusResp.u8OtaUpdateStatusType,strOtaUpdateStatusResp.u8OtaUpdateStatus); } } else { M2M_ERR("Invaild OTA resp %d ?\n",u8OpCode); } }
/*! @fn \ NMI_API sint8 m2m_ota_init(tpfOtaUpdateCb pfOtaUpdateCb, tpfOtaNotifCb pfOtaNotifCb); @brief Initialize the OTA layer. @param [in] pfOtaUpdateCb OTA Update callback function @param [in] pfOtaNotifCb OTA notify callback function @return The function SHALL return 0 for success and a negative value otherwise. */ NMI_API sint8 m2m_ota_init(tpfOtaUpdateCb pfOtaUpdateCb, tpfOtaNotifCb pfOtaNotifCb) { sint8 ret = M2M_SUCCESS; if(pfOtaUpdateCb){ gpfOtaUpdateCb = pfOtaUpdateCb; }else{ M2M_ERR("Invaild Ota update cb\n"); } if(pfOtaNotifCb){ gpfOtaNotifCb = pfOtaNotifCb; }else{ M2M_ERR("Invaild Ota notify cb\n"); } hif_register_cb(M2M_REQ_GROUP_OTA,m2m_ota_cb); return ret; }
sint8 m2m_wifi_set_scan_list(tstrM2MScanList* pstrScanList) { sint8 s8Ret = M2M_ERR_FAIL; s8Ret = hif_send(M2M_REQ_GRP_WIFI, M2M_WIFI_REQ_SET_SCAN_LIST, (uint8*)pstrScanList, sizeof(tstrM2MScanList),NULL, 0,0); if(s8Ret != M2M_SUCCESS) { M2M_ERR("SCAN List Ret = %d\n",s8Ret); } return s8Ret; }
sint8 chip_deinit(void) { uint32 reg = 0; sint8 ret; uint8 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\n"); } reg &= ~(1 << 10); ret = nm_write_reg(NMI_GLB_RESET_0, reg); if (ret != M2M_SUCCESS) { M2M_ERR("Error while writing reg\n"); return ret; } do { ret = nm_read_reg_with_ret(NMI_GLB_RESET_0, ®); if (ret != M2M_SUCCESS) { M2M_ERR("Error while reading reg\n"); return ret; } /*Workaround to ensure that the chip is actually reset*/ if ((reg & (1 << 10))) { M2M_DBG("Bit 10 not reset retry %d\n", timeout); reg &= ~(1 << 10); ret = nm_write_reg(NMI_GLB_RESET_0, reg); timeout--; } else { break; } } while (timeout); return ret; }
sint8 m2m_wifi_req_server_init(uint8 ch) { sint8 ret = M2M_SUCCESS; #ifdef _PS_SERVER_ tstrM2mServerInit strServer; strServer.u8Channel = ch; ret = hif_send(M2M_REQ_GRP_WIFI,M2M_WIFI_REQ_SERVER_INIT, (uint8*)&strServer, sizeof(tstrM2mServerInit), NULL, 0, 0); #else M2M_ERR("_PS_SERVER_ is not defined\n"); #endif return ret; }
sint8 m2m_wifi_req_client_ctrl(uint8 u8Cmd) { sint8 ret = M2M_SUCCESS; #ifdef _PS_SERVER_ tstrM2Mservercmd strCmd; strCmd.u8cmd = u8Cmd; ret = hif_send(M2M_REQ_GRP_WIFI, M2M_WIFI_REQ_CLIENT_CTRL, (uint8*)&strCmd, sizeof(tstrM2Mservercmd), NULL, 0, 0); #else M2M_ERR("_PS_SERVER_ is not defined\n"); #endif return ret; }
sint8 pullup_ctrl(uint32 pinmask, uint8 enable) { sint8 s8Ret; uint32 val32; s8Ret = nm_read_reg_with_ret(0x142c, &val32); if(s8Ret != M2M_SUCCESS) { M2M_ERR("[pullup_ctrl]: failed to read\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\n"); goto _EXIT; } _EXIT: return s8Ret; }
/* * @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 */ sint8 nm_bus_init(void *pvinit) { sint8 result = M2M_SUCCESS; #ifdef CONF_WINC_USE_I2C twihs_options_t opt; /* Enable the peripheral clock for TWI */ pmc_enable_periph_clk(CONF_WINC_I2C_ID); /* Configure the options of TWI driver */ opt.master_clk = sysclk_get_peripheral_hz(); opt.speed = CONF_WINC_TWIHS_CLOCK; if (twihs_master_init(CONF_WINC_I2C, &opt) != TWIHS_SUCCESS) { M2M_ERR("-E-\tTWI master initialization failed.\r"); while (1) { /* Capture error */ } } #elif CONF_WINC_USE_SPI /* Configure SPI pins. */ ioport_set_pin_mode(CONF_WINC_SPI_MISO_GPIO, CONF_WINC_SPI_MISO_FLAGS); ioport_set_pin_mode(CONF_WINC_SPI_MOSI_GPIO, CONF_WINC_SPI_MOSI_FLAGS); ioport_set_pin_mode(CONF_WINC_SPI_CLK_GPIO, CONF_WINC_SPI_CLK_FLAGS); ioport_set_pin_mode(CONF_WINC_SPI_CS_GPIO, CONF_WINC_SPI_CS_FLAGS); ioport_disable_pin(CONF_WINC_SPI_MISO_GPIO); ioport_disable_pin(CONF_WINC_SPI_MOSI_GPIO); ioport_disable_pin(CONF_WINC_SPI_CLK_GPIO); ioport_disable_pin(CONF_WINC_SPI_CS_GPIO); spi_enable_clock(CONF_WINC_SPI); spi_disable(CONF_WINC_SPI); spi_reset(CONF_WINC_SPI); spi_set_master_mode(CONF_WINC_SPI); spi_disable_mode_fault_detect(CONF_WINC_SPI); spi_set_peripheral_chip_select_value(CONF_WINC_SPI, CONF_WINC_SPI_NPCS); spi_set_clock_polarity(CONF_WINC_SPI, CONF_WINC_SPI_NPCS, CONF_WINC_SPI_POL); spi_set_clock_phase(CONF_WINC_SPI, CONF_WINC_SPI_NPCS, CONF_WINC_SPI_PHA); spi_set_bits_per_transfer(CONF_WINC_SPI, CONF_WINC_SPI_NPCS, SPI_CSR_BITS_8_BIT); spi_set_baudrate_div(CONF_WINC_SPI, CONF_WINC_SPI_NPCS, (sysclk_get_cpu_hz() / CONF_WINC_SPI_CLOCK)); spi_set_transfer_delay(CONF_WINC_SPI, CONF_WINC_SPI_NPCS, CONF_WINC_SPI_DLYBS, CONF_WINC_SPI_DLYBCT); spi_enable(CONF_WINC_SPI); nm_bsp_reset(); #endif return result; }
sint8 m2m_wifi_init(tstrWifiInitParam * param) { tstrM2mRev strtmp; sint8 ret = M2M_SUCCESS; uint8 u8WifiMode = M2M_WIFI_MODE_NORMAL; if(param == NULL) { ret = M2M_ERR_FAIL; goto _EXIT0; } gpfAppWifiCb = param->pfAppWifiCb; #ifdef ETH_MODE gpfAppEthCb = param->strEthInitParam.pfAppEthCb; gau8ethRcvBuf = param->strEthInitParam.au8ethRcvBuf; gu16ethRcvBufSize = param->strEthInitParam.u16ethRcvBufSize; u8WifiMode = param->strEthInitParam.u8EthernetEnable; #endif /* ETH_MODE */ #ifdef CONF_MGMT gpfAppMonCb = param->pfAppMonCb; #endif gu8scanInProgress = 0; /* Apply device specific initialization. */ ret = nm_drv_init(&u8WifiMode); if(ret != M2M_SUCCESS) goto _EXIT0; /* Initialize host interface module */ ret = hif_init(NULL); if(ret != M2M_SUCCESS) goto _EXIT1; hif_register_cb(M2M_REQ_GROUP_WIFI,m2m_wifi_cb); ret = nm_get_firmware_info(&strtmp); M2M_INFO("Firmware ver : %u.%u.%u\n", strtmp.u8FirmwareMajor, strtmp.u8FirmwareMinor, strtmp.u8FirmwarePatch); M2M_INFO("Min driver ver : %u.%u.%u\n", strtmp.u8DriverMajor, strtmp.u8DriverMinor, strtmp.u8DriverPatch); M2M_INFO("Curr driver ver: %u.%u.%u\n", M2M_DRIVER_VERSION_MAJOR_NO, M2M_DRIVER_VERSION_MINOR_NO, M2M_DRIVER_VERSION_PATCH_NO); if(M2M_ERR_FW_VER_MISMATCH == ret) { M2M_ERR("Mismatch Firmawre Version\n"); } goto _EXIT0; _EXIT1: nm_drv_deinit(NULL); _EXIT0: return ret; }
/* * @fn nm_i2c_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 * @author M. Abdelmawla * @date 11 July 2012 * @version 1.0 */ sint8 nm_i2c_read_block(uint32 u32Addr, uint8 *pu8Buf, uint16 u16Sz) { tstrNmI2cDefault strI2c; uint8 au8Buf[7]; sint8 s8Ret = M2M_SUCCESS; au8Buf[0] = 0x02; au8Buf[1] = (uint8)(u32Addr >> 24); au8Buf[2] = (uint8)(u32Addr >> 16); au8Buf[3] = (uint8)(u32Addr >> 8); au8Buf[4] = (uint8)(u32Addr >> 0); au8Buf[5] = (uint8)(u16Sz >> 8); au8Buf[6] = (uint8)(u16Sz); strI2c.pu8Buf = au8Buf; strI2c.u16Sz = sizeof(au8Buf); if(M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_W, &strI2c)) { M2M_ERR("write error\n"); s8Ret = M2M_ERR_BUS_FAIL; } else { strI2c.pu8Buf = pu8Buf; strI2c.u16Sz = u16Sz; if(M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_R, &strI2c)) { M2M_ERR("read error\n"); s8Ret = M2M_ERR_BUS_FAIL; } } return s8Ret; }
/*! @fn \ NMI_API sint8 m2m_wifi_set_receive_buffer(void* pvBuffer,uint16 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. */ NMI_API sint8 m2m_wifi_set_receive_buffer(void* pvBuffer,uint16 u16BufferLen) { sint8 s8ret = M2M_SUCCESS; if(pvBuffer != NULL) { gau8ethRcvBuf = pvBuffer; gu16ethRcvBufSize= u16BufferLen; } else { s8ret = M2M_ERR_FAIL; M2M_ERR("Buffer NULL pointer\r\n"); } return s8ret; }
sint8 m2m_wifi_p2p(uint8 u8Channel) { sint8 ret = M2M_SUCCESS; if((u8Channel == 1) || (u8Channel == 6) || (u8Channel == 11)) { tstrM2MP2PConnect strtmp; strtmp.u8ListenChannel = u8Channel; ret = hif_send(M2M_REQ_GRP_WIFI, M2M_WIFI_REQ_ENABLE_P2P, (uint8*)&strtmp, sizeof(tstrM2MP2PConnect), NULL, 0,0); } else { M2M_ERR("Listen channel should only be 1, 6 or 11\n"); ret = M2M_ERR_FAIL; } return ret; }
/*! @fn sint8 m2m_wifi_prng_get_random_bytes(uint8 * pu8PrngBuff,uint16 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_SUCCESE for success and a negative value otherwise. */ sint8 m2m_wifi_prng_get_random_bytes(uint8 * pu8PrngBuff,uint16 u16PrngSize) { sint8 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 *)&strRng, sizeof(tstrPrng),NULL,0, 0); } else { M2M_ERR("PRNG Buffer exceeded maximum size %d or NULL Buffer\n",u16PrngSize); } return ret; }
/* * @fn nm_i2c_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 * @author M. Abdelmawla * @date 11 July 2012 * @version 1.0 */ sint8 nm_i2c_read_reg_with_ret(uint32 u32Addr, uint32* pu32RetVal) { uint8 b[6]; uint8 rsz; tstrNmI2cDefault strI2c; sint8 s8Ret = M2M_SUCCESS; if(u32Addr < 0xff) { /* clockless i2c */ b[0] = 0x09; b[1] = (uint8)(u32Addr); rsz = 1; strI2c.u16Sz = 2; } else { b[0] = 0x80; b[1] = (uint8)(u32Addr >> 24); b[2] = (uint8)(u32Addr >> 16); b[3] = (uint8)(u32Addr >> 8); b[4] = (uint8)(u32Addr); b[5] = 0x04; rsz = 4; strI2c.u16Sz = 6; } strI2c.pu8Buf = b; if(M2M_SUCCESS == nm_bus_ioctl(NM_BUS_IOCTL_W, &strI2c)) { strI2c.u16Sz = rsz; if(M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_R, &strI2c)) { //M2M_ERR("read error\n"); s8Ret = M2M_ERR_BUS_FAIL; } } else { M2M_ERR("failed to send cfg bytes\n"); s8Ret = M2M_ERR_BUS_FAIL; } if (rsz == 1) { *pu32RetVal = b[0]; } else { *pu32RetVal = b[0] | ((uint32)b[1] << 8) | ((uint32)b[2] << 16) | ((uint32)b[3] << 24); } return s8Ret; }
/* * @fn nm_uart_sync_cmd * @brief Check COM Port * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure * @author Dina El Sissy * @date 13 AUG 2012 * @version 1.0 */ sint8 nm_uart_sync_cmd(void) { tstrNmUartDefault strUart; sint8 s8Ret = -1; uint8 b [HDR_SZ+1]; uint8 rsz; uint8 onchip = 0; /*read reg*/ b[0] = 0x12; rsz = 1; strUart.pu8Buf = b; strUart.u16Sz = 1; if(M2M_SUCCESS == nm_bus_ioctl(NM_BUS_IOCTL_W, &strUart)) { strUart.u16Sz = rsz; if(M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_R, &strUart)) { s8Ret = M2M_ERR_BUS_FAIL; } } else { M2M_ERR("failed to send cfg bytes\n"); s8Ret = M2M_ERR_BUS_FAIL; } if (b[0] == 0x5a) { s8Ret = 0; onchip = 1; M2M_INFO("Built-in WINC1500 UART Found\n"); } else if(b[0] == 0x5b) { s8Ret = 0; onchip = 0; M2M_INFO("WINC1500 Serial Bridge Found\n"); } /*TODO: this should be the way we read the register since the cortus is little endian*/ /**pu32RetVal = b[0] | ((uint32)b[1] << 8) | ((uint32)b[2] << 16) | ((uint32)b[3] << 24);*/ if(s8Ret == M2M_SUCCESS) s8Ret = (sint8)onchip; return s8Ret; }
/** * @fn hif_Resp_handler(uint8 *pu8Buffer, uint16 u16BufferSize) * @brief Response handler for HIF layer. * @param [in] pu8Buffer Pointer to the buffer. * @param [in] u16BufferSize Buffer size. * @return The function SHALL return 0 for success and a negative value otherwise. */ sint8 hif_Resp_handler(uint8 *pu8Buffer, uint16 u16BufferSize) { uint8 u8WriteIdx = gstrAppReqList.u8WriteIdx; M2M_DBG("m2m_callback %x %x %x\n",pu8Buffer,u16BufferSize,u8WriteIdx); if (gstrAppReqList.astrReq[u8WriteIdx].u8IsUsed) M2M_ERR("APP Req Overflow\n"); gstrAppReqList.astrReq[u8WriteIdx].u8IsUsed = 1; gstrAppReqList.astrReq[u8WriteIdx].pu8ReqBuffer = pu8Buffer; gstrAppReqList.u8WriteIdx++; if (gstrAppReqList.u8WriteIdx >= APP_MAX_NUM_REQ) gstrAppReqList.u8WriteIdx = 0; app_os_sem_up(&gstrAppSem); return 0; }
sint8 m2m_wifi_set_scan_options(uint8 u8NumOfSlot,uint8 u8SlotTime) { sint8 s8Ret = M2M_ERR_FAIL; tstrM2MScanOption strM2MScan; strM2MScan.u8NumOfSlot = u8NumOfSlot; strM2MScan.u8SlotTime = u8SlotTime; if(strM2MScan.u8NumOfSlot < M2M_SCAN_MIN_NUM_SLOTS) { strM2MScan.u8NumOfSlot = M2M_SCAN_MIN_NUM_SLOTS; } if(strM2MScan.u8SlotTime < M2M_SCAN_MIN_SLOT_TIME) { strM2MScan.u8SlotTime = M2M_SCAN_MIN_SLOT_TIME; } s8Ret = hif_send(M2M_REQ_GRP_WIFI, M2M_WIFI_REQ_SET_SCAN_OPTION, (uint8*)&strM2MScan, sizeof(tstrM2MScan),NULL, 0,0); if(s8Ret != M2M_SUCCESS) { M2M_ERR("SCAN Failed Ret = %d\n",s8Ret); } return s8Ret; }
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); }