static err_t http_recv(void *arg, tcp_pcb *pcb, pbuf *p, err_t err) { HttpState *hs = (HttpState*)arg; if (hs != NULL) { if (hs->pcb != pcb) { RepRapNetworkMessage("Network: mismatched pcb\n"); return ERR_BUF; } if (err == ERR_OK && p != NULL) { tcp_recved(pcb, p->tot_len); // Inform TCP that we have taken the data reprap.GetNetwork()->ReceiveInput(p, hs); #if 0 //debug { char buff[20]; strncpy(buff, (const char*)(p->payload), 18); buff[18] = '\n'; buff[19] = 0; RepRapNetworkMessage("Network: Accepted data: "); RepRapNetworkMessage(buff); } #endif } } return ERR_OK; }
static err_t http_accept(void *arg, tcp_pcb *pcb, err_t err) { LWIP_UNUSED_ARG(arg); LWIP_UNUSED_ARG(err); tcp_setprio(pcb, TCP_PRIO_MIN); //RepRapNetworkMessage("http_accept\n"); HttpState *hs = (HttpState*)mem_malloc(sizeof(HttpState)); if (hs == NULL) { RepRapNetworkMessage("Out of memory!\n"); return ERR_MEM; } /* Initialize the structure. */ hs->pcb = pcb; hs->file = NULL; hs->left = 0; hs->retries = 0; hs->sendingRs = NULL; tcp_accepted(listening_pcb); // tell TCP to accept further connections tcp_arg(pcb, hs); // tell TCP that this is the structure we wish to be passed for our callbacks tcp_recv(pcb, http_recv); // tell TCP that we wish to be informed of incoming data by a call to the http_recv() function tcp_err(pcb, conn_err); tcp_poll(pcb, http_poll, 4); return ERR_OK; }
/** * \brief Status callback used to print address given by DHCP. * * \param netif Instance to network interface. */ void ethernet_status_callback(struct netif *netif) { char c_mess[20]; // 15 for IP address, 1 for \n, 1 for null, so 3 spare if (netif_is_up(netif)) { RepRapNetworkMessage("Network up, IP="); ipaddr_ntoa_r(&(netif->ip_addr), c_mess, sizeof(c_mess)); strncat(c_mess, sizeof(c_mess) - 1, "\n"); RepRapNetworkMessage(c_mess); netif->flags |= NETIF_FLAG_LINK_UP; } else { RepRapNetworkMessage("Network down\n"); } }
static void conn_err(void *arg, err_t err) { // Report the error to the monitor RepRapNetworkMessage("Network connection error, code "); { char tempBuf[10]; snprintf(tempBuf, ARRAY_SIZE(tempBuf), "%d\n", err); RepRapNetworkMessage(tempBuf); } HttpState *hs = (HttpState*)arg; if (hs != NULL) { reprap.GetNetwork()->ConnectionClosing(hs); // tell the higher levels about the error mem_free(hs); // release the state data } }
void Network::SentPacketAcknowledged(HttpState *hs, unsigned int len) { RequestState *r = hs->sendingRs; if (r != NULL) { r->SentPacketAcknowledged(len); return; } // debugPrintf("Network SentPacketAcknowledged: didn't find hs=%08x\n", (unsigned int)hs); RepRapNetworkMessage("Network SentPacketAcknowledged: didn't find hs\n"); }
static void ethernet_configure_interface(const unsigned char ipAddress[], const unsigned char netMask[], const unsigned char gateWay[]) { struct ip_addr x_ip_addr, x_net_mask, x_gateway; extern err_t ethernetif_init(struct netif *netif); IP4_ADDR(&x_ip_addr, ipAddress[0], ipAddress[1], ipAddress[2], ipAddress[3]); // set IP address if (x_ip_addr.addr == 0) { x_net_mask.addr = 0; // not sure this is needed, but the demo program does it } else { IP4_ADDR(&x_net_mask, netMask[0], netMask[1], netMask[2], netMask[3]); // set network mask } IP4_ADDR(&x_gateway, gateWay[0], gateWay[1], gateWay[2], gateWay[3]); // set gateway /* Add data to netif */ netif_add(&gs_net_if, &x_ip_addr, &x_net_mask, &x_gateway, NULL, ethernetif_init, ethernet_input); /* Make it the default interface */ netif_set_default(&gs_net_if); /* Setup callback function for netif status change */ netif_set_status_callback(&gs_net_if, ethernet_status_callback); /* Bring it up */ if (x_ip_addr.addr == 0) { RepRapNetworkMessage("Starting DHCP\n"); dhcp_start(&gs_net_if); } else { RepRapNetworkMessage("Starting network\n"); netif_set_up(&gs_net_if); } }
/** * \brief Status callback used to print address given by DHCP. * * \param netif Instance to network interface. */ void ethernet_status_callback(struct netif *netif) { char c_mess[20]; // 15 for IP address, 1 for \n, 1 for null, so 3 spare if (netif_is_up(netif)) { RepRapNetworkMessage("Network up, IP="); ipaddr_ntoa_r(&(netif->ip_addr), c_mess, sizeof(c_mess)); /* Nasty bug: concatenates whatever is at the address 19 * to the end of c_mess[] AND declares the length of * c_mess[] to be whatever the address of the temp stack * string "\n" is. * * strncat(c_mess, sizeof(c_mess) - 1, "\n"); */ strncat(c_mess, "\n", sizeof(c_mess) - 1); RepRapNetworkMessage(c_mess); netif->flags |= NETIF_FLAG_LINK_UP; } else { RepRapNetworkMessage("Network down\n"); } }
void RepRapNetworkSendOutput(const char* data, int length, HttpState* hs) { if (hs == 0) { RepRapNetworkMessage("Network: Attempt to write with null structure.\n"); return; } hs->file = data; hs->left = length; hs->retries = 0; tcp_sent(hs->pcb, http_sent); // tell lwip that we wish be to informed of data that has been successfully sent by a call to the http_sent() function SendData(hs->pcb, hs); }
void httpd_init() { static int initCount = 0; initCount++; if (initCount > 1) { RepRapNetworkMessage("httpd_init() called more than once.\n"); } tcp_pcb* pcb = tcp_new(); tcp_bind(pcb, IP_ADDR_ANY, 80); listening_pcb = tcp_listen(pcb); tcp_accept(listening_pcb, http_accept); }
static void SendData(tcp_pcb *pcb, HttpState *hs) { err_t err; u16_t len; /* We cannot send more data than space available in the send buffer. */ if (tcp_sndbuf(pcb) < hs->left) { len = tcp_sndbuf(pcb); } else { len = hs->left; } // RepRapNetworkMessage("Sending "); // sprintf(scratch, "%d", len); // RepRapNetworkMessage(scratch); // RepRapNetworkMessage(".."); do { err = tcp_write(pcb, hs->file, len, 0 /*TCP_WRITE_FLAG_COPY*/ ); // Final arg - 1 means make a copy if (err == ERR_MEM) { len /= 2; } } while (err == ERR_MEM && len > 1); if (err == ERR_OK) { tcp_output(pcb); hs->file += len; hs->left -= len; if (hs->left == 0) { hs->file = NULL; } } else { RepRapNetworkMessage("send_data: error\n"); } }