ICACHE_FLASH_ATTR void netclient_espconn_reaper(void) { struct espconn *espconn; struct netclient *netclient; int i; for (i = 0; i < MAXCLIENT; i++) { if (netclients[i].espconn == NULL) continue; if (netclients[i].espconn->state == ESPCONN_NONE || netclients[i].espconn->state == ESPCONN_CLOSE) { /* found disconnected espconn */ espconn = netclients[i].espconn; #ifdef NETDEBUG printf("%s:%d: %p\n", __func__, __LINE__, espconn); #endif /* reap disconnected client info */ netclient = netclient_lookup_by_espconn(espconn); if (netclient != NULL) { syslog_send(LOG_DAEMON|LOG_INFO, "telnet: disconnect from %d.%d.%d.%d:%d", netclient->remote_ip[0], netclient->remote_ip[1], netclient->remote_ip[2], netclient->remote_ip[3], netclient->remote_port); netclient->espconn = NULL; } } } }
//---------------------------------------------------------------------------- //Routine für die Ausgabe gepufferter Zeichen ueber zeilen-orientierte Devices void console_flush(void) { if ( !console_tx_counter ) // buffer is empty return; console_tx_buffer[console_tx_counter] = 0; // terminate buffered string #if USE_TELNET if ( console_mode & CONSOLE_TELNET ) telnetd_write_str ( console_tx_buffer ); #endif #if USE_SYSLOG if ( console_mode & CONSOLE_SYSLOG ) syslog_send ( console_tx_buffer ); #endif console_tx_counter = 0; // clear buffer }
void syslog_net_init(void) { uip_ipaddr_t ip; set_CONF_SYSLOG_SERVER(&ip); syslog_conn = uip_udp_new(&ip, HTONS(SYSLOG_PORT), syslog_net_main); if(! syslog_conn) { debug_printf("syslog: couldn't create connection\n"); return; } uip_udp_bind(syslog_conn, HTONS(SYSLOG_PORT)); syslog_send("booting ethersex\n"); }
ICACHE_FLASH_ATTR void netout_flush(struct netclient *netclient) { char *p; unsigned int l; sint8 rc; if (fifo_len(&netclient->fifo_net) == 0) return; if (netclient->fifo_net_sending == 0) { netclient->fifo_net_sending = 1; p = fifo_getbulk(&netclient->fifo_net, &l); rc = espconn_sent(netclient->espconn, p, l); if (rc != ESPCONN_OK) { syslog_send(LOG_DAEMON|LOG_DEBUG, "telnet: espconn_sent: error %d", rc); espconn_disconnect(netclient->espconn); } } }
int main(int argc, char *argv[]) { /* Disable driver enable for RS485 ASAP */ //DDRC |= (1 << PC2); //PORTC &= ~(1 << PC2); /* init serial line debugging */ UBRR0H = UBRRH_VALUE; UBRR0L = UBRRL_VALUE; UCSR0B = (1 << RXCIE1) | (1 << RXEN1) | (1 << TXEN0); UCSR0C = (1<<UCSZ00) | (1<<UCSZ01); /* Initialize UART */ net_init(); DBG("READY!\r\n"); DBG("Initializing SPI...\r\n"); spi_init(); DBG("Initializing ENC28J60...\r\n"); init_enc28j60(); DBG("Initialized ENC28J60\r\n"); char obuf[64]; snprintf(obuf, sizeof(obuf), "enc28j60 rev 0x%x\n", read_control_register(REG_EREVID)); DBG(obuf); char buf[16] = "serial: X\n"; int cnt = 0; while (1) { if (eth_to_rs_cnt > 0 && eth_to_rs[eth_to_rs_cnt-1] == '$') { eth_to_rs[eth_to_rs_cnt-1] = '\0'; int dest = 0; int pktlen = 0; char minibuf[16]; minibuf[0] = eth_to_rs[1]; minibuf[1] = eth_to_rs[2]; minibuf[2] = '\0'; if (sscanf(minibuf, "%d", &dest) != 1) { DBG("Could not parse dest\r\n"); eth_to_rs_cnt = 0; continue; } minibuf[0] = eth_to_rs[3]; minibuf[1] = eth_to_rs[4]; if (sscanf(minibuf, "%d", &pktlen) != 1) { DBG("Could not parse len\r\n"); eth_to_rs_cnt = 0; continue; } if (pktlen != (eth_to_rs_cnt - 6)) { DBG("lens are not the same\r\n"); minibuf[2] = '\r'; minibuf[3] = '\n'; minibuf[4] = '\0'; DBG(minibuf); snprintf(minibuf, sizeof(minibuf), "e: %d\r\n", eth_to_rs_cnt-6); DBG(minibuf); snprintf(minibuf, sizeof(minibuf), "p: %d\r\n", pktlen); DBG(minibuf); eth_to_rs_cnt = 0; continue; } fmt_packet(lbuffer, dest, 0xFF, eth_to_rs + 5, pktlen); struct buspkt *packet = (struct buspkt*)lbuffer; send_packet(packet); syslog_send("sent packet", strlen("sent packet")); _delay_ms(25); sendit = 0; eth_to_rs_cnt = 0; } #if 0 network_process(); if (uip_recvlen > 0) { syslog_send("handling ethernet packet", strlen("handling ethernet packet")); DBG("Handling packet\r\n"); handle_icmpv6(); if (uip_recvbuf[20] == 0x11) { syslog_send("handling udp packet", strlen("handling udp packet")); /* UDP */ uint8_t *udp = uip_recvbuf + 14 + 40; uint8_t len = udp[5] - 8; /* TODO: sanity check */ uint8_t *recvpayload = udp + 8 /* udp */; fmt_packet(lbuffer, uip_recvbuf[53], 0xFF, recvpayload, len); struct buspkt *packet = (struct buspkt*)lbuffer; //syslog_send("sending packet", strlen("sending packet")); send_packet(packet); _delay_ms(25); cnt = 85; syslog_send("ethernet to rs485 done", strlen("ethernet to rs485 done")); } //syslog_send("received a packet", strlen("received a packet")); buf[14] = uip_recvlen; //syslog_send(uip_recvbuf, uip_recvlen); uip_recvlen = 0; } #endif _delay_ms(10); if (cnt++ == 100) { fmt_packet(lbuffer, 1, 0, "ping", 4); struct buspkt *packet = (struct buspkt*)lbuffer; syslog_send("ping sent", strlen("ping sent")); send_packet(packet); cnt = 0; snprintf(obuf, sizeof(obuf), "cnt = %d, rem = %d\r\n", eth_to_rs_cnt, eth_to_rs_rem); syslog_send(obuf, strlen(obuf)); } uint8_t status = bus_status(); if (status == BUS_STATUS_IDLE) continue; if (status == BUS_STATUS_MESSAGE) { /* get a copy of the current packet */ struct buspkt *packet = current_packet(); uint8_t *walk = packet; uint8_t *payload = (uint8_t*)packet; payload += sizeof(struct buspkt); /* check for ping replies */ if (packet->destination == 0x00 && memcmp(payload, "pong", strlen("pong")) == 0) { syslog_send("pong received", strlen("pong received")); /* TODO: store that this controller is reachable */ /* check if the controller has any waiting messages */ if (payload[4] > 0) { /* request the message */ fmt_packet(lbuffer, packet->source, 0, "send", 4); struct buspkt *reply = (struct buspkt*)lbuffer; //syslog_send("sending packet", strlen("sending packet")); _delay_ms(25); send_packet(reply); syslog_send("sendreq sent", strlen("sendreq sent")); //syslog_send(reply, reply->length_lo + sizeof(struct buspkt)); _delay_ms(25); cnt = 0; } } #if 0 /* copy packet->destination into the MAC and IPv6 address */ uip_buf[5] = packet->destination; /* MAC */ uip_buf[53] = packet->destination; /* IPv6 */ /* copy packet->source into the MAC and IPv6 address */ uip_buf[11] = packet->source; /* MAC */ uip_buf[37] = packet->source; /* IPv6 */ raw_send(payload, packet->length_lo); #endif char minibuf[16]; snprintf(minibuf, sizeof(minibuf), "^%02d%02d", 0, packet->length_lo); uart_puts(minibuf); uint8_t c; for (c = 0; c < sizeof(struct buspkt); c++) { while ( !( UCSR0A & (1<<UDRE0)) ); UDR0 = walk[c]; } for (c = 0; c < packet->length_lo; c++) { while ( !( UCSR0A & (1<<UDRE0)) ); UDR0 = payload[c]; } while ( !( UCSR0A & (1<<UDRE0)) ); UDR0 = '$'; /* discard the packet from serial buffer */ packet_done(); continue; } if (status == BUS_STATUS_WRONG_CRC) { syslog_send("broken", strlen("broken")); struct buspkt *packet = current_packet(); raw_send(packet, 16); skip_byte(); continue; } } }