Skip to content

FOTA Service #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,4 @@ jobs:
./tests/TESTS/build.sh -s DeviceInformation -t GCC_ARM -m NRF52840_DK
./tests/TESTS/build.sh -s LinkLoss -t GCC_ARM -m NRF52840_DK
./tests/TESTS/build.sh -s LinkLoss -t GCC_ARM -m DISCO_L496AG
./tests/TESTS/build.sh -s FOTA -t GCC_ARM -m NRF52840_DK
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should also work on disco. There are MCUboot problems but the mock example works fine locally, i.e. I can transfer a binary into the external flash of my B-L4S5I-IOT01A using the FOTA service.

Note: The disco build is also missing for the Device Information service: #62

40 changes: 40 additions & 0 deletions common/inc/BaseService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Mbed-OS Microcontroller Library
* Copyright (c) 2020 Embedded Planet
* Copyright (c) 2020 ARM Limited
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/

#ifndef MBED_OS_EXPERIMENTAL_BLE_SERVICES_INC_BASESERVICE_H_
#define MBED_OS_EXPERIMENTAL_BLE_SERVICES_INC_BASESERVICE_H_

/* TODO implement as decorator:
* - Way to add security requirements to characteristics (return list of characteristics?)
*
*/

//class BaseService
//{
//public:
//
// BaseService();
//
//
//
//
//};


#endif /* MBED_OS_EXPERIMENTAL_BLE_SERVICES_INC_BASESERVICE_H_ */
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good idea but perhaps we should add it in a separate PR with the other utility files:

  • ConnectionThingy.h
  • GattPresentationFormatDescriptor.h
  • CharacteristicPresentationFormatDescriptor.h
  • CharacteristicUserDescriptionDescriptor.h

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha, sorry, these files weren't supposed to be included in this PR 😅

61 changes: 61 additions & 0 deletions common/inc/ConnectionThingy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Mbed-OS Microcontroller Library
* Copyright (c) 2020 Embedded Planet
* Copyright (c) 2020 ARM Limited
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/

#ifndef MBED_OS_EXPERIMENTAL_BLE_SERVICES_COMMON_INC_CONNECTIONTHINGY_H_
#define MBED_OS_EXPERIMENTAL_BLE_SERVICES_COMMON_INC_CONNECTIONTHINGY_H_

/**
* Connection... Thingy?
* A common problem encountered when developing BLE applications is:
*
* Characteristic read/write/notify/indicate events are handled in batch.
*
* This creates a lot of boilerplate code where a service must filter out by comparing
* which handle was written to the callback's parameters, figure out which connection,
* and then perform the appropriate action.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

@AGlass0fMilk AGlass0fMilk Jun 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to the PR at hand, but:

Those APIs "solve" the problem, but I think it creates a confusing situation in the BLE API:
You can still handle characteristic writes that are authorized but you then have to filter them based on the value handle. It creates two ways to do the same thing which can be confusing to users.

The naming of "setWriteAuthorizationCallback" and "setReadAuthorizationCallback" imply they are only for read authorization/write authorization. Likewise, the naming of GattServer::EventHandler::onCharacteristicWritten (or whatever it is) implies it should be where characteristic writes are handled.

*
* "Global (modify this jargon?)" services are those that do not change behavior (ie: don't care)
* about what connection handle is interacting with the characteristic. The data going in and out
* is treated the same as if each connection handle was the same.
*
* The proposed API introduces a "ConnectionOrientedGattCharacteristic" (working name) that encapsulates
* the logic required to create gatt characteristic event handlers that are instantiated with an associated
* connection handle and share a lifetime with that connection. When a connection is terminated, these event handlers
* are not invalidated or nullified. Doing so would burden application code with the responsibility of keeping track of
* connection/disconnection events so as not to access a "ConnectionOrientedGattCharacteristic" with an invalid connection handle.
* Instead, the "ConnectionOrientedGattCharacteristic" handles are provided to the application as "SharedPtr" instances. This allows
* the "ConnectionOrientedGattCharacteristic" instance to remain valid, lets the service with the characteristic remove its reference,
* and prevents the application from encountering unexpected null pointers when attempting to use a "ConnectionOrientedGattCharacteristic"
* that has been disconnected.
*
* In addition, the logic required to create Characteristic-specific read/write handlers should be added to the BLE API or as an extension.
* Proposed APIs:
* GattCharacteristic::EventHandler::onWritten
* GattCharacteristic::EventHandler::onRead
* GattCharacteristic::... and so on
*
* Maybe connection oriented service? Service logic
*
*
*/




