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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
/* -*- 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 "BluetoothHALInterface.h"
#include "BluetoothHALHelpers.h"
#include "BluetoothA2dpHALInterface.h"
#include "BluetoothAvrcpHALInterface.h"
#include "BluetoothHandsfreeHALInterface.h"
#include "BluetoothSocketHALInterface.h"

BEGIN_BLUETOOTH_NAMESPACE

template<class T>
struct interface_traits
{ };

template<>
struct interface_traits<BluetoothSocketHALInterface>
{
  typedef const btsock_interface_t const_interface_type;

  static const char* profile_id()
  {
    return BT_PROFILE_SOCKETS_ID;
  }
};

template<>
struct interface_traits<BluetoothHandsfreeHALInterface>
{
  typedef const bthf_interface_t const_interface_type;

  static const char* profile_id()
  {
    return BT_PROFILE_HANDSFREE_ID;
  }
};

template<>
struct interface_traits<BluetoothA2dpHALInterface>
{
  typedef const btav_interface_t const_interface_type;

  static const char* profile_id()
  {
    return BT_PROFILE_ADVANCED_AUDIO_ID;
  }
};

#if ANDROID_VERSION >= 18
template<>
struct interface_traits<BluetoothAvrcpHALInterface>
{
  typedef const btrc_interface_t const_interface_type;

  static const char* profile_id()
  {
    return BT_PROFILE_AV_RC_ID;
  }
};
#endif

typedef
  BluetoothHALInterfaceRunnable0<BluetoothResultHandler, void>
  BluetoothHALResultRunnable;

typedef
  BluetoothHALInterfaceRunnable1<BluetoothResultHandler, void,
                                 BluetoothStatus, BluetoothStatus>
  BluetoothHALErrorRunnable;

static nsresult
DispatchBluetoothHALResult(BluetoothResultHandler* aRes,
                           void (BluetoothResultHandler::*aMethod)(),
                           BluetoothStatus aStatus)
{
  MOZ_ASSERT(aRes);

  nsRunnable* runnable;

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

// Notification handling
//

static BluetoothNotificationHandler* sNotificationHandler;

struct BluetoothCallback
{
  class NotificationHandlerWrapper
  {
  public:
    typedef BluetoothNotificationHandler  ObjectType;

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

      return sNotificationHandler;
    }
  };

  // Notifications

  typedef BluetoothNotificationHALRunnable1<NotificationHandlerWrapper, void,
                                            bool>
    AdapterStateChangedNotification;

  typedef BluetoothNotificationHALRunnable3<NotificationHandlerWrapper, void,
                                            BluetoothStatus, int,
                                            nsAutoArrayPtr<BluetoothProperty>,
                                            BluetoothStatus, int,
                                            const BluetoothProperty*>
    AdapterPropertiesNotification;

  typedef BluetoothNotificationHALRunnable4<NotificationHandlerWrapper, void,
                                            BluetoothStatus, nsString, int,
                                            nsAutoArrayPtr<BluetoothProperty>,
                                            BluetoothStatus, const nsAString&,
                                            int, const BluetoothProperty*>
    RemoteDevicePropertiesNotification;

  typedef BluetoothNotificationHALRunnable2<NotificationHandlerWrapper, void,
                                            int,
                                            nsAutoArrayPtr<BluetoothProperty>,
                                            int, const BluetoothProperty*>
    DeviceFoundNotification;

  typedef BluetoothNotificationHALRunnable1<NotificationHandlerWrapper, void,
                                         bool>
    DiscoveryStateChangedNotification;

  typedef BluetoothNotificationHALRunnable3<NotificationHandlerWrapper, void,
                                            nsString, nsString, uint32_t,
                                            const nsAString&, const nsAString&>
    PinRequestNotification;

  typedef BluetoothNotificationHALRunnable5<NotificationHandlerWrapper, void,
                                            nsString, nsString, uint32_t,
                                            nsString, uint32_t,
                                            const nsAString&, const nsAString&,
                                            uint32_t, const nsAString&>
    SspRequestNotification;

  typedef BluetoothNotificationHALRunnable3<NotificationHandlerWrapper, void,
                                            BluetoothStatus, nsString,
                                            BluetoothBondState,
                                            BluetoothStatus, const nsAString&>
    BondStateChangedNotification;

  typedef BluetoothNotificationHALRunnable3<NotificationHandlerWrapper, void,
                                            BluetoothStatus, nsString, bool,
                                            BluetoothStatus, const nsAString&>
    AclStateChangedNotification;

  typedef BluetoothNotificationHALRunnable3<NotificationHandlerWrapper, void,
                                            uint16_t, nsAutoArrayPtr<uint8_t>,
                                            uint8_t, uint16_t, const uint8_t*>
    DutModeRecvNotification;

  typedef BluetoothNotificationHALRunnable2<NotificationHandlerWrapper, void,
                                            BluetoothStatus, uint16_t>
    LeTestModeNotification;

  typedef BluetoothNotificationHALRunnable1<NotificationHandlerWrapper, void,
                                            BluetoothActivityEnergyInfo,
                                            const BluetoothActivityEnergyInfo&>
    EnergyInfoNotification;

  // Bluedroid callbacks

  static const bt_property_t*
  AlignedProperties(bt_property_t* aProperties, size_t aNumProperties,
                    nsAutoArrayPtr<bt_property_t>& aPropertiesArray)
  {
    // See Bug 989976: consider aProperties address is not aligned. If
    // it is aligned, we return the pointer directly; otherwise we make
    // an aligned copy. The argument |aPropertiesArray| keeps track of
    // the memory buffer.
    if (!(reinterpret_cast<uintptr_t>(aProperties) % sizeof(void*))) {
      return aProperties;
    }

    bt_property_t* properties = new bt_property_t[aNumProperties];
    memcpy(properties, aProperties, aNumProperties * sizeof(*properties));
    aPropertiesArray = properties;

    return properties;
  }

  static void
  AdapterStateChanged(bt_state_t aStatus)
  {
    AdapterStateChangedNotification::Dispatch(
      &BluetoothNotificationHandler::AdapterStateChangedNotification,
      aStatus);
  }

  static void
  AdapterProperties(bt_status_t aStatus, int aNumProperties,
                    bt_property_t* aProperties)
  {
    nsAutoArrayPtr<bt_property_t> propertiesArray;

    AdapterPropertiesNotification::Dispatch(
      &BluetoothNotificationHandler::AdapterPropertiesNotification,
      ConvertDefault(aStatus, STATUS_FAIL), aNumProperties,
      ConvertArray<bt_property_t>(
        AlignedProperties(aProperties, aNumProperties, propertiesArray),
      aNumProperties));
  }

  static void
  RemoteDeviceProperties(bt_status_t aStatus, bt_bdaddr_t* aBdAddress,
                         int aNumProperties, bt_property_t* aProperties)
  {
    nsAutoArrayPtr<bt_property_t> propertiesArray;

    RemoteDevicePropertiesNotification::Dispatch(
      &BluetoothNotificationHandler::RemoteDevicePropertiesNotification,
      ConvertDefault(aStatus, STATUS_FAIL), aBdAddress, aNumProperties,
      ConvertArray<bt_property_t>(
        AlignedProperties(aProperties, aNumProperties, propertiesArray),
      aNumProperties));
  }

  static void
  DeviceFound(int aNumProperties, bt_property_t* aProperties)
  {
    nsAutoArrayPtr<bt_property_t> propertiesArray;

    DeviceFoundNotification::Dispatch(
      &BluetoothNotificationHandler::DeviceFoundNotification,
      aNumProperties,
      ConvertArray<bt_property_t>(
        AlignedProperties(aProperties, aNumProperties, propertiesArray),
      aNumProperties));
  }

  static void
  DiscoveryStateChanged(bt_discovery_state_t aState)
  {
    DiscoveryStateChangedNotification::Dispatch(
      &BluetoothNotificationHandler::DiscoveryStateChangedNotification,
      aState);
  }

  static void
  PinRequest(bt_bdaddr_t* aRemoteBdAddress,
             bt_bdname_t* aRemoteBdName, uint32_t aRemoteClass)
  {
    PinRequestNotification::Dispatch(
      &BluetoothNotificationHandler::PinRequestNotification,
      aRemoteBdAddress, aRemoteBdName, aRemoteClass);
  }

  static void
  SspRequest(bt_bdaddr_t* aRemoteBdAddress, bt_bdname_t* aRemoteBdName,
             uint32_t aRemoteClass, bt_ssp_variant_t aPairingVariant,
             uint32_t aPasskey)
  {
    SspRequestNotification::Dispatch(
      &BluetoothNotificationHandler::SspRequestNotification,
      aRemoteBdAddress, aRemoteBdName, aRemoteClass,
      aPairingVariant, aPasskey);
  }

  static void
  BondStateChanged(bt_status_t aStatus, bt_bdaddr_t* aRemoteBdAddress,
                   bt_bond_state_t aState)
  {
    BondStateChangedNotification::Dispatch(
      &BluetoothNotificationHandler::BondStateChangedNotification,
      aStatus, aRemoteBdAddress, aState);
  }

  static void
  AclStateChanged(bt_status_t aStatus, bt_bdaddr_t* aRemoteBdAddress,
                  bt_acl_state_t aState)
  {
    AclStateChangedNotification::Dispatch(
      &BluetoothNotificationHandler::AclStateChangedNotification,
      aStatus, aRemoteBdAddress, aState);
  }

  static void
  ThreadEvt(bt_cb_thread_evt evt)
  {
    // This callback maintains internal state and is not exported.
  }

  static void
  DutModeRecv(uint16_t aOpcode, uint8_t* aBuf, uint8_t aLen)
  {
    DutModeRecvNotification::Dispatch(
      &BluetoothNotificationHandler::DutModeRecvNotification,
      aOpcode, ConvertArray<uint8_t>(aBuf, aLen), aLen);
  }

  static void
  LeTestMode(bt_status_t aStatus, uint16_t aNumPackets)
  {
    LeTestModeNotification::Dispatch(
      &BluetoothNotificationHandler::LeTestModeNotification,
      aStatus, aNumPackets);
  }

#if ANDROID_VERSION >= 21
  static void
  EnergyInfo(bt_activity_energy_info* aEnergyInfo)
  {
    if (NS_WARN_IF(!aEnergyInfo)) {
      return;
    }

    EnergyInfoNotification::Dispatch(
      &BluetoothNotificationHandler::EnergyInfoNotification,
      *aEnergyInfo);
  }
#endif
};

#if ANDROID_VERSION >= 21
struct BluetoothOsCallout
{
  static bool
  SetWakeAlarm(uint64_t aDelayMilliseconds,
               bool aShouldWake,
               void (* aAlarmCallback)(void*),
               void* aData)
  {
    // FIXME: need to be implemented in later patches
    // HAL wants to manage an wake_alarm but Gecko cannot fulfill it for now.
    // Simply pass the request until a proper implementation has been added.
    return true;
  }

  static int
  AcquireWakeLock(const char* aLockName)
  {
    // FIXME: need to be implemented in later patches
    // HAL wants to manage an wake_lock but Gecko cannot fulfill it for now.
    // Simply pass the request until a proper implementation has been added.
    return BT_STATUS_SUCCESS;
  }

  static int
  ReleaseWakeLock(const char* aLockName)
  {
    // FIXME: need to be implemented in later patches
    // HAL wants to manage an wake_lock but Gecko cannot fulfill it for now.
    // Simply pass the request until a proper implementation has been added.
    return BT_STATUS_SUCCESS;
  }
};
#endif

// Interface
//

/* returns the container structure of a variable; _t is the container's
 * type, _v the name of the variable, and _m is _v's field within _t
 */
