Utility functions for working with data streams
Example usage:
http://andy.dynamicbits.com/hdradio/
#include "mbed.h" #include "checksum.h" DigitalOut myled(LED1); template <int T> struct data{ uint8_t buf[T]; uint8_t checksum; }; data<12> all_ones = {{1,1,1,1,1,1,1,1,1,1,1,1}}; int main() { calculateChecksum( all_ones.buf, sizeof(all_ones) ); for(int i=0; i<sizeof(all_ones); i++) { printf("%02d: %d\n", i, *(all_ones.buf+i)); } printf("checksum test: %s\n", (result)?"passed":"failed"); while(1) { myled = 1; wait(0.2); myled = 0; wait(0.2); } }
checksum.h@1:ace4b3aef52b, 2013-03-07 (annotated)
- Committer:
- sam_grove
- Date:
- Thu Mar 07 20:59:49 2013 +0000
- Revision:
- 1:ace4b3aef52b
- Parent:
- 0:39b5762958a4
More documentation updates
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sam_grove | 0:39b5762958a4 | 1 | /** |
sam_grove | 0:39b5762958a4 | 2 | * @file checksum.h |
sam_grove | 0:39b5762958a4 | 3 | * @brief Utility Function - Data integrity helpers |
sam_grove | 0:39b5762958a4 | 4 | * @author sam grove |
sam_grove | 0:39b5762958a4 | 5 | * @version 1.0 |
sam_grove | 0:39b5762958a4 | 6 | * |
sam_grove | 0:39b5762958a4 | 7 | * Copyright (c) 2013 |
sam_grove | 0:39b5762958a4 | 8 | * |
sam_grove | 0:39b5762958a4 | 9 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sam_grove | 0:39b5762958a4 | 10 | * you may not use this file except in compliance with the License. |
sam_grove | 0:39b5762958a4 | 11 | * You may obtain a copy of the License at |
sam_grove | 0:39b5762958a4 | 12 | * |
sam_grove | 0:39b5762958a4 | 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
sam_grove | 0:39b5762958a4 | 14 | * |
sam_grove | 0:39b5762958a4 | 15 | * Unless required by applicable law or agreed to in writing, software |
sam_grove | 0:39b5762958a4 | 16 | * distributed under the License is distributed on an "AS IS" BASIS, |
sam_grove | 0:39b5762958a4 | 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sam_grove | 0:39b5762958a4 | 18 | * See the License for the specific language governing permissions and |
sam_grove | 0:39b5762958a4 | 19 | * limitations under the License. |
sam_grove | 0:39b5762958a4 | 20 | */ |
sam_grove | 0:39b5762958a4 | 21 | |
sam_grove | 1:ace4b3aef52b | 22 | #ifndef CHECKSUM_H |
sam_grove | 1:ace4b3aef52b | 23 | #define CHECKSUM_H |
sam_grove | 1:ace4b3aef52b | 24 | |
sam_grove | 1:ace4b3aef52b | 25 | #include <stdint.h> |
sam_grove | 1:ace4b3aef52b | 26 | |
sam_grove | 0:39b5762958a4 | 27 | /** Helpers for working data streams |
sam_grove | 0:39b5762958a4 | 28 | * |
sam_grove | 0:39b5762958a4 | 29 | * Example: |
sam_grove | 0:39b5762958a4 | 30 | * @code |
sam_grove | 0:39b5762958a4 | 31 | * #include "mbed.h" |
sam_grove | 0:39b5762958a4 | 32 | * #include "checksum.h" |
sam_grove | 0:39b5762958a4 | 33 | * |
sam_grove | 0:39b5762958a4 | 34 | * DigitalOut myled(LED1); |
sam_grove | 0:39b5762958a4 | 35 | * |
sam_grove | 0:39b5762958a4 | 36 | * template <int T> |
sam_grove | 0:39b5762958a4 | 37 | * struct data{ |
sam_grove | 0:39b5762958a4 | 38 | * uint8_t buf[T]; |
sam_grove | 0:39b5762958a4 | 39 | * uint8_t checksum; |
sam_grove | 0:39b5762958a4 | 40 | * }; |
sam_grove | 0:39b5762958a4 | 41 | * data<12> all_ones = {{1,1,1,1,1,1,1,1,1,1,1,1}}; |
sam_grove | 0:39b5762958a4 | 42 | * |
sam_grove | 0:39b5762958a4 | 43 | * int main() |
sam_grove | 0:39b5762958a4 | 44 | * { |
sam_grove | 0:39b5762958a4 | 45 | * calculateChecksum( all_ones.buf, sizeof(all_ones) ); |
sam_grove | 0:39b5762958a4 | 46 | * |
sam_grove | 0:39b5762958a4 | 47 | * for(int i=0; i<sizeof(all_ones); i++) |
sam_grove | 0:39b5762958a4 | 48 | * { |
sam_grove | 0:39b5762958a4 | 49 | * printf("%02d: %d\n", i, *(all_ones.buf+i)); |
sam_grove | 0:39b5762958a4 | 50 | * } |
sam_grove | 0:39b5762958a4 | 51 | * |
sam_grove | 0:39b5762958a4 | 52 | * printf("checksum test: %s\n", (result)?"passed":"failed"); |
sam_grove | 0:39b5762958a4 | 53 | * |
sam_grove | 0:39b5762958a4 | 54 | * while(1) |
sam_grove | 0:39b5762958a4 | 55 | * { |
sam_grove | 0:39b5762958a4 | 56 | * myled = 1; |
sam_grove | 0:39b5762958a4 | 57 | * wait(0.2); |
sam_grove | 0:39b5762958a4 | 58 | * myled = 0; |
sam_grove | 0:39b5762958a4 | 59 | * wait(0.2); |
sam_grove | 0:39b5762958a4 | 60 | * } |
sam_grove | 0:39b5762958a4 | 61 | * } |
sam_grove | 0:39b5762958a4 | 62 | * @endcode |
sam_grove | 0:39b5762958a4 | 63 | */ |
sam_grove | 0:39b5762958a4 | 64 | |
sam_grove | 0:39b5762958a4 | 65 | /** Calculate the checksum of a data stream |
sam_grove | 0:39b5762958a4 | 66 | * Assumes the checksum is in the last position, 8-bits and 0 - the sum of data |
sam_grove | 0:39b5762958a4 | 67 | * @param pkt - A pointer to the data |
sam_grove | 0:39b5762958a4 | 68 | * @param length - The amount of data that the checksum calculates |
sam_grove | 0:39b5762958a4 | 69 | * @returns true if the checksum is correct and false otherwise |
sam_grove | 0:39b5762958a4 | 70 | */ |
sam_grove | 0:39b5762958a4 | 71 | bool validateChecksum( uint8_t const *pkt, uint32_t const length ); |
sam_grove | 0:39b5762958a4 | 72 | |
sam_grove | 0:39b5762958a4 | 73 | /** Calculate and store the checksum into the last position of a data stream |
sam_grove | 0:39b5762958a4 | 74 | * |
sam_grove | 0:39b5762958a4 | 75 | * @param pkt - A pointer to the data |
sam_grove | 0:39b5762958a4 | 76 | * @param length - The amount of data that the checksum calculates |
sam_grove | 0:39b5762958a4 | 77 | */ |
sam_grove | 0:39b5762958a4 | 78 | void calculateChecksum( uint8_t *pkt, uint32_t const length ); |
sam_grove | 0:39b5762958a4 | 79 | |
sam_grove | 0:39b5762958a4 | 80 | #endif |