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.cpp@0:39b5762958a4, 2013-03-07 (annotated)
- Committer:
- sam_grove
- Date:
- Thu Mar 07 20:55:29 2013 +0000
- Revision:
- 0:39b5762958a4
Corrected documentation, updated the example and added links to information about usage
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.cpp |
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 | 0:39b5762958a4 | 22 | #include "checksum.h" |
sam_grove | 0:39b5762958a4 | 23 | |
sam_grove | 0:39b5762958a4 | 24 | bool validateChecksum( uint8_t const *pkt, uint32_t const length ) |
sam_grove | 0:39b5762958a4 | 25 | { |
sam_grove | 0:39b5762958a4 | 26 | uint32_t pos = 0; |
sam_grove | 0:39b5762958a4 | 27 | uint8_t sum = 0; |
sam_grove | 0:39b5762958a4 | 28 | while ( pos < (length-1) ) |
sam_grove | 0:39b5762958a4 | 29 | { |
sam_grove | 0:39b5762958a4 | 30 | sum += *(pkt+pos++); |
sam_grove | 0:39b5762958a4 | 31 | } |
sam_grove | 0:39b5762958a4 | 32 | sum = 0x0 - sum; |
sam_grove | 0:39b5762958a4 | 33 | // return 0 or 1 based on the checksum test |
sam_grove | 0:39b5762958a4 | 34 | return (sum == *(pkt+pos)) ? 1 : 0; |
sam_grove | 0:39b5762958a4 | 35 | } |
sam_grove | 0:39b5762958a4 | 36 | |
sam_grove | 0:39b5762958a4 | 37 | void calculateChecksum( uint8_t *pkt, uint32_t const length ) |
sam_grove | 0:39b5762958a4 | 38 | { |
sam_grove | 0:39b5762958a4 | 39 | uint32_t pos = 0; |
sam_grove | 0:39b5762958a4 | 40 | uint8_t sum = 0; |
sam_grove | 0:39b5762958a4 | 41 | while ( pos < (length-1) ) |
sam_grove | 0:39b5762958a4 | 42 | { |
sam_grove | 0:39b5762958a4 | 43 | sum += *(pkt+pos++); |
sam_grove | 0:39b5762958a4 | 44 | } |
sam_grove | 0:39b5762958a4 | 45 | // put the checksum into the data stream |
sam_grove | 0:39b5762958a4 | 46 | *(pkt+pos) = 0x0 - sum; |
sam_grove | 0:39b5762958a4 | 47 | |
sam_grove | 0:39b5762958a4 | 48 | return; |
sam_grove | 0:39b5762958a4 | 49 | } |
sam_grove | 0:39b5762958a4 | 50 |