#define container(_t, _v, _m) \
  ( (_t*)( ((const unsigned char*)(_v)) - offsetof(_t, _m) ) )

BluetoothHALInterface*
BluetoothHALInterface::GetInstance()
{
  static BluetoothHALInterface* sBluetoothInterface;

  if (sBluetoothInterface) {
    return sBluetoothInterface;
  }

  /* get driver module */

  const hw_module_t* module;
  int err = hw_get_module(BT_HARDWARE_MODULE_ID, &module);
  if (err) {
    BT_WARNING("hw_get_module failed: %s", strerror(err));
    return nullptr;
  }

  /* get device */

  hw_device_t* device;
  err = module->methods->open(module, BT_HARDWARE_MODULE_ID, &device);
  if (err) {
    BT_WARNING("open failed: %s", strerror(err));
    return nullptr;
  }

  const bluetooth_device_t* bt_device =
    container(bluetooth_device_t, device, common);

  /* get interface */

  const bt_interface_t* bt_interface = bt_device->get_bluetooth_interface();
  if (!bt_interface) {
    BT_WARNING("get_bluetooth_interface failed");
    goto err_get_bluetooth_interface;
  }

  if (bt_interface->size != sizeof(*bt_interface)) {
    BT_WARNING("interface of incorrect size");
    goto err_bt_interface_size;
  }

  sBluetoothInterface = new BluetoothHALInterface(bt_interface);

  return sBluetoothInterface;

err_bt_interface_size:
err_get_bluetooth_interface:
  err = device->close(device);
  if (err) {
    BT_WARNING("close failed: %s", strerror(err));
  }
  return nullptr;
}

