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 BLUEKITCHEN 24 * GMBH 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 * [email protected] 35 * 36 */ 37 38 // ***************************************************************************** 39 // 40 // accel_demo 41 // 42 // ***************************************************************************** 43 44 #include <stdint.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <string.h> 48 49 #include <msp430x54x.h> 50 51 #include "btstack_chipset_cc256x.h" 52 #include "hal_adc.h" 53 #include "hal_board.h" 54 #include "hal_compat.h" 55 #include "hal_usb.h" 56 57 #include "hci_cmd.h" 58 #include "btstack_run_loop.h" 59 #include "classic/sdp_util.h" 60 61 #include "hci.h" 62 #include "l2cap.h" 63 #include "btstack_memory.h" 64 #include "classic/btstack_link_key_db.h" 65 #include "classic/rfcomm.h" 66 #include "classic/sdp_server.h" 67 #include "btstack_config.h" 68 69 #define HEARTBEAT_PERIOD_MS 1000 70 71 #define FONT_HEIGHT 12 // Each character has 13 lines 72 #define FONT_WIDTH 8 73 static int row = 0; 74 char lineBuffer[80]; 75 76 static uint8_t rfcomm_channel_nr = 1; 77 static uint16_t rfcomm_channel_id; 78 static uint8_t spp_service_buffer[150]; 79 static btstack_packet_callback_registration_t hci_event_callback_registration; 80 81 // SPP description 82 static uint8_t accel_buffer[6]; 83 84 static void prepare_accel_packet(void){ 85 int16_t accl_x; 86 int16_t accl_y; 87 int16_t accl_z; 88 89 /* read the digital accelerometer x- direction and y - direction output */ 90 halAccelerometerRead((int *)&accl_x, (int *)&accl_y, (int *)&accl_z); 91 92 accel_buffer[0] = 0x01; // Start of "header" 93 accel_buffer[1] = accl_x; 94 accel_buffer[2] = (accl_x >> 8); 95 accel_buffer[3] = accl_y; 96 accel_buffer[4] = (accl_y >> 8); 97 int index; 98 uint8_t checksum = 0; 99 for (index = 0; index < 5; index++) { 100 checksum += accel_buffer[index]; 101 } 102 accel_buffer[5] = checksum; 103 104 /* start the ADC to read the next accelerometer output */ 105 halAdcStartRead(); 106 107 printf("Accel: X: %04d, Y: %04d, Z: %04d\n\r", accl_x, accl_y, accl_z); 108 } 109 110 static void send_packet(void){ 111 int err = rfcomm_send(rfcomm_channel_id, (uint8_t *)accel_buffer, sizeof(accel_buffer)); 112 switch(err){ 113 case 0: 114 prepare_accel_packet(); 115 break; 116 case BTSTACK_ACL_BUFFERS_FULL: 117 break; 118 default: 119 printf("rfcomm_send() -> err %d\n\r", err); 120 break; 121 } 122 } 123 124 // Bluetooth logic 125 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 126 bd_addr_t event_addr; 127 uint8_t rfcomm_channel_nr; 128 uint16_t mtu; 129 int err; 130 131 switch (packet_type) { 132 case HCI_EVENT_PACKET: 133 switch (hci_event_packet_get_type(packet)) { 134 135 case BTSTACK_EVENT_STATE: 136 if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){ 137 printf("BTstack is up and running\n"); 138 } 139 break; 140 141 case HCI_EVENT_COMMAND_COMPLETE: 142 if (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_READ_BD_ADDR) { 143 reverse_bd_addr(&packet[6], event_addr); 144 printf("BD-ADDR: %s\n\r", bd_addr_to_str(event_addr)); 145 break; 146 } 147 break; 148 149 case HCI_EVENT_LINK_KEY_REQUEST: 150 // deny link key request 151 printf("Link key request\n\r"); 152 hci_event_link_key_request_get_bd_addr(packet, event_addr); 153 hci_send_cmd(&hci_link_key_request_negative_reply, &event_addr); 154 break; 155 156 case HCI_EVENT_PIN_CODE_REQUEST: 157 // inform about pin code request 158 printf("Pin code request - using '0000'\n\r"); 159 reverse_bd_addr(&packet[2], event_addr); 160 hci_send_cmd(&hci_pin_code_request_reply, &event_addr, 4, "0000"); 161 break; 162 163 case RFCOMM_EVENT_INCOMING_CONNECTION: 164 // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16) 165 rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr); 166 rfcomm_channel_nr = rfcomm_event_incoming_connection_get_server_channel(packet); 167 rfcomm_channel_id = rfcomm_event_incoming_connection_get_rfcomm_cid(packet); 168 printf("RFCOMM channel %u requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr)); 169 rfcomm_accept_connection(rfcomm_channel_id); 170 break; 171 172 case RFCOMM_EVENT_CHANNEL_OPENED: 173 // data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16) 174 if (rfcomm_event_channel_opened_get_status(packet)) { 175 printf("RFCOMM channel open failed, status %u\n", rfcomm_event_channel_opened_get_status(packet)); 176 } else { 177 rfcomm_channel_id = rfcomm_event_channel_opened_get_rfcomm_cid(packet); 178 mtu = rfcomm_event_channel_opened_get_max_frame_size(packet); 179 printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_channel_id, mtu); 180 } 181 break; 182 183 case RFCOMM_EVENT_CAN_SEND_NOW: 184 if (rfcomm_can_send_packet_now(rfcomm_channel_id)) send_packet(); 185 break; 186 187 case RFCOMM_EVENT_CHANNEL_CLOSED: 188 rfcomm_channel_id = 0; 189 break; 190 191 default: 192 break; 193 } 194 break; 195 196 default: 197 break; 198 } 199 } 200 201 int btstack_main(int argc, const char * argv[]); 202 int btstack_main(int argc, const char * argv[]){ 203 204 // Accel 205 halAccelerometerInit(); 206 prepare_accel_packet(); 207 208 // register for HCI events 209 hci_event_callback_registration.callback = &packet_handler; 210 hci_add_event_handler(&hci_event_callback_registration); 211 212 // init L2CAP 213 l2cap_init(); 214 215 // init RFCOMM 216 rfcomm_init(); 217 rfcomm_register_service(rfcomm_packet_handler, rfcomm_channel_nr, 100); // reserved channel, mtu=100 218 219 // init SDP, create record for SPP and register with SDP 220 sdp_init(); 221 memset(spp_service_buffer, 0, sizeof(spp_service_buffer)); 222 spp_create_sdp_record((uint8_t*) spp_service_buffer, 1, "SPP Accel"); 223 printf("SDP service buffer size: %u\n\r", (uint16_t) de_get_len((uint8_t*) spp_service_buffer)); 224 sdp_register_service((uint8_t*) spp_service_buffer); 225 226 // ready - enable irq used in h4 task 227 __enable_interrupt(); 228 229 // set local name 230 gap_set_local_name("BTstack SPP Sensor"); 231 // make discoverable 232 gap_discoverable_control(1); 233 // turn on! 234 hci_power_control(HCI_POWER_ON); 235 return 0; 236 } 237 238 /* 239 240 rfcomm_send gets called before we have credits 241 rfcomm_send returns undefined error codes??? 242 243 */ 244 245