Simple data buffer splitter and re-assembler.
SplitterAssembler.h@5:b0dc0395174e, 2015-02-17 (annotated)
- Committer:
- ansond
- Date:
- Tue Feb 17 06:59:49 2015 +0000
- Revision:
- 5:b0dc0395174e
- Parent:
- 4:221c8a56a80e
- Child:
- 6:094ed29a8bf7
updates and fixes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 0:12a931a6161c | 1 | /** |
ansond | 1:9f05dbd1c2c7 | 2 | * @file SplitterAssembler.h |
ansond | 1:9f05dbd1c2c7 | 3 | * @brief data buffer splitter and assembler header |
ansond | 0:12a931a6161c | 4 | * @author Doug Anson |
ansond | 0:12a931a6161c | 5 | * @version 1.0 |
ansond | 0:12a931a6161c | 6 | * @see |
ansond | 0:12a931a6161c | 7 | * |
ansond | 0:12a931a6161c | 8 | * Copyright (c) 2014 |
ansond | 0:12a931a6161c | 9 | * |
ansond | 0:12a931a6161c | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); |
ansond | 0:12a931a6161c | 11 | * you may not use this file except in compliance with the License. |
ansond | 0:12a931a6161c | 12 | * You may obtain a copy of the License at |
ansond | 0:12a931a6161c | 13 | * |
ansond | 0:12a931a6161c | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
ansond | 0:12a931a6161c | 15 | * |
ansond | 0:12a931a6161c | 16 | * Unless required by applicable law or agreed to in writing, software |
ansond | 0:12a931a6161c | 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
ansond | 0:12a931a6161c | 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ansond | 0:12a931a6161c | 19 | * See the License for the specific language governing permissions and |
ansond | 0:12a931a6161c | 20 | * limitations under the License. |
ansond | 0:12a931a6161c | 21 | */ |
ansond | 0:12a931a6161c | 22 | |
ansond | 1:9f05dbd1c2c7 | 23 | #ifndef __SPLITTER_ASSEMBLER_H__ |
ansond | 1:9f05dbd1c2c7 | 24 | #define __SPLITTER_ASSEMBLER_H__ |
ansond | 0:12a931a6161c | 25 | |
ansond | 0:12a931a6161c | 26 | #include "mbed.h" |
ansond | 0:12a931a6161c | 27 | |
ansond | 0:12a931a6161c | 28 | // TUNABLES |
ansond | 5:b0dc0395174e | 29 | #define MAX_FRAGMENTS 15 // maximum number of supported fragments... increase as needed... |
ansond | 4:221c8a56a80e | 30 | #define DEF_FRAGMENT_LENGTH 20 // BLE: set to max MTU for BLE UART: defined by BLE_UART_SERVICE_MAX_DATA_LEN in UARTService.h |
ansond | 0:12a931a6161c | 31 | |
ansond | 1:9f05dbd1c2c7 | 32 | class SplitterAssembler { |
ansond | 0:12a931a6161c | 33 | public: |
ansond | 0:12a931a6161c | 34 | /** |
ansond | 0:12a931a6161c | 35 | Constructor |
ansond | 0:12a931a6161c | 36 | */ |
ansond | 1:9f05dbd1c2c7 | 37 | SplitterAssembler(); |
ansond | 0:12a931a6161c | 38 | |
ansond | 0:12a931a6161c | 39 | /** |
ansond | 1:9f05dbd1c2c7 | 40 | Split a data stream into "n" fragments of the given input fragement (from constructor) size |
ansond | 0:12a931a6161c | 41 | @param data input input data buffer |
ansond | 0:12a931a6161c | 42 | @param data_length input input data buffer length |
ansond | 0:12a931a6161c | 43 | @returns number of fragments created for the input buffer |
ansond | 0:12a931a6161c | 44 | */ |
ansond | 1:9f05dbd1c2c7 | 45 | int split(uint8_t *data,int data_length); |
ansond | 0:12a931a6161c | 46 | |
ansond | 0:12a931a6161c | 47 | /** |
ansond | 0:12a931a6161c | 48 | Get the ith fragment |
ansond | 0:12a931a6161c | 49 | @param index input the ith index value |
ansond | 0:12a931a6161c | 50 | @return the ith fragment pointer (null terminated) |
ansond | 0:12a931a6161c | 51 | */ |
ansond | 0:12a931a6161c | 52 | uint8_t *get(int index); |
ansond | 0:12a931a6161c | 53 | |
ansond | 3:00f7a99862a3 | 54 | /** |
ansond | 3:00f7a99862a3 | 55 | Get the splitter fragment length |
ansond | 3:00f7a99862a3 | 56 | @returns the configured splitter fragment length |
ansond | 3:00f7a99862a3 | 57 | */ |
ansond | 3:00f7a99862a3 | 58 | int getSplitLength() { return DEF_FRAGMENT_LENGTH; } |
ansond | 3:00f7a99862a3 | 59 | |
ansond | 0:12a931a6161c | 60 | /** |
ansond | 0:12a931a6161c | 61 | Reset the Fragmenter/Assembler |
ansond | 0:12a931a6161c | 62 | */ |
ansond | 0:12a931a6161c | 63 | void reset(void); |
ansond | 0:12a931a6161c | 64 | |
ansond | 0:12a931a6161c | 65 | /** |
ansond | 0:12a931a6161c | 66 | Add a fragment |
ansond | 0:12a931a6161c | 67 | @param fragment input fragment to add |
ansond | 0:12a931a6161c | 68 | @param fragment_length input the input fragment length (sanity checks...) |
ansond | 0:12a931a6161c | 69 | @return the number of fragments currently stored |
ansond | 0:12a931a6161c | 70 | */ |
ansond | 0:12a931a6161c | 71 | int add(uint8_t *fragment,int fragment_length); |
ansond | 0:12a931a6161c | 72 | |
ansond | 0:12a931a6161c | 73 | /** |
ansond | 0:12a931a6161c | 74 | Assemble fragments |
ansond | 0:12a931a6161c | 75 | @param buffer input the result buffer |
ansond | 0:12a931a6161c | 76 | @param buffer_length input the result buffer length (maximum) |
ansond | 0:12a931a6161c | 77 | @param reset_after_assemble input reset the Fragmenter/Assembler after assembly is complete (default: TRUE) |
ansond | 0:12a931a6161c | 78 | */ |
ansond | 0:12a931a6161c | 79 | void assemble(uint8_t *buffer,int buffer_length,bool reset_after_assemble = true); |
ansond | 0:12a931a6161c | 80 | |
ansond | 0:12a931a6161c | 81 | private: |
ansond | 0:12a931a6161c | 82 | int calculateNumFragments(uint8_t *data,int data_length); |
ansond | 0:12a931a6161c | 83 | int calculateAssemblyLength(int buffer_length); |
ansond | 0:12a931a6161c | 84 | void dump(void); |
ansond | 0:12a931a6161c | 85 | |
ansond | 0:12a931a6161c | 86 | int m_num_fragments; |
ansond | 0:12a931a6161c | 87 | int m_last_fragment_length; |
ansond | 4:221c8a56a80e | 88 | uint8_t m_fragments[MAX_FRAGMENTS][DEF_FRAGMENT_LENGTH+1]; |
ansond | 0:12a931a6161c | 89 | }; |
ansond | 0:12a931a6161c | 90 | |
ansond | 1:9f05dbd1c2c7 | 91 | #endif // __SPLITTER_ASSEMBLER_H__ |