BluetoothHALInterface::BluetoothHALInterface(
  const bt_interface_t* aInterface)
: mInterface(aInterface)
{
  MOZ_ASSERT(mInterface);
}

BluetoothHALInterface::~BluetoothHALInterface()
{ }

void
BluetoothHALInterface::Init(
  BluetoothNotificationHandler* aNotificationHandler,
  BluetoothResultHandler* aRes)
{
  static bt_callbacks_t sBluetoothCallbacks = {
    sizeof(sBluetoothCallbacks),
    BluetoothCallback::AdapterStateChanged,
    BluetoothCallback::AdapterProperties,
    BluetoothCallback::RemoteDeviceProperties,
    BluetoothCallback::DeviceFound,
    BluetoothCallback::DiscoveryStateChanged,
    BluetoothCallback::PinRequest,
    BluetoothCallback::SspRequest,
    BluetoothCallback::BondStateChanged,
    BluetoothCallback::AclStateChanged,
    BluetoothCallback::ThreadEvt,
    BluetoothCallback::DutModeRecv,
#if ANDROID_VERSION >= 18
    BluetoothCallback::LeTestMode,
#endif
#if ANDROID_VERSION >= 21
    BluetoothCallback::EnergyInfo
#endif
  };

#if ANDROID_VERSION >= 21
  static bt_os_callouts_t sBluetoothOsCallouts = {
    sizeof(sBluetoothOsCallouts),
    BluetoothOsCallout::SetWakeAlarm,
    BluetoothOsCallout::AcquireWakeLock,
    BluetoothOsCallout::ReleaseWakeLock
  };
#endif

  sNotificationHandler = aNotificationHandler;

  int status = mInterface->init(&sBluetoothCallbacks);

#if ANDROID_VERSION >= 21
  if (status == BT_STATUS_SUCCESS) {
    status = mInterface->set_os_callouts(&sBluetoothOsCallouts);
    if (status != BT_STATUS_SUCCESS) {
      mInterface->cleanup();
    }
  }
#endif

  if (aRes) {
    DispatchBluetoothHALResult(aRes, &BluetoothResultHandler::Init,
                               ConvertDefault(status, STATUS_FAIL));
  }
}

