config_medium.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /**
  2. * Copyright (c) 2016 - 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_config.h"
  41. #include "socket_api.h"
  42. #include "config_medium.h"
  43. #include "ipv6_medium.h"
  44. #include "iot_errors.h"
  45. #include "app_error.h"
  46. #include "socket_config.h"
  47. static ipv6_medium_instance_t m_ipv6_medium; /**< IPv6 medium instance. */
  48. eui64_t eui64_local_iid; /**< Local EUI64 value that is used as the IID for*/
  49. eui64_t * config_medium_local_iid(void)
  50. {
  51. return &eui64_local_iid;
  52. }
  53. void config_medium_start(void)
  54. {
  55. uint32_t err_code = ipv6_medium_connectable_mode_enter(m_ipv6_medium.ipv6_medium_instance_id);
  56. APP_ERROR_CHECK(err_code);
  57. }
  58. static void on_ipv6_medium_evt(ipv6_medium_evt_t * p_ipv6_medium_evt)
  59. {
  60. switch (p_ipv6_medium_evt->ipv6_medium_evt_id)
  61. {
  62. case IPV6_MEDIUM_EVT_CONN_UP:
  63. {
  64. // TODO: Signal
  65. break;
  66. }
  67. case IPV6_MEDIUM_EVT_CONN_DOWN:
  68. {
  69. config_medium_start();
  70. break;
  71. }
  72. default:
  73. {
  74. break;
  75. }
  76. }
  77. }
  78. static void on_ipv6_medium_error(ipv6_medium_error_t * p_ipv6_medium_error)
  79. {
  80. // Do something.
  81. }
  82. static uint32_t config_medium_init(ipv6_medium_init_params_t * p_medium_params,
  83. ipv6_medium_type_t medium_type)
  84. {
  85. uint32_t err_code = ipv6_medium_init(p_medium_params, medium_type, &m_ipv6_medium);
  86. if (err_code != NRF_SUCCESS)
  87. {
  88. return err_code;
  89. }
  90. eui48_t ipv6_medium_eui48;
  91. err_code = ipv6_medium_eui48_get(m_ipv6_medium.ipv6_medium_instance_id, &ipv6_medium_eui48);
  92. if (err_code != NRF_SUCCESS)
  93. {
  94. return err_code;
  95. }
  96. ipv6_medium_eui48.identifier[EUI_48_SIZE - 1] = 0x00;
  97. err_code = ipv6_medium_eui48_set(m_ipv6_medium.ipv6_medium_instance_id, &ipv6_medium_eui48);
  98. if (err_code != NRF_SUCCESS)
  99. {
  100. return err_code;
  101. }
  102. err_code = ipv6_medium_eui64_get(m_ipv6_medium.ipv6_medium_instance_id, &eui64_local_iid);
  103. return err_code;
  104. }
  105. uint32_t config_medium_init_default(void)
  106. {
  107. static ipv6_medium_init_params_t ipv6_medium_init_params;
  108. memset(&ipv6_medium_init_params, 0x00, sizeof(ipv6_medium_init_params));
  109. ipv6_medium_init_params.ipv6_medium_evt_handler = on_ipv6_medium_evt;
  110. ipv6_medium_init_params.ipv6_medium_error_handler = on_ipv6_medium_error;
  111. return config_medium_init(&ipv6_medium_init_params, IPV6_MEDIUM_ID_BLE);
  112. }
  113. uint32_t config_medium_setopt(int optname, const void * p_optarg, socklen_t optlen)
  114. {
  115. uint32_t err_code = SOCKET_INVALID_PARAM;
  116. if (optname == MEDIUM_INIT_PARAMS)
  117. {
  118. if (optlen == sizeof(config_medium_params_t))
  119. {
  120. const config_medium_params_t * p_cfg = (const config_medium_params_t *)p_optarg;
  121. ipv6_medium_init_params_t ipv6_medium_init_params;
  122. memset(&ipv6_medium_init_params, 0x00, sizeof(ipv6_medium_init_params));
  123. ipv6_medium_init_params.ipv6_medium_evt_handler = p_cfg->evt_handler;
  124. ipv6_medium_init_params.ipv6_medium_error_handler = p_cfg->error_handler;
  125. err_code = config_medium_init(&ipv6_medium_init_params, p_cfg->medium_type);
  126. }
  127. }
  128. return err_code;
  129. }
  130. uint32_t config_medium_getopt(int optname, void * p_optarg, socklen_t * p_optlen)
  131. {
  132. (void) optname;
  133. (void) p_optarg;
  134. (void) p_optlen;
  135. return SOCKET_INVALID_PARAM;
  136. }
  137. uint32_t config_socket_init(void)
  138. {
  139. uint32_t err_code = NRF_SUCCESS;
  140. #if SOCKET_AUTOINIT_ENABLE == 1
  141. err_code = config_medium_init_default();
  142. #endif
  143. return err_code;
  144. }
  145. void config_socket_start(void)
  146. {
  147. #if SOCKET_AUTOINIT_ENABLE == 1
  148. config_medium_start();
  149. #endif
  150. }
  151. static uint32_t config_socket_open(socket_t * p_socket)
  152. {
  153. (void) p_socket;
  154. return NRF_ERROR_NULL;
  155. }
  156. static uint32_t config_socket_close(socket_t * p_socket)
  157. {
  158. (void) p_socket;
  159. return NRF_ERROR_NULL;
  160. }
  161. uint32_t config_socket_setopt(socket_t * p_socket,
  162. int level,
  163. int optname,
  164. const void * p_optarg,
  165. socklen_t optlen)
  166. {
  167. uint32_t err_code = NRF_SUCCESS;
  168. switch (level)
  169. {
  170. case SOL_NRF_MEDIUM:
  171. err_code = config_medium_setopt(optname, p_optarg, optlen);
  172. break;
  173. default:
  174. err_code = SOCKET_INVALID_PARAM;
  175. break;
  176. }
  177. return err_code;
  178. }
  179. uint32_t config_socket_getopt(socket_t * p_socket,
  180. int level,
  181. int optname,
  182. void * p_optarg,
  183. socklen_t * p_optlen)
  184. {
  185. uint32_t err_code = NRF_SUCCESS;
  186. switch (level)
  187. {
  188. case SOL_NRF_MEDIUM:
  189. err_code = config_medium_getopt(optname, p_optarg, p_optlen);
  190. break;
  191. default:
  192. err_code = SOCKET_INVALID_PARAM;
  193. break;
  194. }
  195. return err_code;
  196. }
  197. /**
  198. * @brief Transport for configuration socket.
  199. */
  200. socket_transport_t config_socket_transport =
  201. {
  202. .open = config_socket_open,
  203. .setsockopt = config_socket_setopt,
  204. .getsockopt = config_socket_getopt,
  205. .close = config_socket_close
  206. };