Commit Graph

57 Commits

Author SHA1 Message Date
Anderson Lizardo
d1abf2b7d8 attrib: Reduce scope of variable declarations in char_discovered_cb()
This makes it easy to identify where variables are being used.
2013-07-24 08:44:07 -07:00
Anderson Lizardo
910beb706c attrib: Simplify prepare_write()
All parameters can be retrieved from struct write_long_data fields,
therefore there is no need to pass them separately.
2013-07-24 08:42:05 -07:00
Anderson Lizardo
9b1e7de26c attrib: Fix coding style in prepare_write_cb() 2013-07-24 08:41:58 -07:00
Anderson Lizardo
ccd0e3783d attrib: Do not use Write Command in gatt_write_char()
Previously, if no callback was given to gatt_write_char(), it was
assumed that a "Write Without Response" (which uses Write Command)
should be used instead of Write Request.

This "shortcut" is unnecessary (there is gatt_write_cmd() for the
situations where Write Without Response is required) and just duplicates
code.

This commit also fixes the few places where gatt_write_cmd() should be
used.
2013-07-24 08:41:47 -07:00
Anderson Lizardo
1d4f111fae attrib: Rename gatt_find_info() to gatt_discover_char_desc()
The old name referred to the underlying ATT PDU (Find Information
Request and Find Information Response) and not to the actual GATT
procedure (Discover All Characteristic Descriptors).
2013-07-24 08:41:32 -07:00
Johan Hedberg
88f7ecd64d attrib: Use GAttrib destroy callback for included search 2013-06-07 14:20:19 +03:00
Lucas De Marchi
45abbe7b9a attrib: Get rid of gint
Use plain int instead of gint. In glib gint is always a typedef to int,
so it's safe to use it even for callbacks with glib.
2013-05-01 09:04:16 +03:00
Lucas De Marchi
1bd26421ce attrib: Use gcc builtin instead of g_atomic
g_atomic_* end up using G_STATIC_ASSERT, causing gcc 4.8 to yell due to
-Wunused-local-typedefs.

/usr/include/glib-2.0/glib/gmacros.h:162:53: error: typedef ‘_GStaticAssertCompileTimeAssertion_2’ locally defined but not used [-Werror=unused-local-typedefs]
 #define G_STATIC_ASSERT(expr) typedef char G_PASTE (_GStaticAssertCompileTimeAssertion_, __COUNTER__)[(expr) ? 1 : -1]

Most of the uses of atomic operations were wrong. They were fixed as
well. If we are using atomic operations, reading the variable again
later for logging is not an option, we should use the return of the
atomic function used to fetch the variable.
2013-04-09 07:01:36 +09:00
Anderson Lizardo
8c8cc51b18 attrib: Fix status on memory allocation failure
If memory allocation fails, ATT_ECODE_INSUFF_RESOURCES should be
reported to the callback.
2013-04-04 14:51:21 +03:00
Anderson Lizardo
8b61564c4d attrib: Fix coding style in read_char_helper() 2013-04-04 14:51:08 +03:00
Anderson Lizardo
03f2256f6a attrib: Fix memory leak on low memory condition
If g_try_new0() fails due to low memory condition, "list" should be
freed before returning from primary_all_cb().
2013-04-04 14:50:59 +03:00
Vinicius Costa Gomes
99f609241c device: Fix invalid memory access during Find Included
When doing the Find Included Services GATT procedure, the status of the ATT
procedure was being ignored, and in the case of a timeout it is possible to
crash bluetooth with an invalid memory access.

Valgrind log:

==1755== Invalid read of size 8
==1755==    at 0x46971A: find_included_cb (device.c:2964)
==1755==    by 0x4465AE: isd_unref (gatt.c:92)
==1755==    by 0x446885: find_included_cb (gatt.c:425)
==1755==    by 0x448266: disconnect_timeout (gattrib.c:269)
==1755==    by 0x4E76BCA: g_timeout_dispatch (in /usr/lib64/libglib-2.0.so.0.3400.2)
==1755==    by 0x4E76044: g_main_context_dispatch (in /usr/lib64/libglib-2.0.so.0.3400.2)
==1755==    by 0x4E76377: g_main_context_iterate.isra.24 (in /usr/lib64/libglib-2.0.so.0.3400.2)
==1755==    by 0x4E76771: g_main_loop_run (in /usr/lib64/libglib-2.0.so.0.3400.2)
==1755==    by 0x40A2EE: main (main.c:583)
==1755==  Address 0x69530a8 is 8 bytes inside a block of size 64 free'd
==1755==    at 0x4C2874F: free (vg_replace_malloc.c:446)
==1755==    by 0x40BFA6: service_filter (watch.c:486)
==1755==    by 0x40BC6A: message_filter (watch.c:554)
==1755==    by 0x5160A1D: dbus_connection_dispatch (in /usr/lib64/libdbus-1.so.3.7.2)
==1755==    by 0x40AAB7: message_dispatch (mainloop.c:76)
==1755==    by 0x4E76BCA: g_timeout_dispatch (in /usr/lib64/libglib-2.0.so.0.3400.2)
==1755==    by 0x4E76044: g_main_context_dispatch (in /usr/lib64/libglib-2.0.so.0.3400.2)
==1755==    by 0x4E76377: g_main_context_iterate.isra.24 (in /usr/lib64/libglib-2.0.so.0.3400.2)
==1755==    by 0x4E76771: g_main_loop_run (in /usr/lib64/libglib-2.0.so.0.3400.2)
==1755==    by 0x40A2EE: main (main.c:583)
==1755==
==1755== Invalid read of size 8
==1755==    at 0x4486D5: g_attrib_get_buffer (gattrib.c:657)
==1755==    by 0x4467C5: find_included (gatt.c:363)
==1755==    by 0x4465AE: isd_unref (gatt.c:92)
==1755==    by 0x446885: find_included_cb (gatt.c:425)
==1755==    by 0x448266: disconnect_timeout (gattrib.c:269)
==1755==    by 0x4E76BCA: g_timeout_dispatch (in /usr/lib64/libglib-2.0.so.0.3400.2)
==1755==    by 0x4E76044: g_main_context_dispatch (in /usr/lib64/libglib-2.0.so.0.3400.2)
==1755==    by 0x4E76377: g_main_context_iterate.isra.24 (in /usr/lib64/libglib-2.0.so.0.3400.2)
==1755==    by 0x4E76771: g_main_loop_run (in /usr/lib64/libglib-2.0.so.0.3400.2)
==1755==    by 0x40A2EE: main (main.c:583)
==1755==  Address 0x18 is not stack'd, malloc'd or (recently) free'd
==1755==
==1755==
==1755== Process terminating with default action of signal 11 (SIGSEGV)
==1755==  Access not within mapped region at address 0x18
==1755==    at 0x4486D5: g_attrib_get_buffer (gattrib.c:657)
==1755==    by 0x4467C5: find_included (gatt.c:363)
==1755==    by 0x4465AE: isd_unref (gatt.c:92)
==1755==    by 0x446885: find_included_cb (gatt.c:425)
==1755==    by 0x448266: disconnect_timeout (gattrib.c:269)
==1755==    by 0x4E76BCA: g_timeout_dispatch (in /usr/lib64/libglib-2.0.so.0.3400.2)
==1755==    by 0x4E76044: g_main_context_dispatch (in /usr/lib64/libglib-2.0.so.0.3400.2)
==1755==    by 0x4E76377: g_main_context_iterate.isra.24 (in /usr/lib64/libglib-2.0.so.0.3400.2)
==1755==    by 0x4E76771: g_main_loop_run (in /usr/lib64/libglib-2.0.so.0.3400.2)
==1755==    by 0x40A2EE: main (main.c:583)
2013-01-29 15:57:36 -06:00
Anderson Lizardo
5434627c4c attrib: Fix remaining references to bluetooth/uuid.h
These headers are not public anymore, so need to use "lib/uuid.h".
2013-01-10 15:11:53 +02:00
Johan Hedberg
b8779d2202 Revert "Ensure config.h is included by using CPPFLAGS"
This reverts commit 8a03376544.