void
BluetoothHALInterface::Cleanup(BluetoothResultHandler* aRes)
{
  mInterface->cleanup();

  if (aRes) {
    DispatchBluetoothHALResult(aRes, &BluetoothResultHandler::Cleanup,
                               STATUS_SUCCESS);
  }

  sNotificationHandler = nullptr;
}

void
BluetoothHALInterface::Enable(BluetoothResultHandler* aRes)
{
  int status = mInterface->enable();

  if (aRes) {
    DispatchBluetoothHALResult(aRes, &BluetoothResultHandler::Enable,
                               ConvertDefault(status, STATUS_FAIL));
  }
}

void
BluetoothHALInterface::Disable(BluetoothResultHandler* aRes)
{
  int status = mInterface->disable();

  if (aRes) {
    DispatchBluetoothHALResult(aRes, &BluetoothResultHandler::Disable,
                               ConvertDefault(status, STATUS_FAIL));
  }
}

/* Adapter Properties */

void
BluetoothHALInterface::GetAdapterProperties(BluetoothResultHandler* aRes)
{
  int status = mInterface->get_adapter_properties();

  if (aRes) {
    DispatchBluetoothHALResult(aRes,
                               &BluetoothResultHandler::GetAdapterProperties,
                               ConvertDefault(status, STATUS_FAIL));
  }
}

