// Copyright (C) 2024, Mark Qvist

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

#if MCU_VARIANT == MCU_ESP32

#elif MCU_VARIANT == MCU_NRF52
#endif

#if MCU_VARIANT == MCU_ESP32
  #if HAS_BLUETOOTH == true
    #include "BluetoothSerial.h"
    #include "esp_bt_main.h"
    #include "esp_bt_device.h"
    BluetoothSerial SerialBT;
  #elif HAS_BLE == true
    #include "esp_bt_main.h"
    #include "esp_bt_device.h"
    #include "BLESerial.h"
    BLESerial SerialBT;
  #endif

#elif MCU_VARIANT == MCU_NRF52
  #include <bluefruit.h>
  #include <math.h>
  #define BLE_RX_BUF 6144
  BLEUart SerialBT(BLE_RX_BUF);
  BLEDis  bledis;
  BLEBas  blebas;
  bool SerialBT_init = false;
#endif

#if BOARD_MODEL == BOARD_CARDPUTER_ADV
  #define BT_PAIRING_TIMEOUT 90000
#elif BOARD_MODEL == BOARD_TDECK
  #define BT_PAIRING_TIMEOUT 30000
#else
  #define BT_PAIRING_TIMEOUT 35000
#endif
#define BLE_FLUSH_TIMEOUT 20
uint32_t bt_pairing_started = 0;

#define BT_DEV_ADDR_LEN 6
#define BT_DEV_HASH_LEN 16
uint8_t dev_bt_mac[BT_DEV_ADDR_LEN];
char bt_da[BT_DEV_ADDR_LEN];
char bt_dh[BT_DEV_HASH_LEN];
char bt_devname[11];

#if BOARD_MODEL == BOARD_CARDPUTER_ADV || BOARD_MODEL == BOARD_TDECK
  extern void stopRadio();

  void ble_host_detached_cleanup() {
    if (radio_online) stopRadio();
    cable_state   = CABLE_STATE_DISCONNECTED;
    current_rssi  = -292;
    last_rssi     = -292;
    last_rssi_raw = 0x00;
    last_snr_raw  = 0x80;
  }
#endif

