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