void
BluetoothHALInterface::GetAdapterProperty(const nsAString& aName,
                                          BluetoothResultHandler* aRes)
{
  int status;
  bt_property_type_t type;

  /* FIXME: you need to implement the missing conversion functions */
  NS_NOTREACHED("Conversion function missing");

  if (false /* TODO: we don't support any values for aName currently */) {
    status = mInterface->get_adapter_property(type);
  } else {
    status = BT_STATUS_PARM_INVALID;
  }

  if (aRes) {
    DispatchBluetoothHALResult(aRes,
                               &BluetoothResultHandler::GetAdapterProperties,
                               ConvertDefault(status, STATUS_FAIL));
  }
}

void
BluetoothHALInterface::SetAdapterProperty(
  const BluetoothNamedValue& aProperty, BluetoothResultHandler* aRes)
{
  int status;
  ConvertNamedValue convertProperty(aProperty);
  bt_property_t property;

  if (NS_SUCCEEDED(Convert(convertProperty, property))) {
    status = mInterface->set_adapter_property(&property);
  } else {
    status = BT_STATUS_PARM_INVALID;
  }

  if (aRes) {
    DispatchBluetoothHALResult(aRes,
                               &BluetoothResultHandler::SetAdapterProperty,
                               ConvertDefault(status, STATUS_FAIL));
  }
}

/* Remote Device Properties */

void
BluetoothHALInterface::GetRemoteDeviceProperties(
  const nsAString& aRemoteAddr, BluetoothResultHandler* aRes)
{
  int status;
  bt_bdaddr_t addr;

  if (NS_SUCCEEDED(Convert(aRemoteAddr, addr))) {
    status = mInterface->get_remote_device_properties(&addr);
  } else {
    status = BT_STATUS_PARM_INVALID;
  }

  if (aRes) {
    DispatchBluetoothHALResult(aRes,
                               &BluetoothResultHandler::GetRemoteDeviceProperties,
                               ConvertDefault(status, STATUS_FAIL));
  }
}

void
BluetoothHALInterface::GetRemoteDeviceProperty(
  const nsAString& aRemoteAddr, const nsAString& aName,
  BluetoothResultHandler* aRes)
{
  int status;
  bt_bdaddr_t remoteAddr;
  bt_property_type_t name;

  /* FIXME: you need to implement the missing conversion functions */
  NS_NOTREACHED("Conversion function missing");

  if (NS_SUCCEEDED(Convert(aRemoteAddr, remoteAddr)) &&
      false /* TODO: we don't support any values for aName currently */) {
    status = mInterface->get_remote_device_property(&remoteAddr, name);
  } else {
    status = BT_STATUS_PARM_INVALID;
  }

  if (aRes) {
    DispatchBluetoothHALResult(aRes,
                               &BluetoothResultHandler::GetRemoteDeviceProperty,
                               ConvertDefault(status, STATUS_FAIL));
  }
}