The patch needs to be split up and the gdbus/ changes were bogus
compared to the original commit message.

Conflicts:
	Makefile.am
	Makefile.obexd
	profiles/cyclingspeed/cyclingspeed.c
	profiles/heartrate/heartrate.c
	src/error.c
2012-12-07 12:46:04 +02:00
Lucas De Marchi
8a03376544 Ensure config.h is included by using CPPFLAGS
Instead of trying to include config.h in each file over the tree and
possibly forgetting to include it, give a "-include config.h" argument
to the compiler so it's guaranteed that a) it will be included for all
source files and b) it will be the first header included.

gdbus/ directory is left out, since it would break other projects using
it.
2012-12-05 17:18:07 +02:00
Paulo Borges
a5c67459d8 gatt: Improve characteristics discovery
In the Discover Characteristics by UUID sub-procedure, if a fetched
characteristic doesn't matches with the target UUID, all others
characteristics in that response were discarded.

Because of this, the procedure will make a new request to possibly
rediscover the characteristics in the range beyond this last
characteristic.

At present, this procedure works because the gatt library will send a
Read by Type Request starting at the first attribute after the non
matching characteristic.

This commit makes the rest of the characteristics to be checked for a
matching type, which should reduce the number of requests sent during
the discovery of characteristics.
2012-12-01 11:11:26 +02:00
Paulo Borges
d07b735117 gatt: Fix memory leak in characteristic discovery
If the Discover Characteristics by UUID sub-procedure has been
executed and the first characteristic is not the target, a memory
leak occurs.

This commit fixes this leak by postponing the allocation to after
the UUID verification.
2012-12-01 11:11:16 +02:00
Vinicius Costa Gomes
8a39d6ed2d attrib: Remove opcode parameter from g_attrib_send()
In all uses of g_attrib_send() the opcode of the command/event is
already clear because of the att.h functions used to build the ATT
PDU.
2012-10-09 10:56:52 +02:00
Vinicius Costa Gomes
ed3344c96b gatt: Add support for find included services
Some services like HID over LE can reference another service using
included services.

See Vol 3, Part G, section 2.6.3 of Core specification for more
details.
2012-10-09 10:54:49 +02:00
Jefferson Delfes
3d0e48b334 gatt: Remove offset parameter from gatt_read_char
The Core spec allows to implement Read Long Characteristic Value as a
Read Request, followed by zero or more Read Blob Requests, therefore the
offset parameter is unnecessary and is always 0 for normal use.
2012-10-01 12:54:51 +03:00
Claudio Takahasi
8ff63e2707 gatt: Don't raise error if declaration was found
This patch avoids raising errors to the upper layer if at least one
characteristic declaration has been found. According to the Core SPEC
Characteristic declarations shall be read without authentication or
authorization.
2012-08-20 20:45:25 +03:00
Claudio Takahasi
221fd83c2c gatt: Return not found if characteritics is empty
This patch changes the behaviour of Discover All Characteristics and
Discover Characteristics by UUID returning <<Attribute Not Found>> when
Characteristic list that matches the defined criteria is empty. This
scenario can happen when the GATT client avoids extra iteraction with
the remote attribute server if the last received handle(or handle + 1)
is equal to the end handle of the primary service.
2012-08-20 20:45:23 +03:00
Johan Hedberg
04be4fe0c0 attrib: Use proper types for size variables
size_t/ssize_t/off_t/etc are more appropriate for variables denoting
some kind of size than simply using int.

