Smart-Home am Beispiel der Präsenzerkennung im Raum Projektarbeit Lennart Heimbs, Johannes Krug, Sebastian Dohle und Kevin Holzschuh bei Prof. Oliver Hofmann SS2019
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EthernetServer.cpp 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * The MySensors Arduino library handles the wireless radio link and protocol
  3. * between your home built sensors/actuators and HA controller of choice.
  4. * The sensors forms a self healing radio network with optional repeaters. Each
  5. * repeater and gateway builds a routing tables in EEPROM which keeps track of the
  6. * network topology allowing messages to be routed to nodes.
  7. *
  8. * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
  9. * Copyright (C) 2013-2018 Sensnology AB
  10. * Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
  11. *
  12. * Documentation: http://www.mysensors.org
  13. * Support Forum: http://forum.mysensors.org
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * version 2 as published by the Free Software Foundation.
  18. *
  19. * Based on Arduino ethernet library, Copyright (c) 2010 Arduino LLC. All right reserved.
  20. */
  21. #include "EthernetServer.h"
  22. #include <cstdio>
  23. #include <sys/socket.h>
  24. #include <cstring>
  25. #include <sys/socket.h>
  26. #include <netdb.h>
  27. #include <arpa/inet.h>
  28. #include <unistd.h>
  29. #include <errno.h>
  30. #include <fcntl.h>
  31. #include "log.h"
  32. #include "EthernetClient.h"
  33. EthernetServer::EthernetServer(uint16_t port, uint16_t max_clients) : port(port),
  34. max_clients(max_clients), sockfd(-1)
  35. {
  36. clients.reserve(max_clients);
  37. }
  38. void EthernetServer::begin()
  39. {
  40. begin(IPAddress(0,0,0,0));
  41. }
  42. void EthernetServer::begin(IPAddress address)
  43. {
  44. struct addrinfo hints, *servinfo, *p;
  45. int yes=1;
  46. int rv;
  47. char ipstr[INET_ADDRSTRLEN];
  48. char portstr[6];
  49. if (sockfd != -1) {
  50. close(sockfd);
  51. sockfd = -1;
  52. }
  53. memset(&hints, 0, sizeof hints);
  54. hints.ai_family = AF_UNSPEC;
  55. hints.ai_socktype = SOCK_STREAM;
  56. hints.ai_flags = AI_PASSIVE;
  57. sprintf(portstr, "%d", port);
  58. if ((rv = getaddrinfo(address.toString().c_str(), portstr, &hints, &servinfo)) != 0) {
  59. logError("getaddrinfo: %s\n", gai_strerror(rv));
  60. return;
  61. }
  62. // loop through all the results and bind to the first we can
  63. for (p = servinfo; p != NULL; p = p->ai_next) {
  64. if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
  65. logError("socket: %s\n", strerror(errno));
  66. continue;
  67. }
  68. if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
  69. logError("setsockopt: %s\n", strerror(errno));
  70. freeaddrinfo(servinfo);
  71. return;
  72. }
  73. if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
  74. close(sockfd);
  75. logError("bind: %s\n", strerror(errno));
  76. continue;
  77. }
  78. break;
  79. }
  80. if (p == NULL) {
  81. logError("Failed to bind!\n");
  82. freeaddrinfo(servinfo);
  83. return;
  84. }
  85. if (listen(sockfd, ETHERNETSERVER_BACKLOG) == -1) {
  86. logError("listen: %s\n", strerror(errno));
  87. freeaddrinfo(servinfo);
  88. return;
  89. }
  90. freeaddrinfo(servinfo);
  91. fcntl(sockfd, F_SETFL, O_NONBLOCK);
  92. struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
  93. void *addr = &(ipv4->sin_addr);
  94. inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
  95. logDebug("Listening for connections on %s:%s\n", ipstr, portstr);
  96. }
  97. bool EthernetServer::hasClient()
  98. {
  99. // Check if any client has disconnected
  100. for (size_t i = 0; i < clients.size(); ++i) {
  101. EthernetClient client(clients[i]);
  102. if (!client.connected()) {
  103. // Checks if this disconnected client is also on the new clients list
  104. for (std::list<int>::iterator it = new_clients.begin(); it != new_clients.end(); ++it) {
  105. if (*it == clients[i]) {
  106. new_clients.erase(it);
  107. break;
  108. }
  109. }
  110. client.stop();
  111. clients[i] = clients.back();
  112. clients.pop_back();
  113. logDebug("Ethernet client disconnected.\n");
  114. }
  115. }
  116. _accept();
  117. return !new_clients.empty();
  118. }
  119. EthernetClient EthernetServer::available()
  120. {
  121. if (new_clients.empty()) {
  122. return EthernetClient();
  123. } else {
  124. int sock = new_clients.front();
  125. new_clients.pop_front();
  126. return EthernetClient(sock);
  127. }
  128. }
  129. size_t EthernetServer::write(uint8_t b)
  130. {
  131. return write(&b, 1);
  132. }
  133. size_t EthernetServer::write(const uint8_t *buffer, size_t size)
  134. {
  135. size_t n = 0;
  136. for (size_t i = 0; i < clients.size(); ++i) {
  137. EthernetClient client(clients[i]);
  138. if (client.status() == ETHERNETCLIENT_W5100_ESTABLISHED) {
  139. n += client.write(buffer, size);
  140. }
  141. }
  142. return n;
  143. }
  144. size_t EthernetServer::write(const char *str)
  145. {
  146. if (str == NULL) {
  147. return 0;
  148. }
  149. return write((const uint8_t *)str, strlen(str));
  150. }
  151. size_t EthernetServer::write(const char *buffer, size_t size)
  152. {
  153. return write((const uint8_t *)buffer, size);
  154. }
  155. void EthernetServer::_accept()
  156. {
  157. int new_fd;
  158. socklen_t sin_size;
  159. struct sockaddr_storage client_addr;
  160. char ipstr[INET_ADDRSTRLEN];
  161. sin_size = sizeof client_addr;
  162. new_fd = accept(sockfd, (struct sockaddr *)&client_addr, &sin_size);
  163. if (new_fd == -1) {
  164. if (errno != EAGAIN && errno != EWOULDBLOCK) {
  165. logError("accept: %s\n", strerror(errno));
  166. }
  167. return;
  168. }
  169. if (clients.size() == max_clients) {
  170. // no free slots, search for a dead client
  171. close(new_fd);
  172. logDebug("Max number of ethernet clients reached.\n");
  173. return;
  174. }
  175. new_clients.push_back(new_fd);
  176. clients.push_back(new_fd);
  177. void *addr = &(((struct sockaddr_in*)&client_addr)->sin_addr);
  178. inet_ntop(client_addr.ss_family, addr, ipstr, sizeof ipstr);
  179. logDebug("New connection from %s\n", ipstr);
  180. }