Simple data buffer splitter and re-assembler.

Committer:
ansond
Date:
Sat Jul 25 19:15:31 2015 +0000
Revision:
8:bfa5b7b9b886
Parent:
7:6baecc15cb26
updates for BLE location

Who changed what in which revision?

UserRevisionLine numberNew 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 8:bfa5b7b9b886 29 #define MAX_FRAGMENTS 17 // maximum number of supported fragments... increase as needed... (max payload length = 340/DEF_FRAGMENT_LENGTH ~ 17)
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 7:6baecc15cb26 61 Get the number of fragments
ansond 7:6baecc15cb26 62 @returns the number of fragments
ansond 7:6baecc15cb26 63 */
ansond 7:6baecc15cb26 64 int getNumFragments() { return this->m_num_fragments; }
ansond 7:6baecc15cb26 65
ansond 7:6baecc15cb26 66 /**
ansond 0:12a931a6161c 67 Reset the Fragmenter/Assembler
ansond 0:12a931a6161c 68 */
ansond 0:12a931a6161c 69 void reset(void);
ansond 0:12a931a6161c 70
ansond 0:12a931a6161c 71 /**
ansond 0:12a931a6161c 72 Add a fragment
ansond 0:12a931a6161c 73 @param fragment input fragment to add
ansond 0:12a931a6161c 74 @param fragment_length input the input fragment length (sanity checks...)
ansond 0:12a931a6161c 75 @return the number of fragments currently stored
ansond 0:12a931a6161c 76 */
ansond 0:12a931a6161c 77 int add(uint8_t *fragment,int fragment_length);
ansond 0:12a931a6161c 78
ansond 0:12a931a6161c 79 /**
ansond 0:12a931a6161c 80 Assemble fragments
ansond 0:12a931a6161c 81 @param buffer input the result buffer
ansond 0:12a931a6161c 82 @param buffer_length input the result buffer length (maximum)
ansond 0:12a931a6161c 83 @param reset_after_assemble input reset the Fragmenter/Assembler after assembly is complete (default: TRUE)
ansond 6:094ed29a8bf7 84 @return the length of the assembled buffer
ansond 0:12a931a6161c 85 */
ansond 6:094ed29a8bf7 86 int assemble(uint8_t *buffer,int buffer_length,bool reset_after_assemble = true);
ansond 6:094ed29a8bf7 87
ansond 6:094ed29a8bf7 88 /**
ansond 6:094ed29a8bf7 89 Find a specific character in the collected fragments
ansond 6:094ed29a8bf7 90 @param special_char the special character to look for
ansond 6:094ed29a8bf7 91 @return true - special char is in one of the fragments, false, otherwise
ansond 6:094ed29a8bf7 92 */
ansond 6:094ed29a8bf7 93 bool hasCollectedCharacter(char special_char);
ansond 0:12a931a6161c 94
ansond 0:12a931a6161c 95 private:
ansond 0:12a931a6161c 96 int calculateNumFragments(uint8_t *data,int data_length);
ansond 0:12a931a6161c 97 int calculateAssemblyLength(int buffer_length);
ansond 0:12a931a6161c 98 void dump(void);
ansond 0:12a931a6161c 99
ansond 0:12a931a6161c 100 int m_num_fragments;
ansond 0:12a931a6161c 101 int m_last_fragment_length;
ansond 4:221c8a56a80e 102 uint8_t m_fragments[MAX_FRAGMENTS][DEF_FRAGMENT_LENGTH+1];
ansond 0:12a931a6161c 103 };
ansond 0:12a931a6161c 104
ansond 1:9f05dbd1c2c7 105 #endif // __SPLITTER_ASSEMBLER_H__