DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (4a108e94d3e2)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "BluetoothA2dpHALInterface.h"
#include "BluetoothHALHelpers.h"

BEGIN_BLUETOOTH_NAMESPACE

typedef
  BluetoothHALInterfaceRunnable0<BluetoothA2dpResultHandler, void>
  BluetoothA2dpHALResultRunnable;

typedef
  BluetoothHALInterfaceRunnable1<BluetoothA2dpResultHandler, void,
                                 BluetoothStatus, BluetoothStatus>
  BluetoothA2dpHALErrorRunnable;

static nsresult
DispatchBluetoothA2dpHALResult(
  BluetoothA2dpResultHandler* aRes,
  void (BluetoothA2dpResultHandler::*aMethod)(),
  BluetoothStatus aStatus)
{
  MOZ_ASSERT(aRes);

  nsRunnable* runnable;

  if (aStatus == STATUS_SUCCESS) {
    runnable = new BluetoothA2dpHALResultRunnable(aRes, aMethod);
  } else {
    runnable = new BluetoothA2dpHALErrorRunnable(aRes,
      &BluetoothA2dpResultHandler::OnError, aStatus);
  }
  nsresult rv = NS_DispatchToMainThread(runnable);
  if (NS_FAILED(rv)) {
    BT_WARNING("NS_DispatchToMainThread failed: %X", rv);
  }
  return rv;
}

// Notification handling
//

static BluetoothA2dpNotificationHandler* sA2dpNotificationHandler;

struct BluetoothA2dpHALCallback
{
  class A2dpNotificationHandlerWrapper
  {
  public:
    typedef BluetoothA2dpNotificationHandler ObjectType;

    static ObjectType* GetInstance()
    {
      MOZ_ASSERT(NS_IsMainThread());

      return sA2dpNotificationHandler;
    }
  };

  // Notifications

  typedef BluetoothNotificationHALRunnable2<
    A2dpNotificationHandlerWrapper, void,
    BluetoothA2dpConnectionState, nsString,
    BluetoothA2dpConnectionState, const nsAString&>
    ConnectionStateNotification;

  typedef BluetoothNotificationHALRunnable2<
    A2dpNotificationHandlerWrapper, void,
    BluetoothA2dpAudioState, nsString,
    BluetoothA2dpAudioState, const nsAString&>
    AudioStateNotification;

  typedef BluetoothNotificationHALRunnable3<
    A2dpNotificationHandlerWrapper, void,
    nsString, uint32_t, uint8_t,
    const nsAString&, uint32_t, uint8_t>
    AudioConfigNotification;

  // Bluedroid A2DP callbacks

  static void
  ConnectionState(btav_connection_state_t aState, bt_bdaddr_t* aBdAddr)
  {
    ConnectionStateNotification::Dispatch(
      &BluetoothA2dpNotificationHandler::ConnectionStateNotification,
      aState, aBdAddr);
  }

  static void
  AudioState(btav_audio_state_t aState, bt_bdaddr_t* aBdAddr)
  {
    AudioStateNotification::Dispatch(
      &BluetoothA2dpNotificationHandler::AudioStateNotification,
      aState, aBdAddr);
  }

#if ANDROID_VERSION >= 21
  static void
  AudioConfig(bt_bdaddr_t *aBdAddr, uint32_t aSampleRate, uint8_t aChannelCount)
  {
    AudioConfigNotification::Dispatch(
      &BluetoothA2dpNotificationHandler::AudioConfigNotification,
      aBdAddr, aSampleRate, aChannelCount);
  }
#endif
};

// Interface
//

BluetoothA2dpHALInterface::BluetoothA2dpHALInterface(
  const btav_interface_t* aInterface)
: mInterface(aInterface)
{
  MOZ_ASSERT(mInterface);
}

BluetoothA2dpHALInterface::~BluetoothA2dpHALInterface()
{ }

void
BluetoothA2dpHALInterface::Init(
  BluetoothA2dpNotificationHandler* aNotificationHandler,
  BluetoothA2dpResultHandler* aRes)
{
  static btav_callbacks_t sCallbacks = {
    sizeof(sCallbacks),
    BluetoothA2dpHALCallback::ConnectionState,
    BluetoothA2dpHALCallback::AudioState,
#if ANDROID_VERSION >= 21
    BluetoothA2dpHALCallback::AudioConfig
#endif
  };

  sA2dpNotificationHandler = aNotificationHandler;

  bt_status_t status = mInterface->init(&sCallbacks);

  if (aRes) {
    DispatchBluetoothA2dpHALResult(aRes,
                                   &BluetoothA2dpResultHandler::Init,
                                   ConvertDefault(status, STATUS_FAIL));
  }
}

void
BluetoothA2dpHALInterface::Cleanup(BluetoothA2dpResultHandler* aRes)
{
  mInterface->cleanup();

  if (aRes) {
    DispatchBluetoothA2dpHALResult(aRes,
                                   &BluetoothA2dpResultHandler::Cleanup,
                                   STATUS_SUCCESS);
  }
}

void
BluetoothA2dpHALInterface::Connect(const nsAString& aBdAddr,
                                   BluetoothA2dpResultHandler* aRes)
{
  bt_status_t status;
  bt_bdaddr_t bdAddr;

  if (NS_SUCCEEDED(Convert(aBdAddr, bdAddr))) {
    status = mInterface->connect(&bdAddr);
  } else {
    status = BT_STATUS_PARM_INVALID;
  }

  if (aRes) {
    DispatchBluetoothA2dpHALResult(
      aRes, &BluetoothA2dpResultHandler::Connect,
      ConvertDefault(status, STATUS_FAIL));
  }
}

void
BluetoothA2dpHALInterface::Disconnect(const nsAString& aBdAddr,
                                      BluetoothA2dpResultHandler* aRes)
{
  bt_status_t status;
  bt_bdaddr_t bdAddr;

  if (NS_SUCCEEDED(Convert(aBdAddr, bdAddr))) {
    status = mInterface->disconnect(&bdAddr);
  } else {
    status = BT_STATUS_PARM_INVALID;
  }

  if (aRes) {
    DispatchBluetoothA2dpHALResult(
      aRes, &BluetoothA2dpResultHandler::Disconnect,
      ConvertDefault(status, STATUS_FAIL));
  }
}

END_BLUETOOTH_NAMESPACE