#endif /* MBED_OS_EXPERIMENTAL_BLE_SERVICES_COMMON_INC_CONNECTIONTHINGY_H_ */
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment under BaseService.h

70 changes: 70 additions & 0 deletions common/inc/GattPresentationFormatDescriptor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* ep-oc-mcu
* Embedded Planet Open Core for Microcontrollers
*
* Built with ARM Mbed-OS
*
* Copyright (c) 2019 Embedded Planet, Inc.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#ifndef EP_CORE_FEATURES_FEATURE_BLE_GATTPRESENTATIONFORMATDESCRIPTOR_H_
#define EP_CORE_FEATURES_FEATURE_BLE_GATTPRESENTATIONFORMATDESCRIPTOR_H_

#include "GattCharacteristic.h"
#include "GattAttribute.h"

/** Length of a presentation format descriptor struct */
#define PRESENTATION_DESC_LEN 7

/**
* Class encapsulating a GATT Presentation Format Descriptor
*/
class GattPresentationFormatDescriptor : public GattAttribute
{
public:

GattPresentationFormatDescriptor(uint8_t format_type, uint16_t unit = GattCharacteristic::BLE_GATT_UNIT_NONE,
int8_t exponent = 1, uint8_t namespace_id = 0x01, uint16_t namespace_description = 0x0000) :
GattAttribute((const UUID&) UUID(BLE_UUID_DESCRIPTOR_CHAR_PRESENTATION_FORMAT),
(uint8_t*) format, PRESENTATION_DESC_LEN, PRESENTATION_DESC_LEN, false)
{

/** Populate the format struct */
// format.gatt_format = format_type;
// format.exponent = exponent;
// format.gatt_unit = unit;
// format.gatt_namespace = namespace_id;
// format.gatt_nsdesc = namespace_description;

format[0] = format_type;
format[1] = exponent;
memcpy(&format[2], &unit, sizeof(unit));
format[4] = namespace_id;
memcpy(&format[5], &namespace_description, sizeof(namespace_description));

}

private:

// Wouldn't it be nice if packing structs was more consistently supported by compilers?
//struct GattCharacteristic::PresentationFormat_t format;

// In lieu of using the struct above, packing issues makes us have to use a raw buffer
uint8_t format[7];
};

#endif /* EP_CORE_FEATURES_FEATURE_BLE_GATTPRESENTATIONFORMATDESCRIPTOR_H_ */
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment under BaseService.h

94 changes: 94 additions & 0 deletions descriptors/CharacteristicPresentationFormatDescriptor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Mbed-OS Microcontroller Library
* Copyright (c) 2020 Embedded Planet
* Copyright (c) 2020 ARM Limited
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/

#ifndef MBED_OS_EXPERIMENTAL_BLE_SERVICES_DESCRIPTORS_CHARACTERISTICPRESENTATIONFORMATDESCRIPTOR_H_
#define MBED_OS_EXPERIMENTAL_BLE_SERVICES_DESCRIPTORS_CHARACTERISTICPRESENTATIONFORMATDESCRIPTOR_H_

#include "GattCharacteristic.h"
#include "GattAttribute.h"

/** Length of a presentation format descriptor struct */
#define PRESENTATION_DESC_LEN 7

