MQTTInterface.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include "MQTTInterface.h"
  2. //#include "stm32f4xx_hal.h"
  3. #include "stm32h7xx_hal.h"
  4. #include <string.h>
  5. #include "lwip.h"
  6. #include "lwip/api.h"
  7. #include "lwip/sockets.h"
  8. #define MQTT_PORT 1883
  9. #define SERVER_IP1 192
  10. #define SERVER_IP2 168
  11. #define SERVER_IP3 1
  12. #define SERVER_IP4 34
  13. uint32_t MilliTimer;
  14. //Timer functions
  15. char TimerIsExpired(Timer *timer) {
  16. long left = timer->end_time - MilliTimer;
  17. return (left < 0);
  18. }
  19. void TimerCountdownMS(Timer *timer, unsigned int timeout) {
  20. timer->end_time = MilliTimer + timeout;
  21. }
  22. void TimerCountdown(Timer *timer, unsigned int timeout) {
  23. timer->end_time = MilliTimer + (timeout * 1000);
  24. }
  25. int TimerLeftMS(Timer *timer) {
  26. long left = timer->end_time - MilliTimer;
  27. return (left < 0) ? 0 : left;
  28. }
  29. void TimerInit(Timer *timer) {
  30. timer->end_time = 0;
  31. }
  32. #ifdef MQTT_LWIP_SOCKET
  33. void NewNetwork(Network *n) {
  34. n->socket = 0; //clear
  35. n->mqttread = net_read; //receive function
  36. n->mqttwrite = net_write; //send function
  37. n->disconnect = net_disconnect; //disconnection function
  38. }
  39. int ConnectNetwork(Network *n, char *ip, int port) {
  40. struct sockaddr_in server_addr;
  41. if(n->socket)
  42. {
  43. close(n->socket);
  44. }
  45. n->socket = socket(PF_INET, SOCK_STREAM, 0); //create socket
  46. if(n->socket < 0)
  47. {
  48. n->socket = 0;
  49. return -1;
  50. }
  51. memset(&server_addr, 0, sizeof(struct sockaddr_in)); //broker address info
  52. server_addr.sin_family = AF_INET;
  53. server_addr.sin_addr.s_addr = inet_addr(ip);
  54. server_addr.sin_port = htons(port);
  55. if(connect(n->socket, (struct sockaddr*)&server_addr, sizeof(struct sockaddr_in)) < 0) //connect to the broker
  56. {
  57. close(n->socket);
  58. return -1;
  59. }
  60. return 0;
  61. }
  62. int net_read(Network *n, unsigned char *buffer, int len, int timeout_ms) {
  63. int available;
  64. /* !!! LWIP_SO_RCVBUF must be enabled !!! */
  65. if(ioctl(n->socket, FIONREAD, &available) < 0) return -1; //check receive buffer
  66. if(available > 0)
  67. {
  68. return recv(n->socket, buffer, len, 0);
  69. }
  70. return 0;
  71. }
  72. int net_write(Network *n, unsigned char *buffer, int len, int timeout_ms) {
  73. return send(n->socket, buffer, len, 0);
  74. }
  75. void net_disconnect(Network *n) {
  76. close(n->socket);
  77. n->socket = 0;
  78. }
  79. #elif defined(MQTT_LWIP_NETCONN)
  80. void NewNetwork(Network *n) {
  81. n->conn = NULL;
  82. n->buf = NULL;
  83. n->offset = 0;
  84. n->mqttread = net_read;
  85. n->mqttwrite = net_write;
  86. n->disconnect = net_disconnect;
  87. }
  88. int ConnectNetwork(Network *n, char *ip, int port) {
  89. err_t err;
  90. ip_addr_t server_ip ;
  91. ipaddr_aton(ip, &server_ip);
  92. n->conn = netconn_new(NETCONN_TCP);
  93. if (n->conn != NULL) {
  94. err = netconn_connect(n->conn, &server_ip, port);
  95. if (err != ERR_OK) {
  96. netconn_delete(n->conn); //free memory
  97. return -1;
  98. }
  99. n->conn->recv_timeout = 1000;
  100. }
  101. return 0;
  102. }
  103. int net_read(Network *n, unsigned char *buffer, int len, int timeout_ms) {
  104. int rc;
  105. struct netbuf *inbuf;
  106. int offset = 0;
  107. int bytes = 0;
  108. while(bytes < len) {
  109. if(n->buf != NULL) {
  110. inbuf = n->buf;
  111. offset = n->offset;
  112. rc = ERR_OK;
  113. } else {
  114. rc = netconn_recv(n->conn, &inbuf);
  115. offset = 0;
  116. }
  117. if(rc != ERR_OK) {
  118. if(rc != ERR_TIMEOUT) {
  119. bytes = -1;
  120. }
  121. break;
  122. } else {
  123. int nblen = netbuf_len(inbuf) - offset;
  124. if((bytes+nblen) > len) {
  125. netbuf_copy_partial(inbuf, buffer+bytes, len-bytes,offset);
  126. n->buf = inbuf;
  127. n->offset = offset + len - bytes;
  128. bytes = len;
  129. } else {
  130. netbuf_copy_partial(inbuf, buffer+bytes, nblen, offset);
  131. bytes += nblen;
  132. netbuf_delete(inbuf);
  133. n->buf = NULL;
  134. n->offset = 0;
  135. }
  136. }
  137. }
  138. return bytes;
  139. }
  140. int net_write(Network *n, unsigned char *buffer, int len, int timeout_ms) {
  141. int rc = netconn_write(n->conn, buffer, len, NETCONN_NOCOPY);
  142. if(rc != ERR_OK) return -1;
  143. return len;
  144. }
  145. void net_disconnect(Network *n) {
  146. netconn_close(n->conn); //close session
  147. netconn_delete(n->conn); //free memory
  148. n->conn = NULL;
  149. }
  150. #endif