void
BluetoothHALInterface::SetRemoteDeviceProperty(
  const nsAString& aRemoteAddr, const BluetoothNamedValue& aProperty,
  BluetoothResultHandler* aRes)
{
  int status;
  bt_bdaddr_t remoteAddr;
  bt_property_t property;

  /* FIXME: you need to implement the missing conversion functions */
  NS_NOTREACHED("Conversion function missing");

  if (NS_SUCCEEDED(Convert(aRemoteAddr, remoteAddr)) &&
      false /* TODO: we don't support any values for aProperty currently */) {
    status = mInterface->set_remote_device_property(&remoteAddr, &property);
  } else {
    status = BT_STATUS_PARM_INVALID;
  }

  if (aRes) {
    DispatchBluetoothHALResult(aRes,
                               &BluetoothResultHandler::SetRemoteDeviceProperty,
                               ConvertDefault(status, STATUS_FAIL));
  }
}

/* Remote Services */

void
BluetoothHALInterface::GetRemoteServiceRecord(const nsAString& aRemoteAddr,
                                              const uint8_t aUuid[16],
                                              BluetoothResultHandler* aRes)
{
  int status;
  bt_bdaddr_t remoteAddr;
  bt_uuid_t uuid;

  if (NS_SUCCEEDED(Convert(aRemoteAddr, remoteAddr)) &&
      NS_SUCCEEDED(Convert(aUuid, uuid))) {
    status = mInterface->get_remote_service_record(&remoteAddr, &uuid);
  } else {
    status = BT_STATUS_PARM_INVALID;
  }

  if (aRes) {
    DispatchBluetoothHALResult(aRes,
                               &BluetoothResultHandler::GetRemoteServiceRecord,
                               ConvertDefault(status, STATUS_FAIL));
  }
}

void
BluetoothHALInterface::GetRemoteServices(const nsAString& aRemoteAddr,
                                         BluetoothResultHandler* aRes)
{
  int status;
  bt_bdaddr_t remoteAddr;

  if (NS_SUCCEEDED(Convert(aRemoteAddr, remoteAddr))) {
    status = mInterface->get_remote_services(&remoteAddr);
  } else {
    status = BT_STATUS_PARM_INVALID;
  }

  if (aRes) {
    DispatchBluetoothHALResult(aRes,
                               &BluetoothResultHandler::GetRemoteServices,
                               ConvertDefault(status, STATUS_FAIL));
  }
}

/* Discovery */

void
BluetoothHALInterface::StartDiscovery(BluetoothResultHandler* aRes)
{
  int status = mInterface->start_discovery();

  if (aRes) {
    DispatchBluetoothHALResult(aRes,
                               &BluetoothResultHandler::StartDiscovery,
                               ConvertDefault(status, STATUS_FAIL));
  }
}

void
BluetoothHALInterface::CancelDiscovery(BluetoothResultHandler* aRes)
{
  int status = mInterface->cancel_discovery();

  if (aRes) {
    DispatchBluetoothHALResult(aRes,
                               &BluetoothResultHandler::CancelDiscovery,
                               ConvertDefault(status, STATUS_FAIL));
  }
}

/* Bonds */

