Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(303)

Side by Side Diff: components/arc/bluetooth/arc_bluetooth_bridge.cc

Issue 2104043002: arc: bluetooth: Implement Gatt Server add/delete/start/stop service (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@gs1
Patch Set: More descriptive type name for template Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/arc/bluetooth/arc_bluetooth_bridge.h" 5 #include "components/arc/bluetooth/arc_bluetooth_bridge.h"
6 6
7 #include <fcntl.h> 7 #include <fcntl.h>
8 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include <iomanip> 10 #include <iomanip>
11 #include <string> 11 #include <string>
12 #include <utility>
12 13
13 #include "base/bind.h" 14 #include "base/bind.h"
14 #include "base/logging.h" 15 #include "base/logging.h"
15 #include "base/posix/eintr_wrapper.h" 16 #include "base/posix/eintr_wrapper.h"
16 #include "base/strings/string_number_conversions.h" 17 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/utf_string_conversions.h" 18 #include "base/strings/utf_string_conversions.h"
18 #include "base/threading/thread_task_runner_handle.h" 19 #include "base/threading/thread_task_runner_handle.h"
19 #include "base/time/time.h" 20 #include "base/time/time.h"
20 #include "components/arc/arc_bridge_service.h" 21 #include "components/arc/arc_bridge_service.h"
21 #include "components/arc/bluetooth/bluetooth_type_converters.h" 22 #include "components/arc/bluetooth/bluetooth_type_converters.h"
(...skipping 20 matching lines...) Expand all
42 using device::BluetoothLocalGattService; 43 using device::BluetoothLocalGattService;
43 using device::BluetoothRemoteGattCharacteristic; 44 using device::BluetoothRemoteGattCharacteristic;
44 using device::BluetoothRemoteGattDescriptor; 45 using device::BluetoothRemoteGattDescriptor;
45 using device::BluetoothRemoteGattService; 46 using device::BluetoothRemoteGattService;
46 using device::BluetoothTransport; 47 using device::BluetoothTransport;
47 using device::BluetoothUUID; 48 using device::BluetoothUUID;
48 49
49 namespace { 50 namespace {
50 constexpr int32_t kMinBtleVersion = 1; 51 constexpr int32_t kMinBtleVersion = 1;
51 constexpr int32_t kMinBtleNotifyVersion = 2; 52 constexpr int32_t kMinBtleNotifyVersion = 2;
53 constexpr int32_t kMinGattServerVersion = 3;
52 constexpr uint32_t kGattReadPermission = 54 constexpr uint32_t kGattReadPermission =
53 BluetoothGattCharacteristic::Permission::PERMISSION_READ | 55 BluetoothGattCharacteristic::Permission::PERMISSION_READ |
54 BluetoothGattCharacteristic::Permission::PERMISSION_READ_ENCRYPTED | 56 BluetoothGattCharacteristic::Permission::PERMISSION_READ_ENCRYPTED |
55 BluetoothGattCharacteristic::Permission:: 57 BluetoothGattCharacteristic::Permission::
56 PERMISSION_READ_ENCRYPTED_AUTHENTICATED; 58 PERMISSION_READ_ENCRYPTED_AUTHENTICATED;
57 constexpr uint32_t kGattWritePermission = 59 constexpr uint32_t kGattWritePermission =
58 BluetoothGattCharacteristic::Permission::PERMISSION_WRITE | 60 BluetoothGattCharacteristic::Permission::PERMISSION_WRITE |
59 BluetoothGattCharacteristic::Permission::PERMISSION_WRITE_ENCRYPTED | 61 BluetoothGattCharacteristic::Permission::PERMISSION_WRITE_ENCRYPTED |
60 BluetoothGattCharacteristic::Permission:: 62 BluetoothGattCharacteristic::Permission::
61 PERMISSION_WRITE_ENCRYPTED_AUTHENTICATED; 63 PERMISSION_WRITE_ENCRYPTED_AUTHENTICATED;
64 constexpr int32_t kInvalidGattAttributeHandle = -1;
62 } // namespace 65 } // namespace
63 66
64 namespace arc { 67 namespace arc {
65 68
66 ArcBluetoothBridge::ArcBluetoothBridge(ArcBridgeService* bridge_service) 69 ArcBluetoothBridge::ArcBluetoothBridge(ArcBridgeService* bridge_service)
67 : ArcService(bridge_service), binding_(this), weak_factory_(this) { 70 : ArcService(bridge_service), binding_(this), weak_factory_(this) {
68 if (BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { 71 if (BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) {
69 VLOG(1) << "registering bluetooth adapter"; 72 VLOG(1) << "registering bluetooth adapter";
70 BluetoothAdapterFactory::GetAdapter(base::Bind( 73 BluetoothAdapterFactory::GetAdapter(base::Bind(
71 &ArcBluetoothBridge::OnAdapterInitialized, weak_factory_.GetWeakPtr())); 74 &ArcBluetoothBridge::OnAdapterInitialized, weak_factory_.GetWeakPtr()));
(...skipping 672 matching lines...) Expand 10 before | Expand all | Expand 10 after
744 747
745 // Example of identifier: /org/bluez/hci0/dev_E0_CF_65_8C_86_1A/service001a 748 // Example of identifier: /org/bluez/hci0/dev_E0_CF_65_8C_86_1A/service001a
746 // We want to convert last digit of the identifier to int in base 16 749 // We want to convert last digit of the identifier to int in base 16
747 int ArcBluetoothBridge::ConvertGattIdentifierToId( 750 int ArcBluetoothBridge::ConvertGattIdentifierToId(
748 const std::string identifier) const { 751 const std::string identifier) const {
749 return std::stoi(identifier.substr(identifier.size() - 4), nullptr, 16); 752 return std::stoi(identifier.substr(identifier.size() - 4), nullptr, 16);
750 } 753 }
751 754
752 // Create GattDBElement and fill in common data for 755 // Create GattDBElement and fill in common data for
753 // Gatt Service/Characteristic/Descriptor. 756 // Gatt Service/Characteristic/Descriptor.
754 template <class T> 757 template <class RemoteGattObjectT>
755 mojom::BluetoothGattDBElementPtr ArcBluetoothBridge::CreateGattDBElement( 758 mojom::BluetoothGattDBElementPtr ArcBluetoothBridge::CreateGattDBElement(
756 const mojom::BluetoothGattDBAttributeType type, 759 const mojom::BluetoothGattDBAttributeType type,
757 const T* gattObject) const { 760 const RemoteGattObjectT* gattObject) const {
758 mojom::BluetoothGattDBElementPtr element = 761 mojom::BluetoothGattDBElementPtr element =
759 mojom::BluetoothGattDBElement::New(); 762 mojom::BluetoothGattDBElement::New();
760 element->type = type; 763 element->type = type;
761 element->uuid = mojom::BluetoothUUID::From(gattObject->GetUUID()); 764 element->uuid = mojom::BluetoothUUID::From(gattObject->GetUUID());
762 element->id = element->attribute_handle = element->start_handle = 765 element->id = element->attribute_handle = element->start_handle =
763 element->end_handle = 766 element->end_handle =
764 ConvertGattIdentifierToId(gattObject->GetIdentifier()); 767 ConvertGattIdentifierToId(gattObject->GetIdentifier());
765 element->properties = 0; 768 element->properties = 0;
766 return element; 769 return element;
767 } 770 }
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
806 descriptor)); 809 descriptor));
807 } 810 }
808 } 811 }
809 } 812 }
810 813
811 arc_bridge_service()->bluetooth()->instance()->OnGetGattDB( 814 arc_bridge_service()->bluetooth()->instance()->OnGetGattDB(
812 std::move(remote_addr), std::move(db)); 815 std::move(remote_addr), std::move(db));
813 } 816 }
814 817
815 // Find Gatt Service/Characteristic/Descriptor from std::vector from UUID. 818 // Find Gatt Service/Characteristic/Descriptor from std::vector from UUID.
816 template <class T> 819 template <class RemoteGattObjectT>
817 T* ArcBluetoothBridge::FindGattObjectFromUuid( 820 RemoteGattObjectT* ArcBluetoothBridge::FindGattObjectFromUuid(
818 const std::vector<T*> gatt_objs, 821 const std::vector<RemoteGattObjectT*> gatt_objs,
819 const device::BluetoothUUID uuid) const { 822 const device::BluetoothUUID uuid) const {
820 auto it = std::find_if(gatt_objs.begin(), gatt_objs.end(), 823 auto it = std::find_if(
821 [&](T* obj) { return obj->GetUUID() == uuid; }); 824 gatt_objs.begin(), gatt_objs.end(),
825 [&](RemoteGattObjectT* obj) { return obj->GetUUID() == uuid; });
822 if (it == gatt_objs.end()) 826 if (it == gatt_objs.end())
823 return nullptr; 827 return nullptr;
824 return *it; 828 return *it;
825 } 829 }
826 830
827 BluetoothRemoteGattCharacteristic* ArcBluetoothBridge::FindGattCharacteristic( 831 BluetoothRemoteGattCharacteristic* ArcBluetoothBridge::FindGattCharacteristic(
828 mojom::BluetoothAddressPtr remote_addr, 832 mojom::BluetoothAddressPtr remote_addr,
829 mojom::BluetoothGattServiceIDPtr service_id, 833 mojom::BluetoothGattServiceIDPtr service_id,
830 mojom::BluetoothGattIDPtr char_id) const { 834 mojom::BluetoothGattIDPtr char_id) const {
831 DCHECK(remote_addr); 835 DCHECK(remote_addr);
(...skipping 22 matching lines...) Expand all
854 mojom::BluetoothGattIDPtr desc_id) const { 858 mojom::BluetoothGattIDPtr desc_id) const {
855 BluetoothRemoteGattCharacteristic* characteristic = FindGattCharacteristic( 859 BluetoothRemoteGattCharacteristic* characteristic = FindGattCharacteristic(
856 std::move(remote_addr), std::move(service_id), std::move(char_id)); 860 std::move(remote_addr), std::move(service_id), std::move(char_id));
857 if (!characteristic) 861 if (!characteristic)
858 return nullptr; 862 return nullptr;
859 863
860 return FindGattObjectFromUuid<BluetoothRemoteGattDescriptor>( 864 return FindGattObjectFromUuid<BluetoothRemoteGattDescriptor>(
861 characteristic->GetDescriptors(), desc_id->uuid.To<BluetoothUUID>()); 865 characteristic->GetDescriptors(), desc_id->uuid.To<BluetoothUUID>());
862 } 866 }
863 867
868 // Common callback function for Gatt operation that only need to report
palmer 2016/07/16 00:45:13 Comment style: Common callback for GATT operation
puthik_chromium 2016/07/18 21:30:13 Done.
869 // Gatt status back to Android.
870 void ArcBluetoothBridge::OnGattOperationDone(
871 const GattStatusCallback& callback) const {
872 callback.Run(mojom::BluetoothGattStatus::GATT_SUCCESS);
873 }
874
875 void ArcBluetoothBridge::OnGattOperationError(
876 const GattStatusCallback& callback,
877 BluetoothGattService::GattErrorCode error_code) const {
878 callback.Run(mojo::ConvertTo<mojom::BluetoothGattStatus>(error_code));
879 }
880
864 // Same callback for both ReadGattCharacteristic and ReadGattDescriptor 881 // Same callback for both ReadGattCharacteristic and ReadGattDescriptor
865 void ArcBluetoothBridge::OnGattReadDone( 882 void ArcBluetoothBridge::OnGattReadDone(
866 const GattReadCallback& callback, 883 const GattReadCallback& callback,
867 const std::vector<uint8_t>& result) const { 884 const std::vector<uint8_t>& result) const {
868 mojom::BluetoothGattValuePtr gattValue = mojom::BluetoothGattValue::New(); 885 mojom::BluetoothGattValuePtr gattValue = mojom::BluetoothGattValue::New();
869 gattValue->status = mojom::BluetoothGattStatus::GATT_SUCCESS; 886 gattValue->status = mojom::BluetoothGattStatus::GATT_SUCCESS;
870 gattValue->value = mojo::Array<uint8_t>::From(result); 887 gattValue->value = mojo::Array<uint8_t>::From(result);
871 callback.Run(std::move(gattValue)); 888 callback.Run(std::move(gattValue));
872 } 889 }
873 890
874 void ArcBluetoothBridge::OnGattReadError( 891 void ArcBluetoothBridge::OnGattReadError(
875 const GattReadCallback& callback, 892 const GattReadCallback& callback,
876 BluetoothGattService::GattErrorCode error_code) const { 893 BluetoothGattService::GattErrorCode error_code) const {
877 mojom::BluetoothGattValuePtr gattValue = mojom::BluetoothGattValue::New(); 894 mojom::BluetoothGattValuePtr gattValue = mojom::BluetoothGattValue::New();
878 gattValue->status = mojo::ConvertTo<mojom::BluetoothGattStatus>(error_code); 895 gattValue->status = mojo::ConvertTo<mojom::BluetoothGattStatus>(error_code);
879 gattValue->value = nullptr; 896 gattValue->value = nullptr;
880 897
881 callback.Run(std::move(gattValue)); 898 callback.Run(std::move(gattValue));
882 } 899 }
883 900
884 // Same callback for both WriteGattCharacteristic and WriteGattDescriptor
885 void ArcBluetoothBridge::OnGattWriteDone(
886 const GattWriteCallback& callback) const {
887 callback.Run(mojom::BluetoothGattStatus::GATT_SUCCESS);
888 }
889
890 void ArcBluetoothBridge::OnGattWriteError(
891 const GattWriteCallback& callback,
892 BluetoothGattService::GattErrorCode error_code) const {
893 callback.Run(mojo::ConvertTo<mojom::BluetoothGattStatus>(error_code));
894 }
895
896 void ArcBluetoothBridge::ReadGattCharacteristic( 901 void ArcBluetoothBridge::ReadGattCharacteristic(
897 mojom::BluetoothAddressPtr remote_addr, 902 mojom::BluetoothAddressPtr remote_addr,
898 mojom::BluetoothGattServiceIDPtr service_id, 903 mojom::BluetoothGattServiceIDPtr service_id,
899 mojom::BluetoothGattIDPtr char_id, 904 mojom::BluetoothGattIDPtr char_id,
900 const ReadGattCharacteristicCallback& callback) { 905 const ReadGattCharacteristicCallback& callback) {
901 BluetoothRemoteGattCharacteristic* characteristic = FindGattCharacteristic( 906 BluetoothRemoteGattCharacteristic* characteristic = FindGattCharacteristic(
902 std::move(remote_addr), std::move(service_id), std::move(char_id)); 907 std::move(remote_addr), std::move(service_id), std::move(char_id));
903 DCHECK(characteristic); 908 DCHECK(characteristic);
904 DCHECK(characteristic->GetPermissions() & kGattReadPermission); 909 DCHECK(characteristic->GetPermissions() & kGattReadPermission);
905 910
(...skipping 10 matching lines...) Expand all
916 mojom::BluetoothGattIDPtr char_id, 921 mojom::BluetoothGattIDPtr char_id,
917 mojom::BluetoothGattValuePtr value, 922 mojom::BluetoothGattValuePtr value,
918 const WriteGattCharacteristicCallback& callback) { 923 const WriteGattCharacteristicCallback& callback) {
919 BluetoothRemoteGattCharacteristic* characteristic = FindGattCharacteristic( 924 BluetoothRemoteGattCharacteristic* characteristic = FindGattCharacteristic(
920 std::move(remote_addr), std::move(service_id), std::move(char_id)); 925 std::move(remote_addr), std::move(service_id), std::move(char_id));
921 DCHECK(characteristic); 926 DCHECK(characteristic);
922 DCHECK(characteristic->GetPermissions() & kGattWritePermission); 927 DCHECK(characteristic->GetPermissions() & kGattWritePermission);
923 928
924 characteristic->WriteRemoteCharacteristic( 929 characteristic->WriteRemoteCharacteristic(
925 value->value.To<std::vector<uint8_t>>(), 930 value->value.To<std::vector<uint8_t>>(),
926 base::Bind(&ArcBluetoothBridge::OnGattWriteDone, 931 base::Bind(&ArcBluetoothBridge::OnGattOperationDone,
927 weak_factory_.GetWeakPtr(), callback), 932 weak_factory_.GetWeakPtr(), callback),
928 base::Bind(&ArcBluetoothBridge::OnGattWriteError, 933 base::Bind(&ArcBluetoothBridge::OnGattOperationError,
929 weak_factory_.GetWeakPtr(), callback)); 934 weak_factory_.GetWeakPtr(), callback));
930 } 935 }
931 936
932 void ArcBluetoothBridge::ReadGattDescriptor( 937 void ArcBluetoothBridge::ReadGattDescriptor(
933 mojom::BluetoothAddressPtr remote_addr, 938 mojom::BluetoothAddressPtr remote_addr,
934 mojom::BluetoothGattServiceIDPtr service_id, 939 mojom::BluetoothGattServiceIDPtr service_id,
935 mojom::BluetoothGattIDPtr char_id, 940 mojom::BluetoothGattIDPtr char_id,
936 mojom::BluetoothGattIDPtr desc_id, 941 mojom::BluetoothGattIDPtr desc_id,
937 const ReadGattDescriptorCallback& callback) { 942 const ReadGattDescriptorCallback& callback) {
938 BluetoothRemoteGattDescriptor* descriptor = 943 BluetoothRemoteGattDescriptor* descriptor =
(...skipping 24 matching lines...) Expand all
963 968
964 // To register / deregister GATT notification, we need to 969 // To register / deregister GATT notification, we need to
965 // 1) Write to CCC Descriptor to enable/disable the notification 970 // 1) Write to CCC Descriptor to enable/disable the notification
966 // 2) Ask BT hw to register / deregister the notification 971 // 2) Ask BT hw to register / deregister the notification
967 // The Chrome API groups both steps into one API, and does not support writing 972 // The Chrome API groups both steps into one API, and does not support writing
968 // directly to the CCC Descriptor. Therefore, until we fix 973 // directly to the CCC Descriptor. Therefore, until we fix
969 // https://crbug.com/622832, we return successfully when we encounter this. 974 // https://crbug.com/622832, we return successfully when we encounter this.
970 // TODO(http://crbug.com/622832) 975 // TODO(http://crbug.com/622832)
971 if (descriptor->GetUUID() == 976 if (descriptor->GetUUID() ==
972 BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid()) { 977 BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid()) {
973 OnGattWriteDone(callback); 978 OnGattOperationDone(callback);
974 return; 979 return;
975 } 980 }
976 981
977 descriptor->WriteRemoteDescriptor( 982 descriptor->WriteRemoteDescriptor(
978 value->value.To<std::vector<uint8_t>>(), 983 value->value.To<std::vector<uint8_t>>(),
979 base::Bind(&ArcBluetoothBridge::OnGattWriteDone, 984 base::Bind(&ArcBluetoothBridge::OnGattOperationDone,
980 weak_factory_.GetWeakPtr(), callback), 985 weak_factory_.GetWeakPtr(), callback),
981 base::Bind(&ArcBluetoothBridge::OnGattWriteError, 986 base::Bind(&ArcBluetoothBridge::OnGattOperationError,
982 weak_factory_.GetWeakPtr(), callback)); 987 weak_factory_.GetWeakPtr(), callback));
983 } 988 }
984 989
985 void ArcBluetoothBridge::OnGattNotifyStartDone( 990 void ArcBluetoothBridge::OnGattNotifyStartDone(
986 const RegisterForGattNotificationCallback& callback, 991 const RegisterForGattNotificationCallback& callback,
987 const std::string char_string_id, 992 const std::string char_string_id,
988 std::unique_ptr<BluetoothGattNotifySession> notify_session) { 993 std::unique_ptr<BluetoothGattNotifySession> notify_session) {
989 notification_session_[char_string_id] = std::move(notify_session); 994 notification_session_[char_string_id] = std::move(notify_session);
990 callback.Run(mojom::BluetoothGattStatus::GATT_SUCCESS); 995 callback.Run(mojom::BluetoothGattStatus::GATT_SUCCESS);
991 } 996 }
992 997
993 void ArcBluetoothBridge::OnGattNotifyStartError(
994 const RegisterForGattNotificationCallback& callback,
995 BluetoothGattService::GattErrorCode error_code) const {
996 callback.Run(mojo::ConvertTo<mojom::BluetoothGattStatus>(error_code));
997 }
998
999 void ArcBluetoothBridge::OnGattNotifyStopDone(
1000 const DeregisterForGattNotificationCallback& callback) const {
1001 callback.Run(mojom::BluetoothGattStatus::GATT_SUCCESS);
1002 }
1003
1004 void ArcBluetoothBridge::RegisterForGattNotification( 998 void ArcBluetoothBridge::RegisterForGattNotification(
1005 mojom::BluetoothAddressPtr remote_addr, 999 mojom::BluetoothAddressPtr remote_addr,
1006 mojom::BluetoothGattServiceIDPtr service_id, 1000 mojom::BluetoothGattServiceIDPtr service_id,
1007 mojom::BluetoothGattIDPtr char_id, 1001 mojom::BluetoothGattIDPtr char_id,
1008 const RegisterForGattNotificationCallback& callback) { 1002 const RegisterForGattNotificationCallback& callback) {
1009 BluetoothRemoteGattCharacteristic* characteristic = FindGattCharacteristic( 1003 BluetoothRemoteGattCharacteristic* characteristic = FindGattCharacteristic(
1010 std::move(remote_addr), std::move(service_id), std::move(char_id)); 1004 std::move(remote_addr), std::move(service_id), std::move(char_id));
1011 1005
1012 if (!characteristic) { 1006 if (!characteristic) {
1013 LOG(WARNING) << __func__ << " Characteristic is not existed."; 1007 LOG(WARNING) << __func__ << " Characteristic is not existed.";
1014 return; 1008 return;
1015 } 1009 }
1016 1010
1017 if (characteristic->IsNotifying()) { 1011 if (characteristic->IsNotifying()) {
1018 callback.Run(mojom::BluetoothGattStatus::GATT_SUCCESS); 1012 callback.Run(mojom::BluetoothGattStatus::GATT_SUCCESS);
1019 return; 1013 return;
1020 } 1014 }
1021 1015
1022 characteristic->StartNotifySession( 1016 characteristic->StartNotifySession(
1023 base::Bind(&ArcBluetoothBridge::OnGattNotifyStartDone, 1017 base::Bind(&ArcBluetoothBridge::OnGattNotifyStartDone,
1024 weak_factory_.GetWeakPtr(), callback, 1018 weak_factory_.GetWeakPtr(), callback,
1025 characteristic->GetIdentifier()), 1019 characteristic->GetIdentifier()),
1026 base::Bind(&ArcBluetoothBridge::OnGattNotifyStartError, 1020 base::Bind(&ArcBluetoothBridge::OnGattOperationError,
1027 weak_factory_.GetWeakPtr(), callback)); 1021 weak_factory_.GetWeakPtr(), callback));
1028 } 1022 }
1029 1023
1030 void ArcBluetoothBridge::DeregisterForGattNotification( 1024 void ArcBluetoothBridge::DeregisterForGattNotification(
1031 mojom::BluetoothAddressPtr remote_addr, 1025 mojom::BluetoothAddressPtr remote_addr,
1032 mojom::BluetoothGattServiceIDPtr service_id, 1026 mojom::BluetoothGattServiceIDPtr service_id,
1033 mojom::BluetoothGattIDPtr char_id, 1027 mojom::BluetoothGattIDPtr char_id,
1034 const DeregisterForGattNotificationCallback& callback) { 1028 const DeregisterForGattNotificationCallback& callback) {
1035 BluetoothRemoteGattCharacteristic* characteristic = FindGattCharacteristic( 1029 BluetoothRemoteGattCharacteristic* characteristic = FindGattCharacteristic(
1036 std::move(remote_addr), std::move(service_id), std::move(char_id)); 1030 std::move(remote_addr), std::move(service_id), std::move(char_id));
1037 1031
1038 if (!characteristic) { 1032 if (!characteristic) {
1039 LOG(WARNING) << __func__ << " Characteristic is not existed."; 1033 LOG(WARNING) << __func__ << " Characteristic is not existed.";
1040 return; 1034 return;
1041 } 1035 }
1042 1036
1043 if (!characteristic->IsNotifying()) { 1037 if (!characteristic->IsNotifying()) {
1044 callback.Run(mojom::BluetoothGattStatus::GATT_SUCCESS); 1038 callback.Run(mojom::BluetoothGattStatus::GATT_SUCCESS);
1045 return; 1039 return;
1046 } 1040 }
1047 1041
1048 std::string char_id_str = characteristic->GetIdentifier(); 1042 std::string char_id_str = characteristic->GetIdentifier();
1049 std::unique_ptr<BluetoothGattNotifySession> notify = 1043 std::unique_ptr<BluetoothGattNotifySession> notify =
1050 std::move(notification_session_[char_id_str]); 1044 std::move(notification_session_[char_id_str]);
1051 notification_session_.erase(char_id_str); 1045 notification_session_.erase(char_id_str);
1052 notify->Stop(base::Bind(&ArcBluetoothBridge::OnGattNotifyStopDone, 1046 notify->Stop(base::Bind(&ArcBluetoothBridge::OnGattOperationDone,
1053 weak_factory_.GetWeakPtr(), callback)); 1047 weak_factory_.GetWeakPtr(), callback));
1054 } 1048 }
1055 1049
1056 void ArcBluetoothBridge::ReadRemoteRssi( 1050 void ArcBluetoothBridge::ReadRemoteRssi(
1057 mojom::BluetoothAddressPtr remote_addr, 1051 mojom::BluetoothAddressPtr remote_addr,
1058 const ReadRemoteRssiCallback& callback) { 1052 const ReadRemoteRssiCallback& callback) {
1059 BluetoothDevice* device = 1053 BluetoothDevice* device =
1060 bluetooth_adapter_->GetDevice(remote_addr->To<std::string>()); 1054 bluetooth_adapter_->GetDevice(remote_addr->To<std::string>());
1061 int rssi = device->GetInquiryRSSI(); 1055 int rssi = device->GetInquiryRSSI();
1062 callback.Run(rssi); 1056 callback.Run(rssi);
1063 } 1057 }
1064 1058
1059 template <class LocalGattObjectT>
1060 int32_t ArcBluetoothBridge::CreateGattAttributeHandle(
1061 LocalGattObjectT* gatt_obj) {
1062 if (!gatt_obj)
1063 return kInvalidGattAttributeHandle;
1064 gatt_server_obj_handle++;
1065 std::string identifier = gatt_obj->GetIdentifier();
1066 gatt_identifier_[gatt_server_obj_handle] = identifier;
palmer 2016/07/16 00:45:13 Nit: You could save a line and write: gatt_iden
puthik_chromium 2016/07/18 21:30:13 We will reuse |identifier| in the next patch when
1067 return gatt_server_obj_handle;
1068 }
1069
1065 void ArcBluetoothBridge::AddService(mojom::BluetoothGattServiceIDPtr service_id, 1070 void ArcBluetoothBridge::AddService(mojom::BluetoothGattServiceIDPtr service_id,
1066 int32_t num_handles, 1071 int32_t num_handles,
1067 const AddServiceCallback& callback) {} 1072 const AddServiceCallback& callback) {
1073 base::WeakPtr<BluetoothLocalGattService> service =
1074 BluetoothLocalGattService::Create(
1075 bluetooth_adapter_.get(), service_id->id->uuid.To<BluetoothUUID>(),
1076 service_id->is_primary, nullptr /*included_service */,
1077 this /* delegate*/);
1078 callback.Run(
1079 CreateGattAttributeHandle<BluetoothLocalGattService>(service.get()));
1080 }
1068 1081
1069 void ArcBluetoothBridge::AddCharacteristic( 1082 void ArcBluetoothBridge::AddCharacteristic(
1070 int32_t service_handle, 1083 int32_t service_handle,
1071 mojom::BluetoothUUIDPtr uuid, 1084 mojom::BluetoothUUIDPtr uuid,
1072 int32_t properties, 1085 int32_t properties,
1073 int32_t permissions, 1086 int32_t permissions,
1074 const AddCharacteristicCallback& callback) {} 1087 const AddCharacteristicCallback& callback) {
1088 base::WeakPtr<BluetoothLocalGattCharacteristic> characteristic =
1089 BluetoothLocalGattCharacteristic::Create(
1090 uuid.To<BluetoothUUID>(), properties, permissions,
1091 bluetooth_adapter_->GetGattService(gatt_identifier_[service_handle]));
1092 int32_t characteristic_handle =
1093 CreateGattAttributeHandle<BluetoothLocalGattCharacteristic>(
1094 characteristic.get());
1095 last_characteristic_[service_handle] = characteristic_handle;
1096 callback.Run(characteristic_handle);
1097 }
1075 1098
1076 void ArcBluetoothBridge::AddDescriptor(int32_t service_handle, 1099 void ArcBluetoothBridge::AddDescriptor(int32_t service_handle,
1077 mojom::BluetoothUUIDPtr uuid, 1100 mojom::BluetoothUUIDPtr uuid,
1078 int32_t permissions, 1101 int32_t permissions,
1079 const AddDescriptorCallback& callback) {} 1102 const AddDescriptorCallback& callback) {
1103 // Chrome automatically add CCC Descriptor to a characteristic when needed.
palmer 2016/07/16 00:45:13 Nit: Check your grammar and punctuation in comment
puthik_chromium 2016/07/18 21:30:12 Done.
1104 // So we will just generate bogus handle for android.
1105 if (uuid.To<BluetoothUUID>() ==
1106 BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid()) {
1107 gatt_server_obj_handle++;
1108 callback.Run(gatt_server_obj_handle);
1109 return;
1110 }
1111
1112 BluetoothLocalGattService* service =
1113 bluetooth_adapter_->GetGattService(gatt_identifier_[service_handle]);
1114 // Since Android API does not give information which characteristic is the
palmer 2016/07/16 00:45:13 Comment style: Since the Android API does not giv
puthik_chromium 2016/07/18 21:30:12 I accidentally deleted the last line of comment. D
1115 // parent of the new descriptor. We will just assume that it would be the
1116 // last characteristic that was added to the given service. This matches the
rkc 2016/07/16 00:37:29 Incomplete comment? Please add the Android source
puthik_chromium 2016/07/18 21:30:12 I accidentally deleted the last line of comment. D
1117 BluetoothLocalGattCharacteristic* characteristic = service->GetCharacteristic(
1118 gatt_identifier_[last_characteristic_[service_handle]]);
1119
1120 base::WeakPtr<BluetoothLocalGattDescriptor> descriptor =
1121 BluetoothLocalGattDescriptor::Create(uuid.To<BluetoothUUID>(),
1122 permissions, characteristic);
1123 callback.Run(CreateGattAttributeHandle<BluetoothLocalGattDescriptor>(
1124 descriptor.get()));
1125 }
1080 1126
1081 void ArcBluetoothBridge::StartService(int32_t service_handle, 1127 void ArcBluetoothBridge::StartService(int32_t service_handle,
1082 const StartServiceCallback& callback) {} 1128 const StartServiceCallback& callback) {
1129 BluetoothLocalGattService* service =
1130 bluetooth_adapter_->GetGattService(gatt_identifier_[service_handle]);
1131 service->Register(base::Bind(&ArcBluetoothBridge::OnGattOperationDone,
1132 weak_factory_.GetWeakPtr(), callback),
1133 base::Bind(&ArcBluetoothBridge::OnGattOperationError,
1134 weak_factory_.GetWeakPtr(), callback));
1135 }
1083 1136
1084 void ArcBluetoothBridge::StopService(int32_t service_handle, 1137 void ArcBluetoothBridge::StopService(int32_t service_handle,
1085 const StopServiceCallback& callback) {} 1138 const StopServiceCallback& callback) {
1139 BluetoothLocalGattService* service =
1140 bluetooth_adapter_->GetGattService(gatt_identifier_[service_handle]);
1141 service->Unregister(base::Bind(&ArcBluetoothBridge::OnGattOperationDone,
1142 weak_factory_.GetWeakPtr(), callback),
1143 base::Bind(&ArcBluetoothBridge::OnGattOperationError,
1144 weak_factory_.GetWeakPtr(), callback));
1145 }
1086 1146
1087 void ArcBluetoothBridge::DeleteService(int32_t service_handle, 1147 void ArcBluetoothBridge::DeleteService(int32_t service_handle,
1088 const DeleteServiceCallback& callback) {} 1148 const DeleteServiceCallback& callback) {
1149 BluetoothLocalGattService* service =
1150 bluetooth_adapter_->GetGattService(gatt_identifier_[service_handle]);
1151 service->Delete();
1152 gatt_identifier_.erase(service_handle);
1153 OnGattOperationDone(callback);
1154 }
1089 1155
1090 void ArcBluetoothBridge::SendIndication( 1156 void ArcBluetoothBridge::SendIndication(
1091 int32_t attribute_handle, 1157 int32_t attribute_handle,
1092 mojom::BluetoothAddressPtr address, 1158 mojom::BluetoothAddressPtr address,
1093 bool confirm, 1159 bool confirm,
1094 mojo::Array<uint8_t> value, 1160 mojo::Array<uint8_t> value,
1095 const SendIndicationCallback& callback) {} 1161 const SendIndicationCallback& callback) {}
1096 1162
1097 void ArcBluetoothBridge::SendResponse(int32_t trans_id, 1163 void ArcBluetoothBridge::SendResponse(int32_t trans_id,
1098 int32_t status, 1164 int32_t status,
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after
1430 uint32_t version_need) const { 1496 uint32_t version_need) const {
1431 uint32_t version = arc_bridge_service()->bluetooth()->version(); 1497 uint32_t version = arc_bridge_service()->bluetooth()->version();
1432 if (version >= version_need) 1498 if (version >= version_need)
1433 return true; 1499 return true;
1434 LOG(WARNING) << "Bluetooth instance is too old (version " << version 1500 LOG(WARNING) << "Bluetooth instance is too old (version " << version
1435 << ") need version " << version_need; 1501 << ") need version " << version_need;
1436 return false; 1502 return false;
1437 } 1503 }
1438 1504
1439 } // namespace arc 1505 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698