/**
* Class encapsulating a GATT Presentation Format Descriptor
*
* See Bluetooth Core Specification 5.2, Volume 3, Part G, Section 3.3.3.5
*
*/
class GattPresentationFormatDescriptor : public GattAttribute
{
public:

GattPresentationFormatDescriptor(uint8_t format_type, uint16_t unit = GattCharacteristic::BLE_GATT_UNIT_NONE,
int8_t exponent = 1, uint8_t namespace_id = 0x01, uint16_t namespace_description = 0x0000) :
GattAttribute((const UUID&) UUID(BLE_UUID_DESCRIPTOR_CHAR_PRESENTATION_FORMAT),
(uint8_t*) format, PRESENTATION_DESC_LEN, PRESENTATION_DESC_LEN, false)
{

/** Populate the format struct */
// format.gatt_format = format_type;
// format.exponent = exponent;
// format.gatt_unit = unit;
// format.gatt_namespace = namespace_id;
// format.gatt_nsdesc = namespace_description;

format[0] = format_type;
format[1] = exponent;
memcpy(&format[2], &unit, sizeof(unit));
format[4] = namespace_id;
memcpy(&format[5], &namespace_description, sizeof(namespace_description));

}

uint8_t get_format() const {
return format[0];
}

uint8_t get_exponent() const {
return format[1];
}

uint16_t get_unit_type() const {
// TODO do we need to do this? information is encoded in little endian but could be unaligned
uint16_t retval;
memcpy(&retval, &format[2], sizeof(uint16_t));
return retval;
}

uint8_t get_namespace_id() const {
return format[4];
}

uint16_t get__description() const {
uint16_t retval;
memcpy(&retval, &format[5], sizeof(uint16_t));
return retval;
}

private:

// Wouldn't it be nice if packing structs was more consistently supported by compilers?
//struct GattCharacteristic::PresentationFormat_t format;

// In lieu of using the struct above, packing issues makes us have to use a raw buffer
uint8_t format[7];
};

#endif /* MBED_OS_EXPERIMENTAL_BLE_SERVICES_DESCRIPTORS_CHARACTERISTICPRESENTATIONFORMATDESCRIPTOR_H_ */
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment under BaseService.h

59 changes: 59 additions & 0 deletions descriptors/CharacteristicUserDescriptionDescriptor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Mbed-OS Microcontroller Library
* Copyright (c) 2020 Embedded Planet
* Copyright (c) 2020 ARM Limited
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/

#ifndef MBED_OS_EXPERIMENTAL_BLE_SERVICES_DESCRIPTORS_CHARACTERISTICUSERDESCRIPTIONDESCRIPTOR_H_
#define MBED_OS_EXPERIMENTAL_BLE_SERVICES_DESCRIPTORS_CHARACTERISTICUSERDESCRIPTIONDESCRIPTOR_H_

#include "ble/gatt/GattCharacteristic.h"
#include "ble/gatt/GattAttribute.h"

/**
* Class encapsulating a Characteristic User Description Descriptor (CUDD)
*
* See Bluetooth Core Specification 5.2, Volume 3, Part G, Section 3.3.3.2
*
* TODO extend this in the future to support client-writable CUDD (set Writable Auxiliary bit)
*/
class CharacteristicUserDescriptionDescriptor : public GattAttribute
{
public:

CharacteristicUserDescriptionDescriptor(const char* user_description) :
GattAttribute((const UUID&) UUID(BLE_UUID_DESCRIPTOR_CHAR_USER_DESC),
(uint8_t*) user_description,
(user_description != nullptr) ? strlen(user_description) : 0,
(user_description != nullptr) ? strlen(user_description) : 0,
false),
_user_description(user_description)
{
this->allowWrite(false);
}

const char* get_user_description() const {
return _user_description;
}

private:

const char* _user_description;


};

#endif /* MBED_OS_EXPERIMENTAL_BLE_SERVICES_DESCRIPTORS_CHARACTERISTICUSERDESCRIPTIONDESCRIPTOR_H_ */
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment under BaseService.h

Loading