void
BluetoothHALInterface::CreateBond(const nsAString& aBdAddr,
                                  BluetoothTransport aTransport,
                                  BluetoothResultHandler* aRes)
{
  bt_bdaddr_t bdAddr;
  int status;

#if ANDROID_VERSION >= 21
  int transport = 0; /* TRANSPORT_AUTO */

  if (NS_SUCCEEDED(Convert(aBdAddr, bdAddr)) &&
      NS_SUCCEEDED(Convert(aTransport, transport))) {
    status = mInterface->create_bond(&bdAddr, transport);
#else
  if (NS_SUCCEEDED(Convert(aBdAddr, bdAddr))) {
    status = mInterface->create_bond(&bdAddr);
#endif
  } else {
    status = BT_STATUS_PARM_INVALID;
  }

  if (aRes) {
    DispatchBluetoothHALResult(aRes,
                               &BluetoothResultHandler::CreateBond,
                               ConvertDefault(status, STATUS_FAIL));
  }
}

void
BluetoothHALInterface::RemoveBond(const nsAString& aBdAddr,
                                  BluetoothResultHandler* aRes)
{
  bt_bdaddr_t bdAddr;
  int status;

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

  if (aRes) {
    DispatchBluetoothHALResult(aRes,
                               &BluetoothResultHandler::RemoveBond,
                               ConvertDefault(status, STATUS_FAIL));
  }
}

void
BluetoothHALInterface::CancelBond(const nsAString& aBdAddr,
                                  BluetoothResultHandler* aRes)
{
  bt_bdaddr_t bdAddr;
  int status;

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

  if (aRes) {
    DispatchBluetoothHALResult(aRes,
                               &BluetoothResultHandler::CancelBond,
                               ConvertDefault(status, STATUS_FAIL));
  }
}

/* Connection */

void
BluetoothHALInterface::GetConnectionState(const nsAString& aBdAddr,
                                          BluetoothResultHandler* aRes)
{
  int status;

#if ANDROID_VERSION >= 21
  bt_bdaddr_t bdAddr;

  if (NS_SUCCEEDED(Convert(aBdAddr, bdAddr))) {
    status = mInterface->get_connection_state(&bdAddr);
  } else {
    status = BT_STATUS_PARM_INVALID;
  }
#else
  status = BT_STATUS_UNSUPPORTED;
#endif

  if (aRes) {
    DispatchBluetoothHALResult(aRes,
                               &BluetoothResultHandler::GetConnectionState,
                               ConvertDefault(status, STATUS_FAIL));
  }
}

/* Authentication */

void
BluetoothHALInterface::PinReply(const nsAString& aBdAddr, bool aAccept,
                                const nsAString& aPinCode,
                                BluetoothResultHandler* aRes)
{
  int status;
  bt_bdaddr_t bdAddr;
  uint8_t accept;
  bt_pin_code_t pinCode;

  if (NS_SUCCEEDED(Convert(aBdAddr, bdAddr)) &&
      NS_SUCCEEDED(Convert(aAccept, accept)) &&
      NS_SUCCEEDED(Convert(aPinCode, pinCode))) {
    status = mInterface->pin_reply(&bdAddr, accept, aPinCode.Length(),
                                   &pinCode);
  } else {
    status = BT_STATUS_PARM_INVALID;
  }

  if (aRes) {
    DispatchBluetoothHALResult(aRes,
                               &BluetoothResultHandler::PinReply,
                               ConvertDefault(status, STATUS_FAIL));
  }
}

void
BluetoothHALInterface::SspReply(const nsAString& aBdAddr,
                                const nsAString& aVariant,
                                bool aAccept, uint32_t aPasskey,
                                BluetoothResultHandler* aRes)
{
  int status;
  bt_bdaddr_t bdAddr;
  bt_ssp_variant_t variant;
  uint8_t accept;

  if (NS_SUCCEEDED(Convert(aBdAddr, bdAddr)) &&
      NS_SUCCEEDED(Convert(aVariant, variant)) &&
      NS_SUCCEEDED(Convert(aAccept, accept))) {
    status = mInterface->ssp_reply(&bdAddr, variant, accept, aPasskey);
  } else {
    status = BT_STATUS_PARM_INVALID;
  }

  if (aRes) {
    DispatchBluetoothHALResult(aRes,
                               &BluetoothResultHandler::SspReply,
                               ConvertDefault(status, STATUS_FAIL));
  }
}

/* DUT Mode */

void
BluetoothHALInterface::DutModeConfigure(bool aEnable,
                                        BluetoothResultHandler* aRes)
{
  int status;
  uint8_t enable;

  if (NS_SUCCEEDED(Convert(aEnable, enable))) {
    status = mInterface->dut_mode_configure(enable);
  } else {
    status = BT_STATUS_PARM_INVALID;
  }

  if (aRes) {
    DispatchBluetoothHALResult(aRes,
                               &BluetoothResultHandler::DutModeConfigure,
                               ConvertDefault(status, STATUS_FAIL));
  }
}

void
BluetoothHALInterface::DutModeSend(uint16_t aOpcode, uint8_t* aBuf, uint8_t aLen,
                                   BluetoothResultHandler* aRes)
{
  int status = mInterface->dut_mode_send(aOpcode, aBuf, aLen);

  if (aRes) {
    DispatchBluetoothHALResult(aRes,
                               &BluetoothResultHandler::DutModeSend,
                               ConvertDefault(status, STATUS_FAIL));
  }
}

/* LE Mode */

void
BluetoothHALInterface::LeTestMode(uint16_t aOpcode, uint8_t* aBuf, uint8_t aLen,
                                  BluetoothResultHandler* aRes)
{
#if ANDROID_VERSION >= 18
  int status = mInterface->le_test_mode(aOpcode, aBuf, aLen);
#else
  int status = BT_STATUS_UNSUPPORTED;
#endif

  if (aRes) {
    DispatchBluetoothHALResult(aRes,
                               &BluetoothResultHandler::LeTestMode,
                               ConvertDefault(status, STATUS_FAIL));
  }
}

/* Energy Information */
void
BluetoothHALInterface::ReadEnergyInfo(BluetoothResultHandler* aRes)
{
#if ANDROID_VERSION >= 21
  int status = mInterface->read_energy_info();
#else
  int status = BT_STATUS_UNSUPPORTED;
#endif

  if (aRes) {
    DispatchBluetoothHALResult(aRes,
                               &BluetoothResultHandler::ReadEnergyInfo,
                               ConvertDefault(status, STATUS_FAIL));
  }
}

/* Profile Interfaces */

template <class T>
T*
BluetoothHALInterface::CreateProfileInterface()
{
  typename interface_traits<T>::const_interface_type* interface =
    reinterpret_cast<typename interface_traits<T>::const_interface_type*>(
      mInterface->get_profile_interface(interface_traits<T>::profile_id()));

  if (!interface) {
    BT_WARNING("Bluetooth profile '%s' is not supported",
               interface_traits<T>::profile_id());
    return nullptr;
  }

  if (interface->size != sizeof(*interface)) {
    BT_WARNING("interface of incorrect size");
    return nullptr;
  }

  return new T(interface);
}

#if ANDROID_VERSION < 18
/*
 * Bluedroid versions that don't support AVRCP will call this function
 * to create an AVRCP interface. All interface methods will fail with
 * the error constant STATUS_UNSUPPORTED.
 */
template <>
BluetoothAvrcpHALInterface*
BluetoothHALInterface::CreateProfileInterface<BluetoothAvrcpHALInterface>()
{
  BT_WARNING("Bluetooth profile 'avrcp' is not supported");

  return new BluetoothAvrcpHALInterface();
}
#endif

template <class T>
T*
BluetoothHALInterface::GetProfileInterface()
{
  static T* sBluetoothProfileInterface;

  if (sBluetoothProfileInterface) {
    return sBluetoothProfileInterface;
  }

  sBluetoothProfileInterface = CreateProfileInterface<T>();

  return sBluetoothProfileInterface;
}

BluetoothSocketInterface*
BluetoothHALInterface::GetBluetoothSocketInterface()
{
  return GetProfileInterface<BluetoothSocketHALInterface>();
}

BluetoothHandsfreeInterface*
BluetoothHALInterface::GetBluetoothHandsfreeInterface()
{
  return GetProfileInterface<BluetoothHandsfreeHALInterface>();
}

BluetoothA2dpInterface*
BluetoothHALInterface::GetBluetoothA2dpInterface()
{
  return GetProfileInterface<BluetoothA2dpHALInterface>();
}

BluetoothAvrcpInterface*
BluetoothHALInterface::GetBluetoothAvrcpInterface()
{
  return GetProfileInterface<BluetoothAvrcpHALInterface>();
}

END_BLUETOOTH_NAMESPACE