No Description

nrf8001_helloworld.cxx 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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_helloworld.h"
  28. #include <lib_aci.h>
  29. #include <aci_setup.h>
  30. #include <signal.h>
  31. #include "uart_over_ble.h"
  32. /*
  33. * You can use the nRF UART app in the Apple iOS app store and Google Play for Android 4.3 for Samsung Galaxy S4
  34. * with this helloworld application
  35. */
  36. #ifdef SERVICES_PIPE_TYPE_MAPPING_CONTENT
  37. static services_pipe_type_mapping_t
  38. services_pipe_type_mapping[NUMBER_OF_PIPES] = SERVICES_PIPE_TYPE_MAPPING_CONTENT;
  39. #else
  40. #define NUMBER_OF_PIPES 0
  41. static services_pipe_type_mapping_t * services_pipe_type_mapping = NULL;
  42. #endif
  43. /**
  44. * Store the setup for the nRF8001 in the flash of the AVR to save on RAM
  45. */
  46. static hal_aci_data_t setup_msgs[NB_SETUP_MESSAGES] = SETUP_MESSAGES_CONTENT;
  47. /**
  48. * aci_struct that will contain
  49. * total initial credits
  50. * current credit
  51. * current state of the aci (setup/standby/active/sleep)
  52. * open remote pipe pending
  53. * close remote pipe pending
  54. * Current pipe available bitmap
  55. * Current pipe closed bitmap
  56. * Current connection interval, slave latency and link supervision timeout
  57. * Current State of the the GATT client (Service Discovery)
  58. * Status of the bond (R) Peer address
  59. */
  60. static struct aci_state_t aci_state;
  61. /**
  62. * Temporary buffers for sending ACI commands
  63. */
  64. static hal_aci_evt_t aci_data;
  65. /*
  66. Timing change state variable
  67. */
  68. static bool timing_change_done = false;
  69. /*
  70. Used to test the UART TX characteristic notification
  71. */
  72. static uart_over_ble_t uart_over_ble;
  73. static uint8_t uart_buffer[20];
  74. static uint8_t uart_buffer_len = 0;
  75. static uint8_t dummychar = 0;
  76. void
  77. sig_handler(int signo)
  78. {
  79. printf("got signal\n");
  80. if (signo == SIGINT) {
  81. printf("exiting application\n");
  82. }
  83. }
  84. void
  85. init_aci_setup () {
  86. /**
  87. * Point ACI data structures to the the setup data that the nRFgo studio generated for the nRF8001
  88. */
  89. if (NULL != services_pipe_type_mapping) {
  90. aci_state.aci_setup_info.services_pipe_type_mapping = &services_pipe_type_mapping[0];
  91. } else {
  92. aci_state.aci_setup_info.services_pipe_type_mapping = NULL;
  93. }
  94. aci_state.aci_setup_info.number_of_pipes = NUMBER_OF_PIPES;
  95. aci_state.aci_setup_info.setup_msgs = setup_msgs;
  96. aci_state.aci_setup_info.num_setup_msgs = NB_SETUP_MESSAGES;
  97. }
  98. void
  99. uart_over_ble_init (void) {
  100. uart_over_ble.uart_rts_local = true;
  101. }
  102. bool
  103. uart_tx (uint8_t *buffer, uint8_t buffer_len) {
  104. bool status = false;
  105. if (lib_aci_is_pipe_available(&aci_state, PIPE_UART_OVER_BTLE_UART_TX_TX) &&
  106. (aci_state.data_credit_available >= 1)) {
  107. status = lib_aci_send_data(PIPE_UART_OVER_BTLE_UART_TX_TX, buffer, buffer_len);
  108. if (status) {
  109. aci_state.data_credit_available--;
  110. }
  111. }
  112. return status;
  113. }
  114. bool
  115. uart_process_control_point_rx(uint8_t *byte, uint8_t length) {
  116. bool status = false;
  117. aci_ll_conn_params_t *conn_params;
  118. if (lib_aci_is_pipe_available(&aci_state, PIPE_UART_OVER_BTLE_UART_CONTROL_POINT_TX) ) {
  119. switch (*byte) {
  120. /*
  121. Queues a ACI Disconnect to the nRF8001 when this packet is received.
  122. May cause some of the UART packets being sent to be dropped
  123. */
  124. case UART_OVER_BLE_DISCONNECT:
  125. /*
  126. Parameters:
  127. None
  128. */
  129. lib_aci_disconnect(&aci_state, ACI_REASON_TERMINATE);
  130. status = true;
  131. break;
  132. /*
  133. Queues an ACI Change Timing to the nRF8001
  134. */
  135. case UART_OVER_BLE_LINK_TIMING_REQ:
  136. /*
  137. Parameters:
  138. Connection interval min: 2 bytes
  139. Connection interval max: 2 bytes
  140. Slave latency: 2 bytes
  141. Timeout: 2 bytes
  142. Same format as Peripheral Preferred Connection Parameters (See nRFgo studio -> nRF8001 Configuration -> GAP Settings
  143. Refer to the ACI Change Timing Request in the nRF8001 Product Specifications
  144. */
  145. conn_params = (aci_ll_conn_params_t *)(byte+1);
  146. lib_aci_change_timing( conn_params->min_conn_interval,
  147. conn_params->max_conn_interval,
  148. conn_params->slave_latency,
  149. conn_params->timeout_mult);
  150. status = true;
  151. break;
  152. /*
  153. Clears the RTS of the UART over BLE
  154. */
  155. case UART_OVER_BLE_TRANSMIT_STOP:
  156. /*
  157. Parameters:
  158. None
  159. */
  160. uart_over_ble.uart_rts_local = false;
  161. status = true;
  162. break;
  163. /*
  164. Set the RTS of the UART over BLE
  165. */
  166. case UART_OVER_BLE_TRANSMIT_OK:
  167. /*
  168. Parameters:
  169. None
  170. */
  171. uart_over_ble.uart_rts_local = true;
  172. status = true;
  173. break;
  174. }
  175. }
  176. return status;
  177. }
  178. int
  179. main(int argc, char **argv)
  180. {
  181. //! [Interesting]
  182. init_aci_setup ();
  183. init_local_interfaces (&aci_state, 10, 8, 4);
  184. while (1) {
  185. static bool setup_required = false;
  186. // We enter the if statement only when there is a ACI event available to be processed
  187. if (lib_aci_event_get(&aci_state, &aci_data)) {
  188. aci_evt_t * aci_evt;
  189. aci_evt = &aci_data.evt;
  190. switch(aci_evt->evt_opcode) {
  191. /**
  192. As soon as you reset the nRF8001 you will get an ACI Device Started Event
  193. */
  194. case ACI_EVT_DEVICE_STARTED: {
  195. aci_state.data_credit_total = aci_evt->params.device_started.credit_available;
  196. switch(aci_evt->params.device_started.device_mode) {
  197. case ACI_DEVICE_SETUP:
  198. /**
  199. When the device is in the setup mode
  200. */
  201. printf ("Evt Device Started: Setup \n");
  202. setup_required = true;
  203. break;
  204. case ACI_DEVICE_STANDBY:
  205. printf ("Evt Device Started: Standby \n");
  206. // Looking for an iPhone by sending radio advertisements
  207. // When an iPhone connects to us we will get an ACI_EVT_CONNECTED event from the nRF8001
  208. if (aci_evt->params.device_started.hw_error) {
  209. usleep (20000); //Handle the HW error event correctly.
  210. } else {
  211. lib_aci_connect(0/* in seconds : 0 means forever */, 0x0050 /* advertising interval 50ms*/);
  212. printf ("Advertising started \n");
  213. }
  214. break;
  215. }
  216. }
  217. break; // ACI Device Started Event
  218. case ACI_EVT_CMD_RSP:
  219. //If an ACI command response event comes with an error -> stop
  220. if (ACI_STATUS_SUCCESS != aci_evt->params.cmd_rsp.cmd_status) {
  221. //ACI ReadDynamicData and ACI WriteDynamicData will have status codes of
  222. //TRANSACTION_CONTINUE and TRANSACTION_COMPLETE
  223. //all other ACI commands will have status code of ACI_STATUS_SCUCCESS for a successful command
  224. printf ("ACI_EVT_CMD_RSP \n");
  225. }
  226. if (ACI_CMD_GET_DEVICE_VERSION == aci_evt->params.cmd_rsp.cmd_opcode) {
  227. //Store the version and configuration information of the nRF8001 in the Hardware Revision String Characteristic
  228. lib_aci_set_local_data(&aci_state, PIPE_DEVICE_INFORMATION_HARDWARE_REVISION_STRING_SET,
  229. (uint8_t *)&(aci_evt->params.cmd_rsp.params.get_device_version), sizeof(aci_evt_cmd_rsp_params_get_device_version_t));
  230. }
  231. break;
  232. case ACI_EVT_CONNECTED:
  233. printf ("ACI_EVT_CONNECTED");
  234. uart_over_ble_init ();
  235. timing_change_done = false;
  236. aci_state.data_credit_available = aci_state.data_credit_total;
  237. /*
  238. Get the device version of the nRF8001 and store it in the Hardware Revision String
  239. */
  240. lib_aci_device_version();
  241. break;
  242. case ACI_EVT_PIPE_STATUS:
  243. printf ("ACI_EVT_PIPE_STATUS \n");
  244. if (lib_aci_is_pipe_available(&aci_state, PIPE_UART_OVER_BTLE_UART_TX_TX) && (false == timing_change_done)) {
  245. lib_aci_change_timing_GAP_PPCP(); // change the timing on the link as specified in the nRFgo studio -> nRF8001 conf. -> GAP.
  246. // Used to increase or decrease bandwidth
  247. timing_change_done = true;
  248. char hello[]="Hello World, works";
  249. uart_tx((uint8_t *)&hello[0], strlen(hello));
  250. }
  251. break;
  252. case ACI_EVT_TIMING:
  253. printf ("Evt link connection interval changed \n");
  254. lib_aci_set_local_data(&aci_state,
  255. PIPE_UART_OVER_BTLE_UART_LINK_TIMING_CURRENT_SET,
  256. (uint8_t *)&(aci_evt->params.timing.conn_rf_interval), /* Byte aligned */
  257. PIPE_UART_OVER_BTLE_UART_LINK_TIMING_CURRENT_SET_MAX_SIZE);
  258. break;
  259. case ACI_EVT_DISCONNECTED:
  260. printf ("ACI_EVT_DISCONNECTED \n");
  261. lib_aci_connect(0/* in seconds : 0 means forever */, 0x0050 /* advertising interval 50ms*/);
  262. printf ("Advertising started \n");
  263. break;
  264. case ACI_EVT_DATA_RECEIVED:
  265. if (PIPE_UART_OVER_BTLE_UART_RX_RX == aci_evt->params.data_received.rx_data.pipe_number) {
  266. for(int i=0; i<aci_evt->len - 2; i++) {
  267. uart_buffer[i] = aci_evt->params.data_received.rx_data.aci_data[i];
  268. }
  269. uart_buffer_len = aci_evt->len - 2;
  270. if (lib_aci_is_pipe_available(&aci_state, PIPE_UART_OVER_BTLE_UART_TX_TX)) {
  271. }
  272. }
  273. if (PIPE_UART_OVER_BTLE_UART_CONTROL_POINT_RX == aci_evt->params.data_received.rx_data.pipe_number) {
  274. //Subtract for Opcode and Pipe number
  275. uart_process_control_point_rx(&aci_evt->params.data_received.rx_data.aci_data[0], aci_evt->len - 2);
  276. }
  277. printf ("Incomming data - %s\n", uart_buffer);
  278. break;
  279. case ACI_EVT_DATA_CREDIT:
  280. printf ("ACI_EVT_DATA_CREDIT \n");
  281. aci_state.data_credit_available = aci_state.data_credit_available + aci_evt->params.data_credit.credit;
  282. break;
  283. case ACI_EVT_PIPE_ERROR:
  284. printf ("ACI_EVT_PIPE_ERROR \n");
  285. //Increment the credit available as the data packet was not sent.
  286. //The pipe error also represents the Attribute protocol Error Response sent from the peer and that should not be counted
  287. //for the credit.
  288. if (ACI_STATUS_ERROR_PEER_ATT_ERROR != aci_evt->params.pipe_error.error_code) {
  289. aci_state.data_credit_available++;
  290. }
  291. break;
  292. case ACI_EVT_HW_ERROR:
  293. printf ("ACI_EVT_HW_ERROR \n");
  294. lib_aci_connect(0/* in seconds, 0 means forever */, 0x0050 /* advertising interval 50ms*/);
  295. printf ("Advertising started \n");
  296. break;
  297. }
  298. }
  299. /* setup_required is set to true when the device starts up and enters setup mode.
  300. * It indicates that do_aci_setup() should be called. The flag should be cleared if
  301. * do_aci_setup() returns ACI_STATUS_TRANSACTION_COMPLETE.
  302. */
  303. if(setup_required) {
  304. if (SETUP_SUCCESS == do_aci_setup(&aci_state)) {
  305. setup_required = false;
  306. }
  307. }
  308. usleep (100);
  309. }
  310. close_local_interfaces (&aci_state);
  311. //! [Interesting]
  312. std::cout << "exiting application" << std::endl;
  313. return 0;
  314. }