This patch includes a couple of other related changes to avoid gcc
signedness errors resulting from it treating (for whatever reason) const
variables and integer literals as signed.
2012-08-15 12:20:51 +03:00
Eder Ruiz Maria
7040dc92fc gatt: Add support to GATT Write Long Characteristic
Extending the function gatt_write_char for support GATT Write Long
Characteristics. MTU is checked and if the payload does not fit,
the prepare and execute write are used to do the transaction.
2012-08-15 11:58:43 +03:00
Claudio Takahasi
d65ee066a1 GATT: Avoid unneeded discover transaction
This patch fix unneeded discover characteristics transaction when the
last characteristic declaration contains the handle number equals to the
end handle of the primary service.
2012-05-30 10:42:43 +03:00
Marcel Holtmann
84156dadb2 build: Remove glib-compat.h support 2012-04-16 18:23:02 +02:00
Chen Ganir
1e064929b3 GATT: Replace start/end with att_range
Use att_range struct instead of individual start/end handles in
gatt_primary structure
2012-03-26 12:18:00 +03:00
Chen Ganir
48c82115a2 GATT: Rename service and char structs
Rename the att_primary to gatt_primary and att_char to gatt_char.
Characteristic and Service do not exist in the ATT spec, only in
GATT.
2012-03-26 12:16:44 +03:00
Johan Hedberg
9b98a6c764 Add glib-compat.h to deal with issues in old GLib versions 2011-10-18 10:57:06 +03:00
Anderson Lizardo
71d994a129 Refactor value assignments of bt_uuid_t variables
Prior to this commit, the assignments were made with memcpy(). This can
be unsafe and less readable, therefore it was replaced with code like:

<dst> = *src;

This also allows more compiler safety checks.
2011-09-22 20:01:30 +09:00
Marcel Holtmann
3f553a30ad Allow building with GLib 2.16 for now 2011-07-04 11:09:55 +02:00
Luiz Augusto von Dentz
05cb01fc32 Make use of g_slist_free_full on attrib 2011-06-30 12:32:19 +03:00
Szymon Janc
0c5f016867 Remove unused op variable from encode_discover_primary 2011-05-15 01:47:09 +03:00
Claudio Takahasi
612d020764 Register primary services exported over basic rate
This patch registers the object paths for primary services exported
through SDP. PSM, start and end handle information are available in
the Protocol Descriptor List.
2011-04-14 20:09:21 +03:00
Bruna Moreira
1b07befa15 Add Exchange MTU operation in GATT library 2011-03-18 11:17:58 +02:00
Bruna Moreira
efcef2bd79 Use GAttrib buffer for ATT protocol PDUs
Prior to this commit, there were local buffers inside GATT functions.
Now, a single buffer is used, to make sure the MTU limit is respected.
2011-03-18 11:15:07 +02:00
Sheldon Demario
922c5feb93 Add discover characteristics by UUID to gatttool
According to the specification the characteristics discover and
characteristics discover by UUID use the same opcode and the result
should be filtered by callback.
2011-03-17 14:50:59 +02:00
Elvis Pfützenreuter
a278db781a Use new UUID functions in GATT
This patch puts the new UUID functions into use for GATT-related
code, and adds some convenience functions to ATT API (att.h).
Example GATT server is also changed.
2011-03-15 19:36:48 +02:00
Claudio Takahasi
4fce91f1cf Extend Characteristic Write to support Write Without Response
If callback is not informed, Write Command will be used to execute
a Write Without Response sub-procedure. Error is not returned by the
server no matter the result of the operation.
2011-03-10 11:20:08 +02:00
Sheldon Demario
adc7ca1ece Add offset option to characteristic read by handle
Some tests require an arbitrary offset.
2011-03-04 15:48:21 -03:00
Bruna Moreira
240fcdb006 Fix invalid read after list concatenation
g_slist_concat uses the items from second list directly so they should
not be freed.
2011-02-04 06:55:02 +02:00
Brian Gix
8865beb9a3 Fix incoming attrib-server connection MTUs
It is important for the Attribute Server to be aware of and completely
fill response packets up to the full MTU when reading long attributes.
Some remote devices will only request additional (READ_BLOB) data if the
preceding read sent the maximum amount of data.

