size_t WiFiClient::write(const uint8_t *buf, size_t size) { sint16 err; if (_socket < 0 || size == 0) { setWriteError(); return 0; } // Network led ON. m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 0); m2m_wifi_handle_events(NULL); while ((err = send(_socket, (void *)buf, size, 0)) < 0) { // Exit on fatal error, retry if buffer not ready. if (err != SOCK_ERR_BUFFER_FULL) { setWriteError(); m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 1); return 0; } m2m_wifi_handle_events(NULL); } // Network led OFF. m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 1); return size; }
void WiFiClass::disconnect() { m2m_wifi_disconnect(); // WiFi led OFF (rev A then rev B). m2m_periph_gpio_set_val(M2M_PERIPH_GPIO15, 1); m2m_periph_gpio_set_val(M2M_PERIPH_GPIO4, 1); }
size_t WiFiUDP::write(const uint8_t *buffer, size_t size) { struct sockaddr_in addr; // Network led ON (rev A then rev B). m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 0); m2m_periph_gpio_set_val(M2M_PERIPH_GPIO5, 0); addr.sin_family = AF_INET; addr.sin_port = _htons(_sndPort); addr.sin_addr.s_addr = _sndIP; if (sendto(_socket, (void *)buffer, size, 0, (struct sockaddr *)&addr, sizeof(addr)) < 0) { // Network led OFF (rev A then rev B). m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 1); m2m_periph_gpio_set_val(M2M_PERIPH_GPIO5, 1); return 0; } // Network led OFF (rev A then rev B). m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 1); m2m_periph_gpio_set_val(M2M_PERIPH_GPIO5, 1); return size; }
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; }
int WiFiClass::init() { tstrWifiInitParam param; int8_t ret; // Initialize the WiFi BSP: nm_bsp_init(); // Initialize WiFi module and register status callback: param.pfAppWifiCb = wifi_cb; ret = m2m_wifi_init(¶m); if (M2M_SUCCESS != ret) { // Error led ON (rev A then rev B). m2m_periph_gpio_set_val(M2M_PERIPH_GPIO18, 0); m2m_periph_gpio_set_dir(M2M_PERIPH_GPIO6, 1); return ret; } // Initialize socket API and register socket callback: socketInit(); socketBufferInit(); registerSocketCallback(socketBufferCb, resolve_cb); _init = 1; _status = WL_IDLE_STATUS; _localip = 0; _submask = 0; _gateway = 0; _dhcp = 1; memset(_client, 0, sizeof(WiFiClient *) * TCP_SOCK_MAX); // Initialize IO expander LED control (rev A then rev B).. m2m_periph_gpio_set_val(M2M_PERIPH_GPIO15, 1); m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 1); m2m_periph_gpio_set_val(M2M_PERIPH_GPIO18, 1); m2m_periph_gpio_set_dir(M2M_PERIPH_GPIO15, 1); m2m_periph_gpio_set_dir(M2M_PERIPH_GPIO16, 1); m2m_periph_gpio_set_dir(M2M_PERIPH_GPIO18, 1); m2m_periph_gpio_set_val(M2M_PERIPH_GPIO4, 1); m2m_periph_gpio_set_val(M2M_PERIPH_GPIO5, 1); m2m_periph_gpio_set_val(M2M_PERIPH_GPIO6, 1); m2m_periph_gpio_set_dir(M2M_PERIPH_GPIO4, 1); m2m_periph_gpio_set_dir(M2M_PERIPH_GPIO5, 1); m2m_periph_gpio_set_dir(M2M_PERIPH_GPIO6, 1); return ret; }
int WiFiClass::hostByName(const char* aHostname, IPAddress& aResult) { // check if aHostname is already an ipaddress if (aResult.fromString(aHostname)) { // if fromString returns true we have an IP address ready return 1; } else { // Network led ON (rev A then rev B). m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 0); m2m_periph_gpio_set_val(M2M_PERIPH_GPIO5, 0); // Send DNS request: _resolve = 0; if (gethostbyname((uint8 *)aHostname) < 0) { // Network led OFF (rev A then rev B). m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 1); m2m_periph_gpio_set_val(M2M_PERIPH_GPIO5, 1); return 0; } // Wait for connection or timeout: unsigned long start = millis(); while (_resolve == 0 && millis() - start < 20000) { m2m_wifi_handle_events(NULL); } // Network led OFF (rev A then rev B). m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 1); m2m_periph_gpio_set_val(M2M_PERIPH_GPIO5, 1); if (_resolve == 0) { return 0; } aResult = _resolve; return 1; } }
static void wifi_cb(uint8_t u8MsgType, void *pvMsg) { switch (u8MsgType) { case M2M_WIFI_RESP_CON_STATE_CHANGED: { tstrM2mWifiStateChanged *pstrWifiState = (tstrM2mWifiStateChanged *)pvMsg; if (pstrWifiState->u8CurrState == M2M_WIFI_CONNECTED) { //SERIAL_PORT_MONITOR.println("wifi_cb: M2M_WIFI_RESP_CON_STATE_CHANGED: CONNECTED"); if (WiFi._mode == WL_STA_MODE && !WiFi._dhcp) { WiFi._status = WL_CONNECTED; // WiFi led ON. m2m_periph_gpio_set_val(M2M_PERIPH_GPIO15, 0); } } else if (pstrWifiState->u8CurrState == M2M_WIFI_DISCONNECTED) { //SERIAL_PORT_MONITOR.println("wifi_cb: M2M_WIFI_RESP_CON_STATE_CHANGED: DISCONNECTED"); if (WiFi._mode == WL_STA_MODE) { WiFi._status = WL_DISCONNECTED; if (WiFi._dhcp) { WiFi._localip = 0; WiFi._submask = 0; WiFi._gateway = 0; } } // WiFi led OFF (rev A then rev B). m2m_periph_gpio_set_val(M2M_PERIPH_GPIO15, 1); m2m_periph_gpio_set_val(M2M_PERIPH_GPIO4, 1); } } break; case M2M_WIFI_REQ_DHCP_CONF: { if (WiFi._mode == WL_STA_MODE) { tstrM2MIPConfig *pstrIPCfg = (tstrM2MIPConfig *)pvMsg; WiFi._localip = pstrIPCfg->u32StaticIP; WiFi._submask = pstrIPCfg->u32SubnetMask; WiFi._gateway = pstrIPCfg->u32Gateway; WiFi._status = WL_CONNECTED; // 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); } /*uint8_t *pu8IPAddress = (uint8_t *)pvMsg; SERIAL_PORT_MONITOR.print("wifi_cb: M2M_WIFI_REQ_DHCP_CONF: IP is "); SERIAL_PORT_MONITOR.print(pu8IPAddress[0], 10); SERIAL_PORT_MONITOR.print("."); SERIAL_PORT_MONITOR.print(pu8IPAddress[1], 10); SERIAL_PORT_MONITOR.print("."); SERIAL_PORT_MONITOR.print(pu8IPAddress[2], 10); SERIAL_PORT_MONITOR.print("."); SERIAL_PORT_MONITOR.print(pu8IPAddress[3], 10); SERIAL_PORT_MONITOR.println("");*/ } break; case M2M_WIFI_RESP_CURRENT_RSSI: { WiFi._resolve = *((int8_t *)pvMsg); } break; case M2M_WIFI_RESP_PROVISION_INFO: { tstrM2MProvisionInfo *pstrProvInfo = (tstrM2MProvisionInfo *)pvMsg; //SERIAL_PORT_MONITOR.println("wifi_cb: M2M_WIFI_RESP_PROVISION_INFO"); if (pstrProvInfo->u8Status == M2M_SUCCESS) { memset(WiFi._ssid, 0, M2M_MAX_SSID_LEN); memcpy(WiFi._ssid, (char *)pstrProvInfo->au8SSID, strlen((char *)pstrProvInfo->au8SSID)); WiFi._mode = WL_STA_MODE; WiFi._localip = 0; WiFi._submask = 0; WiFi._gateway = 0; m2m_wifi_connect((char *)pstrProvInfo->au8SSID, strlen((char *)pstrProvInfo->au8SSID), pstrProvInfo->u8SecType, pstrProvInfo->au8Password, M2M_WIFI_CH_ALL); } else { WiFi._status = WL_CONNECT_FAILED; //SERIAL_PORT_MONITOR.println("wifi_cb: Provision failed.\r\n"); } } break; case M2M_WIFI_RESP_SCAN_DONE: { tstrM2mScanDone *pstrInfo = (tstrM2mScanDone *)pvMsg; if (pstrInfo->u8NumofCh >= 1) { WiFi._status = WL_SCAN_COMPLETED; } } break; case M2M_WIFI_RESP_SCAN_RESULT: { tstrM2mWifiscanResult *pstrScanResult = (tstrM2mWifiscanResult *)pvMsg; uint16_t scan_ssid_len = strlen((const char *)pstrScanResult->au8SSID); memset(WiFi._scan_ssid, 0, M2M_MAX_SSID_LEN); if (scan_ssid_len) { memcpy(WiFi._scan_ssid, (const char *)pstrScanResult->au8SSID, scan_ssid_len); } if (WiFi._bssid) { memcpy(WiFi._bssid, (const char *)pstrScanResult->au8BSSID, 6); } WiFi._resolve = pstrScanResult->s8rssi; WiFi._scan_auth = pstrScanResult->u8AuthType; WiFi._status = WL_SCAN_COMPLETED; } break; default: break; } }
void socketBufferCb(SOCKET sock, uint8 u8Msg, void *pvMsg) { switch (u8Msg) { /* Socket connected. */ case SOCKET_MSG_CONNECT: { tstrSocketConnectMsg *pstrConnect = (tstrSocketConnectMsg *)pvMsg; if (pstrConnect && pstrConnect->s8Error >= 0) { recv(sock, gastrSocketBuffer[sock].buffer, SOCKET_BUFFER_MTU, 0); *(gastrSocketBuffer[sock].flag) |= SOCKET_BUFFER_FLAG_CONNECTED; } else { close(sock); } } break; /* TCP Data receive. */ case SOCKET_MSG_RECV: { // Network led ON (rev A then rev B). m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 0); m2m_periph_gpio_set_val(M2M_PERIPH_GPIO5, 0); tstrSocketRecvMsg *pstrRecv = (tstrSocketRecvMsg *)pvMsg; if (pstrRecv && pstrRecv->s16BufferSize > 0) { /* Protect against overflow. */ if (*(gastrSocketBuffer[sock].head) + pstrRecv->s16BufferSize > SOCKET_BUFFER_TCP_SIZE) { *(gastrSocketBuffer[sock].flag) |= SOCKET_BUFFER_FLAG_FULL; break; } /* Add data size. */ *(gastrSocketBuffer[sock].head) += pstrRecv->s16BufferSize; /* Buffer full, stop reception. */ if (SOCKET_BUFFER_TCP_SIZE - *(gastrSocketBuffer[sock].head) < SOCKET_BUFFER_MTU) { if (pstrRecv->u16RemainingSize != 0) { *(gastrSocketBuffer[sock].flag) |= SOCKET_BUFFER_FLAG_FULL; } } else { recv(sock, gastrSocketBuffer[sock].buffer + *(gastrSocketBuffer[sock].head), SOCKET_BUFFER_MTU, 0); } } /* Test EOF (Socket closed) condition for TCP socket. */ else { *(gastrSocketBuffer[sock].flag) &= ~SOCKET_BUFFER_FLAG_CONNECTED; close(sock); } // Network led OFF (rev A then rev B). m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 1); m2m_periph_gpio_set_val(M2M_PERIPH_GPIO5, 1); } break; /* UDP Data receive. */ case SOCKET_MSG_RECVFROM: { // Network led ON (rev A then rev B). m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 0); m2m_periph_gpio_set_val(M2M_PERIPH_GPIO5, 0); tstrSocketRecvMsg *pstrRecv = (tstrSocketRecvMsg *)pvMsg; if (pstrRecv && pstrRecv->s16BufferSize > 0) { if (hif_small_xfer < 2) { uint32 h = *(gastrSocketBuffer[sock].head); uint8 *buf = gastrSocketBuffer[sock].buffer; uint16 sz = pstrRecv->s16BufferSize + pstrRecv->u16RemainingSize; /* Store packet size. */ buf[h++] = sz >> 8; buf[h++] = sz; /* Store remote host port. */ buf[h++] = pstrRecv->strRemoteAddr.sin_port; buf[h++] = pstrRecv->strRemoteAddr.sin_port >> 8; /* Store remote host IP. */ buf[h++] = pstrRecv->strRemoteAddr.sin_addr.s_addr >> 24; buf[h++] = pstrRecv->strRemoteAddr.sin_addr.s_addr >> 16; buf[h++] = pstrRecv->strRemoteAddr.sin_addr.s_addr >> 8; buf[h++] = pstrRecv->strRemoteAddr.sin_addr.s_addr; /* Data received. */ *(gastrSocketBuffer[sock].head) = h + pstrRecv->s16BufferSize; } else { /* Data received. */ *(gastrSocketBuffer[sock].head) += pstrRecv->s16BufferSize; } /* Buffer full, stop reception. */ if (SOCKET_BUFFER_UDP_SIZE - *(gastrSocketBuffer[sock].head) < SOCKET_BUFFER_MTU + SOCKET_BUFFER_UDP_HEADER_SIZE) { if (pstrRecv->u16RemainingSize != 0) { *(gastrSocketBuffer[sock].flag) |= SOCKET_BUFFER_FLAG_FULL; } } else { if (hif_small_xfer && hif_small_xfer != 3) { recvfrom(sock, gastrSocketBuffer[sock].buffer + *(gastrSocketBuffer[sock].head), SOCKET_BUFFER_MTU, 0); } else { recvfrom(sock, gastrSocketBuffer[sock].buffer + *(gastrSocketBuffer[sock].head) + SOCKET_BUFFER_UDP_HEADER_SIZE, SOCKET_BUFFER_MTU, 0); } } } // Network led OFF (rev A then rev B). m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 1); m2m_periph_gpio_set_val(M2M_PERIPH_GPIO5, 1); }