mqtt_interface.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //*****************************************************************************
  2. //! \file mqtt_interface.c
  3. //! \brief Paho MQTT to WIZnet Chip interface implement file.
  4. //! \details The process of porting an interface to use paho MQTT.
  5. //! \version 1.0.0
  6. //! \date 2016/12/06
  7. //! \par Revision history
  8. //! <2016/12/06> 1st Release
  9. //!
  10. //! \author Peter Bang & Justin Kim
  11. //! \copyright
  12. //!
  13. //! Copyright (c) 2016, WIZnet Co., LTD.
  14. //! All rights reserved.
  15. //!
  16. //! Redistribution and use in source and binary forms, with or without
  17. //! modification, are permitted provided that the following conditions
  18. //! are met:
  19. //!
  20. //! * Redistributions of source code must retain the above copyright
  21. //! notice, this list of conditions and the following disclaimer.
  22. //! * Redistributions in binary form must reproduce the above copyright
  23. //! notice, this list of conditions and the following disclaimer in the
  24. //! documentation and/or other materials provided with the distribution.
  25. //! * Neither the name of the <ORGANIZATION> nor the names of its
  26. //! contributors may be used to endorse or promote products derived
  27. //! from this software without specific prior written permission.
  28. //!
  29. //! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  30. //! AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  31. //! IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  32. //! ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  33. //! LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  34. //! CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  35. //! SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  36. //! INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  37. //! CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  38. //! ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  39. //! THE POSSIBILITY OF SUCH DAMAGE.
  40. //
  41. //*****************************************************************************
  42. #include "mqtt_interface.h"
  43. #include "wizchip_conf.h"
  44. #include "socket.h"
  45. unsigned long MilliTimer;
  46. /*
  47. * @brief MQTT MilliTimer handler
  48. * @note MUST BE register to your system 1m Tick timer handler.
  49. */
  50. void MilliTimer_Handler(void) {
  51. MilliTimer++;
  52. }
  53. /*
  54. * @brief Timer Initialize
  55. * @param timer : pointer to a Timer structure
  56. * that contains the configuration information for the Timer.
  57. */
  58. void TimerInit(Timer* timer) {
  59. timer->end_time = 0;
  60. }
  61. /*
  62. * @brief expired Timer
  63. * @param timer : pointer to a Timer structure
  64. * that contains the configuration information for the Timer.
  65. */
  66. char TimerIsExpired(Timer* timer) {
  67. long left = timer->end_time - MilliTimer;
  68. return (left < 0);
  69. }
  70. /*
  71. * @brief Countdown millisecond Timer
  72. * @param timer : pointer to a Timer structure
  73. * that contains the configuration information for the Timer.
  74. * timeout : setting timeout millisecond.
  75. */
  76. void TimerCountdownMS(Timer* timer, unsigned int timeout) {
  77. timer->end_time = MilliTimer + timeout;
  78. }
  79. /*
  80. * @brief Countdown second Timer
  81. * @param timer : pointer to a Timer structure
  82. * that contains the configuration information for the Timer.
  83. * timeout : setting timeout millisecond.
  84. */
  85. void TimerCountdown(Timer* timer, unsigned int timeout) {
  86. timer->end_time = MilliTimer + (timeout * 1000);
  87. }
  88. /*
  89. * @brief left millisecond Timer
  90. * @param timer : pointer to a Timer structure
  91. * that contains the configuration information for the Timer.
  92. */
  93. int TimerLeftMS(Timer* timer) {
  94. long left = timer->end_time - MilliTimer;
  95. return (left < 0) ? 0 : left;
  96. }
  97. /*
  98. * @brief New network setting
  99. * @param n : pointer to a Network structure
  100. * that contains the configuration information for the Network.
  101. * sn : socket number where x can be (0..7).
  102. * @retval None
  103. */
  104. void NewNetwork(Network* n, int sn) {
  105. n->my_socket = sn;
  106. n->mqttread = w5x00_read;
  107. n->mqttwrite = w5x00_write;
  108. n->disconnect = w5x00_disconnect;
  109. }
  110. /*
  111. * @brief read function
  112. * @param n : pointer to a Network structure
  113. * that contains the configuration information for the Network.
  114. * buffer : pointer to a read buffer.
  115. * len : buffer length.
  116. * @retval received data length or SOCKERR code
  117. */
  118. int w5x00_read(Network* n, unsigned char* buffer, int len, long time)
  119. {
  120. if((getSn_SR(n->my_socket) == SOCK_ESTABLISHED) && (getSn_RX_RSR(n->my_socket)>0))
  121. return recv(n->my_socket, buffer, len);
  122. return SOCK_ERROR;
  123. }
  124. /*
  125. * @brief write function
  126. * @param n : pointer to a Network structure
  127. * that contains the configuration information for the Network.
  128. * buffer : pointer to a read buffer.
  129. * len : buffer length.
  130. * @retval length of data sent or SOCKERR code
  131. */
  132. int w5x00_write(Network* n, unsigned char* buffer, int len, long time)
  133. {
  134. if(getSn_SR(n->my_socket) == SOCK_ESTABLISHED)
  135. return send(n->my_socket, buffer, len);
  136. return SOCK_ERROR;
  137. }
  138. /*
  139. * @brief disconnect function
  140. * @param n : pointer to a Network structure
  141. * that contains the configuration information for the Network.
  142. */
  143. void w5x00_disconnect(Network* n)
  144. {
  145. disconnect(n->my_socket);
  146. }
  147. /*
  148. * @brief connect network function
  149. * @param n : pointer to a Network structure
  150. * that contains the configuration information for the Network.
  151. * ip : server iP.
  152. * port : server port.
  153. * @retval SOCKOK code or SOCKERR code
  154. */
  155. int ConnectNetwork(Network* n, uint8_t* ip, uint16_t port)
  156. {
  157. uint16_t myport = 12345;
  158. if(socket(n->my_socket, Sn_MR_TCP, myport, 0) != n->my_socket)
  159. return SOCK_ERROR;
  160. if(connect(n->my_socket, ip, port) != SOCK_OK)
  161. return SOCK_ERROR;
  162. return SOCK_OK;
  163. }