Commit Graph

93 Commits

Author SHA1 Message Date
Alexander Ganslandt
720e8ec976 client/gatt: Set handle before calling print functions
The print functions (print_service, print_chrc and print_desc) all print
the handle, but the handle is never set in the struct object. This
results in the handle always printing as 0x0000. Set the handle before
calling the print function.
2024-08-01 04:25:27 -04:00
Bastien Nocera
4262170a79 client/gatt: Check write_value() retval
Error: CHECKED_RETURN (CWE-252): [#def35]
client/gatt.c:3191:3: check_return: Calling "write_value" without checking return value (as is done elsewhere 5 out of 6 times).
client/gatt.c:2371:2: example_checked: Example 1: "write_value(&chrc->value_len, &chrc->value, value, value_len, aad->offset, chrc->max_val_len)" has its value checked in "write_value(&chrc->value_len, &chrc->value, value, value_len, aad->offset, chrc->max_val_len)".
client/gatt.c:2502:2: example_checked: Example 2: "write_value(&chrc->value_len, &chrc->value, value, value_len, offset, chrc->max_val_len)" has its value checked in "write_value(&chrc->value_len, &chrc->value, value, value_len, offset, chrc->max_val_len)".
client/gatt.c:2919:2: example_checked: Example 3: "write_value(&desc->value_len, &desc->value, value, value_len, offset, desc->max_val_len)" has its value checked in "write_value(&desc->value_len, &desc->value, value, value_len, offset, desc->max_val_len)".
client/gatt.c:759:3: example_checked: Example 4: "write_value(&c->value_len, &c->value, value, value_len, offset, c->max_val_len)" has its value checked in "write_value(&c->value_len, &c->value, value, value_len, offset, c->max_val_len)".
client/gatt.c:775:3: example_checked: Example 5: "write_value(&d->value_len, &d->value, value, value_len, offset, d->max_val_len)" has its value checked in "write_value(&d->value_len, &d->value, value, value_len, offset, d->max_val_len)".
3189|			}
3190|
3191|->			write_value(&chrc->value_len, &chrc->value, value, len,
3192|					0, chrc->max_val_len);
2024-05-10 11:26:48 -04:00
Bastien Nocera
1ba9e5f21c client/gatt: Don't pass negative fd on error
Error: NEGATIVE_RETURNS (CWE-394): [#def33]
client/gatt.c:973:2: negative_return_fn: Function "io_get_fd(io)" returns a negative number.
client/gatt.c:973:2: negative_returns: "io_get_fd(io)" is passed to a parameter that cannot be negative.
971|	msg.msg_iovlen = iovlen;
972|
973|->	ret = sendmsg(io_get_fd(io), &msg, MSG_NOSIGNAL);
974|	if (ret < 0) {
975|		ret = -errno;

Error: NEGATIVE_RETURNS (CWE-394): [#def34]
client/gatt.c:1049:2: negative_return_fn: Function "io_get_fd(io)" returns a negative number.
client/gatt.c:1049:2: assign: Assigning: "fd" = "io_get_fd(io)".
client/gatt.c:1062:2: negative_returns: "fd" is passed to a parameter that cannot be negative.
1060|		msg.msg_iovlen = 1;
1061|
1062|->		bytes_read = recvmsg(fd, &msg, MSG_DONTWAIT);
1063|		if (bytes_read < 0) {
1064|			bt_shell_printf("recvmsg: %s", strerror(errno));
2024-05-10 11:26:20 -04:00
Christian Eggers
14057f2cc0 client/gatt: gatt_register_app: fix parsing of UUIDs
- argv[0] (shell command) must not be used as UUID argument
- fixed linked list iteration
2024-05-02 10:29:32 -04:00
Iulia Tanasescu
352e752636 client/gatt: Quit shell after user input has been processed
Inside gatt_register_service and gatt_register_chrc,
bt_shell_noninteractive_quit should be called after
the input was processed.
2024-03-08 11:11:49 -05:00
Luiz Augusto von Dentz
9a550d43b8 client: Allow gatt.select-attribute to work with local attributes
This allows gatt.select-attribute local to select from the registered
attributes:

[bluetooth]# gatt.select-attribute local /org/bluez/app/service0/chrc0
[/org/bluez/app/service0/chrc0]# gatt.write 0x01
[CHG] Attribute /org/bluez/app/service0/chrc0 (%UUID) written
[/org/bluez/app/service0/chrc0]# gatt.read
01                                               .
[/org/bluez/app/service0/chrc0]# gatt.select-attribute local
 /org/bluez/app/service0/chrc1
[/org/bluez/app/service0/chrc1]# gatt.write 0x01
[CHG] Attribute /org/bluez/app/service0/chrc1 (%UUID) written
[/org/bluez/app/service0/chrc1]# gatt.read
01                                               .
[/org/bluez/app/service0/chrc1]#
2022-12-06 13:01:28 -08:00
Luiz Augusto von Dentz
6c71203056 client/gatt: Fix scan-build warning
This fixes the following warning:

client/gatt.c:2146:2: warning: Null pointer passed to 2nd parameter
expecting 'nonnull' [core.NonNullParamChecker]
        memcpy(*dst_value + offset, src_val, src_len);
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2022-09-23 13:47:10 -07:00
Christian Eggers
ee750bbaf6 client/gatt: proxy_property_changed: check for NULL iterator
The passed iterator can be NULL as in
gdbus/client.c::properties_changed():
...
   proxy->prop_func(..., ..., iter=NULL, ...)
   +--client/gatt.c::proxy_property_changed(..., ..., iter, ...);
      +--dbus_message_iter_get_arg_type(iter);
...
2022-09-23 13:42:49 -07:00
Luiz Augusto von Dentz
c2734c41f7 client/gatt: Fix notification enabled/disabled output
When notifications are enabled/disable the output was not print a new
line.
2022-09-21 14:45:03 -07:00
Gopal Tiwari
b4233bca18 client/gatt: Fix memory leak issues
While performing the static tool analysis using coverity tool
found following reports

Error: RESOURCE_LEAK (CWE-772):
bluez-5.64/client/gatt.c:1531: leaked_storage: Variable "service"
going out of scope leaks the storage it points to.

Error: RESOURCE_LEAK (CWE-772):
bluez-5.64/client/gatt.c:2626: leaked_storage: Variable "chrc"
going out of scope leaks the storage it points to.

Error: RESOURCE_LEAK (CWE-772):
bluez-5.64/client/gatt.c:2906: leaked_storage: Variable "desc"
going out of scope leaks the storage it points to.
2022-05-31 12:48:33 -07:00
Luiz Augusto von Dentz
cfab569484 build: Replace use of g_memdup with util_memdup
This replaces the uses of g_memdup with util_memdup since the former has
been deprecated:

  warning: ‘g_memdup’ is deprecated: Use 'g_memdup2' instead
  [-Wdeprecated-declarations]

g_memdup2 requires bumping glib version which would likely have its
own problems thus why util_memdup was introduced.
2022-01-06 12:58:39 -08:00
Ulisses Costa
433f7cce3b client/gatt: Fix property_change in WriteValue for desc
g_dbus_emit_property_changed was not working properly in the WriteValue
for the descriptor because the Characteristic interface was being used.
2021-12-07 18:48:46 -08:00
Tedd Ho-Jeong An
6ea642f6ef client: Fix uninitiailzed scalar variable
This patch fixes the uninitiailzed varialble(CWE-457) reported by
the Coverity scan.
2021-11-15 13:07:08 -08:00
Luiz Augusto von Dentz
8d2b847b00 client/gatt: Fix using atoi
atoi doesn't support values entered in hexadecimal (0x...) which is
likely the prefered format for the likes of handles, etc, so this
replaces the uses of atoi with strtol.
2021-11-09 14:45:23 -08:00
Tedd Ho-Jeong An
f22f166d6e client: Add SPDX License Identifier
This patch adds SPDX License Identifier and removes the license text.

-------------------------------------
       License            COUNT
-------------------------------------
 GPL-2.0-or-later     :      11

License: GPL-2.0-or-later
   client/adv_monitor.c
   client/adv_monitor.h
   client/advertising.h
   client/display.c
   client/main.c
   client/display.h
   client/agent.h
   client/gatt.c
   client/agent.c
   client/advertising.c
   client/gatt.h
2020-09-21 16:19:33 -07:00
Mariusz Skamra
236fb26ff4 client: Update write callbacks with invalid offset error handlers
This patch adds invalid offset handlers to write callbacks of attributes.
2020-05-11 15:26:55 +02:00
Łukasz Rymanowski
1c2271575d client: Fix possible stack corruption
DBUS_TYPE_BOOLEAN is 'int', which does not have to be the same size as
'bool'.
On architecture where bool is smaller than in, getting prepare-authorize
will corrupt the stack
2020-05-11 15:26:55 +02:00
Luiz Augusto von Dentz
19329b3f68 client: Fix not able to select attributes if parent exists
When selecting attributes by UUID the code was not checking attributes
that are not a children of the current selected attribute.
2019-08-20 16:47:29 +03:00
Luiz Augusto von Dentz
31d7430759 client: Allow UUID 16 bits on select-attribute
This allow passing UUIDs on 16 bits format.
2019-08-20 16:47:29 +03:00
Luiz Augusto von Dentz
bbfaae247f client: Fix build with older version on libdbus
This fixes the following error when building with older version of
D-Bus:

client/gatt.c:2973: undefined reference to `dbus_message_iter_get_element_count'
2019-02-26 19:24:40 +02:00
Luiz Augusto von Dentz
64438092cb client: Fix uninitialized error
trusted should be initialized with false since the property may not be
available.
2019-02-13 17:09:13 +02:00
Luiz Augusto von Dentz
15cbd14aaa client: Improve logging of GATT operations
This prints the UUID along with attribute path and also print the
hexdump when payload is available.
2019-02-13 17:09:13 +02:00
Luiz Augusto von Dentz
f12dc48511 client: Forward notifications from cloned attributes
Notifications come in a form of 'Value' property changes thus this
copies the values to the local attribute so they are in sync.
2019-02-13 17:09:13 +02:00
Luiz Augusto von Dentz
40ebc70dae client: Remove cloned services if the proxy is removed
This unregister the cloned services if the proxy is removed since they
would no longer be accessible.
2019-02-13 17:09:13 +02:00
Luiz Augusto von Dentz
8ba5b4fb21 client: Proxy calls to StartNotify/StopNotify
This uses the proxies created by clone command to forward the
subscriptions to the cloned services.
2019-02-13 17:09:13 +02:00
Luiz Augusto von Dentz
32232aba53 client: Proxy calls to ReadValue and WriteValue
This uses the proxies created by clone command to forward the requests
to the cloned services.
2019-02-13 17:09:13 +02:00
Luiz Augusto von Dentz
183d47c176 client: Add gatt.clone command
This adds clone command to gatt submenu which can be use to clone
services as follow:

Clone all services from the connected device:
> gatt.clone

Clone a given service
> gatt.select-attribute <attribute/uuid>
> gatt.clone

In either case there is a prompt to confirm since this may add a lot of
service the user must confim before proceding.

Then finally:

> gatt.register-application
2019-02-13 17:09:13 +02:00
Luiz Augusto von Dentz
db15160d5c client: Add type to write command
This enables setting the write type as optional third parameter:

write <data=xx xx ...> [offset] [type]

The type can be used to force a specific procedure to be used, for
example to force reliable writes one can enter:

> write 00 0 reliable
2019-02-11 11:29:21 +02:00
Luiz Augusto von Dentz
b52543ae3d client: Don't expose pointer value in attribute path
Instead use the position in the list to create a unique path:

[bluetooth]# register-service 0x1820
[NEW] Primary Service (Handle 0x0000)
	/org/bluez/app/service0
	0x1820
	Internet Protocol Support
2019-01-31 14:10:56 +02:00
Luiz Augusto von Dentz
d5cb29ce5a client: Enable list-attributes to print local attributes
This enable passing "local" to list-attributes to print the attributes
registered locally:

> list-attributes local
Primary Service (Handle 0x0400)
	/org/bluez/app/service0x74ccb0
	0x1820
	Internet Protocol Support
2019-01-31 14:10:54 +02:00
Luiz Augusto von Dentz
918f6f932b client: Enable setting attribute handles
This emulates application setting their attribute handles.
2019-01-31 14:10:51 +02:00
Luiz Augusto von Dentz
ae63206ff8 client: Enable Handle property for GATT attributes
This enable bluetoothd to write back the actual value of attribute
handles.
2019-01-31 14:10:50 +02:00
Marcel Holtmann
261948090e build: Move declaration of _GNU_SOURCE back into individual source files 2018-12-06 21:28:18 +01:00
Luiz Augusto von Dentz
91df85690c client: Switch from write to sendmsg for Acquire*
Use sendmsg with MSG_NOSIGNAL to prevent crashes involving SIGPIPE.
2018-11-20 11:28:29 +02:00
Grzegorz Kolodziejczyk
1e32752f38 client: Don't require authorization for trusted devices
This patch adds possibility to ommit authorization request from trusted
devices.
2018-05-29 14:06:34 +03:00
Grzegorz Kolodziejczyk
822f082063 client: Add authorized property handling to characteristic attribute
This patch adds handling of characteristic prepare write authorized
property to bluetoothctl.
2018-05-28 16:29:11 +03:00
Avichal Agarwal
8f970c44e6 client/gatt : Add support for Included Service
included service support implemented at service registration
2018-05-21 11:30:05 +03:00
Grzegorz Kolodziejczyk
d2277dfa5f client: Define maximum attribute value length as initial value
Initial registered attribute value is set as maximal attribute length.
2018-05-21 11:09:30 +03:00
Grzegorz Kolodziejczyk
48dd3a49fb client: Add missing duplicated string free
This patch free duplicated strings in read, write attribute callbacks.
2018-05-21 11:09:29 +03:00
Grzegorz Kolodziejczyk
7f9dcb3c45 client: Add support for optional gatt write offset parameter
This patch extends missing optional gatt write offset parameter.
2018-04-26 15:53:38 +03:00
Grzegorz Kolodziejczyk
5d50dc020b client: Add support for optional gatt read offset parameter
This patch extends missing optional gatt read offset parameter.
2018-04-26 15:53:37 +03:00
Grzegorz Kolodziejczyk
1dd33d584c client: Fix writing attribute values
Attribute values is not copied with dbus_message_iter_get_fixed_array,
so gatt write callback needs to replace old value with reallocation and
copy.
2018-04-26 15:53:37 +03:00
Luiz Augusto von Dentz
9f1388f2cf client: Attempt to convert the device path to address
Printing the object path is not only long but it also may change.
2018-03-29 14:10:25 +03:00
Luiz Augusto von Dentz
db663b1d57 client: Parse all options passed for GATT attribute
This parses the options given to Acquire*, WriteValue and ReadValue.
2018-03-26 16:19:29 +03:00
Grzegorz Kolodziejczyk
d3f155e458 client: Add authorization request handling for attribute operations
This patch adds optional authorization request for reading, writing of
gatt database attributes.
2018-03-21 11:25:52 +01:00
Grzegorz Kolodziejczyk
bc6facb0cf client: Update read callbacks with invalid offset error handlers
This patch adds invalid offset handlers to read callbacks of attributes.
2018-03-21 10:47:07 +01:00
Grzegorz Kolodziejczyk
1abee58711 client: Fix reading long values
While value has more than single MTU can carry long read procedure will
be triggered. In such cases offset need to bo considered while getting
value from storage.
2018-03-21 10:47:07 +01:00
Luiz Augusto von Dentz
41465ce57c client: Fix lines going beyond 80 columns 2018-03-01 11:23:09 +02:00
Luiz Augusto von Dentz
936ad5ef7b client: Quit when done with command
This ensures that the commands don't stay hanging since bt_shell no
longer quits immediatelly after executing a command.
2018-02-28 16:17:13 +02:00
Luiz Augusto von Dentz
17c8980ad3 client: Fix not cleaning up notify pipe
If remote unsubscribe, disconnects or the daemon closes the socket the
io shall be destroyed properly otherwise it will prevent new
subscriptions.
2018-02-06 09:54:13 -02:00