#if MCU_VARIANT == MCU_ESP32
  #if HAS_BLUETOOTH == true

    void bt_confirm_pairing(uint32_t numVal) {
      bt_ssp_pin = numVal;
      kiss_indicate_btpin();
      if (bt_allow_pairing) {
        SerialBT.confirmReply(true);
      } else {
        SerialBT.confirmReply(false);
      }
    }

    void bt_stop() {
      display_unblank();
      if (bt_state != BT_STATE_OFF) {
        SerialBT.end();
        bt_allow_pairing = false;
        bt_state = BT_STATE_OFF;
      }
    }

    void bt_start() {
      display_unblank();
      if (bt_state == BT_STATE_OFF) {
        SerialBT.begin(bt_devname);
        bt_state = BT_STATE_ON;
       }
    }

    void bt_enable_pairing() {
      display_unblank();
      if (bt_state == BT_STATE_OFF) bt_start();
      bt_allow_pairing = true;
      bt_pairing_started = millis();
      bt_state = BT_STATE_PAIRING;
    }

    void bt_disable_pairing() {
      display_unblank();
      bt_allow_pairing = false;
      bt_ssp_pin = 0;
      bt_state = BT_STATE_ON;
    }

    void bt_pairing_complete(boolean success) {
      display_unblank();
      if (success) {
        bt_disable_pairing();
      } else {
        bt_ssp_pin = 0;
      }
    }

    void bt_connection_callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {
      display_unblank();
      if(event == ESP_SPP_SRV_OPEN_EVT) {
        bt_state = BT_STATE_CONNECTED;
        cable_state = CABLE_STATE_DISCONNECTED;
      }
       
      if(event == ESP_SPP_CLOSE_EVT ){
        bt_state = BT_STATE_ON;
      }
    }

    bool bt_setup_hw() {
      if (!bt_ready) {
        if (EEPROM.read(eeprom_addr(ADDR_CONF_BT)) == BT_ENABLE_BYTE) {
          bt_enabled = true;
        } else {
          bt_enabled = false;
        }
        if (btStart()) {
          if (esp_bluedroid_init() == ESP_OK) {
            if (esp_bluedroid_enable() == ESP_OK) {
              const uint8_t* bda_ptr = esp_bt_dev_get_address();
              char *data = (char*)malloc(BT_DEV_ADDR_LEN+1);
              for (int i = 0; i < BT_DEV_ADDR_LEN; i++) {
                  data[i] = bda_ptr[i];
              }
              data[BT_DEV_ADDR_LEN] = EEPROM.read(eeprom_addr(ADDR_SIGNATURE));
              unsigned char *hash = MD5::make_hash(data, BT_DEV_ADDR_LEN);
              memcpy(bt_dh, hash, BT_DEV_HASH_LEN);
              sprintf(bt_devname, "RNode %02X%02X", bt_dh[14], bt_dh[15]);
              free(data);

              SerialBT.enableSSP();
              SerialBT.onConfirmRequest(bt_confirm_pairing);
              SerialBT.onAuthComplete(bt_pairing_complete);
              SerialBT.register_callback(bt_connection_callback);
              
              bt_ready = true;
              return true;

            } else { return false; }
          } else { return false; }
        } else { return false; }
      } else { return false; }
    }

    bool bt_init() {
        bt_state = BT_STATE_OFF;
        if (bt_setup_hw()) {
          if (bt_enabled && !console_active) bt_start();
          return true;
        } else {
          return false;
        }
    }

    void update_bt() {
      if (bt_allow_pairing && millis()-bt_pairing_started >= BT_PAIRING_TIMEOUT) {
        bt_disable_pairing();
      }
    }

  #elif HAS_BLE == true
    bool bt_setup_hw(); void bt_security_setup();
    BLESecurity *ble_security = new BLESecurity();
    bool ble_authenticated = false;
    // True only while a fresh SMP passkey exchange is in flight this session.
    bool ble_fresh_smp = false;
    uint32_t pairing_pin = 0;

    void bt_flush() { if (bt_state == BT_STATE_CONNECTED) { SerialBT.flush(); } }

    void bt_start() {
      // Serial.println("BT start");
      display_unblank();
      if (bt_state == BT_STATE_OFF) {
        bt_state = BT_STATE_ON;
        SerialBT.begin(bt_devname);
        SerialBT.setTimeout(10);
      }
    }

    void bt_stop() {
      // Serial.println("BT stop");
      display_unblank();
      if (bt_state != BT_STATE_OFF) {
        bt_allow_pairing = false;
        bt_state = BT_STATE_OFF;
        SerialBT.end();
      }
    }

    bool bt_init() {
      // Serial.println("BT init");
      bt_state = BT_STATE_OFF;
      if (bt_setup_hw()) {
        if (bt_enabled && !console_active) bt_start();
        return true;
      } else {
        return false;
      }
    }

    void bt_debond_all() {
      // Serial.println("Debonding all");
      int dev_num = esp_ble_get_bond_device_num();
      if (dev_num <= 0) return;
      esp_ble_bond_dev_t *dev_list = (esp_ble_bond_dev_t *)malloc(sizeof(esp_ble_bond_dev_t) * dev_num);
      if (dev_list == NULL) return;
      esp_ble_get_bond_device_list(&dev_num, dev_list);
      for (int i = 0; i < dev_num; i++) { esp_ble_remove_bond_device(dev_list[i].bd_addr); }
      free(dev_list);
    }

    int bt_bond_count() {
      return esp_ble_get_bond_device_num();
    }

    void bt_enable_pairing(bool show_pin) {
      // Serial.println("BT enable pairing");
      display_unblank();
      if (bt_state == BT_STATE_OFF) bt_start();

      bt_security_setup();

      bt_allow_pairing = true;
      bt_pairing_started = millis();
      bt_state = BT_STATE_PAIRING;
      bt_ssp_pin = show_pin ? pairing_pin : 0;
    }

    void bt_enable_pairing() {
      bt_enable_pairing(true);
    }

    void bt_enable_pairing_on_request() {
      bt_enable_pairing(false);
    }

    void bt_disable_pairing() {
      // Serial.println("BT disable pairing");
      display_unblank();
      bt_allow_pairing = false;
      bt_ssp_pin = 0;
      // Window expiry while a host is connected must not drop CONNECTED —
      // update_bt() only flushes the TX buffer in that state.
      bt_state = SerialBT.connected() ? BT_STATE_CONNECTED : BT_STATE_ON;
    }

    void bt_passkey_notify_callback(uint32_t passkey) {
      // Serial.printf("Got passkey notification: %d\n", passkey);
      if (bt_allow_pairing) {
        ble_fresh_smp = true;
        bt_ssp_pin = passkey;
        bt_pairing_started = millis();
        kiss_indicate_btpin();
      } else {
        // Serial.println("Pairing not allowed, re-init");
        SerialBT.disconnect();
      }
    }

    bool bt_confirm_pin_callback(uint32_t pin) {
      // Serial.printf("Confirm PIN callback: %d\n", pin);
      return bt_allow_pairing;
    }

    void bt_update_passkey() {
      // Serial.println("Updating passkey");
      pairing_pin = random(899999)+100000;
      bt_ssp_pin = pairing_pin;
    }

    uint32_t bt_passkey_callback() {
      // Serial.println("API passkey request");
      if (!bt_allow_pairing) {
        SerialBT.disconnect();
        return 0;
      }
      if (pairing_pin == 0) { bt_update_passkey(); }
      return pairing_pin;
    }

    bool bt_client_authenticated() {
      return ble_authenticated;
    }

    bool bt_security_request_callback() {
      if (bt_allow_pairing || bt_bond_count() > 0) {
        // Accept explicit pairing, and accept bonded reconnect encryption.
        // Requiring the pairing window here breaks already-bonded Android
        // reconnects after the timer expires.
        return true;
      }

      SerialBT.disconnect();
      return false;
    }

    void bt_authentication_complete_callback(esp_ble_auth_cmpl_t auth_result) {
      if (auth_result.success == true) {
        // Serial.println("Authentication success");
        ble_authenticated = true;
        // Disconnect-after-pair applies only to a fresh SMP exchange (passkey
        // ran). A bonded host re-encrypting while the pairing window happens
        // to be armed must stay connected, or a live session gets dropped.
        if (bt_state == BT_STATE_PAIRING && ble_fresh_smp) {
          // Serial.println("Pairing complete, disconnecting");
          delay(2000); SerialBT.disconnect();
        } else { bt_state = BT_STATE_CONNECTED; }
      } else {
        // Serial.println("Authentication fail");
        ble_authenticated = false;
        bt_state = BT_STATE_ON;
        bt_update_passkey();
        bt_security_setup();
      }
      bt_allow_pairing = false;
      bt_ssp_pin = 0;
      ble_fresh_smp = false;
    }

    void bt_connect_callback(BLEServer *server) {
      uint16_t conn_id = server->getConnId();
      // Serial.printf("Connected: %d\n", conn_id);
      display_unblank();
      ble_authenticated = false;
      if (bt_state != BT_STATE_PAIRING) { bt_state = BT_STATE_CONNECTED; }
      cable_state = CABLE_STATE_DISCONNECTED;
    }

    void bt_disconnect_callback(BLEServer *server) {
      uint16_t conn_id = server->getConnId();
      // Serial.printf("Disconnected: %d\n", conn_id);
      display_unblank();
      ble_authenticated = false;
      bt_state = BT_STATE_ON;
      #if BOARD_MODEL == BOARD_CARDPUTER_ADV || BOARD_MODEL == BOARD_TDECK
        ble_host_detached_cleanup();
      #endif
    }

    bool bt_setup_hw() {
      // Serial.println("BT setup hw");
      if (!bt_ready) {
        if (EEPROM.read(eeprom_addr(ADDR_CONF_BT)) == BT_ENABLE_BYTE) {
          bt_enabled = true;
        } else {
          bt_enabled = false;
        }
        if (btStart()) {
          if (esp_bluedroid_init() == ESP_OK) {
            if (esp_bluedroid_enable() == ESP_OK) {
              const uint8_t* bda_ptr = esp_bt_dev_get_address();
              char *data = (char*)malloc(BT_DEV_ADDR_LEN+1);
              for (int i = 0; i < BT_DEV_ADDR_LEN; i++) {
                  data[i] = bda_ptr[i];
              }
              data[BT_DEV_ADDR_LEN] = EEPROM.read(eeprom_addr(ADDR_SIGNATURE));
              unsigned char *hash = MD5::make_hash(data, BT_DEV_ADDR_LEN);
              memcpy(bt_dh, hash, BT_DEV_HASH_LEN);
              sprintf(bt_devname, "RNode %02X%02X", bt_dh[14], bt_dh[15]);
              free(data);

              bt_security_setup();

              bt_ready = true;
              return true;

            } else { return false; }
          } else { return false; }
        } else { return false; }
      } else { return false; }
    }

    void bt_security_setup() {
      // Serial.println("Executing BT security setup");
      if (pairing_pin == 0) { bt_update_passkey(); }
      uint32_t passkey = pairing_pin;
      // Serial.printf("Passkey is %d\n", passkey);

      uint8_t key_size = 16;
      uint8_t init_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;
      uint8_t rsp_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;

      esp_ble_auth_req_t auth_req = ESP_LE_AUTH_REQ_SC_MITM_BOND;
      uint8_t auth_option = ESP_BLE_ONLY_ACCEPT_SPECIFIED_AUTH_ENABLE;
      uint8_t oob_support = ESP_BLE_OOB_DISABLE;

      esp_ble_io_cap_t iocap = ESP_IO_CAP_OUT;

      esp_ble_gap_set_security_param(ESP_BLE_SM_SET_STATIC_PASSKEY, &passkey, sizeof(uint32_t));
      esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &auth_req, sizeof(uint8_t));
      esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &iocap, sizeof(uint8_t));
      esp_ble_gap_set_security_param(ESP_BLE_SM_MAX_KEY_SIZE, &key_size, sizeof(uint8_t));
      esp_ble_gap_set_security_param(ESP_BLE_SM_ONLY_ACCEPT_SPECIFIED_SEC_AUTH, &auth_option, sizeof(uint8_t));
      esp_ble_gap_set_security_param(ESP_BLE_SM_OOB_SUPPORT, &oob_support, sizeof(uint8_t));
      esp_ble_gap_set_security_param(ESP_BLE_SM_SET_INIT_KEY, &init_key, sizeof(uint8_t));
      esp_ble_gap_set_security_param(ESP_BLE_SM_SET_RSP_KEY, &rsp_key, sizeof(uint8_t));
    }

    void update_bt() {
      if (bt_allow_pairing && millis()-bt_pairing_started >= BT_PAIRING_TIMEOUT) {
        bt_disable_pairing();
      }
      if (bt_state == BT_STATE_CONNECTED && millis()-SerialBT.lastFlushTime >= BLE_FLUSH_TIMEOUT) {
        if (SerialBT.transmitBufferLength > 0) {
          bt_flush();
        }
      }
    }
  #endif

