Geen omschrijving

nrf8001-broadcast.cxx 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
  3. * Copyright (c) 2014 Intel Corporation.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. #include <unistd.h>
  25. #include <iostream>
  26. #include "nrf8001.h"
  27. #include "nrf8001-broadcast.h"
  28. #include <lib_aci.h>
  29. #include <aci_setup.h>
  30. #include <signal.h>
  31. #ifdef SERVICES_PIPE_TYPE_MAPPING_CONTENT
  32. static services_pipe_type_mapping_t
  33. services_pipe_type_mapping[NUMBER_OF_PIPES] = SERVICES_PIPE_TYPE_MAPPING_CONTENT;
  34. #else
  35. #define NUMBER_OF_PIPES 0
  36. static services_pipe_type_mapping_t * services_pipe_type_mapping = NULL;
  37. #endif
  38. /**
  39. * Store the setup for the nRF8001 in the flash of the AVR to save on RAM
  40. */
  41. static hal_aci_data_t setup_msgs[NB_SETUP_MESSAGES] = SETUP_MESSAGES_CONTENT;
  42. /**
  43. * aci_struct that will contain
  44. * total initial credits
  45. * current credit
  46. * current state of the aci (setup/standby/active/sleep)
  47. * open remote pipe pending
  48. * close remote pipe pending
  49. * Current pipe available bitmap
  50. * Current pipe closed bitmap
  51. * Current connection interval, slave latency and link supervision timeout
  52. * Current State of the the GATT client (Service Discovery)
  53. * Status of the bond (R) Peer address
  54. */
  55. static struct aci_state_t aci_state;
  56. /**
  57. * Temporary buffers for sending ACI commands
  58. */
  59. static hal_aci_evt_t aci_data;
  60. void
  61. sig_handler(int signo)
  62. {
  63. printf("got signal\n");
  64. if (signo == SIGINT) {
  65. printf("exiting application\n");
  66. }
  67. }
  68. void
  69. init_aci_setup () {
  70. /**
  71. * Point ACI data structures to the the setup data that the nRFgo studio generated for the nRF8001
  72. */
  73. if (NULL != services_pipe_type_mapping) {
  74. aci_state.aci_setup_info.services_pipe_type_mapping = &services_pipe_type_mapping[0];
  75. } else {
  76. aci_state.aci_setup_info.services_pipe_type_mapping = NULL;
  77. }
  78. aci_state.aci_setup_info.number_of_pipes = NUMBER_OF_PIPES;
  79. aci_state.aci_setup_info.setup_msgs = setup_msgs;
  80. aci_state.aci_setup_info.num_setup_msgs = NB_SETUP_MESSAGES;
  81. }
  82. int
  83. main(int argc, char **argv)
  84. {
  85. //! [Interesting]
  86. init_aci_setup ();
  87. init_local_interfaces (&aci_state, 10, 8, 4);
  88. while (1) {
  89. static bool setup_required = false;
  90. if (lib_aci_event_get (&aci_state, &aci_data)) {
  91. aci_evt_t * aci_evt;
  92. aci_evt = &aci_data.evt;
  93. switch(aci_evt->evt_opcode) {
  94. /**
  95. As soon as you reset the nRF8001 you will get an ACI Device Started Event
  96. */
  97. case ACI_EVT_DEVICE_STARTED: {
  98. aci_state.data_credit_available = aci_evt->params.device_started.credit_available;
  99. switch(aci_evt->params.device_started.device_mode) {
  100. case ACI_DEVICE_SETUP:
  101. /**
  102. When the device is in the setup mode
  103. */
  104. printf ("Evt Device Started: Setup\n");
  105. setup_required = true;
  106. break;
  107. case ACI_DEVICE_STANDBY:
  108. printf ("Evt Device Started: Standby\n");
  109. lib_aci_broadcast(10/* in seconds */, 0x0100 /* advertising interval 100ms */);
  110. printf ("Broadcasting started\n");
  111. break;
  112. }
  113. }
  114. break; //ACI Device Started Event
  115. case ACI_EVT_CMD_RSP:
  116. if (ACI_STATUS_SUCCESS != aci_evt->params.cmd_rsp.cmd_status) {
  117. printf ("ACI_EVT_CMD_RSP\n");
  118. while (1);
  119. }
  120. break;
  121. case ACI_EVT_CONNECTED:
  122. printf ("ACI_EVT_CONNECTED\n");
  123. break;
  124. case ACI_EVT_PIPE_STATUS:
  125. printf ("ACI_EVT_PIPE_STATUS\n");
  126. break;
  127. case ACI_EVT_DISCONNECTED:
  128. if (ACI_STATUS_ERROR_ADVT_TIMEOUT == aci_evt->params.disconnected.aci_status) {
  129. printf ("Broadcasting timed out\n");
  130. } else {
  131. printf ("Evt Disconnected. Link Loss\n");
  132. }
  133. break;
  134. case ACI_EVT_DATA_RECEIVED:
  135. printf ("ACI_EVT_DATA_RECEIVED\n");
  136. break;
  137. case ACI_EVT_HW_ERROR:
  138. printf ("ACI_EVT_HW_ERROR\n");
  139. break;
  140. }
  141. }
  142. if (setup_required) {
  143. if (SETUP_SUCCESS == do_aci_setup(&aci_state)) {
  144. setup_required = false;
  145. }
  146. }
  147. usleep (100);
  148. }
  149. close_local_interfaces (&aci_state);
  150. //! [Interesting]
  151. std::cout << "exiting application" << std::endl;
  152. return 0;
  153. }