]> git.karo-electronics.de Git - oswald.git/blobdiff - metawatch/bt_hci.c
Sanity check for enabled BT
[oswald.git] / metawatch / bt_hci.c
index 265b00c839b9180627d0549ff775889e4d7608ca..56e490ece0b20bfdb3c75ef1eaccbde00ca7f9b5 100644 (file)
@@ -232,6 +232,7 @@ void bt_hci_process_event_packet(unsigned char *packet)
 #endif
                        memmove(packet, (packet+2), 6);
                        packet[6] = 0x04; // PIN has length of 4
+                       memcpy((packet+7), BT_PIN, 4);
                        packet[7] = '4';
                        packet[8] = '3';
                        packet[9] = '1';
@@ -326,10 +327,14 @@ unsigned char bt_feed_packet_data(unsigned char pdata)
                                        state = HCI_PACKET_START;
                                        // disable BT UART?
                                        // mabye UCA1CTL1 = UCSWRST ?
+
                                        pdata = EHCILL_GO_TO_SLEEP_ACK;
                                        mw_bt_uart_tx(&pdata, 0x01);
-                                       BT_IO_POUT |= BT_IO_RTS; // pull RTS -> stop data
 
+                                       // pull RTS -> stop data
+                                       BT_IO_POUT |= BT_IO_RTS;
+
+                                       // enable IRQ on CTS
                                        P1IFG &= ~BT_IO_CTS;
                                        P1IE |= BT_IO_CTS;
 
@@ -417,10 +422,14 @@ void bt_hci_cmd(const uint8_t OGF, const uint8_t OCF, const uint8_t data_len, co
 {
        bt_hci_cmd_t packet;
 
+       // refuse any HCI if interface is not enabled
+       if (mw_bt_is_enabled() == 0)
+               return;
+
        if (state == EHCILL_SLEEPING) {
                uint8_t ehcill_p = EHCILL_WAKE_UP_IND;
 
-               debug_uart_tx("HCILL wakeup\n");
+               debug_uart_tx("wakeup HCILL\n");
                state = HCI_PACKET_START;
                mw_bt_uart_tx(&ehcill_p, 1);
                __delay_cycles(300000);
@@ -434,6 +443,27 @@ void bt_hci_cmd(const uint8_t OGF, const uint8_t OCF, const uint8_t data_len, co
                mw_bt_uart_tx(data, data_len);
 }
 
+typedef struct {
+       uint16_t acl_handle;
+       uint16_t max_interval;
+       uint16_t min_interval;
+       uint16_t sniff_attempt;
+       uint16_t sniff_timeout;
+} __attribute__((packed)) bt_hci_sniff_cmd_t;
+
+void bt_hci_set_sniff_mode(const uint16_t acl_handle, const uint16_t max_interval, const uint16_t min_interval, const uint16_t sniff_attempt, const uint16_t sniff_timeout)
+{
+       bt_hci_sniff_cmd_t sniff_cmd;
+
+       sniff_cmd.acl_handle = acl_handle;
+       sniff_cmd.max_interval = max_interval;
+       sniff_cmd.min_interval = min_interval;
+       sniff_cmd.sniff_attempt = sniff_attempt;
+       sniff_cmd.sniff_timeout = sniff_timeout;
+       
+       bt_hci_cmd(HCI_LINK_POLICY_OGF, HCI_SNIFF_MODE_OCF, sizeof(sniff_cmd), &sniff_cmd);
+}
+
 typedef struct {
        uint8_t type;
        uint16_t handle;
@@ -446,6 +476,10 @@ void bt_acl_send(const uint16_t handle, const uint8_t PB, const uint8_t BC, cons
 {
        bt_hci_acl_t packet;
 
+       // refuse any HCI if interface is not enabled
+       if (mw_bt_is_enabled() == 0)
+               return;
+
        packet.type = HCI_ACL_DATA_PACKET;
        packet.handle = handle | ((PB & 0x03) << 12) | ((BC & 0x03) << 14);
        packet.total_length = dlen + 4;
@@ -462,11 +496,12 @@ void bt_hci_init(void)
 
 void bt_hci_ehcill_wake(void)
 {
-       uint8_t ehcill_p = EHCILL_WAKE_UP_ACK;
+       const uint8_t ehcill_p = EHCILL_WAKE_UP_ACK;
 
        debug_uart_tx("HCILL wakeup\n");
 
        P1IE &= ~BT_IO_CTS;
+       P1IFG &= ~BT_IO_CTS;
        state = HCI_PACKET_START;
 
        BT_IO_POUT &= ~BT_IO_RTS; // drop RTS -> start data