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

Commit bc9ecb57 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Keep UIPC in local variable, rather than global struct."

parents f4291d7d c10ec59c
Loading
Loading
Loading
Loading
+37 −30
Original line number Original line Diff line number Diff line
@@ -48,15 +48,16 @@ static void btif_a2dp_ctrl_cb(tUIPC_CH_ID ch_id, tUIPC_EVENT event);


/* We can have max one command pending */
/* We can have max one command pending */
static tA2DP_CTRL_CMD a2dp_cmd_pending = A2DP_CTRL_CMD_NONE;
static tA2DP_CTRL_CMD a2dp_cmd_pending = A2DP_CTRL_CMD_NONE;
std::unique_ptr<tUIPC_STATE> a2dp_uipc;


void btif_a2dp_control_init(void) {
void btif_a2dp_control_init(void) {
  UIPC_Init(NULL, UIPC_USER_A2DP);
  a2dp_uipc = UIPC_Init();
  UIPC_Open(UIPC_CH_ID_AV_CTRL, btif_a2dp_ctrl_cb);
  UIPC_Open(*a2dp_uipc, UIPC_CH_ID_AV_CTRL, btif_a2dp_ctrl_cb);
}
}


void btif_a2dp_control_cleanup(void) {
void btif_a2dp_control_cleanup(void) {
  /* This calls blocks until UIPC is fully closed */
  /* This calls blocks until UIPC is fully closed */
  UIPC_Close(UIPC_CH_ID_ALL, UIPC_USER_A2DP);
  UIPC_Close(*a2dp_uipc, UIPC_CH_ID_ALL);
}
}


