2018-07-18 19:01:41 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2019-06-19 10:42:36 -04:00
|
|
|
* Copyright (c) 2019 Dan Halbert for Adafruit Industries
|
2018-07-18 19:01:41 -04:00
|
|
|
* Copyright (c) 2018 Artur Pacholec
|
2019-06-19 10:42:36 -04:00
|
|
|
* Copyright (c) 2017 Glenn Ruben Bakke
|
2018-07-18 19:01:41 -04:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "py/objproperty.h"
|
|
|
|
#include "py/runtime.h"
|
2019-08-29 18:44:27 -04:00
|
|
|
#include "shared-bindings/_bleio/Characteristic.h"
|
|
|
|
#include "shared-bindings/_bleio/Service.h"
|
|
|
|
#include "shared-bindings/_bleio/UUID.h"
|
2018-07-18 19:01:41 -04:00
|
|
|
|
2020-05-01 18:23:27 -04:00
|
|
|
//| class Service:
|
2020-05-12 20:15:28 -04:00
|
|
|
//| """Stores information about a BLE service and its characteristics."""
|
2018-07-18 19:01:41 -04:00
|
|
|
//|
|
2020-07-03 14:23:34 -04:00
|
|
|
//| def __init__(self, uuid: UUID, *, secondary: bool = False) -> None:
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """Create a new Service identified by the specified UUID. It can be accessed by all
|
|
|
|
//| connections. This is known as a Service server. Client Service objects are created via
|
|
|
|
//| `Connection.discover_remote_services`.
|
2019-08-29 17:58:21 -04:00
|
|
|
//|
|
2020-05-01 18:23:27 -04:00
|
|
|
//| To mark the Service as secondary, pass `True` as :py:data:`secondary`.
|
2018-07-18 19:01:41 -04:00
|
|
|
//|
|
2020-05-01 18:23:27 -04:00
|
|
|
//| :param UUID uuid: The uuid of the service
|
|
|
|
//| :param bool secondary: If the service is a secondary one
|
2019-08-28 23:15:22 -04:00
|
|
|
//|
|
2020-05-01 18:23:27 -04:00
|
|
|
//| :return: the new Service"""
|
|
|
|
//| ...
|
2021-10-15 14:43:12 -04:00
|
|
|
STATIC mp_obj_t bleio_service_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
|
2019-09-14 15:40:24 -04:00
|
|
|
enum { ARG_uuid, ARG_secondary };
|
2019-08-28 23:15:22 -04:00
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_uuid, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
|
|
|
{ MP_QSTR_secondary, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
|
|
|
|
};
|
|
|
|
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
2021-10-15 14:43:12 -04:00
|
|
|
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
2019-08-28 23:15:22 -04:00
|
|
|
|
2021-10-10 02:46:14 -04:00
|
|
|
bleio_uuid_obj_t *uuid = mp_arg_validate_type(args[ARG_uuid].u_obj, &bleio_uuid_type, MP_QSTR_uuid);
|
2019-08-28 23:15:22 -04:00
|
|
|
|
|
|
|
const bool is_secondary = args[ARG_secondary].u_bool;
|
|
|
|
|
2023-08-07 20:45:57 -04:00
|
|
|
bleio_service_obj_t *service = mp_obj_malloc(bleio_service_obj_t, &bleio_service_type);
|
2019-08-28 23:15:22 -04:00
|
|
|
|
2021-10-10 02:46:14 -04:00
|
|
|
common_hal_bleio_service_construct(service, uuid, is_secondary);
|
2019-08-28 23:15:22 -04:00
|
|
|
|
|
|
|
return MP_OBJ_FROM_PTR(service);
|
|
|
|
}
|
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| characteristics: Tuple[Characteristic, ...]
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """A tuple of :py:class:`Characteristic` designating the characteristics that are offered by
|
|
|
|
//| this service. (read-only)"""
|
2018-07-18 19:01:41 -04:00
|
|
|
STATIC mp_obj_t bleio_service_get_characteristics(mp_obj_t self_in) {
|
|
|
|
bleio_service_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2020-08-11 16:21:16 -04:00
|
|
|
return MP_OBJ_FROM_PTR(common_hal_bleio_service_get_characteristics(self));
|
2018-07-18 19:01:41 -04:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_service_get_characteristics_obj, bleio_service_get_characteristics);
|
|
|
|
|
2022-05-01 12:24:05 -04:00
|
|
|
MP_PROPERTY_GETTER(bleio_service_characteristics_obj,
|
2022-05-01 11:16:53 -04:00
|
|
|
(mp_obj_t)&bleio_service_get_characteristics_obj);
|
2018-07-18 19:01:41 -04:00
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| remote: bool
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """True if this is a service provided by a remote device. (read-only)"""
|
2019-07-27 13:20:59 -04:00
|
|
|
STATIC mp_obj_t bleio_service_get_remote(mp_obj_t self_in) {
|
|
|
|
bleio_service_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
|
|
|
|
return mp_obj_new_bool(common_hal_bleio_service_get_is_remote(self));
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_service_get_remote_obj, bleio_service_get_remote);
|
|
|
|
|
2022-05-01 12:24:05 -04:00
|
|
|
MP_PROPERTY_GETTER(bleio_service_remote_obj,
|
2022-05-01 11:16:53 -04:00
|
|
|
(mp_obj_t)&bleio_service_get_remote_obj);
|
2019-07-27 13:20:59 -04:00
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| secondary: bool
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """True if this is a secondary service. (read-only)"""
|
2019-05-31 18:03:05 -04:00
|
|
|
STATIC mp_obj_t bleio_service_get_secondary(mp_obj_t self_in) {
|
|
|
|
bleio_service_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
|
2019-06-18 23:46:20 -04:00
|
|
|
return mp_obj_new_bool(common_hal_bleio_service_get_is_secondary(self));
|
2019-05-31 18:03:05 -04:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_service_get_secondary_obj, bleio_service_get_secondary);
|
|
|
|
|
2022-05-01 12:24:05 -04:00
|
|
|
MP_PROPERTY_GETTER(bleio_service_secondary_obj,
|
2022-05-01 11:16:53 -04:00
|
|
|
(mp_obj_t)&bleio_service_get_secondary_obj);
|
2019-05-31 18:03:05 -04:00
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| uuid: Optional[UUID]
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """The UUID of this service. (read-only)
|
2019-12-05 18:02:52 -05:00
|
|
|
//|
|
2020-05-01 18:23:27 -04:00
|
|
|
//| Will be ``None`` if the 128-bit UUID for this service is not known."""
|
2022-09-29 20:22:32 -04:00
|
|
|
//|
|
2018-07-18 19:01:41 -04:00
|
|
|
STATIC mp_obj_t bleio_service_get_uuid(mp_obj_t self_in) {
|
|
|
|
bleio_service_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
|
2019-06-29 00:20:06 -04:00
|
|
|
bleio_uuid_obj_t *uuid = common_hal_bleio_service_get_uuid(self);
|
|
|
|
return uuid ? MP_OBJ_FROM_PTR(uuid) : mp_const_none;
|
2018-07-18 19:01:41 -04:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_service_get_uuid_obj, bleio_service_get_uuid);
|
|
|
|
|
2022-05-01 12:24:05 -04:00
|
|
|
MP_PROPERTY_GETTER(bleio_service_uuid_obj,
|
2022-05-01 11:16:53 -04:00
|
|
|
(mp_obj_t)&bleio_service_get_uuid_obj);
|
2018-07-18 19:01:41 -04:00
|
|
|
|
2019-08-28 16:15:09 -04:00
|
|
|
|
2018-07-18 19:01:41 -04:00
|
|
|
STATIC const mp_rom_map_elem_t bleio_service_locals_dict_table[] = {
|
2019-08-28 23:15:22 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_characteristics), MP_ROM_PTR(&bleio_service_characteristics_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_secondary), MP_ROM_PTR(&bleio_service_secondary_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_uuid), MP_ROM_PTR(&bleio_service_uuid_obj) },
|
2020-08-11 16:21:16 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_remote), MP_ROM_PTR(&bleio_service_remote_obj) },
|
2018-07-18 19:01:41 -04:00
|
|
|
};
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(bleio_service_locals_dict, bleio_service_locals_dict_table);
|
|
|
|
|
2019-06-29 00:20:06 -04:00
|
|
|
STATIC void bleio_service_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
|
|
|
bleio_service_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
if (self->uuid) {
|
2019-07-16 19:53:36 -04:00
|
|
|
mp_printf(print, "Service(");
|
2019-06-29 00:20:06 -04:00
|
|
|
bleio_uuid_print(print, MP_OBJ_FROM_PTR(self->uuid), kind);
|
2019-07-16 19:53:36 -04:00
|
|
|
mp_printf(print, ")");
|
2019-06-29 00:20:06 -04:00
|
|
|
} else {
|
2019-07-16 19:53:36 -04:00
|
|
|
mp_printf(print, "<Service with unregistered UUID>");
|
2019-06-29 00:20:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-19 21:09:29 -04:00
|
|
|
MP_DEFINE_CONST_OBJ_TYPE(
|
|
|
|
bleio_service_type,
|
|
|
|
MP_QSTR_Service,
|
2023-10-19 17:27:20 -04:00
|
|
|
MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS,
|
2023-09-19 21:09:29 -04:00
|
|
|
make_new, bleio_service_make_new,
|
|
|
|
print, bleio_service_print,
|
|
|
|
locals_dict, &bleio_service_locals_dict
|
|
|
|
);
|