int connect(int s, const struct sockaddr *name, socklen_t namelen) { struct lwip_socket *sock; err_t err; sock = get_socket(s); if (!sock) return -1; if (((struct sockaddr_in *)name)->sin_family == AF_UNSPEC) { err = netconn_disconnect(sock->conn); } else { struct ip_addr remote_addr; u16_t remote_port; remote_addr.addr = ((struct sockaddr_in *)name)->sin_addr.s_addr; remote_port = ((struct sockaddr_in *)name)->sin_port; err = netconn_connect(sock->conn, &remote_addr, ntohs(remote_port)); } if (err != ERR_OK) { return -1; } return 0; }
static void run_webserver(void *p) { struct ip_addr listenaddr = { 0 }; struct netconn *listener; struct netconn *session; struct timeval tv; err_t rc; DEBUG("Opening connection fir webserver \n"); listener = netconn_new(NETCONN_TCP); DEBUG("Connection at %x\n", &listener); rc = netconn_bind(listener, &listenaddr, 80); if (rc != ERR_OK) { DEBUG("Failed to bind connection: %i\n", rc); return; } rc = netconn_listen(listener); if (rc != ERR_OK) { DEBUG("Failed to listen on connection: %i\n", rc); return; } DEBUG("sucessfully listening the webserver \n"); while (1) { int i; session = netconn_accept(listener); if (session == NULL) continue; ut_sprintf(message, "<html><body><pre> Jiny Kernel Dmesg max_len:%d curr_len:%d \n",MAX_DMESG_LOG,g_dmesg_index); (void) netconn_write(session, message, ut_strlen(message), NETCONN_COPY); i=0; while (i<g_dmesg_index) { if (g_dmesg_index < MAX_DMESG_LOG) (void) netconn_write(session, &g_dmesg[i],100, NETCONN_COPY); i=i+100; } ut_sprintf(message, "</pre></body></html>"); (void) netconn_write(session, message, ut_strlen(message), NETCONN_COPY); (void) netconn_disconnect(session); (void) netconn_delete(session); } }
void ICACHE_FLASH_ATTR _setBulbState(int idx, uint16_t state) { struct netconn * sendconn; struct netbuf * sendbuf; char * data; err_t ret; if (!network_ready) return; sendconn = netconn_new( NETCONN_UDP ); if (sendconn == NULL) { os_printf("[setbulb] Couldn't get netconn\r\n"); return; } sendbuf = netbuf_new(); if (sendbuf == NULL) { os_printf("[setbulb] couldn't get sendbuff\r\n"); return; } data = netbuf_alloc(sendbuf, 38); if (data == NULL) { os_printf("[setbulb] couldn't alloc data\r\n"); return; } getPktSetBulbPower(data, 38, idx, state); ret = netconn_bind(sendconn, &myaddr, 34435); if (ret != ERR_OK) { os_printf("[setbulb] connect error: %d\r\n", ret); return; } netconn_set_recvtimeout( sendconn, 1000); ret = netconn_connect(sendconn, getBulbAddr(idx), 56700); if (ret != ERR_OK) { os_printf("[setbulb] connect error: %d\r\n", ret); return; } ret = netconn_send(sendconn, sendbuf); if (ret != ERR_OK) { os_printf("[setbulb] send error: %d\r\n", ret); } else { } netbuf_free(sendbuf); netbuf_delete(sendbuf); netconn_disconnect(sendconn); netconn_delete(sendconn); }
int netstack_socket_disconnect(struct netstack_socket *sk) { err_t err; if (!sk || !sk->priv) { return VMM_EINVALID; } err = netconn_disconnect(sk->priv); if (err != ERR_OK) { return VMM_EFAIL; } sk->ipaddr[0] = 0; sk->ipaddr[1] = 0; sk->ipaddr[2] = 0; sk->ipaddr[3] = 0; sk->port = 0; return VMM_OK; }
/*-----------------------------------------------------------------------------------*/ int lwip_connect(int s, struct sockaddr *name, socklen_t namelen) { struct lwip_socket *sock; err_t err; sock = get_socket(s); if (!sock) { return -1; } if (((struct sockaddr_in *)name)->sin_family == AF_UNSPEC) { DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d, AF_UNSPEC)\n", s)); err = netconn_disconnect(sock->conn); } else { struct ip_addr remote_addr; u16_t remote_port; remote_addr.addr = ((struct sockaddr_in *)name)->sin_addr.s_addr; remote_port = ((struct sockaddr_in *)name)->sin_port; #if SOCKETS_DEBUG DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d, addr=", s)); ip_addr_debug_print(&remote_addr); DEBUGF(SOCKETS_DEBUG, (" port=%u)\n", ntohs(remote_port))); #endif err = netconn_connect(sock->conn, &remote_addr, ntohs(remote_port)); } if (err != ERR_OK) { DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d) failed, err=%d\n", s, err)); sock_set_errno(sock, err_to_errno(err)); return -1; } DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d) succeeded\n", s)); sock_set_errno(sock, 0); return 0; }
/*-----------------------------------------------------------------------------------*/ int lwip_sendto(int s, void *data, int size, unsigned int flags, struct sockaddr *to, socklen_t tolen) { struct lwip_socket *sock; struct ip_addr remote_addr, addr; u16_t remote_port, port; int ret,connected; sock = get_socket(s); if (!sock) { return -1; } /* get the peer if currently connected */ connected = (netconn_peer(sock->conn, &addr, &port) == ERR_OK); remote_addr.addr = ((struct sockaddr_in *)to)->sin_addr.s_addr; remote_port = ((struct sockaddr_in *)to)->sin_port; #if SOCKETS_DEBUG DEBUGF(SOCKETS_DEBUG, ("lwip_sendto(%d, data=%p, size=%d, flags=0x%x to=", s, data, size, flags)); ip_addr_debug_print(&remote_addr); DEBUGF(SOCKETS_DEBUG, (" port=%u\n", ntohs(remote_port))); #endif netconn_connect(sock->conn, &remote_addr, ntohs(remote_port)); ret = lwip_send(s, data, size, flags); /* reset the remote address and port number of the connection */ if (connected) netconn_connect(sock->conn, &addr, port); else netconn_disconnect(sock->conn); return ret; }
static void LwipInitTask(void* pvArguments) { err_t err; struct netif fsl_netif0; ip_addr_t fsl_netif0_ipaddr, fsl_netif0_netmask, fsl_netif0_gw; char msg[] = "This is my message"; (void)pvArguments; // Init lwip stack tcpip_init(NULL,NULL); printf("%s: lwip init called ..\n", __FUNCTION__); // Setup IP Config for DHCP ... IP4_ADDR(&fsl_netif0_ipaddr, 0,0,0,0); IP4_ADDR(&fsl_netif0_netmask, 0,0,0,0); IP4_ADDR(&fsl_netif0_gw, 0,0,0,0); /* Add a network interface to the list of lwIP netifs. */ netif_add(&fsl_netif0, &fsl_netif0_ipaddr, &fsl_netif0_netmask, &fsl_netif0_gw, NULL, ethernetif_init, ethernet_input); /* Set the network interface as the default network interface. */ netif_set_default(&fsl_netif0); /* obtain the IP address, default gateway and subnet mask by using DHCP*/ err = dhcp_start(&fsl_netif0); printf("%s : Started DCHP request (%s)\n", __FUNCTION__, lwip_strerr(err)); for(int i=0; i < DHCP_TIMEOUT && fsl_netif0.dhcp->state != DHCP_BOUND; i++) { printf("%s : Current DHCP State : %d\n", __FUNCTION__, fsl_netif0.dhcp->state); // Wait a second vTaskDelay(1000/portTICK_PERIOD_MS); } // Make it active ... netif_set_up(&fsl_netif0); printf("%s : Interface is up : %d\n", __FUNCTION__, fsl_netif0.dhcp->state); printf("%s : IP %s\n", __FUNCTION__, ipaddr_ntoa(&fsl_netif0.ip_addr)); printf("%s : NM %s\n", __FUNCTION__, ipaddr_ntoa(&fsl_netif0.netmask)); printf("%s : GW %s\n", __FUNCTION__, ipaddr_ntoa(&fsl_netif0.gw)); if (fsl_netif0.dhcp->state == DHCP_BOUND) { // Send out some UDP data struct netconn* pConnection; // Create UDP connection pConnection = netconn_new(NETCONN_UDP); // Connect to local port err = netconn_bind(pConnection, IP_ADDR_ANY, 12345); printf("%s : Bound to IP_ADDR_ANY port 12345 (%s)\n", __FUNCTION__, lwip_strerr(err)); err = netconn_connect(pConnection, IP_ADDR_BROADCAST, 12346 ); printf("%s : Connected to IP_ADDR_BROADCAST port 12346 (%s)\n", __FUNCTION__, lwip_strerr(err)); for(int i = 0; i < 10; i++ ){ struct netbuf* buf = netbuf_new(); void* data = netbuf_alloc(buf, sizeof(msg)); memcpy (data, msg, sizeof (msg)); err = netconn_send(pConnection, buf); printf("%s : Sending to IP_ADDR_BROADCAST port 12346 (%s)\n", __FUNCTION__, lwip_strerr(err)); netbuf_delete(buf); // De-allocate packet buffer // Wait a second vTaskDelay(1000/portTICK_PERIOD_MS); } err = netconn_disconnect(pConnection); printf("%s : Disconnected from IP_ADDR_BROADCAST port 12346 (%s)\n", __FUNCTION__, lwip_strerr(err)); err = netconn_delete(pConnection); printf("%s : Deleted connection (%s)\n", __FUNCTION__, lwip_strerr(err)); } // Wait a second vTaskDelay(1000/portTICK_PERIOD_MS); /* finish the lease of the IP address */ err = dhcp_release(&fsl_netif0); printf("%s : DHCP Release (%s)\n", __FUNCTION__, lwip_strerr(err)); for(;;) {}; }
void broadcast_temperature(void *pvParameters) { uint8_t amount = 0; uint8_t sensors = 1; ds18b20_addr_t addrs[sensors]; float results[sensors]; // Use GPIO 13 as one wire pin. uint8_t GPIO_FOR_ONE_WIRE = 13; char msg[100]; // Broadcaster part err_t err; while(1) { // Send out some UDP data struct netconn* conn; // Create UDP connection conn = netconn_new(NETCONN_UDP); // Connect to local port err = netconn_bind(conn, IP_ADDR_ANY, 8004); if (err != ERR_OK) { netconn_delete(conn); printf("%s : Could not bind! (%s)\n", __FUNCTION__, lwip_strerr(err)); continue; } err = netconn_connect(conn, IP_ADDR_BROADCAST, 8005); if (err != ERR_OK) { netconn_delete(conn); printf("%s : Could not connect! (%s)\n", __FUNCTION__, lwip_strerr(err)); continue; } for(;;) { // Search all DS18B20, return its amount and feed 't' structure with result data. amount = ds18b20_scan_devices(GPIO_FOR_ONE_WIRE, addrs, sensors); if (amount < sensors){ printf("Something is wrong, I expect to see %d sensors \nbut just %d was detected!\n", sensors, amount); } ds18b20_measure_and_read_multi(GPIO_FOR_ONE_WIRE, addrs, sensors, results); for (int i = 0; i < sensors; ++i) { // ("\xC2\xB0" is the degree character (U+00B0) in UTF-8) sprintf(msg, "Sensor %08x%08x reports: %f \xC2\xB0""C\n", (uint32_t)(addrs[i] >> 32), (uint32_t)addrs[i], results[i]); printf("%s", msg); struct netbuf* buf = netbuf_new(); void* data = netbuf_alloc(buf, strlen(msg)); memcpy (data, msg, strlen(msg)); err = netconn_send(conn, buf); if (err != ERR_OK) { printf("%s : Could not send data!!! (%s)\n", __FUNCTION__, lwip_strerr(err)); continue; } netbuf_delete(buf); // De-allocate packet buffer } vTaskDelay(1000/portTICK_PERIOD_MS); } err = netconn_disconnect(conn); printf("%s : Disconnected from IP_ADDR_BROADCAST port 12346 (%s)\n", __FUNCTION__, lwip_strerr(err)); err = netconn_delete(conn); printf("%s : Deleted connection (%s)\n", __FUNCTION__, lwip_strerr(err)); vTaskDelay(1000/portTICK_PERIOD_MS); } }
void broadcast_temperature(void *pvParameters) { uint8_t amount = 0; uint8_t sensors = 2; ds_sensor_t t[sensors]; // Use GPIO 13 as one wire pin. uint8_t GPIO_FOR_ONE_WIRE = 13; char msg[100]; // Broadcaster part err_t err; // Initialize one wire bus. onewire_init(GPIO_FOR_ONE_WIRE); while(1) { // Send out some UDP data struct netconn* conn; // Create UDP connection conn = netconn_new(NETCONN_UDP); // Connect to local port err = netconn_bind(conn, IP_ADDR_ANY, 8004); if (err != ERR_OK) { netconn_delete(conn); printf("%s : Could not bind! (%s)\n", __FUNCTION__, lwip_strerr(err)); continue; } err = netconn_connect(conn, IP_ADDR_BROADCAST, 8005); if (err != ERR_OK) { netconn_delete(conn); printf("%s : Could not connect! (%s)\n", __FUNCTION__, lwip_strerr(err)); continue; } for(;;) { // Search all DS18B20, return its amount and feed 't' structure with result data. amount = ds18b20_read_all(GPIO_FOR_ONE_WIRE, t); if (amount < sensors){ printf("Something is wrong, I expect to see %d sensors \nbut just %d was detected!\n", sensors, amount); } for (int i = 0; i < amount; ++i) { int intpart = (int)t[i].value; int fraction = (int)((t[i].value - intpart) * 100); // Multiple "" here is just to satisfy compiler and don`t raise 'hex escape sequence out of range' warning. sprintf(msg, "Sensor %d report: %d.%02d ""\xC2""\xB0""C\n",t[i].id, intpart, fraction); printf("%s", msg); struct netbuf* buf = netbuf_new(); void* data = netbuf_alloc(buf, strlen(msg)); memcpy (data, msg, strlen(msg)); err = netconn_send(conn, buf); if (err != ERR_OK) { printf("%s : Could not send data!!! (%s)\n", __FUNCTION__, lwip_strerr(err)); continue; } netbuf_delete(buf); // De-allocate packet buffer } vTaskDelay(1000/portTICK_RATE_MS); } err = netconn_disconnect(conn); printf("%s : Disconnected from IP_ADDR_BROADCAST port 12346 (%s)\n", __FUNCTION__, lwip_strerr(err)); err = netconn_delete(conn); printf("%s : Deleted connection (%s)\n", __FUNCTION__, lwip_strerr(err)); vTaskDelay(1000/portTICK_RATE_MS); } }