static void btif_a2dp_recv_ctrl_data(void) {
static void btif_a2dp_recv_ctrl_data(void) {
@@ -64,13 +65,13 @@ static void btif_a2dp_recv_ctrl_data(void) {
  int n;
  int n;


  uint8_t read_cmd = 0; /* The read command size is one octet */
  uint8_t read_cmd = 0; /* The read command size is one octet */
  n = UIPC_Read(UIPC_CH_ID_AV_CTRL, NULL, &read_cmd, 1);
  n = UIPC_Read(*a2dp_uipc, UIPC_CH_ID_AV_CTRL, NULL, &read_cmd, 1);
  cmd = static_cast<tA2DP_CTRL_CMD>(read_cmd);
  cmd = static_cast<tA2DP_CTRL_CMD>(read_cmd);


  /* detach on ctrl channel means audioflinger process was terminated */
  /* detach on ctrl channel means audioflinger process was terminated */
  if (n == 0) {
  if (n == 0) {
    APPL_TRACE_WARNING("%s: CTRL CH DETACHED", __func__);
    APPL_TRACE_WARNING("%s: CTRL CH DETACHED", __func__);
    UIPC_Close(UIPC_CH_ID_AV_CTRL, UIPC_USER_A2DP);
    UIPC_Close(*a2dp_uipc, UIPC_CH_ID_AV_CTRL);
    return;
    return;
  }
  }


@@ -117,7 +118,7 @@ static void btif_a2dp_recv_ctrl_data(void) {


      if (btif_av_stream_ready()) {
      if (btif_av_stream_ready()) {
        /* Setup audio data channel listener */
        /* Setup audio data channel listener */
        UIPC_Open(UIPC_CH_ID_AV_AUDIO, btif_a2dp_data_cb);
        UIPC_Open(*a2dp_uipc, UIPC_CH_ID_AV_AUDIO, btif_a2dp_data_cb);


        /*
        /*
         * Post start event and wait for audio path to open.
         * Post start event and wait for audio path to open.
@@ -135,7 +136,7 @@ static void btif_a2dp_recv_ctrl_data(void) {
         * Already started, setup audio data channel listener and ACK
         * Already started, setup audio data channel listener and ACK
         * back immediately.
         * back immediately.
         */
         */
        UIPC_Open(UIPC_CH_ID_AV_AUDIO, btif_a2dp_data_cb);
        UIPC_Open(*a2dp_uipc, UIPC_CH_ID_AV_AUDIO, btif_a2dp_data_cb);
        btif_a2dp_command_ack(A2DP_CTRL_ACK_SUCCESS);
        btif_a2dp_command_ack(A2DP_CTRL_ACK_SUCCESS);
        break;
        break;
      }
      }
@@ -174,9 +175,10 @@ static void btif_a2dp_recv_ctrl_data(void) {
      tA2DP_CHANNEL_COUNT channel_count = btif_a2dp_sink_get_channel_count();
      tA2DP_CHANNEL_COUNT channel_count = btif_a2dp_sink_get_channel_count();


      btif_a2dp_command_ack(A2DP_CTRL_ACK_SUCCESS);
      btif_a2dp_command_ack(A2DP_CTRL_ACK_SUCCESS);
      UIPC_Send(UIPC_CH_ID_AV_CTRL, 0, reinterpret_cast<uint8_t*>(&sample_rate),
      UIPC_Send(*a2dp_uipc, UIPC_CH_ID_AV_CTRL, 0,
                reinterpret_cast<uint8_t*>(&sample_rate),
                sizeof(tA2DP_SAMPLE_RATE));
                sizeof(tA2DP_SAMPLE_RATE));
      UIPC_Send(UIPC_CH_ID_AV_CTRL, 0, &channel_count,
      UIPC_Send(*a2dp_uipc, UIPC_CH_ID_AV_CTRL, 0, &channel_count,
                sizeof(tA2DP_CHANNEL_COUNT));
                sizeof(tA2DP_CHANNEL_COUNT));
      break;
      break;
    }
    }
@@ -199,24 +201,26 @@ static void btif_a2dp_recv_ctrl_data(void) {


      btif_a2dp_command_ack(A2DP_CTRL_ACK_SUCCESS);
      btif_a2dp_command_ack(A2DP_CTRL_ACK_SUCCESS);
      // Send the current codec config
      // Send the current codec config
      UIPC_Send(UIPC_CH_ID_AV_CTRL, 0,
      UIPC_Send(*a2dp_uipc, UIPC_CH_ID_AV_CTRL, 0,
                reinterpret_cast<const uint8_t*>(&codec_config.sample_rate),
                reinterpret_cast<const uint8_t*>(&codec_config.sample_rate),
                sizeof(btav_a2dp_codec_sample_rate_t));
                sizeof(btav_a2dp_codec_sample_rate_t));
      UIPC_Send(UIPC_CH_ID_AV_CTRL, 0,
      UIPC_Send(*a2dp_uipc, UIPC_CH_ID_AV_CTRL, 0,
                reinterpret_cast<const uint8_t*>(&codec_config.bits_per_sample),
                reinterpret_cast<const uint8_t*>(&codec_config.bits_per_sample),
                sizeof(btav_a2dp_codec_bits_per_sample_t));
                sizeof(btav_a2dp_codec_bits_per_sample_t));
      UIPC_Send(UIPC_CH_ID_AV_CTRL, 0,
      UIPC_Send(*a2dp_uipc, UIPC_CH_ID_AV_CTRL, 0,
                reinterpret_cast<const uint8_t*>(&codec_config.channel_mode),
                reinterpret_cast<const uint8_t*>(&codec_config.channel_mode),
                sizeof(btav_a2dp_codec_channel_mode_t));
                sizeof(btav_a2dp_codec_channel_mode_t));
      // Send the current codec capability
      // Send the current codec capability
      UIPC_Send(UIPC_CH_ID_AV_CTRL, 0,
      UIPC_Send(*a2dp_uipc, UIPC_CH_ID_AV_CTRL, 0,
                reinterpret_cast<const uint8_t*>(&codec_capability.sample_rate),
                reinterpret_cast<const uint8_t*>(&codec_capability.sample_rate),
                sizeof(btav_a2dp_codec_sample_rate_t));
                sizeof(btav_a2dp_codec_sample_rate_t));
      UIPC_Send(UIPC_CH_ID_AV_CTRL, 0, reinterpret_cast<const uint8_t*>(
      UIPC_Send(
                                           &codec_capability.bits_per_sample),
          *a2dp_uipc, UIPC_CH_ID_AV_CTRL, 0,
          reinterpret_cast<const uint8_t*>(&codec_capability.bits_per_sample),
          sizeof(btav_a2dp_codec_bits_per_sample_t));
          sizeof(btav_a2dp_codec_bits_per_sample_t));
      UIPC_Send(UIPC_CH_ID_AV_CTRL, 0, reinterpret_cast<const uint8_t*>(
      UIPC_Send(
                                           &codec_capability.channel_mode),
          *a2dp_uipc, UIPC_CH_ID_AV_CTRL, 0,
          reinterpret_cast<const uint8_t*>(&codec_capability.channel_mode),
          sizeof(btav_a2dp_codec_channel_mode_t));
          sizeof(btav_a2dp_codec_channel_mode_t));
      break;
      break;
    }
    }
@@ -229,7 +233,7 @@ static void btif_a2dp_recv_ctrl_data(void) {


      btif_a2dp_command_ack(A2DP_CTRL_ACK_SUCCESS);
      btif_a2dp_command_ack(A2DP_CTRL_ACK_SUCCESS);
      // Send the current codec config
      // Send the current codec config
      if (UIPC_Read(UIPC_CH_ID_AV_CTRL, 0,
      if (UIPC_Read(*a2dp_uipc, UIPC_CH_ID_AV_CTRL, 0,
                    reinterpret_cast<uint8_t*>(&codec_config.sample_rate),
                    reinterpret_cast<uint8_t*>(&codec_config.sample_rate),
                    sizeof(btav_a2dp_codec_sample_rate_t)) !=
                    sizeof(btav_a2dp_codec_sample_rate_t)) !=
          sizeof(btav_a2dp_codec_sample_rate_t)) {
          sizeof(btav_a2dp_codec_sample_rate_t)) {
@@ -237,7 +241,7 @@ static void btif_a2dp_recv_ctrl_data(void) {
                         __func__);
                         __func__);
        break;
        break;
      }
      }
      if (UIPC_Read(UIPC_CH_ID_AV_CTRL, 0,
      if (UIPC_Read(*a2dp_uipc, UIPC_CH_ID_AV_CTRL, 0,
                    reinterpret_cast<uint8_t*>(&codec_config.bits_per_sample),
                    reinterpret_cast<uint8_t*>(&codec_config.bits_per_sample),
                    sizeof(btav_a2dp_codec_bits_per_sample_t)) !=
                    sizeof(btav_a2dp_codec_bits_per_sample_t)) !=
          sizeof(btav_a2dp_codec_bits_per_sample_t)) {
          sizeof(btav_a2dp_codec_bits_per_sample_t)) {
@@ -245,7 +249,7 @@ static void btif_a2dp_recv_ctrl_data(void) {
                         __func__);
                         __func__);
        break;
        break;
      }
      }
      if (UIPC_Read(UIPC_CH_ID_AV_CTRL, 0,
      if (UIPC_Read(*a2dp_uipc, UIPC_CH_ID_AV_CTRL, 0,
                    reinterpret_cast<uint8_t*>(&codec_config.channel_mode),
                    reinterpret_cast<uint8_t*>(&codec_config.channel_mode),
                    sizeof(btav_a2dp_codec_channel_mode_t)) !=
                    sizeof(btav_a2dp_codec_channel_mode_t)) !=
          sizeof(btav_a2dp_codec_channel_mode_t)) {
          sizeof(btav_a2dp_codec_channel_mode_t)) {
@@ -270,17 +274,19 @@ static void btif_a2dp_recv_ctrl_data(void) {
    case A2DP_CTRL_GET_PRESENTATION_POSITION: {
    case A2DP_CTRL_GET_PRESENTATION_POSITION: {
      btif_a2dp_command_ack(A2DP_CTRL_ACK_SUCCESS);
      btif_a2dp_command_ack(A2DP_CTRL_ACK_SUCCESS);


      UIPC_Send(UIPC_CH_ID_AV_CTRL, 0,
      UIPC_Send(*a2dp_uipc, UIPC_CH_ID_AV_CTRL, 0,
                (uint8_t*)&(delay_report_stats.total_bytes_read),
                (uint8_t*)&(delay_report_stats.total_bytes_read),
                sizeof(uint64_t));
                sizeof(uint64_t));
      UIPC_Send(UIPC_CH_ID_AV_CTRL, 0,
      UIPC_Send(*a2dp_uipc, UIPC_CH_ID_AV_CTRL, 0,
                (uint8_t*)&(delay_report_stats.audio_delay), sizeof(uint16_t));
                (uint8_t*)&(delay_report_stats.audio_delay), sizeof(uint16_t));


      uint32_t seconds = delay_report_stats.timestamp.tv_sec;
      uint32_t seconds = delay_report_stats.timestamp.tv_sec;
      UIPC_Send(UIPC_CH_ID_AV_CTRL, 0, (uint8_t*)&seconds, sizeof(seconds));
      UIPC_Send(*a2dp_uipc, UIPC_CH_ID_AV_CTRL, 0, (uint8_t*)&seconds,
                sizeof(seconds));


      uint32_t nsec = delay_report_stats.timestamp.tv_nsec;
      uint32_t nsec = delay_report_stats.timestamp.tv_nsec;
      UIPC_Send(UIPC_CH_ID_AV_CTRL, 0, (uint8_t*)&nsec, sizeof(nsec));
      UIPC_Send(*a2dp_uipc, UIPC_CH_ID_AV_CTRL, 0, (uint8_t*)&nsec,
                sizeof(nsec));
      break;
      break;
    }
    }
    default:
    default:
@@ -304,7 +310,7 @@ static void btif_a2dp_ctrl_cb(UNUSED_ATTR tUIPC_CH_ID ch_id,
    case UIPC_CLOSE_EVT:
    case UIPC_CLOSE_EVT:
      /* restart ctrl server unless we are shutting down */
      /* restart ctrl server unless we are shutting down */
      if (btif_a2dp_source_media_task_is_running())
      if (btif_a2dp_source_media_task_is_running())
        UIPC_Open(UIPC_CH_ID_AV_CTRL, btif_a2dp_ctrl_cb);
        UIPC_Open(*a2dp_uipc, UIPC_CH_ID_AV_CTRL, btif_a2dp_ctrl_cb);
      break;
      break;


    case UIPC_RX_DATA_READY_EVT:
    case UIPC_RX_DATA_READY_EVT:
@@ -329,8 +335,9 @@ static void btif_a2dp_data_cb(UNUSED_ATTR tUIPC_CH_ID ch_id,
       * Read directly from media task from here on (keep callback for
       * Read directly from media task from here on (keep callback for
       * connection events.
       * connection events.
       */
       */
      UIPC_Ioctl(UIPC_CH_ID_AV_AUDIO, UIPC_REG_REMOVE_ACTIVE_READSET, NULL);
      UIPC_Ioctl(*a2dp_uipc, UIPC_CH_ID_AV_AUDIO,
      UIPC_Ioctl(UIPC_CH_ID_AV_AUDIO, UIPC_SET_READ_POLL_TMO,
                 UIPC_REG_REMOVE_ACTIVE_READSET, NULL);
      UIPC_Ioctl(*a2dp_uipc, UIPC_CH_ID_AV_AUDIO, UIPC_SET_READ_POLL_TMO,
                 reinterpret_cast<void*>(A2DP_DATA_READ_POLL_MS));
                 reinterpret_cast<void*>(A2DP_DATA_READ_POLL_MS));


      if (btif_av_get_peer_sep() == AVDT_TSEP_SNK) {
      if (btif_av_get_peer_sep() == AVDT_TSEP_SNK) {
@@ -378,7 +385,7 @@ void btif_a2dp_command_ack(tA2DP_CTRL_ACK status) {
  a2dp_cmd_pending = A2DP_CTRL_CMD_NONE;
  a2dp_cmd_pending = A2DP_CTRL_CMD_NONE;


  /* Acknowledge start request */
  /* Acknowledge start request */
  UIPC_Send(UIPC_CH_ID_AV_CTRL, 0, &ack, sizeof(ack));
  UIPC_Send(*a2dp_uipc, UIPC_CH_ID_AV_CTRL, 0, &ack, sizeof(ack));
}
}


void btif_a2dp_control_log_bytes_read(uint32_t bytes_read) {
void btif_a2dp_control_log_bytes_read(uint32_t bytes_read) {
+7 −4
Original line number Original line Diff line number Diff line
@@ -52,6 +52,8 @@
using system_bt_osi::BluetoothMetricsLogger;
using system_bt_osi::BluetoothMetricsLogger;
using system_bt_osi::A2dpSessionMetrics;
using system_bt_osi::A2dpSessionMetrics;


extern std::unique_ptr<tUIPC_STATE> a2dp_uipc;

/**
/**
 * The typical runlevel of the tx queue size is ~1 buffer
 * The typical runlevel of the tx queue size is ~1 buffer
 * but due to link flow control or thread preemption in lower
 * but due to link flow control or thread preemption in lower
@@ -694,13 +696,13 @@ static void btif_a2dp_source_audio_tx_stop_event(void) {


  // Keep track of audio data still left in the pipe
  // Keep track of audio data still left in the pipe
  btif_a2dp_control_log_bytes_read(
  btif_a2dp_control_log_bytes_read(
      UIPC_Read(UIPC_CH_ID_AV_AUDIO, &event, p_buf, sizeof(p_buf)));
      UIPC_Read(*a2dp_uipc, UIPC_CH_ID_AV_AUDIO, &event, p_buf, sizeof(p_buf)));


  /* Stop the timer first */
  /* Stop the timer first */
  alarm_free(btif_a2dp_source_cb.media_alarm);
  alarm_free(btif_a2dp_source_cb.media_alarm);
  btif_a2dp_source_cb.media_alarm = nullptr;
  btif_a2dp_source_cb.media_alarm = nullptr;


  UIPC_Close(UIPC_CH_ID_AV_AUDIO, UIPC_USER_A2DP);
  UIPC_Close(*a2dp_uipc, UIPC_CH_ID_AV_AUDIO);


  /*
  /*
   * Try to send acknowldegment once the media stream is
   * Try to send acknowldegment once the media stream is
@@ -759,7 +761,8 @@ static void btif_a2dp_source_audio_handle_timer(void) {


static uint32_t btif_a2dp_source_read_callback(uint8_t* p_buf, uint32_t len) {
static uint32_t btif_a2dp_source_read_callback(uint8_t* p_buf, uint32_t len) {
  uint16_t event;
  uint16_t event;
  uint32_t bytes_read = UIPC_Read(UIPC_CH_ID_AV_AUDIO, &event, p_buf, len);
  uint32_t bytes_read =
      UIPC_Read(*a2dp_uipc, UIPC_CH_ID_AV_AUDIO, &event, p_buf, len);


  if (bytes_read < len) {
  if (bytes_read < len) {
    LOG_WARN(LOG_TAG, "%s: UNDERFLOW: ONLY READ %d BYTES OUT OF %d", __func__,
    LOG_WARN(LOG_TAG, "%s: UNDERFLOW: ONLY READ %d BYTES OUT OF %d", __func__,
@@ -869,7 +872,7 @@ static void btif_a2dp_source_audio_tx_flush_event(void) {
      time_get_os_boottime_us();
      time_get_os_boottime_us();
  fixed_queue_flush(btif_a2dp_source_cb.tx_audio_queue, osi_free);
  fixed_queue_flush(btif_a2dp_source_cb.tx_audio_queue, osi_free);


  UIPC_Ioctl(UIPC_CH_ID_AV_AUDIO, UIPC_REQ_RX_FLUSH, nullptr);
  UIPC_Ioctl(*a2dp_uipc, UIPC_CH_ID_AV_AUDIO, UIPC_REQ_RX_FLUSH, nullptr);
}
}


static bool btif_a2dp_source_audio_tx_flush_req(void) {
static bool btif_a2dp_source_audio_tx_flush_req(void) {
+30 −16
Original line number Original line Diff line number Diff line
@@ -37,13 +37,6 @@ typedef enum {
  UIPC_TX_DATA_READY_EVT = 0x0010
  UIPC_TX_DATA_READY_EVT = 0x0010
} tUIPC_EVENT;
} tUIPC_EVENT;


/* UIPC users */
typedef enum {
  UIPC_USER_A2DP = 0,
  UIPC_USER_HEARING_AID = 1,
  UIPC_USER_NUM = 2
} tUIPC_USER;

/*
/*
 * UIPC IOCTL Requests
 * UIPC IOCTL Requests
 */
 */
@@ -62,12 +55,33 @@ typedef void(tUIPC_RCV_CBACK)(


const char* dump_uipc_event(tUIPC_EVENT event);
const char* dump_uipc_event(tUIPC_EVENT event);


typedef struct {
  int srvfd;
  int fd;
  int read_poll_tmo_ms;
  int task_evt_flags; /* event flags pending to be processed in read task */
  tUIPC_RCV_CBACK* cback;
} tUIPC_CHAN;

struct tUIPC_STATE {
  pthread_t tid; /* main thread id */
  int running;
  std::recursive_mutex mutex;

  fd_set active_set;
  fd_set read_set;
  int max_fd;
  int signal_fds[2];

  tUIPC_CHAN ch[UIPC_CH_NUM];
};

/**
/**
 * Initialize UIPC module
 * Initialize UIPC module
 *
 *
 * @param user User ID who uses UIPC
 * @param user User ID who uses UIPC
 */
 */
void UIPC_Init(void*, int user);
std::unique_ptr<tUIPC_STATE> UIPC_Init();


/**
/**
 * Open a UIPC channel
 * Open a UIPC channel
@@ -76,15 +90,14 @@ void UIPC_Init(void*, int user);
 * @param p_cback Callback handler
 * @param p_cback Callback handler
 * @return true on success, otherwise false
 * @return true on success, otherwise false
 */
 */
bool UIPC_Open(tUIPC_CH_ID ch_id, tUIPC_RCV_CBACK* p_cback);
bool UIPC_Open(tUIPC_STATE& uipc, tUIPC_CH_ID ch_id, tUIPC_RCV_CBACK* p_cback);


/**
/**
 * Closes a channel in UIPC or the entire UIPC module
 * Closes a channel in UIPC or the entire UIPC module
 *
 *
 * @param ch_id Channel ID; if ch_id is UIPC_CH_ID_ALL, then cleanup UIPC
 * @param ch_id Channel ID; if ch_id is UIPC_CH_ID_ALL, then cleanup UIPC
 * @param user User ID who uses UIPC
 */
 */
void UIPC_Close(tUIPC_CH_ID ch_id, int user);
void UIPC_Close(tUIPC_STATE& uipc, tUIPC_CH_ID ch_id);


/**
/**
 * Send a message over UIPC
 * Send a message over UIPC
@@ -95,8 +108,8 @@ void UIPC_Close(tUIPC_CH_ID ch_id, int user);
 * @param msglen Message length
 * @param msglen Message length
 * @return true on success, otherwise false
 * @return true on success, otherwise false
 */
 */
bool UIPC_Send(tUIPC_CH_ID ch_id, uint16_t msg_evt, const uint8_t* p_buf,
bool UIPC_Send(tUIPC_STATE& uipc, tUIPC_CH_ID ch_id, uint16_t msg_evt,
               uint16_t msglen);
               const uint8_t* p_buf, uint16_t msglen);


/**
/**
 * Read a message from UIPC
 * Read a message from UIPC
@@ -107,8 +120,8 @@ bool UIPC_Send(tUIPC_CH_ID ch_id, uint16_t msg_evt, const uint8_t* p_buf,
 * @param len Bytes to read
 * @param len Bytes to read
 * @return true on success, otherwise false
 * @return true on success, otherwise false
 */
 */
uint32_t UIPC_Read(tUIPC_CH_ID ch_id, uint16_t* p_msg_evt, uint8_t* p_buf,
uint32_t UIPC_Read(tUIPC_STATE& uipc, tUIPC_CH_ID ch_id, uint16_t* p_msg_evt,
                   uint32_t len);
                   uint8_t* p_buf, uint32_t len);


/**
/**
 * Control the UIPC parameter
 * Control the UIPC parameter
@@ -118,6 +131,7 @@ uint32_t UIPC_Read(tUIPC_CH_ID ch_id, uint16_t* p_msg_evt, uint8_t* p_buf,
 * @param param Optional parameters
 * @param param Optional parameters
 * @return true on success, otherwise false
 * @return true on success, otherwise false
 */
 */
bool UIPC_Ioctl(tUIPC_CH_ID ch_id, uint32_t request, void* param);
bool UIPC_Ioctl(tUIPC_STATE& uipc, tUIPC_CH_ID ch_id, uint32_t request,
                void* param);


#endif /* UIPC_H */
#endif /* UIPC_H */