Incoming connections are identified as L2CAP or LE by pointers to the
Service IO channel the incoming connection was recieved on in the
user_data parameter. L2CAP channels are set to the BR/EDR minimum MTU of
48, and LE channels to the LE payload size of 23.
2011-01-21 12:19:12 +05:30
Bruna Moreira
46be88cb8b Move common code from Discover all Characteristics to GATT library
The attribute client (attrib/client.c) and gatttool share similar code
to parse the PDU coming from server. This commit moves this common code
to attrib/gatt.c, and simplifies the callbacks implemented by the
clients. The client callbacks are now called just once and get a GSList
of characteristics, instead of the raw PDU.
2011-01-19 20:41:11 +05:30
Bruna Moreira
e04efbaddf Rename gatt_primary_t to more generic name
The gatt_primary_t typedef was renamed to gatt_cb_t because it will be
used for primary and characteristic callbacks.
2011-01-19 20:40:56 +05:30
Claudio Takahasi
5399423bd6 Move interactive code of Discover Primary to gatt.c
Initial patch to move the shared code related to Discover All Primary
Services and Discover Primary Services by UUID to gatt.c.
2011-01-08 11:02:38 +02:00
Brian Gix
acdc181a5d Fix gatt_read_char() to support long Attrib Values
Fix gatt_read_char() to support long Attribute Values by recognizing
that results longer that 21 octets may include data beyond
what has been returned with the first read. Extra data is
obtained by issuing READ_BLOB requests until either a
result is returned shorter than 22 octets, or an error
is recieved indicating that no further data is available.
The API for this function has not changed.
2011-01-07 21:31:34 +02:00
Brian Gix
7f0b31cbb4 Fix g_attrib_send() to include a new ID parameter
Overall purpose of change is to enable a GATT procedure to be
executed atomically, even if it requires multiple ATT
request/response transactions.

Fix g_attrib_send() to include an ID parameter, if the pkt to
be sent should be added to the Head of the pkt queue.  If the
ID is Zero, legacy functionality is maintained, and the pkt will
be added at the tail of the queuer, and a new ID will be generated,
and returned to the caller. If ID is non-zero, the pkt will be
added to the head of the queue, with the ID value requested, which
will also be returned to the caller.

Fix received_data() to not service the send queue until after the
received data has been processed by calling the cmd->func()
callback, to allow the callback to insert another pkt on the head
of the queue.

Fix all callers of g_attrib_send() to include new parameter.
2011-01-07 21:31:31 +02:00
Sheldon Demario
ad0e7f2c5a Implement Characteristic Value Read using UUID in the gatttool
Sub-procedure used to read a Characteristic Value when the client
only knows the characteristic UUID and doesn't know the handle.
More than one handle and attribute value pair can be returned,
it is up to the user define the handles range based on the service
handles range.

Usage example:
$gatttool --char-read --uuid=2a00 -i hcix -b xx:xx:xx:xx:xx:xx
2010-11-19 17:25:30 +02:00
Claudio Takahasi
8022b94b05 Add an extra parameter in the discovery primary to specify the UUID
Extends discover primary function to perform discover by UUID. UUID
parameter defines which procedure will be executed: Discover All
Primary Services or Discover Primary Service by Service UUID.
2010-11-18 21:58:22 +02:00
Anderson Lizardo
0a70694d3a Add support for Attribute Write Request
Implement encoders/decoders for Write Request/Response and the handling
on attribute server. The attribute client still uses the Write Command
because currently SetProperty() has no means to wait for the server
response.
2010-10-09 18:22:09 +03:00