24
24
#include < gio/gio.h>
25
25
#include < glib.h>
26
26
27
+ #include < ble/Ble.h>
27
28
#include < lib/support/CHIPMem.h>
28
29
#include < lib/support/CodeUtils.h>
29
30
#include < platform/ConnectivityManager.h>
@@ -42,29 +43,31 @@ namespace Internal {
42
43
43
44
namespace {
44
45
45
- gboolean BluezIsServiceOnDevice (BluezGattService1 * aService, BluezDevice1 * aDevice)
46
+ bool BluezIsServiceOnDevice (BluezGattService1 * aService, BluezDevice1 * aDevice)
46
47
{
47
- const auto * servicePath = bluez_gatt_service1_get_device (aService);
48
- const auto * devicePath = g_dbus_proxy_get_object_path (reinterpret_cast <GDBusProxy *>(aDevice));
49
- return strcmp (servicePath, devicePath) == 0 ? TRUE : FALSE ;
48
+ auto servicePath = bluez_gatt_service1_get_device (aService);
49
+ auto devicePath = g_dbus_proxy_get_object_path (reinterpret_cast <GDBusProxy *>(aDevice));
50
+ return strcmp (servicePath, devicePath) == 0 ;
50
51
}
51
52
52
- gboolean BluezIsCharOnService (BluezGattCharacteristic1 * aChar, BluezGattService1 * aService)
53
+ bool BluezIsCharOnService (BluezGattCharacteristic1 * aChar, BluezGattService1 * aService)
53
54
{
54
- const auto * charPath = bluez_gatt_characteristic1_get_service (aChar);
55
- const auto * servicePath = g_dbus_proxy_get_object_path (reinterpret_cast <GDBusProxy *>(aService));
56
- ChipLogDetail (DeviceLayer, " Char %s on service %s" , charPath, servicePath);
57
- return strcmp (charPath, servicePath) == 0 ? TRUE : FALSE ;
55
+ auto charPath = bluez_gatt_characteristic1_get_service (aChar);
56
+ auto servicePath = g_dbus_proxy_get_object_path (reinterpret_cast <GDBusProxy *>(aService));
57
+ return strcmp (charPath, servicePath) == 0 ;
58
58
}
59
59
60
- } // namespace
61
-
62
- BluezConnection::BluezConnection (const BluezEndpoint & aEndpoint, BluezDevice1 & aDevice) :
63
- mDevice (reinterpret_cast <BluezDevice1 *>(g_object_ref(&aDevice)))
60
+ bool BluezIsFlagOnChar (BluezGattCharacteristic1 * aChar, const char * flag)
64
61
{
65
- Init (aEndpoint);
62
+ auto charFlags = bluez_gatt_characteristic1_get_flags (aChar);
63
+ for (size_t i = 0 ; charFlags[i] != nullptr ; i++)
64
+ if (strcmp (charFlags[i], flag) == 0 )
65
+ return true ;
66
+ return false ;
66
67
}
67
68
69
+ } // namespace
70
+
68
71
BluezConnection::IOChannel::~IOChannel ()
69
72
{
70
73
if (mWatchSource != nullptr )
@@ -85,73 +88,69 @@ CHIP_ERROR BluezConnection::Init(const BluezEndpoint & aEndpoint)
85
88
mService .reset (reinterpret_cast <BluezGattService1 *>(g_object_ref (aEndpoint.mService .get ())));
86
89
mC1 .reset (reinterpret_cast <BluezGattCharacteristic1 *>(g_object_ref (aEndpoint.mC1 .get ())));
87
90
mC2 .reset (reinterpret_cast <BluezGattCharacteristic1 *>(g_object_ref (aEndpoint.mC2 .get ())));
91
+ return CHIP_NO_ERROR;
88
92
}
89
- else
93
+
94
+ for (BluezObject & object : aEndpoint.mObjectManager .GetObjects ())
90
95
{
91
- for (BluezObject & object : aEndpoint.mObjectManager .GetObjects ())
96
+ GAutoPtr<BluezGattService1> service (bluez_object_get_gatt_service1 (&object));
97
+ if (service && BluezIsServiceOnDevice (service.get (), mDevice .get ()))
92
98
{
93
- BluezGattService1 * service = bluez_object_get_gatt_service1 (&object);
94
- if (service != nullptr )
99
+ if (strcmp (bluez_gatt_service1_get_uuid (service.get ()), Ble::CHIP_BLE_SERVICE_LONG_UUID_STR) == 0 )
95
100
{
96
- if ((BluezIsServiceOnDevice (service, mDevice .get ())) == TRUE &&
97
- (strcmp (bluez_gatt_service1_get_uuid (service), Ble::CHIP_BLE_SERVICE_LONG_UUID_STR) == 0 ))
98
- {
99
- mService .reset (service);
100
- break ;
101
- }
102
- g_object_unref (service);
101
+ ChipLogDetail (DeviceLayer, " CHIP service found" );
102
+ mService .reset (service.release ());
103
+ break ;
103
104
}
104
105
}
106
+ }
105
107
106
- VerifyOrExit (mService , ChipLogError (DeviceLayer, " FAIL: NULL service in %s" , __func__));
108
+ VerifyOrReturnError (
109
+ mService , BLE_ERROR_NOT_CHIP_DEVICE,
110
+ ChipLogError (DeviceLayer, " CHIP service (%s) not found on %s" , Ble::CHIP_BLE_SERVICE_LONG_UUID_STR, GetPeerAddress ()));
107
111
108
- for (BluezObject & object : aEndpoint.mObjectManager .GetObjects ())
112
+ for (BluezObject & object : aEndpoint.mObjectManager .GetObjects ())
113
+ {
114
+ GAutoPtr<BluezGattCharacteristic1> chr (bluez_object_get_gatt_characteristic1 (&object));
115
+ if (chr && BluezIsCharOnService (chr.get (), mService .get ()))
109
116
{
110
- BluezGattCharacteristic1 * char1 = bluez_object_get_gatt_characteristic1 (&object);
111
- if (char1 != nullptr )
117
+ if ( strcmp ( bluez_gatt_characteristic1_get_uuid (chr. get ()), Ble::CHIP_BLE_CHAR_1_UUID_STR) == 0 &&
118
+ BluezIsFlagOnChar (chr. get (), " write " ) )
112
119
{
113
- if ((BluezIsCharOnService (char1, mService .get ()) == TRUE ) &&
114
- (strcmp (bluez_gatt_characteristic1_get_uuid (char1), Ble::CHIP_BLE_CHAR_1_UUID_STR) == 0 ))
115
- {
116
- mC1 .reset (char1);
117
- }
118
- else if ((BluezIsCharOnService (char1, mService .get ()) == TRUE ) &&
119
- (strcmp (bluez_gatt_characteristic1_get_uuid (char1), Ble::CHIP_BLE_CHAR_2_UUID_STR) == 0 ))
120
- {
121
- mC2 .reset (char1);
122
- }
120
+ ChipLogDetail (DeviceLayer, " Valid C1 characteristic found" );
121
+ mC1 .reset (chr.release ());
122
+ }
123
+ else if (strcmp (bluez_gatt_characteristic1_get_uuid (chr.get ()), Ble::CHIP_BLE_CHAR_2_UUID_STR) == 0 &&
124
+ BluezIsFlagOnChar (chr.get (), " indicate" ))
125
+ {
126
+ ChipLogDetail (DeviceLayer, " Valid C2 characteristic found" );
127
+ mC2 .reset (chr.release ());
128
+ }
123
129
#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
124
- else if ((BluezIsCharOnService (char1, mService .get ()) == TRUE ) &&
125
- (strcmp (bluez_gatt_characteristic1_get_uuid (char1), Ble::CHIP_BLE_CHAR_3_UUID_STR) == 0 ))
126
- {
127
- mC3 .reset (char1);
128
- }
129
- #endif
130
- else
131
- {
132
- g_object_unref (char1);
133
- }
134
- if (mC1 && mC2 )
135
- {
136
- break ;
137
- }
130
+ else if (strcmp (bluez_gatt_characteristic1_get_uuid (chr.get ()), Ble::CHIP_BLE_CHAR_3_UUID_STR) == 0 &&
131
+ BluezIsFlagOnChar (chr.get (), " read" ))
132
+ {
133
+ ChipLogDetail (DeviceLayer, " Valid C3 characteristic found" );
134
+ mC3 .reset (chr.release ());
138
135
}
136
+ #endif
139
137
}
140
-
141
- VerifyOrExit (mC1 , ChipLogError (DeviceLayer, " FAIL: NULL C1 in %s" , __func__));
142
- VerifyOrExit (mC2 , ChipLogError (DeviceLayer, " FAIL: NULL C2 in %s" , __func__));
143
138
}
144
139
145
- exit :
140
+ VerifyOrReturnError (mC1 , BLE_ERROR_NOT_CHIP_DEVICE,
141
+ ChipLogError (DeviceLayer, " No valid C1 (%s) on %s" , Ble::CHIP_BLE_CHAR_1_UUID_STR, GetPeerAddress ()));
142
+ VerifyOrReturnError (mC2 , BLE_ERROR_NOT_CHIP_DEVICE,
143
+ ChipLogError (DeviceLayer, " No valid C2 (%s) on %s" , Ble::CHIP_BLE_CHAR_2_UUID_STR, GetPeerAddress ()));
144
+
146
145
return CHIP_NO_ERROR;
147
146
}
148
147
149
- CHIP_ERROR BluezConnection::BluezDisconnect (BluezConnection * conn)
148
+ CHIP_ERROR BluezConnection::CloseConnectionImpl (BluezConnection * conn)
150
149
{
151
150
GAutoPtr<GError> error;
152
151
gboolean success;
153
152
154
- ChipLogDetail (DeviceLayer, " %s peer=%s" , __func__ , conn->GetPeerAddress ());
153
+ ChipLogDetail (DeviceLayer, " Close BLE connection: peer=%s" , conn->GetPeerAddress ());
155
154
156
155
success = bluez_device1_call_disconnect_sync (conn->mDevice .get (), nullptr , &error.GetReceiver ());
157
156
VerifyOrExit (success == TRUE , ChipLogError (DeviceLayer, " FAIL: Disconnect: %s" , error->message ));
@@ -162,7 +161,7 @@ CHIP_ERROR BluezConnection::BluezDisconnect(BluezConnection * conn)
162
161
163
162
CHIP_ERROR BluezConnection::CloseConnection ()
164
163
{
165
- return PlatformMgrImpl ().GLibMatterContextInvokeSync (BluezDisconnect , this );
164
+ return PlatformMgrImpl ().GLibMatterContextInvokeSync (CloseConnectionImpl , this );
166
165
}
167
166
168
167
const char * BluezConnection::GetPeerAddress () const
0 commit comments