#elif MCU_VARIANT == MCU_NRF52
    uint32_t pairing_pin = 0;

  uint8_t eeprom_read(uint32_t mapped_addr);

  void bt_stop() {
    // Serial.println("BT Stop");
    if (bt_state != BT_STATE_OFF) {
      bt_allow_pairing = false;
      bt_state = BT_STATE_OFF;
    }
  }

  void bt_flush() { if (bt_state == BT_STATE_CONNECTED) { SerialBT.flushTXD(); } }

  void bt_disable_pairing() {
    // Serial.println("BT Disable pairing");
    bt_allow_pairing = false;
    pairing_pin = 0;
    bt_ssp_pin = 0;
    bt_state = BT_STATE_ON;
  }

  void bt_pairing_complete(uint16_t conn_handle, uint8_t auth_status) {
    // Serial.println("BT pairing complete");
    BLEConnection* connection = Bluefruit.Connection(conn_handle);
    if (auth_status == BLE_GAP_SEC_STATUS_SUCCESS) {
      ble_gap_conn_sec_mode_t security = connection->getSecureMode();
      // Serial.println("Bonding success");

      // On the NRF52 it is not possible with the Arduino library to reject
      // requests from devices with no IO capabilities, which would allow
      // bypassing pin entry through pairing using the "just works" mode.
      // Therefore, we must check the security level of the connection after
      // pairing to ensure "just works" has not been used. If it has, we need
      // to disconnect, unpair and delete any bonding information immediately.
      // Settings on the SerialBT service should prevent unauthorised access to
      // the serial port anyway, but this is still wise to do regardless.
      //
      // Note: It may be nice to have this done in the BLESecurity class in the
      // future, but as it stands right now I'd have to fork the BSP to do
      // that, which I don't fancy doing. Impact on security is likely minimal.
      // Requires investigation.

      if (security.sm == 1 && security.lv >= 3) {
          // Serial.println("Auth level success");
          bt_state = BT_STATE_CONNECTED;
          cable_state = CABLE_STATE_DISCONNECTED;
          connection->disconnect();
          bt_disable_pairing();
      } else {
          // Serial.println("Auth level failure, debonding");
          if (connection->bonded()) { connection->removeBondKey(); }
          connection->disconnect();
          bt_disable_pairing();
      }
    } else {
      // Serial.println("Bonding failure");
      connection->disconnect();
      bt_disable_pairing();
    }
  }

  bool bt_passkey_callback(uint16_t conn_handle, uint8_t const passkey[6], bool match_request) {
    // Serial.println("Passkey callback");
    if (bt_allow_pairing) {
      return true;
    }
    return false;
  }

  void bt_connect_callback(uint16_t conn_handle) {
    // Serial.println("Connect callback");
    bt_state = BT_STATE_CONNECTED;
    cable_state = CABLE_STATE_DISCONNECTED;

    BLEConnection* conn = Bluefruit.Connection(conn_handle);
    conn->requestPHY(BLE_GAP_PHY_2MBPS);
    conn->requestMtuExchange(512+3);
    conn->requestDataLengthUpdate();
  }

  void bt_disconnect_callback(uint16_t conn_handle, uint8_t reason) {
    // Serial.println("Disconnect callback");
    if (reason != BLE_GAP_SEC_STATUS_SUCCESS) {
        bt_state = BT_STATE_ON;
    }
  }

  void bt_update_passkey() {
    // Serial.println("Update passkey");
    pairing_pin = random(899999)+100000;
    bt_ssp_pin = pairing_pin;
  }

  uint32_t bt_get_passkey() {
    // Serial.println("API passkey request");
    if (pairing_pin == 0) { bt_update_passkey(); }
    return pairing_pin;
  }

  bool bt_setup_hw() {
    // Serial.println("Setup HW");
    if (!bt_ready) {
      #if HAS_EEPROM 
          if (EEPROM.read(eeprom_addr(ADDR_CONF_BT)) == BT_ENABLE_BYTE) {
      #else
          if (eeprom_read(eeprom_addr(ADDR_CONF_BT)) == BT_ENABLE_BYTE) {
      #endif
        bt_enabled = true;
      } else {
        bt_enabled = false;
      }
      Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
      Bluefruit.autoConnLed(false);
      if (Bluefruit.begin()) {
        uint32_t pin = bt_get_passkey();
        char pin_char[6];
        sprintf(pin_char,"%lu", pin);

        Bluefruit.setTxPower(8);    // Check bluefruit.h for supported values
        Bluefruit.Security.setIOCaps(true, false, false); // display, yes; yes / no, no; keyboard, no
        // This device is indeed capable of yes / no through the pairing mode
        // being set, but I have chosen to set it thus to force the input of the
        // pin on the device initiating the pairing.

        Bluefruit.Security.setMITM(true);
        Bluefruit.Security.setPairPasskeyCallback(bt_passkey_callback);
        Bluefruit.Security.setSecuredCallback(bt_connect_callback);
        Bluefruit.Security.setPIN(pin_char);
        Bluefruit.Periph.setDisconnectCallback(bt_disconnect_callback);
        Bluefruit.Security.setPairCompleteCallback(bt_pairing_complete);
        Bluefruit.Periph.setConnInterval(6, 12); // 7.5 - 15 ms

        const ble_gap_addr_t gap_addr = Bluefruit.getAddr();
        char *data = (char*)malloc(BT_DEV_ADDR_LEN+1);
        for (int i = 0; i < BT_DEV_ADDR_LEN; i++) {
            data[i] = gap_addr.addr[i];
        }
        #if HAS_EEPROM 
            data[BT_DEV_ADDR_LEN] = EEPROM.read(eeprom_addr(ADDR_SIGNATURE));
        #else
            data[BT_DEV_ADDR_LEN] = eeprom_read(eeprom_addr(ADDR_SIGNATURE));
        #endif
        unsigned char *hash = MD5::make_hash(data, BT_DEV_ADDR_LEN);
        memcpy(bt_dh, hash, BT_DEV_HASH_LEN);
        sprintf(bt_devname, "RNode %02X%02X", bt_dh[14], bt_dh[15]);
        free(data);

        bt_ready = true;
        return true;

      } else { return false; }
    } else { return false; }
  }

  void bt_start() {
    // Serial.println("BT Start");
    if (bt_state == BT_STATE_OFF) {
      Bluefruit.setName(bt_devname);
      bledis.setManufacturer(BLE_MANUFACTURER);
      bledis.setModel(BLE_MODEL);
      // start device information service
      bledis.begin();
      blebas.begin();

      // Guard to ensure SerialBT service is not duplicated through BT being power cycled
      if (!SerialBT_init) {
          SerialBT.bufferTXD(true); // enable buffering

          SerialBT.setPermission(SECMODE_ENC_WITH_MITM, SECMODE_ENC_WITH_MITM); // enable encryption for BLE serial
          SerialBT.begin();
          SerialBT_init = true;
      }

      Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
      Bluefruit.Advertising.addTxPower();

      // Include bleuart 128-bit uuid
      Bluefruit.Advertising.addService(SerialBT);

      // There is no room for Name in Advertising packet
      // Use Scan response for Name
      Bluefruit.ScanResponse.addName();

      Bluefruit.Advertising.start(0);

      bt_state = BT_STATE_ON;
     }
  }

  bool bt_init() {
    // Serial.println("BT init");
    bt_state = BT_STATE_OFF;
    if (bt_setup_hw()) {
      if (bt_enabled && !console_active) bt_start();
      return true;
    } else {
      return false;
    }
  }

  void bt_enable_pairing() {
    // Serial.println("BT enable pairing");
    if (bt_state == BT_STATE_OFF) bt_start();

    uint32_t pin = bt_get_passkey();
    char pin_char[6];
    sprintf(pin_char,"%lu", pin);
    Bluefruit.Security.setPIN(pin_char);

    bt_allow_pairing = true;
    bt_pairing_started = millis();
    bt_state = BT_STATE_PAIRING;
    kiss_indicate_btpin();
  }

  void bt_debond_all() { }

  void update_bt() {
    if (bt_allow_pairing && millis()-bt_pairing_started >= BT_PAIRING_TIMEOUT) {
      bt_disable_pairing();
    }
  }
#endif
