Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
checksum.cpp@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.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 |