coap_transport_lwip.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /**
  2. * Copyright (c) 2014 - 2018, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include <stdint.h>
  41. #include "sdk_errors.h"
  42. #include "sdk_config.h"
  43. #include "iot_common.h"
  44. #include "coap_transport.h"
  45. #include "coap.h"
  46. #include "lwip/ip6_addr.h"
  47. /*lint -save -e607 Suppress warning 607 "Parameter p of macro found within string" */
  48. #include "lwip/udp.h"
  49. /*lint -restore */
  50. /**@brief UDP port information. */
  51. typedef struct
  52. {
  53. struct udp_pcb * p_socket; /**< Socket information provided by UDP. */
  54. uint16_t port_number; /**< Associated port number. */
  55. }udp_port_t;
  56. static udp_port_t m_port_table[COAP_PORT_COUNT]; /**< Table maintaining association between CoAP ports and corresponding UDP socket identifiers. */
  57. /**@brief Callback handler to receive data on the UDP port.
  58. *
  59. * @details Callback handler to receive data on the UDP port.
  60. *
  61. * @param[in] p_arg Receive argument associated with the UDP socket.
  62. * @param[in] p_socket Socket identifier.
  63. * @param[in] p_ip_header IPv6 header containing source and destination addresses.
  64. * @param[in] p_remote_addr IPv6 address of the remote device.
  65. * @param[in] port Port number identifying the remote endpoint.
  66. *
  67. * @retval NRF_SUCCESS Indicates received data was handled successfully, else an an
  68. * error code indicating reason for failure.
  69. */
  70. static void udp_recv_data_handler(void * p_arg,
  71. struct udp_pcb * p_socket,
  72. struct pbuf * p_buffer,
  73. const ip6_addr_t * p_remote_addr,
  74. u16_t port)
  75. {
  76. uint32_t index;
  77. coap_remote_t remote_endpoint;
  78. coap_port_t local_port = {p_socket->local_port};
  79. for (index = 0; index < COAP_PORT_COUNT; index++)
  80. {
  81. if (m_port_table[index].p_socket == p_socket)
  82. {
  83. memcpy (remote_endpoint.addr, p_remote_addr, 16);
  84. remote_endpoint.port_number = port;
  85. COAP_MUTEX_LOCK();
  86. UNUSED_VARIABLE(coap_transport_read(&local_port,
  87. &remote_endpoint,
  88. NULL,
  89. NRF_SUCCESS,
  90. (uint8_t *)p_buffer->payload,
  91. (uint32_t)p_buffer->len));
  92. COAP_MUTEX_UNLOCK();
  93. break;
  94. }
  95. }
  96. //Freeing packet (irrespective of matching p_socket is found or not
  97. //to avoid memory leaks in the system.
  98. UNUSED_VARIABLE(pbuf_free(p_buffer));
  99. }
  100. /**@brief Creates port as requested in p_port.
  101. *
  102. * @details Creates port as requested in p_port.
  103. *
  104. * @param[in] index Index to the m_port_table where entry of the port created is to be made.
  105. * @param[in] p_port Port information to be created.
  106. *
  107. * @retval NRF_SUCCESS Indicates if port was created successfully, else an an error code
  108. * indicating reason for failure.
  109. */
  110. static uint32_t port_create(uint32_t index, coap_port_t * p_port)
  111. {
  112. err_t err = NRF_ERROR_NO_MEM;
  113. ip6_addr_t any_addr;
  114. struct udp_pcb * p_socket = m_port_table[index].p_socket;
  115. ip6_addr_set_any(&any_addr);
  116. //Request new socket creation.
  117. p_socket = udp_new();
  118. if (NULL != p_socket)
  119. {
  120. // Bind the socket to the local port.
  121. err = udp_bind(p_socket, &any_addr, p_port->port_number);
  122. if (err == ERR_OK)
  123. {
  124. //Register data receive callback.
  125. udp_recv(p_socket, udp_recv_data_handler, &m_port_table[index]);
  126. //All procedure with respect to port creation succeeded, make entry in the table.
  127. m_port_table[index].port_number = p_port->port_number;
  128. m_port_table[index].p_socket = p_socket;
  129. }
  130. else
  131. {
  132. //Not all procedures succeeded with allocated socket, hence free it.
  133. err = NRF_ERROR_INVALID_PARAM;
  134. udp_remove(p_socket);
  135. }
  136. }
  137. return err;
  138. }
  139. uint32_t coap_transport_init(const coap_transport_init_t * p_param)
  140. {
  141. uint32_t err_code = NRF_SUCCESS;
  142. uint32_t index;
  143. NULL_PARAM_CHECK(p_param);
  144. NULL_PARAM_CHECK(p_param->p_port_table);
  145. for (index = 0; index < COAP_PORT_COUNT; index++)
  146. {
  147. // Create end point for each of the COAP ports.
  148. err_code = port_create(index, &p_param->p_port_table[index]);
  149. if (err_code != NRF_SUCCESS)
  150. {
  151. break;
  152. }
  153. }
  154. return err_code;
  155. }
  156. uint32_t coap_transport_write(const coap_port_t * p_port,
  157. const coap_remote_t * p_remote,
  158. const uint8_t * p_data,
  159. uint16_t datalen)
  160. {
  161. err_t err = NRF_ERROR_NOT_FOUND;
  162. uint32_t index;
  163. NULL_PARAM_CHECK(p_port);
  164. NULL_PARAM_CHECK(p_remote);
  165. NULL_PARAM_CHECK(p_data);
  166. //Search for the corresponding port.
  167. for (index = 0; index < COAP_PORT_COUNT; index++)
  168. {
  169. if (m_port_table[index].port_number == p_port->port_number)
  170. {
  171. //Allocate Buffer to send the data on port.
  172. struct pbuf * lwip_buffer = pbuf_alloc(PBUF_TRANSPORT, datalen, PBUF_RAM);
  173. if (NULL != lwip_buffer)
  174. {
  175. //Make a copy of the data onto the buffer.
  176. memcpy(lwip_buffer->payload, p_data, datalen);
  177. COAP_MUTEX_UNLOCK();
  178. //Send on UDP port.
  179. err = udp_sendto(m_port_table[index].p_socket,
  180. lwip_buffer,
  181. (ip6_addr_t *)p_remote->addr,
  182. p_remote->port_number);
  183. COAP_MUTEX_LOCK();
  184. if (err != ERR_OK)
  185. {
  186. //Free the allocated buffer as send procedure has failed.
  187. err = NRF_ERROR_INTERNAL;
  188. }
  189. UNUSED_VARIABLE(pbuf_free(lwip_buffer));
  190. }
  191. else
  192. {
  193. //Buffer allocation failed, cannot send data.
  194. err = NRF_ERROR_NO_MEM;
  195. }
  196. break;
  197. }
  198. }
  199. return err;
  200. }
  201. void coap_transport_process(void)
  202. {
  203. return;
  204. }
  205. uint32_t coap_security_setup(uint16_t local_port,
  206. nrf_tls_role_t role,
  207. coap_remote_t * const p_remote,
  208. nrf_tls_key_settings_t * const p_settings)
  209. {
  210. return NRF_ERROR_API_NOT_IMPLEMENTED;
  211. }
  212. uint32_t coap_security_destroy(uint16_t local_port,
  213. coap_remote_t * const p_remote)
  214. {
  215. return NRF_ERROR_API_NOT_IMPLEMENTED;
  216. }