Example program with HTTPServer and sensor data streaming over TCPSockets, using Donatien Garnier's Net APIs and services code on top of LWIP. Files StreamServer.h and .cpp encapsulate streaming over TCPSockets. Broadcast is done by sendToAll(), and all incoming data is echoed back to the client. Echo code can be replaced with some remote control of the streaming interface. See main() that shows how to periodically send some data to all subscribed clients. To subscribe, a client should open a socket at <mbed_ip> port 123. I used few lines in TCL code to set up a quick sink for the data. HTTP files are served on port 80 concurrently to the streaming.
lwipNetDnsRequest.cpp
00001 00002 /* 00003 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) 00004 00005 Permission is hereby granted, free of charge, to any person obtaining a copy 00006 of this software and associated documentation files (the "Software"), to deal 00007 in the Software without restriction, including without limitation the rights 00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00009 copies of the Software, and to permit persons to whom the Software is 00010 furnished to do so, subject to the following conditions: 00011 00012 The above copyright notice and this permission notice shall be included in 00013 all copies or substantial portions of the Software. 00014 00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00021 THE SOFTWARE. 00022 */ 00023 00024 #include "lwipNetDnsRequest.h" 00025 #include "lwip/err.h" //err_t, ERR_xxx 00026 #include "lwip/dns.h" 00027 00028 #include "netCfg.h" 00029 #if NET_LWIP_STACK 00030 00031 //#define __DEBUG 00032 #include "dbg/dbg.h" 00033 00034 LwipNetDnsRequest::LwipNetDnsRequest(const char* hostname) : NetDnsRequest(hostname), m_state(LWIPNETDNS_START), m_cbFired(false) 00035 { 00036 DBG("New LwipNetDnsRequest %p\n", this); 00037 } 00038 00039 LwipNetDnsRequest::LwipNetDnsRequest(Host* pHost) : NetDnsRequest(pHost), m_state(LWIPNETDNS_START), m_cbFired(false) 00040 { 00041 DBG("New LwipNetDnsRequest %p\n", this); 00042 } 00043 00044 LwipNetDnsRequest::~LwipNetDnsRequest() 00045 { 00046 DBG("LwipNetDnsRequest %p destroyed\n", this); 00047 } 00048 00049 /* 00050 Main useful function here (see lwip/dns.h): 00051 err_t dns_gethostbyname(const char *hostname, ip_addr_t *addr, 00052 dns_found_callback found, void *callback_arg); 00053 */ 00054 00055 //Execute request & return OK if found, NOTFOUND or ERROR on error, or PROCESSING if the request has not completed yet 00056 void LwipNetDnsRequest::poll() 00057 { 00058 err_t err; 00059 switch(m_state) 00060 { 00061 case LWIPNETDNS_START: //First req, let's call dns_gethostbyname 00062 ip_addr_t ipStruct; 00063 err = dns_gethostbyname(m_hostname, &ipStruct, LwipNetDnsRequest::sFoundCb, (void*) this ); 00064 if( err == ERR_OK ) 00065 { 00066 m_ip = IpAddr(&ipStruct); 00067 m_state = LWIPNETDNS_OK; 00068 DBG("DNS: Ip found in cache.\n"); 00069 } 00070 else if( err == ERR_INPROGRESS) 00071 { 00072 DBG("DNS: Processing.\n"); 00073 m_state = LWIPNETDNS_PROCESSING; 00074 } 00075 else //Likely ERR_VAL 00076 { 00077 DBG("DNS: Error on init.\n"); 00078 m_state = LWIPNETDNS_ERROR; 00079 } 00080 break; 00081 case LWIPNETDNS_PROCESSING: 00082 break; //Nothing to do, DNS is polled on interrupt 00083 case LWIPNETDNS_OK: 00084 if(!m_cbFired) 00085 { 00086 DBG("DNS: Ip found.\n"); 00087 m_cbFired = true; 00088 onReply(NETDNS_FOUND); //Raise callback 00089 } 00090 break; 00091 case LWIPNETDNS_NOTFOUND: 00092 if(!m_cbFired) 00093 { 00094 DBG("DNS: could not be resolved.\n"); 00095 m_cbFired = true; 00096 onReply(NETDNS_NOTFOUND); //Raise callback 00097 } 00098 break; 00099 case LWIPNETDNS_ERROR: 00100 default: 00101 if(!m_cbFired) 00102 { 00103 DBG("DNS: Error.\n"); 00104 m_cbFired = true; 00105 onReply(NETDNS_ERROR); //Raise callback 00106 } 00107 break; 00108 } 00109 // return m_state; 00110 } 00111 00112 void LwipNetDnsRequest::foundCb(const char *name, ip_addr_t *ipaddr) 00113 { 00114 if( ipaddr == NULL ) 00115 { 00116 m_state = LWIPNETDNS_NOTFOUND; 00117 return; 00118 } 00119 m_ip = IpAddr(ipaddr); 00120 m_state = LWIPNETDNS_OK; 00121 } 00122 00123 00124 void LwipNetDnsRequest::sFoundCb(const char *name, ip_addr_t *ipaddr, void *arg) 00125 { 00126 LwipNetDnsRequest* pMe = (LwipNetDnsRequest*) arg; 00127 return pMe->foundCb( name, ipaddr ); 00128 } 00129 00130 #endif 00131
Generated on Tue Jul 12 2022 21:10:25 by 1.7.2