Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b63e2798 authored by Jakub Pawlowski's avatar Jakub Pawlowski Committed by Gerrit Code Review
Browse files

Merge changes If7a360bf,Ibc20c110

* changes:
  gatt_cleanup_upon_disc refactor
  Simplify read/write start
parents ea1a1ad9 fa636bde
Loading
Loading
Loading
Loading
+2 −9
Original line number Diff line number Diff line
@@ -903,10 +903,7 @@ tGATT_STATUS GATTC_Read(uint16_t conn_id, tGATT_READ_TYPE type,
        break;
    }
    /* start security check */
    if (gatt_security_check_start(p_clcb) == false) {
      status = GATT_NO_RESOURCES;
      gatt_clcb_dealloc(p_clcb);
    }
    gatt_security_check_start(p_clcb);
  } else {
    status = GATT_NO_RESOURCES;
  }
@@ -965,11 +962,7 @@ tGATT_STATUS GATTC_Write(uint16_t conn_id, tGATT_WRITE_TYPE type,
      p->offset = 0;
    }

    if (gatt_security_check_start(p_clcb) == false) {
      status = GATT_NO_RESOURCES;
    }

    if (status == GATT_NO_RESOURCES) gatt_clcb_dealloc(p_clcb);
    gatt_security_check_start(p_clcb);
  } else {
    status = GATT_NO_RESOURCES;
  }
+16 −32
Original line number Diff line number Diff line
@@ -390,51 +390,42 @@ static bool gatt_convert_sec_action(tGATT_SEC_ACTION gatt_sec_act,

  return status;
}
/*******************************************************************************
 *
 * Function         gatt_check_enc_req
 *
 * Description      check link security.
 *
 * Returns          true if encrypted, otherwise false.
 *
 ******************************************************************************/
bool gatt_security_check_start(tGATT_CLCB* p_clcb) {

/** check link security */
void gatt_security_check_start(tGATT_CLCB* p_clcb) {
  tGATT_TCB* p_tcb = p_clcb->p_tcb;
  tGATT_SEC_ACTION gatt_sec_act;
  tBTM_BLE_SEC_ACT btm_ble_sec_act;
  bool status = true;
  tBTM_STATUS btm_status;
  tGATT_SEC_ACTION sec_act_old = gatt_get_sec_act(p_tcb);

  gatt_sec_act = gatt_determine_sec_act(p_clcb);
  tGATT_SEC_ACTION gatt_sec_act = gatt_determine_sec_act(p_clcb);

  if (sec_act_old == GATT_SEC_NONE) gatt_set_sec_act(p_tcb, gatt_sec_act);

  switch (gatt_sec_act) {
    case GATT_SEC_SIGN_DATA:
      GATT_TRACE_DEBUG("gatt_security_check_start: Do data signing");
      GATT_TRACE_DEBUG("%s: Do data signing", __func__);
      gatt_sign_data(p_clcb);
      break;
    case GATT_SEC_ENCRYPT:
    case GATT_SEC_ENCRYPT_NO_MITM:
    case GATT_SEC_ENCRYPT_MITM:
      if (sec_act_old < GATT_SEC_ENCRYPT) {
        GATT_TRACE_DEBUG(
            "gatt_security_check_start: Encrypt now or key upgreade first");
        GATT_TRACE_DEBUG("%s: Encrypt now or key upgreade first", __func__);
        tBTM_BLE_SEC_ACT btm_ble_sec_act;
        gatt_convert_sec_action(gatt_sec_act, &btm_ble_sec_act);
        btm_status =
        tBTM_STATUS btm_status =
            BTM_SetEncryption(p_tcb->peer_bda, p_tcb->transport,
                              gatt_enc_cmpl_cback, NULL, btm_ble_sec_act);
        if ((btm_status != BTM_SUCCESS) && (btm_status != BTM_CMD_STARTED)) {
          GATT_TRACE_ERROR(
              "gatt_security_check_start BTM_SetEncryption failed "
              "btm_status=%d",
              btm_status);
          status = false;
          GATT_TRACE_ERROR("%s BTM_SetEncryption failed btm_status=%d",
                           __func__, btm_status);
          gatt_set_sec_act(p_tcb, GATT_SEC_NONE);
          gatt_set_ch_state(p_tcb, GATT_CH_OPEN);

          gatt_end_operation(p_clcb, GATT_INSUF_ENCRYPTION, NULL);
          return;
        }
      }
      if (status) p_tcb->pending_enc_clcb.push(p_clcb);
      p_tcb->pending_enc_clcb.push(p_clcb);
      break;
    case GATT_SEC_ENC_PENDING:
      p_tcb->pending_enc_clcb.push(p_clcb);
@@ -444,11 +435,4 @@ bool gatt_security_check_start(tGATT_CLCB* p_clcb) {
      gatt_sec_check_complete(true, p_clcb, gatt_sec_act);
      break;
  }

  if (status == false) {
    gatt_set_sec_act(p_tcb, GATT_SEC_NONE);
    gatt_set_ch_state(p_tcb, GATT_CH_OPEN);
  }

  return status;
}
+1 −2
Original line number Diff line number Diff line
@@ -549,9 +549,8 @@ extern void gatt_send_queue_write_cancel(tGATT_TCB& tcb, tGATT_CLCB* p_clcb,
                                         tGATT_EXEC_FLAG flag);

/* gatt_auth.cc */
extern bool gatt_security_check_start(tGATT_CLCB* p_clcb);
extern void gatt_security_check_start(tGATT_CLCB* p_clcb);
extern void gatt_verify_signature(tGATT_TCB& tcb, BT_HDR* p_buf);
extern tGATT_SEC_ACTION gatt_determine_sec_act(tGATT_CLCB* p_clcb);
extern tGATT_STATUS gatt_get_link_encrypt_status(tGATT_TCB& tcb);
extern tGATT_SEC_ACTION gatt_get_sec_act(tGATT_TCB* p_tcb);
extern void gatt_set_sec_act(tGATT_TCB* p_tcb, tGATT_SEC_ACTION sec_act);
+36 −48
Original line number Diff line number Diff line
@@ -1042,6 +1042,7 @@ tGATT_CLCB* gatt_clcb_alloc(uint16_t conn_id) {
      break;
    }
  }

  return p_clcb;
}

