Simple data buffer splitter and re-assembler.

Committer:
ansond
Date:
Thu Feb 12 20:06:15 2015 +0000
Revision:
1:9f05dbd1c2c7
Parent:
DataFragmenterAssembler.h@0:12a931a6161c
Child:
3:00f7a99862a3
updates and renamed

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 0:12a931a6161c 29 #define MAX_FRAGMENTS 100 // maximum number of supported fragments... increase as needed...
ansond 0:12a931a6161c 30 #define DEF_FRAGMENT_LENGTH 20 // Typically identical to 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 0:12a931a6161c 54 /**
ansond 0:12a931a6161c 55 Reset the Fragmenter/Assembler
ansond 0:12a931a6161c 56 */
ansond 0:12a931a6161c 57 void reset(void);
ansond 0:12a931a6161c 58
ansond 0:12a931a6161c 59 /**
ansond 0:12a931a6161c 60 Add a fragment
ansond 0:12a931a6161c 61 @param fragment input fragment to add
ansond 0:12a931a6161c 62 @param fragment_length input the input fragment length (sanity checks...)
ansond 0:12a931a6161c 63 @return the number of fragments currently stored
ansond 0:12a931a6161c 64 */
ansond 0:12a931a6161c 65 int add(uint8_t *fragment,int fragment_length);
ansond 0:12a931a6161c 66
ansond 0:12a931a6161c 67 /**
ansond 0:12a931a6161c 68 Assemble fragments
ansond 0:12a931a6161c 69 @param buffer input the result buffer
ansond 0:12a931a6161c 70 @param buffer_length input the result buffer length (maximum)
ansond 0:12a931a6161c 71 @param reset_after_assemble input reset the Fragmenter/Assembler after assembly is complete (default: TRUE)
ansond 0:12a931a6161c 72 */
ansond 0:12a931a6161c 73 void assemble(uint8_t *buffer,int buffer_length,bool reset_after_assemble = true);
ansond 0:12a931a6161c 74
ansond 0:12a931a6161c 75 private:
ansond 0:12a931a6161c 76 int calculateNumFragments(uint8_t *data,int data_length);
ansond 0:12a931a6161c 77 int calculateAssemblyLength(int buffer_length);
ansond 0:12a931a6161c 78 void dump(void);
ansond 0:12a931a6161c 79
ansond 0:12a931a6161c 80 int m_num_fragments;
ansond 0:12a931a6161c 81 int m_last_fragment_length;
ansond 0:12a931a6161c 82 uint8_t m_fragments[DEF_FRAGMENT_LENGTH+1][MAX_FRAGMENTS];
ansond 0:12a931a6161c 83 };
ansond 0:12a931a6161c 84
ansond 1:9f05dbd1c2c7 85 #endif // __SPLITTER_ASSEMBLER_H__