ant_test.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Copyright (C) 2014 BlueKitchen GmbH
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. * 4. Any redistribution, use, or modification is done solely for
  17. * personal benefit and not for any commercial purpose or for
  18. * monetary gain.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
  24. * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * Please inquire about commercial licensing options at
  34. * contact@bluekitchen-gmbh.com
  35. *
  36. */
  37. #define BTSTACK_FILE__ "ant_test.c"
  38. // *****************************************************************************
  39. // ANT + SPP Counter demo for TI CC2567
  40. // - it provides a SPP port and and sends a counter every second
  41. // - it also listens on ANT channel 33,1,1
  42. // *****************************************************************************
  43. #include <stdio.h>
  44. #include "btstack_chipset_cc256x.h"
  45. #include "btstack.h"
  46. #include "ant_cmd.h"
  47. #define RFCOMM_SERVER_CHANNEL 1
  48. #define HEARTBEAT_PERIOD_MS 1000
  49. static uint8_t rfcomm_channel_nr = 1;
  50. static uint16_t rfcomm_channel_id = 0;
  51. static uint8_t spp_service_buffer[100];
  52. static btstack_timer_source_t heartbeat;
  53. static btstack_packet_callback_registration_t hci_event_callback_registration;
  54. // ant logic
  55. static enum {
  56. ANT_IDLE,
  57. ANT_SEND_RESET,
  58. ANT_W4_RESET_COMPLETE,
  59. ANT_SEND_ASSIGN_CHANNEL,
  60. ANT_W4_ASSIGN_CHANNEL_COMPLETE,
  61. ANT_SEND_SET_CHANNEL_ID,
  62. ANT_W4_SET_CHANNEL_ID_COMPLETE,
  63. ANT_SEND_CHANNEL_OPEN,
  64. ANT_W4_CHANNEL_OPEN_COMPLETE,
  65. ANT_ACTIVE
  66. } ant_state = ANT_IDLE;
  67. static void ant_run(void){
  68. // check if ANT/HCI command can be sent
  69. if (!hci_can_send_command_packet_now()) return;
  70. // send next command
  71. switch(ant_state){
  72. case ANT_SEND_RESET:
  73. ant_state = ANT_W4_RESET_COMPLETE;
  74. // 1. reset
  75. printf("Send ANT Reset\n");
  76. ant_send_cmd(&ant_reset);
  77. break;
  78. case ANT_SEND_ASSIGN_CHANNEL:
  79. ant_state = ANT_W4_ASSIGN_CHANNEL_COMPLETE;
  80. // 2. assign channel
  81. printf("Send Assign Channel\n");
  82. ant_send_cmd(&ant_assign_channel, 0, 0x00, 0);
  83. break;
  84. case ANT_SEND_SET_CHANNEL_ID:
  85. ant_state = ANT_W4_SET_CHANNEL_ID_COMPLETE;
  86. // 3. set channel ID
  87. printf("Send Set Channel ID\n");
  88. ant_send_cmd(&ant_channel_id, 0, 33, 1, 1);
  89. break;
  90. case ANT_SEND_CHANNEL_OPEN:
  91. ant_state = ANT_W4_CHANNEL_OPEN_COMPLETE;
  92. // 4. open channel
  93. printf("Send Channel Open\n");
  94. ant_send_cmd(&ant_open_channel, 0);
  95. break;
  96. default:
  97. break;
  98. }
  99. }
  100. // Bluetooth logic
  101. static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
  102. bd_addr_t event_addr;
  103. uint8_t rfcomm_channel_nr;
  104. uint16_t mtu;
  105. uint8_t event_code;
  106. // uint8_t channel;
  107. uint8_t message_id;
  108. switch (packet_type) {
  109. case HCI_EVENT_PACKET:
  110. switch (hci_event_packet_get_type(packet)) {
  111. case BTSTACK_EVENT_STATE:
  112. if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
  113. printf("BTstack is up and running\n");
  114. // start ANT init
  115. ant_state = ANT_SEND_RESET;
  116. }
  117. break;
  118. case HCI_EVENT_PIN_CODE_REQUEST:
  119. // inform about pin code request
  120. printf("Pin code request - using '0000'\n\r");
  121. reverse_bd_addr(&packet[2], event_addr);
  122. hci_send_cmd(&hci_pin_code_request_reply, &event_addr, 4, "0000");
  123. break;
  124. case RFCOMM_EVENT_INCOMING_CONNECTION:
  125. // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
  126. rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr);
  127. rfcomm_channel_nr = rfcomm_event_incoming_connection_get_server_channel(packet);
  128. rfcomm_channel_id = rfcomm_event_incoming_connection_get_rfcomm_cid(packet);
  129. printf("RFCOMM channel %u requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr));
  130. rfcomm_accept_connection(rfcomm_channel_id);
  131. break;
  132. case RFCOMM_EVENT_CHANNEL_OPENED:
  133. // data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16)
  134. if (rfcomm_event_channel_opened_get_status(packet)) {
  135. printf("RFCOMM channel open failed, status %u\n", rfcomm_event_channel_opened_get_status(packet));
  136. } else {
  137. rfcomm_channel_id = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
  138. mtu = rfcomm_event_channel_opened_get_max_frame_size(packet);
  139. printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_channel_id, mtu);
  140. }
  141. break;
  142. case RFCOMM_EVENT_CHANNEL_CLOSED:
  143. rfcomm_channel_id = 0;
  144. break;
  145. case 0xff: // vendor specific -> ANT
  146. // vendor specific ant message
  147. if (packet[2] != 0x00) break;
  148. if (packet[3] != 0x05) break;
  149. event_code = packet[7];
  150. printf("ANT Event: ");
  151. printf_hexdump(packet, size);
  152. switch(event_code){
  153. case MESG_STARTUP_MESG_ID:
  154. ant_state = ANT_SEND_ASSIGN_CHANNEL;
  155. break;
  156. case MESG_RESPONSE_EVENT_ID:
  157. // channel = packet[8];
  158. message_id = packet[9];
  159. switch (message_id){
  160. case MESG_ASSIGN_CHANNEL_ID:
  161. ant_state = ANT_SEND_SET_CHANNEL_ID;
  162. break;
  163. case MESG_CHANNEL_ID_ID:
  164. ant_state = ANT_SEND_CHANNEL_OPEN;
  165. break;
  166. default:
  167. break;
  168. }
  169. break;
  170. default:
  171. break;
  172. }
  173. break;
  174. default:
  175. break;
  176. }
  177. break;
  178. default:
  179. break;
  180. }
  181. ant_run();
  182. }
  183. static void heartbeat_handler(struct btstack_timer_source *ts){
  184. if (rfcomm_channel_id){
  185. static int counter = 0;
  186. char lineBuffer[30];
  187. sprintf(lineBuffer, "BTstack counter %04u\n\r", ++counter);
  188. puts(lineBuffer);
  189. if (rfcomm_can_send_packet_now(rfcomm_channel_id)){
  190. int err = rfcomm_send(rfcomm_channel_id, (uint8_t*) lineBuffer, strlen(lineBuffer));
  191. if (err) {
  192. printf("rfcomm_send -> error %d", err);
  193. }
  194. }
  195. }
  196. btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS);
  197. btstack_run_loop_add_timer(ts);
  198. }
  199. int btstack_main(int argc, const char * argv[]);
  200. int btstack_main(int argc, const char * argv[]){
  201. // init L2CAP
  202. l2cap_init();
  203. // init RFCOMM
  204. rfcomm_init();
  205. rfcomm_register_service(packet_handler, rfcomm_channel_nr, 100); // reserved channel, mtu=100
  206. // init SDP, create record for SPP and register with SDP
  207. sdp_init();
  208. memset(spp_service_buffer, 0, sizeof(spp_service_buffer));
  209. spp_create_sdp_record(spp_service_buffer, 0x10001, RFCOMM_SERVER_CHANNEL, "SPP Counter");
  210. sdp_register_service(spp_service_buffer);
  211. // register for HCI events
  212. hci_event_callback_registration.callback = &packet_handler;
  213. hci_add_event_handler(&hci_event_callback_registration);
  214. // set local name
  215. gap_set_local_name("ANT Demo");
  216. // make discoverable
  217. gap_discoverable_control(1);
  218. // set one-shot timer
  219. heartbeat.process = &heartbeat_handler;
  220. btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
  221. btstack_run_loop_add_timer(&heartbeat);
  222. printf("Run...\n\r");
  223. // turn on!
  224. hci_power_control(HCI_POWER_ON);
  225. return 0;
  226. }