@@ -1386,40 +1387,27 @@ void gatt_end_operation(tGATT_CLCB* p_clcb, tGATT_STATUS status, void* p_data) {
        operation, p_disc_cmpl_cb, p_cmpl_cb);
}

/*******************************************************************************
 *
 * Function         gatt_cleanup_upon_disc
 *
 * Description      This function cleans up the control blocks when L2CAP
 *                  channel disconnect.
 *
 * Returns          16 bits uuid.
 *
 ******************************************************************************/
/** This function cleans up the control blocks when L2CAP channel disconnect */
void gatt_cleanup_upon_disc(BD_ADDR bda, uint16_t reason,
                            tBT_TRANSPORT transport) {
  tGATT_TCB* p_tcb = NULL;
  tGATT_CLCB* p_clcb;
  uint8_t i;
  uint16_t conn_id;
  tGATT_REG* p_reg = NULL;
  GATT_TRACE_DEBUG("%s", __func__);

  GATT_TRACE_DEBUG("gatt_cleanup_upon_disc ");
  tGATT_TCB* p_tcb = gatt_find_tcb_by_addr(bda, transport);
  if (!p_tcb) return;

  p_tcb = gatt_find_tcb_by_addr(bda, transport);
  if (p_tcb != NULL) {
    GATT_TRACE_DEBUG("found p_tcb ");
  gatt_set_ch_state(p_tcb, GATT_CH_CLOSE);
    for (i = 0; i < GATT_CL_MAX_LCB; i++) {
      p_clcb = &gatt_cb.clcb[i];
      if (p_clcb->in_use && p_clcb->p_tcb == p_tcb) {
  for (uint8_t i = 0; i < GATT_CL_MAX_LCB; i++) {
    tGATT_CLCB* p_clcb = &gatt_cb.clcb[i];
    if (!p_clcb->in_use || p_clcb->p_tcb != p_tcb) continue;

    alarm_cancel(p_clcb->gatt_rsp_timer_ent);
    GATT_TRACE_DEBUG("found p_clcb conn_id=%d", p_clcb->conn_id);
        if (p_clcb->operation != GATTC_OPTYPE_NONE)
          gatt_end_operation(p_clcb, GATT_ERROR, NULL);

    if (p_clcb->operation == GATTC_OPTYPE_NONE) {
      gatt_clcb_dealloc(p_clcb);
      continue;
    }

    gatt_end_operation(p_clcb, GATT_ERROR, NULL);
  }

  alarm_free(p_tcb->ind_ack_timer);
@@ -1430,19 +1418,19 @@ void gatt_cleanup_upon_disc(BD_ADDR bda, uint16_t reason,
  fixed_queue_free(p_tcb->sr_cmd.multi_rsp_q, NULL);
  p_tcb->sr_cmd.multi_rsp_q = NULL;

    for (i = 0; i < GATT_MAX_APPS; i++) {
      p_reg = &gatt_cb.cl_rcb[i];
  for (uint8_t i = 0; i < GATT_MAX_APPS; i++) {
    tGATT_REG* p_reg = &gatt_cb.cl_rcb[i];
    if (p_reg->in_use && p_reg->app_cb.p_conn_cb) {
        conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_reg->gatt_if);
      uint16_t conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_reg->gatt_if);
      GATT_TRACE_DEBUG("found p_reg tcb_idx=%d gatt_if=%d  conn_id=0x%x",
                       p_tcb->tcb_idx, p_reg->gatt_if, conn_id);
      (*p_reg->app_cb.p_conn_cb)(p_reg->gatt_if, bda, conn_id, false, reason,
                                 transport);
    }
  }

  *p_tcb = tGATT_TCB();
  }
  GATT_TRACE_DEBUG("exit gatt_cleanup_upon_disc ");
  GATT_TRACE_DEBUG("%s: exit", __func__);
}
/*******************************************************************************
 *