-
Notifications
You must be signed in to change notification settings - Fork 52
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
MicroBitUtilityService - replaces PR 178 #287
Merged
JohnVidler
merged 4 commits into
lancaster-university:master
from
martinwork:ble-utility-service
Jul 13, 2023
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
/* | ||
The MIT License (MIT) | ||
|
||
This class has been written for the Microbit Educational Foundation. | ||
|
||
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. | ||
*/ | ||
|
||
#ifndef MICROBIT_UTILITY_SERVICE_H | ||
#define MICROBIT_UTILITY_SERVICE_H | ||
|
||
#include "MicroBitConfig.h" | ||
|
||
#if CONFIG_ENABLED(DEVICE_BLE) | ||
|
||
#include "MicroBitBLEManager.h" | ||
#include "MicroBitBLEService.h" | ||
#include "MicroBitMemoryMap.h" | ||
|
||
#include "MicroBitFlash.h" | ||
#include "MicroBitStorage.h" | ||
|
||
#include "MicroBitComponent.h" | ||
#include "MicroBitEvent.h" | ||
#include "EventModel.h" | ||
#include "MicroBitLog.h" | ||
|
||
/* Set this to enable an alternative implementation, which creates a fiber to process requests. | ||
* The default simply uses an event to trigger processing, and if processRequest() blocks, | ||
* a temporary fiber is created as necessary. | ||
*/ | ||
#ifndef MicroBitUtilityService_FIBER | ||
#define MicroBitUtilityService_FIBER 0 | ||
#endif | ||
|
||
|
||
class MicroBitUtilityWorkspace; | ||
|
||
|
||
/** | ||
* Class declration for the custom MicroBit Utility Service. | ||
* Provides simple BLE request/response | ||
* See MicroBitUtilityTypes.h | ||
*/ | ||
class MicroBitUtilityService : public MicroBitBLEService | ||
{ | ||
static MicroBitUtilityService *shared; | ||
|
||
public: | ||
/** | ||
* Return a pointer to the shared service | ||
* @return a pointer to the service, or NULL if it has not been created | ||
*/ | ||
static MicroBitUtilityService *getShared() | ||
{ | ||
return shared; | ||
} | ||
|
||
/** | ||
* Return a pointer to the shared service, creating it if necessary | ||
* @param _ble an instance of BLEDevice | ||
* @param _messageBus An instance of a MessageBus to interface with. | ||
* @param _storage A persistent storage manager to use to hold non-volatile state. | ||
* @param _log a log storage manager to read stored data. | ||
* @return a pointer to the service, or NULL if it has not been created | ||
*/ | ||
static MicroBitUtilityService *createShared( BLEDevice &_ble, EventModel &_messageBus, MicroBitStorage &_storage, MicroBitLog &_log); | ||
|
||
public: | ||
/** | ||
* Constructor. | ||
* @param _ble an instance of BLEDevice | ||
* @param _messageBus An instance of a MessageBus to interface with. | ||
* @param _storage A persistent storage manager to use to hold non-volatile state. | ||
* @param _log a log storage manager to read stored data. | ||
*/ | ||
MicroBitUtilityService( BLEDevice &_ble, EventModel &_messageBus, MicroBitStorage &_storage, MicroBitLog &_log); | ||
|
||
private: | ||
/** | ||
* Invoked when BLE connects. | ||
* Start listening for events | ||
*/ | ||
void onConnect( const microbit_ble_evt_t *p_ble_evt); | ||
|
||
/** | ||
* Invoked when BLE disconnects. | ||
* Stop listening for events | ||
*/ | ||
void onDisconnect( const microbit_ble_evt_t *p_ble_evt); | ||
|
||
/** | ||
* Callback. Invoked when any of our attributes are written via BLE. | ||
* Record the request, and trigger processing. | ||
* Requests are not queued. If a request is actively being processed, ignore the new request, otherwise simply override the previous one. | ||
*/ | ||
void onDataWritten(const microbit_ble_evt_write_t *params); | ||
|
||
/** | ||
* Callback. Invoked when a registered event occurs. | ||
*/ | ||
void onEvent(MicroBitEvent e); | ||
|
||
private: | ||
// MessageBus we're using | ||
EventModel &messageBus; | ||
MicroBitStorage &storage; | ||
MicroBitLog &log; | ||
|
||
uint8_t characteristicValue[ 20]; | ||
|
||
// Index for each charactersitic in arrays of handles and UUIDs | ||
typedef enum mbbs_cIdx | ||
{ | ||
mbbs_cIdxCTRL, | ||
mbbs_cIdxCOUNT | ||
} mbbs_cIdx; | ||
|
||
// UUIDs for our service and characteristics | ||
static const uint16_t serviceUUID; | ||
static const uint16_t charUUID[ mbbs_cIdxCOUNT]; | ||
|
||
// Data for each characteristic when they are held by Soft Device. | ||
MicroBitBLEChar chars[ mbbs_cIdxCOUNT]; | ||
|
||
public: | ||
int characteristicCount() { return mbbs_cIdxCOUNT; }; | ||
MicroBitBLEChar *characteristicPtr( int idx) { return &chars[ idx]; }; | ||
|
||
private: | ||
|
||
MicroBitUtilityWorkspace *workspace; | ||
|
||
/** | ||
* Listen for or ignore events for processing requests | ||
* @param yes true to listen, otherwise ignore | ||
*/ | ||
void listen( bool yes); | ||
|
||
/** | ||
* Send a reply packet | ||
* @param data Data to send | ||
* @param length Length if the data | ||
* @return DEVICE_OK if sent | ||
*/ | ||
int sendReply( const void *data, uint16_t length); | ||
|
||
/** | ||
* Process the current request. This may block. | ||
* @return DEVICE_OK if finished | ||
*/ | ||
int processRequest(); | ||
|
||
/** | ||
* Process request typeLogLength | ||
* @return DEVICE_OK if finished | ||
*/ | ||
int processLogLength(); | ||
|
||
/** | ||
* Process request typeLogRead | ||
* @return DEVICE_OK if finished | ||
*/ | ||
int processLogRead(); | ||
|
||
#if MicroBitUtilityService_FIBER | ||
volatile int8_t fiberActive; | ||
volatile bool fiberExists; | ||
|
||
/** | ||
* Keep the fiber running or create a new one | ||
*/ | ||
void ensureFiber(); | ||
|
||
/** | ||
* Callback invoked when the fiber exits | ||
* @param param Pointer to the MicroBitUtilityService | ||
*/ | ||
static void fiberComplete( void *param); | ||
|
||
/** | ||
* Fiber to process requests, which releases itself after a period of inactivity | ||
* @param param Pointer to the MicroBitUtilityService | ||
*/ | ||
static void fiberEntry( void *param); | ||
#endif | ||
}; | ||
|
||
|
||
#endif | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
The MIT License (MIT) | ||
|
||
This header has been written for the Microbit Educational Foundation. | ||
|
||
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. | ||
*/ | ||
|
||
#ifndef MICROBIT_UTILITY_TYPES_H | ||
#define MICROBIT_UTILITY_TYPES_H | ||
|
||
#include <stdint.h> | ||
|
||
/* Define types for the MicroBitUtilityService | ||
* | ||
* request_t - Request sent from client to service. | ||
* job (1 byte) - see below | ||
* type (1 byte) - specifies the request type | ||
* data (up to 18 bytes) - depends on request type | ||
* | ||
* reply_t - Reply from service to client. | ||
* job (1 byte) - see below | ||
* data (up to 19 bytes) - depends on request type | ||
* | ||
* job - Synchronize requests and replies. | ||
* Enables the client to ignore replies to a previous request, and detect missing reply packets. | ||
* Client cycles high nibble i.e. { 0x00, 0x10, 0x20, ..., 0xF0, 0x00, ...} | ||
* Service reply cycles low nibble, i.e client job + { 0x00, 0x01, 0x02, ..., 0x0E, 0x00, ... } | ||
* low nibble == 0x0F (jobLowERR) in a reply indicates error (data = 4 bytes signed integer error) | ||
* | ||
* Request types and data contents: | ||
* | ||
* type 1 - Log file length | ||
* request format (1 byte) - 0 = HTML header; 1 = HTML; 2 = CSV | ||
* reply length (4 bytes) - unsigned integer log data length | ||
* | ||
* type 2 - Log file data | ||
* request format (1 byte) - 0 = HTML header; 1 = HTML; 2 = CSV | ||
* reserved (1 byte) - set to zero | ||
* index (4 bytes) - unsigned integer index into file | ||
* batchlen (4 bytes) - unsigned size in bytes to return | ||
* length (4 bytes) - length of whole file, from request type 1 (Log file length) | ||
* reply data (up to 19 bytes) - 1 or more reply packets, to total batchlen bytes | ||
* | ||
*/ | ||
namespace MicroBitUtility | ||
{ | ||
typedef enum requestType_t | ||
{ | ||
requestTypeNone, | ||
requestTypeLogLength, // reply data = 4 bytes log data length | ||
requestTypeLogRead // reply data = up to 19 bytes of log data | ||
} requestType_t; | ||
|
||
typedef struct request_t | ||
{ | ||
uint8_t job; // Client cycles high nibble i.e. { 0x00, 0x10, 0x20, ..., 0xF0, 0x00, ...} | ||
uint8_t type; // requestType_t | ||
uint8_t data[18]; | ||
} request_t; | ||
|
||
typedef struct reply_t | ||
{ | ||
uint8_t job; // Service cycles low nibble i.e client job + { 0x00, 0x01, 0x02, ..., 0x0E, 0x00, ... } | ||
// low nibble == 0x0F (jobLowERR) indicates error and data = 4 bytes signed integer error | ||
uint8_t data[19]; | ||
} reply_t; | ||
|
||
typedef enum requestLogFormat | ||
{ | ||
requestLogHTMLHeader = 0, | ||
requestLogHTML = 1, | ||
requestLogCSV = 2 | ||
} requestLogFormat; | ||
|
||
typedef struct requestLog_t | ||
{ | ||
uint8_t job; | ||
uint8_t type; // requestType_t | ||
uint8_t format; // requestLogFormat | ||
} requestLog_t; | ||
|
||
typedef struct requestLogRead_t | ||
{ | ||
uint8_t job; | ||
uint8_t type; // requestType_t | ||
uint8_t format; // requestLogFormat | ||
uint8_t reserved; // set to zero | ||
uint32_t index; // index into data | ||
uint32_t batchlen; // size in bytes to return | ||
uint32_t length; // length of whole file, from requestTypeLogLength | ||
} requestLogRead_t; | ||
|
||
const uint8_t jobLowMAX = 0x0E; | ||
const uint8_t jobLowERR = 0x0F; // reply data = 4 bytes signed integer error | ||
}; | ||
|
||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason to hold both these implementations in the final revision @martinwork? or shall we just pick one? (e.g. the event based approach?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes please pick one - whichever you think is the best idea.