networking library files

Dependents:   ELEC350_Project2

Committer:
Swabey89
Date:
Wed Jan 02 19:45:02 2019 +0000
Revision:
10:b8214cc88e85
Parent:
9:e22fd5795831
Updated to use queues more

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Swabey89 3:71f9a18a216d 1 #include "sample_hardware.hpp"
Swabey89 3:71f9a18a216d 2 #include "Networking.hpp"
Swabey89 3:71f9a18a216d 3
Swabey89 3:71f9a18a216d 4 //Network thread - responsible for listening for connectinos and responding with updated tempature values
Swabey89 3:71f9a18a216d 5 void network()
Swabey89 3:71f9a18a216d 6 {
Swabey89 3:71f9a18a216d 7 EthernetInterface eth;
Swabey89 3:71f9a18a216d 8 eth.set_network(IP, NETMASK, GATEWAY);
Swabey89 3:71f9a18a216d 9 eth.connect();
Swabey89 10:b8214cc88e85 10 /*
Swabey89 6:31a5f28336e8 11 printlock.lock();
Swabey89 6:31a5f28336e8 12 pc->printf("The target IP address is '%s'\n\n\n\r", eth.get_ip_address());
Swabey89 6:31a5f28336e8 13 printlock.unlock();
Swabey89 10:b8214cc88e85 14 */
Swabey89 10:b8214cc88e85 15 printQueue.call(printf, "The target IP address is '%s'\n\n\n\r", eth.get_ip_address());
Swabey89 3:71f9a18a216d 16
Swabey89 3:71f9a18a216d 17 //Now setup a web server
Swabey89 3:71f9a18a216d 18 TCPServer srv; //TCP/IP Server
Swabey89 3:71f9a18a216d 19 TCPSocket clt_sock; //Socket for communication
Swabey89 3:71f9a18a216d 20 SocketAddress clt_addr; //Address of incoming connection
Swabey89 3:71f9a18a216d 21
Swabey89 3:71f9a18a216d 22 /* Open the server on ethernet stack */
Swabey89 3:71f9a18a216d 23 srv.open(&eth);
Swabey89 3:71f9a18a216d 24
Swabey89 3:71f9a18a216d 25 /* Bind the HTTP port (TCP 80) to the server */
Swabey89 3:71f9a18a216d 26 srv.bind(eth.get_ip_address(), 80);
Swabey89 3:71f9a18a216d 27
Swabey89 3:71f9a18a216d 28 /* Can handle 5 simultaneous connections */
Swabey89 3:71f9a18a216d 29 srv.listen(5);
Swabey89 3:71f9a18a216d 30
Swabey89 3:71f9a18a216d 31 while(true)
Swabey89 3:71f9a18a216d 32 {
Swabey89 3:71f9a18a216d 33 //Block and wait on an incoming connection
Swabey89 3:71f9a18a216d 34 srv.accept(&clt_sock, &clt_addr);
Swabey89 8:8d2f71a08a31 35
Swabey89 9:e22fd5795831 36 network_tout.attach(network_toutISR,TOUT_TIME_DEF);
Swabey89 8:8d2f71a08a31 37
Swabey89 9:e22fd5795831 38 if(logging)
Swabey89 9:e22fd5795831 39 {
Swabey89 10:b8214cc88e85 40 /*
Swabey89 9:e22fd5795831 41 printlock.lock();
Swabey89 9:e22fd5795831 42 printf("Incoming connection accepted on %s:%d\r\n", clt_addr.get_ip_address(), clt_addr.get_port());
Swabey89 9:e22fd5795831 43 printlock.unlock();
Swabey89 10:b8214cc88e85 44 */
Swabey89 10:b8214cc88e85 45 printQueue.call(printf,"Incoming connection accepted on %s:%d\r\n", clt_addr.get_ip_address(), clt_addr.get_port());
Swabey89 9:e22fd5795831 46 }
Swabey89 3:71f9a18a216d 47
Swabey89 3:71f9a18a216d 48 //Uses a C++ string to make it easier to concatinate
Swabey89 6:31a5f28336e8 49 {
Swabey89 6:31a5f28336e8 50 string response;
Swabey89 6:31a5f28336e8 51 char temp_str[10];
Swabey89 6:31a5f28336e8 52 char press_str[10];
Swabey89 6:31a5f28336e8 53 char light_str[10];
Swabey89 6:31a5f28336e8 54
Swabey89 6:31a5f28336e8 55 bufferLock.lock();
Swabey89 8:8d2f71a08a31 56 double temp = buffer[newestIndex].gettemp();
Swabey89 8:8d2f71a08a31 57 double press = buffer[newestIndex].getpress();
Swabey89 8:8d2f71a08a31 58 float light = buffer[newestIndex].getlight()*100;
Swabey89 8:8d2f71a08a31 59 string time_str = buffer[newestIndex].getTime();
Swabey89 6:31a5f28336e8 60 bufferLock.unlock();
Swabey89 6:31a5f28336e8 61
Swabey89 6:31a5f28336e8 62 //Convert to a C String
Swabey89 10:b8214cc88e85 63 //printlock.lock(); //necessary?
Swabey89 6:31a5f28336e8 64 sprintf(temp_str, "%5.2f", temp);
Swabey89 6:31a5f28336e8 65 sprintf(press_str, "%5.2f", press);
Swabey89 6:31a5f28336e8 66 sprintf(light_str, "%5.2f", light);
Swabey89 6:31a5f28336e8 67
Swabey89 6:31a5f28336e8 68
Swabey89 6:31a5f28336e8 69 //Build the C++ string response
Swabey89 6:31a5f28336e8 70 response = HTTP_RESPONSE;
Swabey89 6:31a5f28336e8 71
Swabey89 6:31a5f28336e8 72 response += HTTP_MESSAGE_TIME;
Swabey89 6:31a5f28336e8 73 response += time_str;
Swabey89 6:31a5f28336e8 74
Swabey89 6:31a5f28336e8 75 response += HTTP_MESSAGE_TEMP;
Swabey89 6:31a5f28336e8 76 response += temp_str;
Swabey89 6:31a5f28336e8 77
Swabey89 6:31a5f28336e8 78 response += HTTP_MESSAGE_PRESSURE;
Swabey89 6:31a5f28336e8 79 response += press_str;
Swabey89 6:31a5f28336e8 80
Swabey89 6:31a5f28336e8 81 response += HTTP_MESSAGE_LIGHT;
Swabey89 6:31a5f28336e8 82 response += light_str;
Swabey89 6:31a5f28336e8 83
Swabey89 6:31a5f28336e8 84 response += HTTP_MESSAGE_FOOT;
Swabey89 6:31a5f28336e8 85
Swabey89 6:31a5f28336e8 86 response += "\r\n";
Swabey89 10:b8214cc88e85 87 //printlock.unlock();
Swabey89 6:31a5f28336e8 88
Swabey89 6:31a5f28336e8 89 //Send static HTML response (as a C string)
Swabey89 9:e22fd5795831 90 clt_sock.send(response.c_str(), response.size());
Swabey89 9:e22fd5795831 91
Swabey89 9:e22fd5795831 92 if(logging)
Swabey89 9:e22fd5795831 93 {
Swabey89 10:b8214cc88e85 94 /*
Swabey89 9:e22fd5795831 95 printlock.lock();
Swabey89 9:e22fd5795831 96 pc->printf("Network thread responded with time: %s, temperature: %s, pressure:%s and light level: %s\r\n\n",time_str, temp_str, press_str, light_str);
Swabey89 10:b8214cc88e85 97 printlock.unlock();
Swabey89 10:b8214cc88e85 98 */
Swabey89 10:b8214cc88e85 99 printQueue.call(printf, "Network thread responded with time: %s, temperature: %s, pressure:%s and light level: %s\r\n\n",time_str, temp_str, press_str, light_str);
Swabey89 9:e22fd5795831 100 }
Swabey89 8:8d2f71a08a31 101 }
Swabey89 8:8d2f71a08a31 102 network_tout.detach();
Swabey89 3:71f9a18a216d 103 }
Swabey89 3:71f9a18a216d 104 }