server.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * server.cpp
  3. *
  4. * Created on: Jan 4, 2018
  5. * Author: Philipp Hinz
  6. * based on: https://gist.github.com/suyash/2488ff6996c98a8ee3a84fe3198a6f85
  7. */
  8. #include <arpa/inet.h>
  9. #include <netinet/in.h>
  10. #include <stdbool.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <unistd.h>
  15. #include <pthread.h>
  16. #include "server.h"
  17. #include "logger.h"
  18. #include "hal.h"
  19. #include "coffee.h"
  20. #include "stripe.h"
  21. /**
  22. * TCP Uses 2 types of sockets, the connection socket and the listen socket.
  23. * The Goal is to separate the connection phase from the data exchange phase.
  24. * */
  25. int listen_sock;
  26. int sock;
  27. char buffer_ok[] = "OK\n";
  28. /**
  29. * Just sends an ACK message back
  30. */
  31. void sendACK() {
  32. send(sock, buffer_ok, strlen(buffer_ok), 0);
  33. }
  34. /**
  35. * Main thread to handle networking stuff
  36. * @param *threadid Thread ID
  37. */
  38. void* serverThread(void* threadid) {
  39. sleep(1); // Wait for other components to initialize
  40. // socket address used for the server
  41. struct sockaddr_in server_address;
  42. memset(&server_address, 0, sizeof(server_address));
  43. server_address.sin_family = AF_INET;
  44. // htons: host to network short: transforms a value in host byte
  45. // ordering format to a short value in network byte ordering format
  46. server_address.sin_port = htons(SERVER_PORT);
  47. // htonl: host to network long: same as htons but to long
  48. server_address.sin_addr.s_addr = htonl(INADDR_ANY);
  49. // create a TCP socket, creation returns -1 on failure
  50. if ((listen_sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
  51. logger_error("could not create listen socket\n");
  52. pthread_exit(0);
  53. }
  54. // bind it to listen to the incoming connections on the created server
  55. // address, will return -1 on error
  56. if ((bind(listen_sock, (struct sockaddr *) &server_address,
  57. sizeof(server_address))) < 0) {
  58. logger_error("could not bind socket\n");
  59. pthread_exit(0);
  60. }
  61. int wait_size = 16; // maximum number of waiting clients, after which
  62. // dropping begins
  63. if (listen(listen_sock, wait_size) < 0) {
  64. logger_error("could not open socket for listening\n");
  65. pthread_exit(0);
  66. }
  67. logger(V_BASIC, "Now listening for commands on port %d\n", SERVER_PORT);
  68. // socket address used to store client address
  69. struct sockaddr_in client_address;
  70. unsigned int client_address_len = 0;
  71. // run indefinitely
  72. while (true) {
  73. // open a new socket to transmit data per connection
  74. if ((sock = accept(listen_sock, (struct sockaddr *) &client_address,
  75. &client_address_len)) < 0) {
  76. logger_error("could not open a socket to accept data\n");
  77. pthread_exit(0);
  78. }
  79. int n = 0;
  80. int len = 0, maxlen = 100, lastlen = 0;
  81. char buffer[maxlen];
  82. char cmd[maxlen];
  83. char *pcmd = cmd;
  84. char *pbuffer = buffer;
  85. strncpy(buffer, "", maxlen);
  86. strncpy(cmd, "", maxlen);
  87. logger(V_HAL, "client connected with ip address: %s\n",
  88. inet_ntoa(client_address.sin_addr));
  89. // keep running as long as the client keeps the connection open
  90. while ((n = recv(sock, pbuffer, maxlen, 0)) > 0) {
  91. pbuffer += n;
  92. maxlen -= n;
  93. len += n;
  94. strncpy(cmd, buffer, len - 1);
  95. cmd[len - 1] = '\0';
  96. pcmd = &cmd[lastlen];
  97. logger(V_SPI, "received %d bytes: '%s' (cmd: %s)\n", len, buffer,
  98. pcmd);
  99. if (!strcmp(SERVER_CMD_ON, pcmd)) {
  100. if (getState() == STATE_OFF) {
  101. halSendSignal(SigInt0Rls);
  102. sendACK();
  103. }
  104. } else if (!strcmp(SERVER_CMD_OFF, pcmd)) {
  105. if (getState() != STATE_OFF) {
  106. halSendSignal(SigInt1RlsLong);
  107. sendACK();
  108. }
  109. } else if (!strcmp(SERVER_CMD_BREW, pcmd)) {
  110. if (getState() == STATE_IDLE || getState() == STATE_HEATING) {
  111. halSendSignal(SigInt1Rls);
  112. sendACK();
  113. }
  114. } else if (!strcmp(SERVER_CMD_STATUS, pcmd)) {
  115. char tmp[] = "0\n";
  116. tmp[0] += getState();
  117. send(sock, tmp, sizeof(tmp), 0);
  118. } else if (!strcmp(SERVER_CMD_STRIPEON, pcmd)) {
  119. stripeOn();
  120. sendACK();
  121. } else if (!strcmp(SERVER_CMD_STRIPEOFF, pcmd)) {
  122. stripeOff();
  123. sendACK();
  124. }
  125. lastlen = len;
  126. // echo received content back
  127. //send(sock, buffer, len, 0);
  128. }
  129. close(sock);
  130. }
  131. close(listen_sock);
  132. pthread_exit(EXIT_SUCCESS);
  133. }
  134. /**
  135. * Closes all active connections
  136. */
  137. void serverTerminate() {
  138. close(sock);
  139. close(listen_sock);
  140. }