/* * Background thread for playing stream. */ THREAD(Scanner, arg) { TCPSOCKET *sock; RADIOSTATION *rsp; uint8_t rs; uint32_t rx_to = 10000UL; NutThreadSetPriority(128); NutSleep(10000); for (;;) { for (rs = 0; rs < MAXNUM_STATIONS; rs++) { NutSleep(2000); if (rs == radio.rc_rstation || rs == radio.rc_cstation) continue; rsp = &station[rs]; if (rsp->rs_ip == 0 || rsp->rs_port == 0 || radio.rc_off) { continue; } /* Delay if this isn't the first connection. */ if (rsp->rs_name) { printf("%lu bytes free\n", NutHeapAvailable()); NutSleep(30000); } /* Create a socket. */ if ((sock = NutTcpCreateSocket()) == 0) { break; } NutTcpSetSockOpt(sock, SO_RCVTIMEO, &rx_to, sizeof(rx_to)); /* Connect the stream server. */ printf("[Scan %s:%u]\n", inet_ntoa(rsp->rs_ip), rsp->rs_port); if (NutTcpConnect(sock, rsp->rs_ip, rsp->rs_port) == 0) { /* Process header from server. */ if (ScanStreamHeader(sock, rsp) == 0) { if (rsp->rs_scantitle) { free(rsp->rs_scantitle); rsp->rs_scantitle = 0; } if (rsp->rs_metaint) { if ((rsp->rs_scantitle = ReadMetaTitle(sock, rsp->rs_metaint)) != 0) { printf("%03u: %s\n", rs, rsp->rs_scantitle); rsp->rs_scandead = 0; } else rsp->rs_scandead = 1; } else rsp->rs_scandead = 0; } else rsp->rs_scandead = 1; } else { rsp->rs_scandead = 1; printf("[SERR=%d]\n", NutTcpError(sock)); } NutTcpCloseSocket(sock); } } NutSleep(30000); }
/*! * \brief Initialize the segmented buffer. * * \param size Number of bytes to allocate for the global buffer. * In systems with banked memory this parameter is * ignored and all banked memory is occupied for the * global buffer. In systems without banked memory, * the specified number of bytes is taken from heap * memory. * * \return Pointer to the first buffer segment or null on failures. */ char *NutSegBufInit(size_t size) { #if NUTBANK_COUNT segbuf_start = (char *)(NUTBANK_START); segbuf_end = (char *)(NUTBANK_START) + NUTBANK_SIZE; segbuf_total = (uint32_t) NUTBANK_COUNT *(uint32_t) NUTBANK_SIZE; #else if (size == 0) size = NutHeapAvailable() / 2; if (segbuf_start) { NutHeapFree(segbuf_start); } if ((segbuf_start = NutHeapAlloc(size)) != NULL) segbuf_end = segbuf_start + size; segbuf_total = size; #endif return NutSegBufReset(); }
/* * Main application entry. */ int play_stream(RADIO_STREAM rStream) { TCPSOCKET *sock; FILE *stream; u_long baud = DBG_BAUDRATE; u_long radio_ip = inet_addr(rStream.radio_ip); u_short tcpbufsiz = TCPIP_BUFSIZ; u_long rx_to = TCPIP_READTIMEOUT; u_short mss = TCPIP_MSS; u_long metaint; /* * Register UART device and assign stdout to it. */ NutRegisterDevice(&DBG_DEVICE, 0, 0); freopen(DBG_DEVNAME, "w", stdout); _ioctl(_fileno(stdout), UART_SETSPEED, &baud); /* * Display system information. */ printf("\n\nMedianut Tuotrial Part 3 - Nut/OS %s - " CC_STRING "\n", NutVersionString()); printf("%u bytes free\n\n", NutHeapAvailable()); /* * Register LAN device. */ if (NutRegisterDevice(&DEV_ETHER, 0x8300, 5)) { puts("Error: No LAN device"); for (; ;); } puts("configure LAN"); /* * Configure LAN. */ if (ConfigureLan("eth0")) { for (; ;); } puts("create a TCP socket"); /* * Create a TCP socket. */ if ((sock = NutTcpCreateSocket()) == 0) { puts("Error: Can't create socket"); for (; ;); } puts("set socket options"); /* * Set socket options. Failures are ignored. */ if (NutTcpSetSockOpt(sock, TCP_MAXSEG, &mss, sizeof(mss))) printf("Sockopt MSS failed\n"); if (NutTcpSetSockOpt(sock, SO_RCVTIMEO, &rx_to, sizeof(rx_to))) printf("Sockopt TO failed\n"); if (NutTcpSetSockOpt(sock, SO_RCVBUF, &tcpbufsiz, sizeof(tcpbufsiz))) printf("Sockopt rxbuf failed\n"); puts("connect the radio station"); /* * Connect the radio station. */ radio_ip = inet_addr(rStream.radio_ip); stream = ConnectStation(sock, rStream.radio_ip, rStream.radio_port, &metaint, rStream); send_message(rStream.radio_ip, rStream.radio_port, &metaint); /* * Play the stream. */ if (stream) { puts("playing stream"); PlayMp3Stream(stream, metaint); fclose(stream); } NutTcpCloseSocket(sock); NutThreadKill(); NutThreadDestroy(); return; puts("Reset me!"); for (; ;); }
void startStream(void) { printf("------starting stream-----\n"); stopAlarm=0; TCPSOCKET *sock; FILE *stream; u_long baud = DBG_BAUDRATE; u_long radio_ip = inet_addr(RADIO_IPADDR); u_short tcpbufsiz = TCPIP_BUFSIZ; u_long rx_to = TCPIP_READTIMEOUT; u_short mss = TCPIP_MSS; u_long metaint; /* * Register UART device and assign stdout to it. */ NutRegisterDevice(&DBG_DEVICE, 0, 0); freopen(DBG_DEVNAME, "w", stdout); _ioctl(_fileno(stdout), UART_SETSPEED, &baud); /* * Display system information. */ //printf("\n\nMedianut Tuotrial Part 3 - Nut/OS %s - " CC_STRING "\n", NutVersionString()); printf("%u bytes free\n\n", NutHeapAvailable()); // /* // * Register LAN device. // */ //if(NutRegisterDevice(&DEV_ETHER, 0x8300, 5)) { // puts("Error: No LAN device"); // for(;;); //} /* * Configure LAN. */ if(ConfigureLan("eth0")) { for(;;); } /* * Create a TCP socket. */ if ((sock = NutTcpCreateSocket()) == 0) { puts("Error: Can't create socket"); for(;;); } /* * Set socket options. Failures are ignored. */ if (NutTcpSetSockOpt(sock, TCP_MAXSEG, &mss, sizeof(mss))) printf("Sockopt MSS failed\n"); if (NutTcpSetSockOpt(sock, SO_RCVTIMEO, &rx_to, sizeof(rx_to))) printf("Sockopt TO failed\n"); if (NutTcpSetSockOpt(sock, SO_RCVBUF, &tcpbufsiz, sizeof(tcpbufsiz))) printf("Sockopt rxbuf failed\n"); /* * Connect the radio station. */ radio_ip = inet_addr(RADIO_IPADDR); stream = ConnectStation(sock, radio_ip, RADIO_PORT, &metaint); /* * Play the stream. */ if(stream) { puts("testingstart"); PlayMp3Stream(stream, metaint); fclose(stream); } NutTcpCloseSocket(sock); VsPlayerStop(); //Tell the thread that he needs to exit //Make sure the thread is gone, so wait half a second NutSleep(500); //Close the stream fclose(stream); puts("Reset me!"); for(;;) { NutSleep(100); } }
static int CgiStatusRow( FILE * stream, int row_no ) { time_t secs = time(NULL); switch( row_no ) { case -1: //HTML("<TR><TH> Type </TH><TH> Value </TH><TH> Comment </TH></TR>\r\n"); break; //HTML("<TR><TH align=left> </TH><TH align=left> </TH></TR>\r\n"); break; //HTML("<th colspan=\"2\" align=left>Time</th>"); break; subhdr( stream, "Time" ); break; case 0: { HTML("<TR><TD> GMT Time </TD><TD> "); printTime( stream, gmtime(&secs) ); HTML(" </TD></TR>\r\n"); //ShowTableRow3( stream, char *c1, char *c2, char *c3 ); } break; case 1: { HTML("<TR><TD> Local Time </TD><TD> "); printTime( stream, localtime(&secs) ); HTML(" </TD></TR>\r\n"); } break; case 2: { char minus = _timezone < 0; int32_t seconds = labs(_timezone); int32_t minutes = seconds / 60UL; int32_t hours = minutes / 60UL; minutes %= 60UL; seconds %= 60UL; HTML("<TR><TD> TimeZone </TD><TD> "); fprintf(stream, "%s%ld:%ld:%ld (%ld)", (minus ? "-" : ""), hours, minutes, seconds, _timezone ); HTML(" </TD></TR>\r\n"); } break; case 3: { uint32_t seconds = NutGetSeconds(); uint32_t minutes = seconds / 60UL; uint32_t hours = minutes / 60UL; uint32_t days = hours / 24UL; minutes %= 60UL; seconds %= 60UL; hours %= 24UL; HTML("<TR><TD> UpTime </TD><TD> "); fprintf(stream, "%lu days %lu:%lu:%lu", days, hours, minutes, seconds); HTML(" </TD></TR>\r\n"); } break; //case 4: ShowTableRow2b( stream, "DST", _daylight ); break; case 4: ShowTableRow2b( stream, "Used SNTP", sntp_available ); break; //case 6: HTML("<th colspan=\"2\">FirmWare</th>"); break; case 5: subhdr( stream, "FirmWare" ); break; case 6: ShowTableRow2( stream, "Build", makeDate ); break; case 7: ShowTableRow2( stream, "Name", DEVICE_NAME ); break; case 8: ShowTableRow2( stream, "ModBus Id", modbus_device_id ); break; //case 9: HTML("<th colspan=\"2\">ModBus</th>"); break; case 9: subhdr( stream, "ModBus" ); break; case 10: ShowTableRow2i( stream, "IO count", modbus_event_cnt ); break; case 11: ShowTableRow2i( stream, "CRC count", modbus_crc_cnt ); break; case 12: ShowTableRow2i( stream, "exceptions count", modbus_exceptions_cnt );break; case 13: ShowTableRow2i( stream, "err flags", modbus_error_flags ); break; case 14: subhdr( stream, "1-Wire" ); break; case 15: ShowTableRow2b( stream, "Devices detected", onewire_available ); break; case 16: ShowTableRow2i( stream, "Temp sensors count", nTempSensors ); break; case 17: #if SERVANT_1WMAC { HTML("<TR><TD> 2401 Id </TD><TD> "); fprintf(stream, "%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X", serialNumber [7], serialNumber [6], serialNumber [5], serialNumber [4], serialNumber [3], serialNumber [2], serialNumber [1], serialNumber [0] ); HTML(" </TD></TR>\r\n"); } #endif break; case 18: subhdr( stream, "OS" ); break; case 19: ShowTableRow2i( stream, "Free mem, K", NutHeapAvailable()/1024 ); break; case 20: ShowTableRow2i( stream, "Size of EEPROM cfg", sizeof(struct eeprom_cfg) ); break; case 21: { uint32_t tx_total, rx_total; uint8_t active; #if SERVANT_TUN0 || SERVANT_TUN1 subhdr( stream, "Tunnels" ); #if SERVANT_TUN0 get_tunnel_stats( 0, &tx_total, &rx_total, &active ); ShowTableRow2b( stream, "Tun0 active", active ); ShowTableRow2i( stream, "Tun0 RX, K", rx_total/1024 ); ShowTableRow2i( stream, "Tun0 TX, K", tx_total/1024 ); #endif #if SERVANT_TUN1 get_tunnel_stats( 1, &tx_total, &rx_total, &active ); ShowTableRow2b( stream, "Tun1 active", active ); ShowTableRow2i( stream, "Tun1 RX, K", rx_total/1024 ); ShowTableRow2i( stream, "Tun1 TX, K", tx_total/1024 ); #endif #endif break; } case 22: subhdr( stream, "DHT11" ); break; case 23: ShowTableRow2i( stream, "Errors count", dht11_errorCnt ); break; default: return 0; } return 1; }
/*! \fn Service(void *arg) * \brief HTTP service thread. * * The endless loop in this thread waits for a client connect, * processes the HTTP request and disconnects. Nut/Net doesn't * support a server backlog. If one client has established a * connection, further connect attempts will be rejected. * Typically browsers open more than one connection in order * to load images concurrently. So we run this routine by * several threads. * */ THREAD(Service, arg) { TCPSOCKET *sock; FILE *stream; uint8_t id = (uint8_t) ((uintptr_t) arg); /* * Now loop endless for connections. */ for (;;) { /* * Create a socket. */ if ((sock = NutTcpCreateSocket()) == 0) { printf("[%u] Creating socket failed\n", id); NutSleep(5000); continue; } /* * Listen on port 80. This call will block until we get a connection * from a client. */ NutTcpAccept(sock, 80); #if defined(__AVR__) printf("[%u] Connected, %u bytes free\n", id, NutHeapAvailable()); #else printf("[%u] Connected, %lu bytes free\n", id, NutHeapAvailable()); #endif /* * Wait until at least 8 kByte of free RAM is available. This will * keep the client connected in low memory situations. */ #if defined(__AVR__) while (NutHeapAvailable() < 8192) { #else while (NutHeapAvailable() < 4096) { #endif printf("[%u] Low mem\n", id); NutSleep(1000); } /* * Associate a stream with the socket so we can use standard I/O calls. */ if ((stream = _fdopen((int) ((uintptr_t) sock), "r+b")) == 0) { printf("[%u] Creating stream device failed\n", id); } else { /* * This API call saves us a lot of work. It will parse the * client's HTTP request, send any requested file from the * registered file system or handle CGI requests by calling * our registered CGI routine. */ NutHttpProcessRequest(stream); /* * Destroy the virtual stream device. */ fclose(stream); } /* * Close our socket. */ NutTcpCloseSocket(sock); printf("[%u] Disconnected\n", id); } } #endif /* DEV_ETHER */ /*! * \brief Main application routine. * * Nut/OS automatically calls this entry after initialization. */ int main(void) { uint32_t baud = 115200; uint8_t i; /* * Initialize the uart device. */ NutRegisterDevice(&DEV_DEBUG, 0, 0); freopen(DEV_DEBUG_NAME, "w", stdout); _ioctl(_fileno(stdout), UART_SETSPEED, &baud); NutSleep(200); printf("\n\nNut/OS %s HTTP Daemon...", NutVersionString()); #ifdef DEV_ETHER #ifdef NUTDEBUG NutTraceTcp(stdout, 0); NutTraceOs(stdout, 0); NutTraceHeap(stdout, 0); NutTracePPP(stdout, 0); #endif /* * Register Ethernet controller. */ if (NutRegisterDevice(&DEV_ETHER, 0, 0)) { puts("Registering device failed"); } printf("Configure %s...", DEV_ETHER_NAME); if (NutNetLoadConfig(DEV_ETHER_NAME)) { uint8_t mac[] = MY_MAC; printf("initial boot..."); #ifdef USE_DHCP if (NutDhcpIfConfig(DEV_ETHER_NAME, mac, 60000)) #endif { uint32_t ip_addr = inet_addr(MY_IPADDR); uint32_t ip_mask = inet_addr(MY_IPMASK); uint32_t ip_gate = inet_addr(MY_IPGATE); printf("No DHCP..."); if (NutNetIfConfig(DEV_ETHER_NAME, mac, ip_addr, ip_mask) == 0) { /* Without DHCP we had to set the default gateway manually.*/ if(ip_gate) { printf("hard coded gate..."); NutIpRouteAdd(0, 0, ip_gate, &DEV_ETHER); } puts("OK"); } else { puts("failed"); } } } else { #ifdef USE_DHCP if (NutDhcpIfConfig(DEV_ETHER_NAME, 0, 60000)) { puts("failed"); } else { puts("OK"); } #else if (NutNetIfConfig(DEV_ETHER_NAME, 0, 0, confnet.cdn_ip_mask)) { puts("failed"); } else { puts("OK"); } #endif } printf("%s ready\n", inet_ntoa(confnet.cdn_ip_addr)); #ifdef USE_DISCOVERY NutRegisterDiscovery((uint32_t)-1, 0, DISF_INITAL_ANN); #endif /* * Register our device for the file system. */ NutRegisterDevice(&MY_FSDEV, 0, 0); #ifdef MY_BLKDEV /* Register block device. */ printf("Registering block device '" MY_BLKDEV_NAME "'..."); if (NutRegisterDevice(&MY_BLKDEV, 0, 0)) { puts("failed"); for (;;); } puts("OK"); /* Mount partition. */ printf("Mounting block device '" MY_BLKDEV_NAME ":1/" MY_FSDEV_NAME "'..."); if (_open(MY_BLKDEV_NAME ":1/" MY_FSDEV_NAME, _O_RDWR | _O_BINARY) == -1) { puts("failed"); for (;;); } puts("OK"); #endif #ifdef MY_HTTPROOT /* Register root path. */ printf("Registering HTTP root '" MY_HTTPROOT "'..."); if (NutRegisterHttpRoot(MY_HTTPROOT)) { puts("failed"); for (;;); } puts("OK"); #endif NutRegisterCgiBinPath("cgi-bin/;user/cgi-bin/;admin/cgi-bin/"); /* * Register our CGI sample. This will be called * by http://host/cgi-bin/test.cgi?anyparams */ NutRegisterCgi("test.cgi", ShowQuery); #if defined(USE_SSI) /* * Register a cgi included by the ssi demo. This will show how dynamic * content is included in a ssi page and how the request parameters for * a site are passed down to the included cgi. */ NutRegisterCgi("ssi-demo.cgi", SSIDemoCGI); #endif /* * Register some CGI samples, which display interesting * system informations. */ NutRegisterCgi("threads.cgi", ShowThreads); NutRegisterCgi("timers.cgi", ShowTimers); NutRegisterCgi("sockets.cgi", ShowSockets); /* * Finally a CGI example to process a form. */ NutRegisterCgi("form.cgi", ShowForm); /* * Protect the cgi-bin directory with * user and password. */ NutRegisterAuth("admin", "root:root"); NutRegisterAuth("user", "user:user"); /* * Register SSI and ASP handler */ #if defined(USE_SSI) NutRegisterSsi(); #endif #if defined(USE_ASP) NutRegisterAsp(); NutRegisterAspCallback(ASPCallback); #endif /* * Start four server threads. */ for (i = 1; i <= 4; i++) { char thname[] = "httpd0"; thname[5] = '0' + i; NutThreadCreate(thname, Service, (void *) (uintptr_t) i, (HTTPD_SERVICE_STACK * NUT_THREAD_STACK_MULT) + NUT_THREAD_STACK_ADD); } #endif /* DEV_ETHER */ /* * We could do something useful here, like serving a watchdog. */ NutThreadSetPriority(254); for (;;) { NutSleep(60000); } return 0; }
/* * Process client requests. */ void ProcessRequests(FILE * stream) { char buff[128]; char *cp; int stat = -1; fputs("200 Welcome to portdio. Type help to get help.\r\n", stream); for (;;) { fflush(stream); /* * Read a line from the client. Ignore * blank lines. */ if (fgets(buff, sizeof(buff), stream) == 0) break; if ((cp = strchr(buff, '\r')) != 0) *cp = 0; if ((cp = strchr(buff, '\n')) != 0) *cp = 0; if (buff[0] == 0) continue; /* * Memory info. */ if (strncmp(buff, "memory", strlen(buff)) == 0) { fprintf(stream, "210 %u bytes RAM free\r\n", (unsigned int)NutHeapAvailable()); continue; } #ifdef OUTBANK /* * Reset output bit. */ if (strlen(buff) > 1 && strncmp(buff, "reset", strlen(buff) - 1) == 0) { int ok = 1; switch (buff[strlen(buff) - 1]) { #ifdef OUTPIN1 case '1': GpioPinSetLow(OUTBANK, OUTPIN1); break; #endif #ifdef OUTPIN2 case '2': GpioPinSetLow(OUTBANK, OUTPIN2); break; #endif #ifdef OUTPIN3 case '3': GpioPinSetLow(OUTBANK, OUTPIN3); break; #endif #ifdef OUTPIN4 case '4': GpioPinSetLow(OUTBANK, OUTPIN4); break; #endif default: ok = 0; break; } if (ok) { fputs("210 OK\r\n", stream); } else fputs("410 Bad pin\r\n", stream); continue; } /* * Set output bit. */ if (strlen(buff) > 1 && strncmp(buff, "set", strlen(buff) - 1) == 0) { int ok = 1; switch (buff[strlen(buff) - 1]) { #ifdef OUTPIN1 case '1': GpioPinSetHigh(OUTBANK, OUTPIN1); break; #endif #ifdef OUTPIN2 case '2': GpioPinSetHigh(OUTBANK, OUTPIN2); break; #endif #ifdef OUTPIN3 case '3': GpioPinSetHigh(OUTBANK, OUTPIN3); break; #endif #ifdef OUTPIN4 case '4': GpioPinSetHigh(OUTBANK, OUTPIN4); break; #endif default: ok = 0; break; } if (ok) { fputs("210 OK\r\n", stream); } else fputs("410 Bad pin\r\n", stream); continue; } #endif /* OUTBANK */ #ifdef INBANK /* * Port status. */ if (strncmp(buff, "query", strlen(buff)) == 0) { stat = PortStatus(); fprintf(stream, "210 %02X\r\n", stat); continue; } /* * wait for status change. */ if (strncmp(buff, "wait", strlen(buff)) == 0) { while (stat == PortStatus()) NutThreadYield(); stat = PortStatus(); fprintf(stream, "210 %02X\r\n", stat); continue; } #endif /* INBANK */ /* * Help. */ fputs("400 List of commands follows\r\n", stream); fputs("memory\tQueries number of RAM bytes free\r\n", stream); #if OUTBANK fputs("reset#\tSet output bit 1..4 low\r\n", stream); fputs("set#\tSet output bit 1..4 high\r\n", stream); #endif #if INBANK fputs("query\tQuery digital i/o status\r\n", stream); fputs("wait\tWaits for digital i/o change\r\n", stream); #endif fputs(".\r\n", stream); } }
/*! \fn Service(void *arg) * \brief HTTP service thread. * * The endless loop in this thread waits for a client connect, * processes the HTTP request and disconnects. Nut/Net doesn't * support a server backlog. If one client has established a * connection, further connect attempts will be rejected. * Typically browsers open more than one connection in order * to load images concurrently. So we run this routine by * several threads. * */ THREAD(Service, arg) { TCPSOCKET *sock; FILE *stream; u_char id = (u_char) ((uptr_t) arg); /* * Now loop endless for connections. */ for (;;) { /* * Create a socket. */ if ((sock = NutTcpCreateSocket()) == 0) { printf("[%u] Creating socket failed\n", id); NutSleep(5000); continue; } /* * Listen on port 80. This call will block until we get a connection * from a client. */ NutTcpAccept(sock, 80); #if defined(__AVR_ATmega128__) || defined(__AVR_ATmega103__) printf("[%u] Connected, %u bytes free\n", id, NutHeapAvailable()); #else printf("[%u] Connected, %lu bytes free\n", id, NutHeapAvailable()); #endif /* * Wait until at least 8 kByte of free RAM is available. This will * keep the client connected in low memory situations. */ #if defined(__AVR_ATmega128__) || defined(__AVR_ATmega103__) while (NutHeapAvailable() < 8192) { #else while (NutHeapAvailable() < 4096) { #endif printf("[%u] Low mem\n", id); NutSleep(1000); } /* * Associate a stream with the socket so we can use standard I/O calls. */ if ((stream = _fdopen((int) ((uptr_t) sock), "r+b")) == 0) { printf("[%u] Creating stream device failed\n", id); } else { /* * This API call saves us a lot of work. It will parse the * client's HTTP request, send any requested file from the * registered file system or handle CGI requests by calling * our registered CGI routine. */ NutHttpProcessRequest(stream); /* * Destroy the virtual stream device. */ fclose(stream); } /* * Close our socket. */ NutTcpCloseSocket(sock); printf("[%u] Disconnected\n", id); } }