coap_transport_ipv6.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /**
  2. * Copyright (c) 2014 - 2019, 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 "sdk_errors.h"
  41. #include "sdk_config.h"
  42. #include "iot_common.h"
  43. #include "iot_pbuffer.h"
  44. #include "coap_transport.h"
  45. #include "coap.h"
  46. #include "udp_api.h"
  47. /**@brief UDP port information. */
  48. typedef struct
  49. {
  50. uint32_t socket_id; /**< Socket information provided by UDP. */
  51. uint16_t port_number; /**< Associated port number. */
  52. } udp_port_t;
  53. static udp_port_t m_port_table[COAP_PORT_COUNT]; /**< Table maintaining association between CoAP ports and corresponding UDP socket identifiers. */
  54. /**@brief Callback handler to receive data on the UDP port.
  55. *
  56. * @details Callback handler to receive data on the UDP port.
  57. *
  58. * @param[in] p_socket Socket identifier.
  59. * @param[in] p_ip_header IPv6 header containing source and destination addresses.
  60. * @param[in] p_udp_header UDP header identifying local and remote endpoints.
  61. * @param[in] process_result Result of data reception, there could be possible errors like
  62. * invalid checksum etc.
  63. * @param[in] iot_pbuffer_t Packet buffer containing the received data packet.
  64. *
  65. * @retval NRF_SUCCESS Indicates received data was handled successfully, else an an
  66. * error code indicating reason for failure..
  67. */
  68. static uint32_t port_data_callback(const udp6_socket_t * p_socket,
  69. const ipv6_header_t * p_ip_header,
  70. const udp6_header_t * p_udp_header,
  71. uint32_t process_result,
  72. iot_pbuffer_t * p_rx_packet)
  73. {
  74. uint32_t index;
  75. uint32_t retval = NRF_ERROR_NOT_FOUND;
  76. //Search for the port.
  77. for (index = 0; index < COAP_PORT_COUNT; index++)
  78. {
  79. //Matching port found.
  80. if (m_port_table[index].socket_id == p_socket->socket_id)
  81. {
  82. coap_remote_t remote_endpoint;
  83. coap_remote_t local_endpoint;
  84. const coap_port_t port = {p_udp_header->destport};
  85. memcpy (remote_endpoint.addr, p_ip_header->srcaddr.u8, IPV6_ADDR_SIZE);
  86. remote_endpoint.port_number = p_udp_header->srcport;
  87. memcpy (local_endpoint.addr, p_ip_header->destaddr.u8, IPV6_ADDR_SIZE);
  88. local_endpoint.port_number = p_udp_header->destport;
  89. COAP_MUTEX_LOCK();
  90. //Notify the module of received data.
  91. retval = coap_transport_read(&port,
  92. &remote_endpoint,
  93. &local_endpoint,
  94. process_result,
  95. p_rx_packet->p_payload,
  96. p_rx_packet->length);
  97. COAP_MUTEX_UNLOCK();
  98. }
  99. }
  100. return retval;
  101. }
  102. /**@brief Creates port as requested in p_port.
  103. *
  104. * @details Creates port as requested in p_port.
  105. *
  106. * @param[in] index Index to the m_port_table where entry of the port created is to be made.
  107. * @param[in] p_port Port information to be created.
  108. *
  109. * @retval NRF_SUCCESS Indicates if port was created successfully, else an an error code
  110. * indicating reason for failure.
  111. */
  112. static uint32_t port_create(uint32_t index, coap_port_t * p_port)
  113. {
  114. uint32_t err_code;
  115. udp6_socket_t socket;
  116. //Request new socket creation.
  117. err_code = udp6_socket_allocate(&socket);
  118. if (err_code == NRF_SUCCESS)
  119. {
  120. // Bind the socket to the local port.
  121. err_code = udp6_socket_bind(&socket, IPV6_ADDR_ANY, p_port->port_number);
  122. if (err_code == NRF_SUCCESS)
  123. {
  124. //Register data receive callback.
  125. err_code = udp6_socket_recv(&socket, port_data_callback);
  126. if (err_code == NRF_SUCCESS)
  127. {
  128. //All procedure with respect to port creation succeeded, make entry in the table.
  129. m_port_table[index].socket_id = socket.socket_id;
  130. m_port_table[index].port_number = p_port->port_number;
  131. socket.p_app_data = &m_port_table[index];
  132. UNUSED_VARIABLE(udp6_socket_app_data_set(&socket));
  133. }
  134. }
  135. if (err_code != NRF_SUCCESS)
  136. {
  137. //Not all procedures succeeded with allocated socket, hence free it.
  138. UNUSED_VARIABLE(udp6_socket_free(&socket));
  139. }
  140. }
  141. return err_code;
  142. }
  143. uint32_t coap_transport_init(const coap_transport_init_t * p_param)
  144. {
  145. uint32_t err_code = NRF_ERROR_NO_MEM;
  146. uint32_t index;
  147. NULL_PARAM_CHECK(p_param);
  148. NULL_PARAM_CHECK(p_param->p_port_table);
  149. for (index = 0; index < COAP_PORT_COUNT; index++)
  150. {
  151. // Create end point for each of the CoAP ports.
  152. err_code = port_create(index, &p_param->p_port_table[index]);
  153. if (err_code != NRF_SUCCESS)
  154. {
  155. break;
  156. }
  157. }
  158. return err_code;
  159. }
  160. uint32_t coap_transport_write(const coap_port_t * p_port,
  161. const coap_remote_t * p_remote,
  162. const uint8_t * p_data,
  163. uint16_t datalen)
  164. {
  165. uint32_t err_code = NRF_ERROR_NOT_FOUND;
  166. uint32_t index;
  167. udp6_socket_t socket;
  168. ipv6_addr_t remote_addr;
  169. iot_pbuffer_t * p_buffer;
  170. iot_pbuffer_alloc_param_t buffer_param;
  171. NULL_PARAM_CHECK(p_port);
  172. NULL_PARAM_CHECK(p_remote);
  173. NULL_PARAM_CHECK(p_data);
  174. memcpy(remote_addr.u8, p_remote->addr, 16);
  175. buffer_param.type = UDP6_PACKET_TYPE;
  176. buffer_param.flags = PBUFFER_FLAG_DEFAULT;
  177. buffer_param.length = datalen;
  178. //Search for the corresponding port.
  179. for (index = 0; index < COAP_PORT_COUNT; index ++)
  180. {
  181. if (m_port_table[index].port_number == p_port->port_number)
  182. {
  183. //Allocate buffer to send the data on port.
  184. err_code = iot_pbuffer_allocate(&buffer_param, &p_buffer);
  185. if (err_code == NRF_SUCCESS)
  186. {
  187. socket.socket_id = m_port_table[index].socket_id;
  188. //Make a copy of the data onto the buffer.
  189. memcpy (p_buffer->p_payload, p_data, datalen);
  190. COAP_MUTEX_UNLOCK();
  191. //Send on UDP port.
  192. err_code = udp6_socket_sendto(&socket,
  193. &remote_addr,
  194. p_remote->port_number,
  195. p_buffer);
  196. COAP_MUTEX_LOCK();
  197. if (err_code != NRF_SUCCESS)
  198. {
  199. //Free the allocated buffer as send procedure has failed.
  200. UNUSED_VARIABLE(iot_pbuffer_free(p_buffer, true));
  201. }
  202. }
  203. break;
  204. }
  205. }
  206. return err_code;
  207. }
  208. void coap_transport_process(void)
  209. {
  210. return;
  211. }
  212. uint32_t coap_security_setup(uint16_t local_port,
  213. nrf_tls_role_t role,
  214. coap_remote_t * const p_remote,
  215. nrf_tls_key_settings_t * const p_settings)
  216. {
  217. return NRF_ERROR_API_NOT_IMPLEMENTED;
  218. }
  219. uint32_t coap_security_destroy(uint16_t local_port,
  220. coap_remote_t * const p_remote)
  221. {
  222. return NRF_ERROR_API_NOT_IMPLEMENTED;
  223. }