- Timestamp:
- Jul 28, 2014, 5:53:28 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/Pacpus/PacpusTools/BinaryDecoder.h
r305 r316 1 <<<<<<< .mine2 1 // %pacpus:license{ 3 2 // This file is part of the PACPUS framework distributed under the … … 37 36 #endif // __cplusplus 38 37 38 #include "PacpusToolsConfig.h" 39 39 40 #include <stdio.h> 40 41 … … 1392 1393 1393 1394 #endif // BINARYDECODER_H 1394 =======1395 // %pacpus:license{1396 // This file is part of the PACPUS framework distributed under the1397 // CECILL-C License, Version 1.0.1398 // %pacpus:license}1399 /// @file1400 /// @author Gerald Dherbomez <firstname.surname@utc.fr>1401 /// @date July, 20081402 /// @version $Id: BinaryDecoder.h 76 2013-01-10 17:05:10Z kurdejma $1403 /// @copyright Copyright (c) UTC/CNRS Heudiasyc 2006 - 2013. All rights reserved.1404 /// @brief Extracts data of specified type in a string.1405 ///1406 /// Purpose: Extract data of specified type in a string (char *)1407 /// Usage:1408 /// bool xDecodeToXXX(T* result, const char * data, const unsigned int startBit, const unsigned int length)1409 /// => x = format (i:Intel, Little Endian)1410 /// (m:Motorola, Big Endian)1411 /// => XXX = type of return value1412 /// (Bool = bool)1413 /// (I8 = char - 8 bits)1414 /// (UI8 = unsigned char - 8 bits)1415 /// (I16 = short - 16 bits)1416 /// (UI16 = unsigned short - 16 bits)1417 /// (I32 = long - 32 bits)1418 /// (UI32 = unsigned long - 32 bits)1419 /// (I64 = long long - 64 bits)1420 /// (UI16 = unisgned long long - 64 bits)1421 ///1422 /// ex: inline bool mDecodeToUI16(r, str, 8, 12);1423 1424 #ifndef BINARYDECODER_H1425 #define BINARYDECODER_H1426 1427 #ifdef __cplusplus1428 extern "C"1429 {1430 #endif // __cplusplus1431 1432 #include "PacpusToolsConfig.h"1433 1434 #include <stdio.h>1435 1436 //////////////////////////////////////////////////////////////////////////1437 // print the data in the hexadecimal format in the console1438 inline void displayData(const unsigned char * data, const unsigned long length, const int id)1439 {1440 printf("\ndata = ");1441 for (unsigned int i = 0;i< length;++i )1442 printf("%02x ",data[i]);1443 printf(" dlc = %ld ID = 0x%x\n", length, id);1444 }1445 1446 //////////////////////////////////////////////////////////////////////////1447 // for Big Endian - Motorola coding1448 //------------------------------------------------------------------------1449 // return true if the signal is spread on more than 1 byte1450 // ie if length is greater than ((startBit%8) + 1)1451 // example:1452 // 7654 32101453 // 0 .... ..||1454 // 1 |||| ....1455 // startBit is 1 and the length is 8: result gives (1%8)+1 = 2 < length(=6)1456 inline bool mSpreadOnMoreThan1Byte(const unsigned int startBit, const unsigned int length)1457 {1458 if ( (length > (startBit & 0x07) + 1) ) {1459 return true;1460 } else {1461 return false;1462 }1463 }1464 1465 //////////////////////////////////////////////////////////////////////////1466 // for Little Endian - Intel coding1467 //------------------------------------------------------------------------1468 // return true if the signal is spread on more than 1 byte1469 // ie if length is greater than 8-(startBit%8)1470 // example:1471 // 7654 32101472 // 0 ||.. ....1473 // 1 .... ||||1474 // startBit is 6 and the length is 6: result gives 8-((6%8)) = 2 < length(=6)1475 inline bool iSpreadOnMoreThan1Byte(const unsigned int startBit, const unsigned int length)1476 {1477 if ( length > 8 - (startBit & 0x07) ) {1478 return true;1479 } else {1480 return false;1481 }1482 }1483 1484 //////////////////////////////////////////////////////////////////////////1485 // for Big Endian - Motorola coding1486 //------------------------------------------------------------------------1487 // return true if the signal is spread on more than 2 bytes1488 // ie if (length - ((startBit%8) + 1) is greater than 81489 // example:1490 // 7654 32101491 // 0 .... ...|1492 // 1 |||| ||||1493 // 2 ||.. ....1494 // the start bit is 0 and the length is 11: result gives 11 - (0%8+1) = 10 (>8)1495 //////////////////////////////////////////////////////////////////////////1496 inline bool mSpreadOnMoreThan2Bytes(const unsigned int startBit, const unsigned int length)1497 {1498 if ( length - ((startBit & 0x07) + 1) > 8 )1499 return true;1500 else1501 return false;1502 }1503 1504 //////////////////////////////////////////////////////////////////////////1505 // for Little Endian - Intel coding1506 //------------------------------------------------------------------------1507 // return true if the signal is spread on more than 2 bytes1508 // ie if (length - (8 - startBit)) is greater than 81509 // example:1510 // 7654 32101511 // 0 |... ....1512 // 1 |||| ||||1513 // 2 .... ..||1514 // the start bit is 7 and the length is 11: result gives 11 - (8 - 7) = 10 (>8)1515 //////////////////////////////////////////////////////////////////////////1516 inline bool iSpreadOnMoreThan2Bytes(const unsigned int startBit, const unsigned int length)1517 {1518 if ( length - (8 - (startBit & 0x07)) > 8 )1519 return true;1520 else1521 return false;1522 }1523 1524 //////////////////////////////////////////////////////////////////////////1525 // for Big Endian - Motorola coding1526 //------------------------------------------------------------------------1527 // return true if the signal is spread on more than 4 bytes1528 // ie if length is greater than ((startBit%8) + 1)1529 // example:1530 // 7654 32101531 // 0 .... ..||1532 // 1 |||| ||||1533 // 2 |||| ||||1534 // 3 |||| ||||1535 // 4 |||. ....1536 // startBit is 1 and the length is 29: result gives 29 - ((1%8)+1) = 27 (> 24)1537 inline bool mSpreadOnMoreThan4Bytes(const unsigned int startBit, const unsigned int length)1538 {1539 return (3*8) < (length - ((startBit & 0x07) + 1));1540 }1541 1542 //////////////////////////////////////////////////////////////////////////1543 // for Little Endian - Intel coding1544 //------------------------------------------------------------------------1545 // return true if the signal is spread on more than 4 bytes1546 // ie if length is greater than 8-(startBit%8)1547 // example:1548 // 7654 32101549 // 0 ||.. ....1550 // 1 |||| ||||1551 // 2 |||| ||||1552 // 3 |||| ||||1553 // 4 .... ..||1554 // startBit is 6 and the length is 28: result gives 28 - (8-((6%8))) = 26 (>24)1555 inline bool iSpreadOnMoreThan4Bytes(const unsigned int startBit, const unsigned int length)1556 {1557 return (3*8) < (length - (8 - (startBit & 0x07)));1558 }1559 1560 //////////////////////////////////////////////////////////////////////////1561 // return a 8-bits shifted-left mask corresponding to the length parameter1562 // ex: shift = 2 and length = 31563 // mask will be 0b000111001564 inline unsigned char getShiftMask8(const unsigned int shift, const unsigned int length)1565 {1566 unsigned char mask;1567 switch (length)1568 {1569 case 1: mask = 0x01; break;1570 case 2: mask = 0x03; break;1571 case 3: mask = 0x07; break;1572 case 4: mask = 0x0F; break;1573 case 5: mask = 0x1F; break;1574 case 6: mask = 0x3F; break;1575 case 7: mask = 0x7F; break;1576 case 8: mask = 0xFF; break;1577 default: mask = 0; break;1578 }1579 return mask << shift;1580 }1581 1582 //////////////////////////////////////////////////////////////////////////1583 // return a 8-bits mask corresponding to the length parameter with bit order decreasing1584 // ex: startBit = 2 and length = 31585 // mask will be 0b000001111586 inline unsigned char getMask8( const unsigned int length )1587 {1588 unsigned char mask;1589 switch (length) {1590 case 1: mask = 0x01; break;1591 case 2: mask = 0x03; break;1592 case 3: mask = 0x07; break;1593 case 4: mask = 0x0F; break;1594 case 5: mask = 0x1F; break;1595 case 6: mask = 0x3F; break;1596 case 7: mask = 0x7F; break;1597 case 8: mask = 0xFF; break;1598 default: mask = 0; break;1599 }1600 return mask;1601 }1602 1603 //////////////////////////////////////////////////////////////////////////1604 // return a 16-bits shifted-left mask corresponding to the length parameter1605 inline unsigned short getShiftMask16(const unsigned int shift, const unsigned int length)1606 {1607 unsigned short mask;1608 switch (length) {1609 case 1: mask = 0x0001; break;1610 case 2: mask = 0x0003; break;1611 case 3: mask = 0x0007; break;1612 case 4: mask = 0x000F; break;1613 case 5: mask = 0x001F; break;1614 case 6: mask = 0x003F; break;1615 case 7: mask = 0x007F; break;1616 case 8: mask = 0x00FF; break;1617 case 9: mask = 0x01FF; break;1618 case 10: mask = 0x03FF; break;1619 case 11: mask = 0x07FF; break;1620 case 12: mask = 0x0FFF; break;1621 case 13: mask = 0x1FFF; break;1622 case 14: mask = 0x3FFF; break;1623 case 15: mask = 0x7FFF; break;1624 case 16: mask = 0xFFFF; break;1625 default: mask = 0; break;1626 }1627 return mask << shift;1628 }1629 1630 //////////////////////////////////////////////////////////////////////////1631 // return a 16-bits mask corresponding to the length parameter with bit order decreasing1632 inline unsigned short getMask16( const unsigned int length )1633 {1634 unsigned short mask;1635 switch (length) {1636 case 1: mask = 0x0001; break;1637 case 2: mask = 0x0003; break;1638 case 3: mask = 0x0007; break;1639 case 4: mask = 0x000F; break;1640 case 5: mask = 0x001F; break;1641 case 6: mask = 0x003F; break;1642 case 7: mask = 0x007F; break;1643 case 8: mask = 0x00FF; break;1644 case 9: mask = 0x01FF; break;1645 case 10: mask = 0x03FF; break;1646 case 11: mask = 0x07FF; break;1647 case 12: mask = 0x0FFF; break;1648 case 13: mask = 0x1FFF; break;1649 case 14: mask = 0x3FFF; break;1650 case 15: mask = 0x7FFF; break;1651 case 16: mask = 0xFFFF; break;1652 default: mask = 0; break;1653 }1654 return mask;1655 }1656 1657 //////////////////////////////////////////////////////////////////////////1658 // return a 32-bits shifted-left mask corresponding to the length parameter1659 inline unsigned long getShiftMask32(const unsigned int shift, const unsigned int length)1660 {1661 unsigned long mask;1662 switch (length) {1663 case 1: mask = 0x00000001; break;1664 case 2: mask = 0x00000003; break;1665 case 3: mask = 0x00000007; break;1666 case 4: mask = 0x0000000F; break;1667 case 5: mask = 0x0000001F; break;1668 case 6: mask = 0x0000003F; break;1669 case 7: mask = 0x0000007F; break;1670 case 8: mask = 0x000000FF; break;1671 case 9: mask = 0x000001FF; break;1672 case 10: mask = 0x000003FF; break;1673 case 11: mask = 0x000007FF; break;1674 case 12: mask = 0x00000FFF; break;1675 case 13: mask = 0x00001FFF; break;1676 case 14: mask = 0x00003FFF; break;1677 case 15: mask = 0x00007FFF; break;1678 case 16: mask = 0x0000FFFF; break;1679 case 17: mask = 0x0001FFFF; break;1680 case 18: mask = 0x0003FFFF; break;1681 case 19: mask = 0x0007FFFF; break;1682 case 20: mask = 0x000FFFFF; break;1683 case 21: mask = 0x001FFFFF; break;1684 case 22: mask = 0x003FFFFF; break;1685 case 23: mask = 0x007FFFFF; break;1686 case 24: mask = 0x00FFFFFF; break;1687 case 25: mask = 0x01FFFFFF; break;1688 case 26: mask = 0x03FFFFFF; break;1689 case 27: mask = 0x07FFFFFF; break;1690 case 28: mask = 0x0FFFFFFF; break;1691 case 29: mask = 0x1FFFFFFF; break;1692 case 30: mask = 0x3FFFFFFF; break;1693 case 31: mask = 0x7FFFFFFF; break;1694 case 32: mask = 0xFFFFFFFF; break;1695 default: mask = 0; break;1696 }1697 return mask << shift;1698 }1699 1700 //////////////////////////////////////////////////////////////////////////1701 // return a 32-bits mask corresponding to the length parameter with bit order decreasing1702 inline unsigned long getMask32( const unsigned int length )1703 {1704 unsigned long mask;1705 switch (length) {1706 case 1: mask = 0x00000001; break;1707 case 2: mask = 0x00000003; break;1708 case 3: mask = 0x00000007; break;1709 case 4: mask = 0x0000000F; break;1710 case 5: mask = 0x0000001F; break;1711 case 6: mask = 0x0000003F; break;1712 case 7: mask = 0x0000007F; break;1713 case 8: mask = 0x000000FF; break;1714 case 9: mask = 0x000001FF; break;1715 case 10: mask = 0x000003FF; break;1716 case 11: mask = 0x000007FF; break;1717 case 12: mask = 0x00000FFF; break;1718 case 13: mask = 0x00001FFF; break;1719 case 14: mask = 0x00003FFF; break;1720 case 15: mask = 0x00007FFF; break;1721 case 16: mask = 0x0000FFFF; break;1722 case 17: mask = 0x0001FFFF; break;1723 case 18: mask = 0x0003FFFF; break;1724 case 19: mask = 0x0007FFFF; break;1725 case 20: mask = 0x000FFFFF; break;1726 case 21: mask = 0x001FFFFF; break;1727 case 22: mask = 0x003FFFFF; break;1728 case 23: mask = 0x007FFFFF; break;1729 case 24: mask = 0x00FFFFFF; break;1730 case 25: mask = 0x01FFFFFF; break;1731 case 26: mask = 0x03FFFFFF; break;1732 case 27: mask = 0x07FFFFFF; break;1733 case 28: mask = 0x0FFFFFFF; break;1734 case 29: mask = 0x1FFFFFFF; break;1735 case 30: mask = 0x3FFFFFFF; break;1736 case 31: mask = 0x7FFFFFFF; break;1737 case 32: mask = 0xFFFFFFFF; break;1738 default: mask = 0; break;1739 }1740 return mask;1741 }1742 1743 //////////////////////////////////////////////////////////////////////////1744 // return a 64-bits mask corresponding to the length parameter with bit order decreasing1745 inline unsigned long long getMask64( const unsigned int length )1746 {1747 unsigned long long mask;1748 switch (length) {1749 case 1: mask = 0x0000000000000001ULL; break;1750 case 2: mask = 0x0000000000000003ULL; break;1751 case 3: mask = 0x0000000000000007ULL; break;1752 case 4: mask = 0x000000000000000FULL; break;1753 case 5: mask = 0x000000000000001FULL; break;1754 case 6: mask = 0x000000000000003FULL; break;1755 case 7: mask = 0x000000000000007FULL; break;1756 case 8: mask = 0x00000000000000FFULL; break;1757 case 9: mask = 0x00000000000001FFULL; break;1758 case 10: mask = 0x00000000000003FFULL; break;1759 case 11: mask = 0x00000000000007FFULL; break;1760 case 12: mask = 0x0000000000000FFFULL; break;1761 case 13: mask = 0x0000000000001FFFULL; break;1762 case 14: mask = 0x0000000000003FFFULL; break;1763 case 15: mask = 0x0000000000007FFFULL; break;1764 case 16: mask = 0x000000000000FFFFULL; break;1765 case 17: mask = 0x000000000001FFFFULL; break;1766 case 18: mask = 0x000000000003FFFFULL; break;1767 case 19: mask = 0x000000000007FFFFULL; break;1768 case 20: mask = 0x00000000000FFFFFULL; break;1769 case 21: mask = 0x00000000001FFFFFULL; break;1770 case 22: mask = 0x00000000003FFFFFULL; break;1771 case 23: mask = 0x00000000007FFFFFULL; break;1772 case 24: mask = 0x0000000000FFFFFFULL; break;1773 case 25: mask = 0x0000000001FFFFFFULL; break;1774 case 26: mask = 0x0000000003FFFFFFULL; break;1775 case 27: mask = 0x0000000007FFFFFFULL; break;1776 case 28: mask = 0x000000000FFFFFFFULL; break;1777 case 29: mask = 0x000000001FFFFFFFULL; break;1778 case 30: mask = 0x000000003FFFFFFFULL; break;1779 case 31: mask = 0x000000007FFFFFFFULL; break;1780 case 32: mask = 0x00000000FFFFFFFFULL; break;1781 case 33: mask = 0x00000001FFFFFFFFULL; break;1782 case 34: mask = 0x00000003FFFFFFFFULL; break;1783 case 35: mask = 0x00000007FFFFFFFFULL; break;1784 case 36: mask = 0x0000000FFFFFFFFFULL; break;1785 case 37: mask = 0x0000001FFFFFFFFFULL; break;1786 case 38: mask = 0x0000003FFFFFFFFFULL; break;1787 case 39: mask = 0x0000007FFFFFFFFFULL; break;1788 case 40: mask = 0x000000FFFFFFFFFFULL; break;1789 case 41: mask = 0x000001FFFFFFFFFFULL; break;1790 case 42: mask = 0x000003FFFFFFFFFFULL; break;1791 case 43: mask = 0x000007FFFFFFFFFFULL; break;1792 case 44: mask = 0x00000FFFFFFFFFFFULL; break;1793 case 45: mask = 0x00001FFFFFFFFFFFULL; break;1794 case 46: mask = 0x00003FFFFFFFFFFFULL; break;1795 case 47: mask = 0x00007FFFFFFFFFFFULL; break;1796 case 48: mask = 0x0000FFFFFFFFFFFFULL; break;1797 case 49: mask = 0x0001FFFFFFFFFFFFULL; break;1798 case 50: mask = 0x0003FFFFFFFFFFFFULL; break;1799 case 51: mask = 0x0007FFFFFFFFFFFFULL; break;1800 case 52: mask = 0x000FFFFFFFFFFFFFULL; break;1801 case 53: mask = 0x001FFFFFFFFFFFFFULL; break;1802 case 54: mask = 0x003FFFFFFFFFFFFFULL; break;1803 case 55: mask = 0x007FFFFFFFFFFFFFULL; break;1804 case 56: mask = 0x00FFFFFFFFFFFFFFULL; break;1805 case 57: mask = 0x01FFFFFFFFFFFFFFULL; break;1806 case 58: mask = 0x03FFFFFFFFFFFFFFULL; break;1807 case 59: mask = 0x07FFFFFFFFFFFFFFULL; break;1808 case 60: mask = 0x0FFFFFFFFFFFFFFFULL; break;1809 case 61: mask = 0x1FFFFFFFFFFFFFFFULL; break;1810 case 62: mask = 0x3FFFFFFFFFFFFFFFULL; break;1811 case 63: mask = 0x7FFFFFFFFFFFFFFFULL; break;1812 case 64: mask = 0xFFFFFFFFFFFFFFFFULL; break;1813 default: mask = 0; break;1814 }1815 return mask;1816 }1817 1818 //////////////////////////////////////////////////////////////////////////1819 /// Returns a 64-bits shifted-left mask corresponding to the length parameter1820 inline unsigned long long getShiftMask64(const unsigned int shift, const unsigned int length)1821 {1822 unsigned long long mask;1823 mask = getMask64( length );1824 mask <<= shift;1825 return mask;1826 }1827 1828 //////////////////////////////////////////////////////////////////////////1829 /// Big Endian - Motorola coding1830 inline bool mDecodeToBool(const unsigned char * data, const unsigned int startBit)1831 {1832 return (data[startBit>>3] & getShiftMask8(startBit&0x07,1)) != 0;1833 }1834 1835 //////////////////////////////////////////////////////////////////////////1836 // Big Endian - Motorola coding1837 // to test ?1838 inline bool mDecodeToUI64(unsigned long long * result, const unsigned char * data, const unsigned int startBit, const unsigned int length)1839 {1840 if (length > 64) {1841 *result = 0;1842 return false;1843 }1844 1845 if (length == 64) {1846 // only work if data are correctly byte-aligned1847 unsigned char c[8]; // c[0] = MSB1848 for (int i = 0 ; i < 8 ; i++)1849 c[i] = data[(startBit>>3) + i];1850 1851 unsigned long long temp = 0;1852 for (int i = 0 ; i < 8 ; i++)1853 temp += (c[i] << ( 56 - (8*i) ) );1854 *result = temp;1855 return true;1856 } else {1857 unsigned char c[8], mask[8], nbBits[8]; // MSB = c[0]1858 nbBits[0] = (startBit & 0x07)+1;1859 mask[0] = getMask8( nbBits[0] );1860 c[0] = data[startBit>>3] & mask[0];1861 unsigned short nbBitsIncrement = nbBits[0];1862 for (int i = 1 ; i < 8 ; i++) {1863 nbBits[i] = static_cast<unsigned char>( (length - nbBitsIncrement) < 8 ? length - nbBitsIncrement : 8 );1864 nbBitsIncrement += nbBits[i];1865 mask[i] = getShiftMask8(8 - nbBits[i],nbBits[i]);1866 c[i] = data[(startBit>>3) + i] & mask[i];1867 }1868 1869 unsigned long long temp = 0;1870 for (int i = 0 ; i < 8 ; i++)1871 temp += (c[i] << ( 56 - (8*i) ) );1872 *result = temp >> (56 + (startBit&0x07) + 1 - length);1873 return true;1874 }1875 }1876 1877 //////////////////////////////////////////////////////////////////////////1878 // Big Endian - Motorola coding1879 // to test ?1880 inline bool mDecodeToI64(long long *result, const unsigned char * data, const unsigned int startBit, const unsigned int length)1881 {1882 if (length>64) {1883 *result = 0;1884 return false;1885 }1886 1887 if (length == 64) {1888 // only work if data are correctly byte-aligned1889 unsigned char c[8]; // c[0] = MSB1890 for (int i = 0 ; i < 8 ; i++)1891 c[i] = data[(startBit>>3) + i];1892 1893 for (int i = 0 ; i < 8 ; i++)1894 *result += (c[i] << ( 56 - (8*i) ) );1895 1896 // need to be signed ??1897 return true;1898 } else {1899 unsigned char c[8], mask[8], nbBits[8]; // MSB = c[0]1900 nbBits[0] = (startBit & 0x07)+1;1901 mask[0] = getMask8( nbBits[0] );1902 c[0] = data[startBit>>3] & mask[0];1903 unsigned short nbBitsIncrement = nbBits[0];1904 for (int i = 1 ; i < 8 ; i++) {1905 nbBits[i] = static_cast<unsigned char>( (length - nbBitsIncrement) < 8 ? (length - nbBitsIncrement) : 8 );1906 nbBitsIncrement += nbBits[i];1907 mask[i] = getShiftMask8(8 - nbBits[i],nbBits[i]);1908 c[i] = data[(startBit>>3) + i] & mask[i];1909 }1910 unsigned long long temp = 0;1911 for (int i = 0 ; i < 8 ; i++)1912 temp += (c[i] << ( 56 - (8*i) ) );1913 temp >>= (56 + (startBit&0x07) + 1 - length);1914 1915 if ( temp & getShiftMask64(length-1,1) )1916 // do the two's complement to get the signed value if the msb=11917 *result = -1 * ( ( (~temp) + 1 ) & getMask64(length) );1918 else1919 *result = temp;1920 1921 return true;1922 }1923 }1924 1925 //////////////////////////////////////////////////////////////////////////1926 // Big Endian - Motorola coding1927 // ok1928 inline bool mDecodeToUI32(unsigned long *result, const unsigned char * data, const unsigned int startBit, const unsigned int length)1929 {1930 if (length>32) {1931 *result = 0;1932 return false;1933 }1934 1935 // verify that the frame is not spread in 5 bytes1936 if (mSpreadOnMoreThan4Bytes(startBit, length)) {1937 // decode in a 64-bits integer1938 unsigned long long temp;1939 if (mDecodeToUI64(&temp,data,startBit,length))1940 {1941 *result = static_cast<unsigned long> (temp);1942 return true;1943 } else {1944 *result = 0;1945 return false;1946 }1947 } else {1948 if (length == 32) {1949 // only work if data are correctly byte-aligned1950 unsigned char c1, c2, c3, c4;1951 c1 = data[startBit>>3];1952 c2 = data[(startBit>>3) + 1];1953 c3 = data[(startBit>>3) + 2];1954 c4 = data[(startBit>>3) + 3];1955 1956 *result = (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;1957 return true;1958 } else {1959 unsigned char c[4], mask[4], nbBits[4]; // MSB = c[0]1960 nbBits[0] = (startBit & 0x07)+1;1961 nbBits[1] = static_cast<unsigned char>( (length - nbBits[0]) < 8 ? length - nbBits[0] : 8 );1962 nbBits[2] = static_cast<unsigned char>( (length - nbBits[0] - nbBits[1]) < 8 ? (length - nbBits[0] - nbBits[1]) : 8 );1963 nbBits[3] = static_cast<unsigned char>( (length - nbBits[0] - nbBits[1] - nbBits[2]) < 8 ? (length - nbBits[0] - nbBits[1] - nbBits[2]) : 8 );1964 mask[0] = getMask8( nbBits[0] );1965 mask[1] = getShiftMask8(8 - nbBits[1],nbBits[1]);1966 mask[2] = getShiftMask8(8 - nbBits[2],nbBits[2]);1967 mask[3] = getShiftMask8(8 - nbBits[3],nbBits[3]);1968 c[0] = data[startBit>>3] & mask[0];1969 c[1] = data[(startBit>>3) + 1] & mask[1];1970 c[2] = data[(startBit>>3) + 2] & mask[2];1971 c[3] = data[(startBit>>3) + 3] & mask[3];1972 *result = ( (c[0]<<24) + (c[1]<<16) + (c[2]<<8) + c[3]) >> (24 + (startBit&0x07) + 1 - length);1973 return true;1974 }1975 }1976 }1977 1978 //////////////////////////////////////////////////////////////////////////1979 // Big Endian - Motorola coding1980 // ok1981 inline bool mDecodeToI32(long *result, const unsigned char * data, const unsigned int startBit, const unsigned int length)1982 {1983 if (length>32) {1984 *result = 0;1985 return false;1986 }1987 1988 // verify that the frame is not spread in 5 bytes1989 if (mSpreadOnMoreThan4Bytes(startBit, length)) {1990 // decode in a 64-bits integer1991 long long temp;1992 if (mDecodeToI64(&temp,data,startBit,length))1993 {1994 *result = static_cast<long>(temp);1995 return true;1996 } else {1997 *result = 0;1998 return false;1999 }2000 } else {2001 if (length == 32)2002 {2003 // only work if data are correctly byte-aligned2004 unsigned char c1, c2, c3, c4;2005 c1 = data[startBit>>3];2006 c2 = data[(startBit>>3) + 1];2007 c3 = data[(startBit>>3) + 2];2008 c4 = data[(startBit>>3) + 3];2009 2010 *result = (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;2011 return true;2012 } else {2013 unsigned char c[4], mask[4], nbBits[4]; // MSB = c[0]2014 nbBits[0] = (startBit & 0x07) + 1;2015 nbBits[1] = static_cast<unsigned char>( (length - nbBits[0]) < 8 ? length - nbBits[0] : 8 );2016 nbBits[2] = static_cast<unsigned char>( (length - nbBits[0] - nbBits[1]) < 8 ? (length - nbBits[0] - nbBits[1]) : 8 );2017 nbBits[3] = static_cast<unsigned char>( (length - nbBits[0] - nbBits[1] - nbBits[2]) < 8 ? length - nbBits[0] - nbBits[1] - nbBits[2] : 8 );2018 mask[0] = getMask8( nbBits[0] );2019 mask[1] = getShiftMask8(8 - nbBits[1],nbBits[1]);2020 mask[2] = getShiftMask8(8 - nbBits[2],nbBits[2]);2021 mask[3] = getShiftMask8(8 - nbBits[3],nbBits[3]);2022 c[0] = data[startBit>>3] & mask[0];2023 c[1] = data[(startBit>>3) + 1] & mask[1];2024 c[2] = data[(startBit>>3) + 2] & mask[2];2025 c[3] = data[(startBit>>3) + 3] & mask[3];2026 2027 unsigned long temp = ( (c[0]<<24) + (c[1]<<16) + (c[2]<<8) + c[3]) >> (24 + (startBit&0x07) + 1 - length);2028 if (temp & getShiftMask32(length-1, 1))2029 // do the two's complement to get the signed value if the msb=12030 *result = -1 * ( ( (~temp) + 1 ) & getMask32(length) );2031 else2032 *result = temp;2033 return true;2034 }2035 }2036 }2037 2038 2039 2040 //////////////////////////////////////////////////////////////////////////2041 // Big Endian - Motorola coding2042 // ok2043 inline bool mDecodeToUI16(unsigned short * result, const unsigned char * data, const unsigned int startBit, const unsigned int length)2044 {2045 if (length > 16) {2046 *result = 0;2047 return false;2048 }2049 2050 // verify that the frame is not spread in 3 bytes2051 if (mSpreadOnMoreThan2Bytes(startBit, length)) {2052 // decode in a 32-bits integer2053 unsigned long temp;2054 if (mDecodeToUI32(&temp,data,startBit,length)) {2055 *result = static_cast<unsigned short>(temp);2056 return true;2057 } else {2058 *result = 0;2059 return false;2060 }2061 } else {2062 if (length == 16) {2063 // only work if data are correctly byte-aligned2064 unsigned char LSB, MSB;2065 MSB = data[startBit>>3];2066 LSB = data[(startBit>>3) + 1];2067 2068 *result = ( (MSB << 8) + LSB );2069 return true;2070 } else {2071 unsigned char LSB, MSB, maskMSB, maskLSB, nbBitsMSB, nbBitsLSB;2072 nbBitsMSB = (startBit & 0x07) + 1; // the number of bits contained in the MSB2073 nbBitsLSB = static_cast<unsigned char>(length - nbBitsMSB);2074 maskMSB = getMask8( nbBitsMSB );2075 maskLSB = getShiftMask8(8 - nbBitsLSB , nbBitsLSB);2076 MSB = data[startBit>>3] & maskMSB;2077 LSB = data[(startBit>>3) + 1] & maskLSB;2078 2079 *result = ( (MSB << 8) + LSB ) >> (8 + (startBit&0x07) + 1 - length);2080 return true;2081 }2082 }2083 }2084 2085 //////////////////////////////////////////////////////////////////////////2086 // Big Endian - Motorola coding2087 // ok2088 inline bool mDecodeToI16(short *result, const unsigned char * data, const unsigned int startBit, const unsigned int length)2089 {2090 if (length > 16) {2091 *result = 0;2092 return false;2093 }2094 2095 // verify that the frame is not spread in 3 bytes2096 if (mSpreadOnMoreThan2Bytes(startBit, length)) {2097 // decode in a 32-bits integer2098 long temp;2099 if (mDecodeToI32(&temp, data, startBit, length)) {2100 *result = (short)temp;2101 return true;2102 } else {2103 *result = 0;2104 return false;2105 }2106 } else {2107 // ok data are stored at most in 2 bytes2108 if (length == 16) {2109 // only work if data are correctly byte-aligned2110 unsigned char LSB, MSB;2111 MSB = data[startBit>>3];2112 LSB = data[(startBit>>3) + 1];2113 short temp = (MSB << 8) + LSB;2114 2115 // Attention : a-t on besoin d'appliquer le signe ?2116 // n'est-il pas deja inclu dans la donn�e comme elle est correctement align�e sur l'octet ?2117 if (temp & 0x8000) {2118 // do the two's complement to get the signed value2119 *result = - ( ( (~temp) + 1 ) & 0xFFFF );2120 } else {2121 *result = temp;2122 }2123 return true;2124 } else {2125 unsigned char LSB, MSB, maskMSB, maskLSB, nbBitsMSB, nbBitsLSB;2126 nbBitsMSB = (startBit & 0x07) + 1; // the number of bits contained in the MSB2127 nbBitsLSB = static_cast<unsigned char>(length - nbBitsMSB);2128 maskMSB = getMask8( nbBitsMSB );2129 maskLSB = getShiftMask8(8 - nbBitsLSB , nbBitsLSB);2130 MSB = data[startBit>>3] & maskMSB;2131 LSB = data[(startBit>>3) + 1] & maskLSB;2132 // assign the MSB and LSB char in the short integer value and right-shift2133 // to place the lsb to the bit 02134 unsigned short temp = ( (MSB << 8) + LSB ) >> (8 + (startBit&0x07) + 1 - length);2135 if (temp & getShiftMask16(length-1,1))2136 // do the two's complement to get the signed value if the msb=12137 *result = -1 * ( ( (~temp) + 1 ) & getMask16(length) );2138 else2139 *result = temp;2140 return true;2141 }2142 }2143 }2144 2145 //////////////////////////////////////////////////////////////////////////2146 // Big Endian - Motorola coding2147 // OK2148 inline bool mDecodeToUI8(unsigned char * result, const unsigned char * data, const unsigned int startBit, const unsigned int length)2149 {2150 if (length > 8) {2151 *result = 0;2152 return false;2153 }2154 2155 // verify that the frame is not spread in 2 bytes2156 if (mSpreadOnMoreThan1Byte(startBit, length)) {2157 // decode in a 16-bit integer2158 unsigned short temp;2159 if ( mDecodeToUI16(&temp, data, startBit, length) ) {2160 *result = static_cast<unsigned char>(temp);2161 return true;2162 } else {2163 *result = 0;2164 return false;2165 }2166 } else {2167 // ok data is stored at most in 1 byte2168 unsigned char c;2169 2170 c = data[startBit>>3]; // >>3 <=> div 82171 //c >>= (8-((startBit & 0x07)+1)); // &0x07 <=> modulo 82172 c >>= startBit & 0x07 - (length - 1); // &0x07 <=> modulo 82173 *result = c & getMask8( length );2174 2175 return true;2176 }2177 }2178 2179 //////////////////////////////////////////////////////////////////////////2180 // Big Endian - Motorola coding2181 // OK2182 inline bool mDecodeToI8(char * result, const unsigned char * data, const unsigned int startBit, const unsigned int length)2183 {2184 if (length > 8) {2185 *result = 0;2186 return false;2187 }2188 2189 // verify that the frame is not spread in 2 bytes2190 if (mSpreadOnMoreThan1Byte(startBit, length)) {2191 // decode in a 16-bit integer2192 short temp;2193 if ( mDecodeToI16(&temp, data, startBit, length) ) {2194 *result = static_cast<char>(temp);2195 return true;2196 } else {2197 *result = 0;2198 return false;2199 }2200 }2201 else {2202 // ok data is stored at most in 1 byte2203 char c;2204 c = data[startBit>>3];2205 c >>= (8-((startBit & 0x07)+1));2206 c = c & getMask8( length );2207 2208 // a-t-on besoin d'appliquer le signe dans le cas ou length = 82209 // la donnee est correctement alignee sur l'octet donc le signe est peut-etre compris2210 if (c & getShiftMask8(length-1,1))2211 // do the two's complement to get the signed value if the msb=12212 *result = - ( ( (~c) + 1 ) & getMask8(length) );2213 else2214 *result = c;2215 return true;2216 }2217 }2218 2219 //////////////////////////////////////////////////////////////////////////2220 // Little Endian - Intel coding2221 // ok2222 inline bool iDecodeToBool(const unsigned char * data, const unsigned int startBit)2223 {2224 mDecodeToBool(data, startBit);2225 }2226 2227 //////////////////////////////////////////////////////////////////////////2228 // Little Endian - Intel coding2229 // pas de depassement possible - la trame CAN fait 64 bits max2230 // gerer le signe2231 inline bool iDecodeToUI64(unsigned long long * /*result*/, const unsigned char * /*data*/, const unsigned int /*startBit*/, const unsigned int /*length*/)2232 {2233 return true;2234 }2235 2236 //////////////////////////////////////////////////////////////////////////2237 // Little Endian - Intel coding2238 // pas de depassement possible - la trame CAN fait 64 bits max2239 // gerer le signe2240 inline bool iDecodeToI64(long long * /*result*/, const unsigned char * /*data*/, const unsigned int /*startBit*/, const unsigned int /*length*/)2241 {2242 return true;2243 }2244 2245 //////////////////////////////////////////////////////////////////////////2246 // Little Endian - Intel coding2247 // gerer le depassement sur plus de 4 octets : necessite la fonction de decodage 64 bits2248 // verifier le depassement avant cette ligne : if (length == 32)2249 inline bool iDecodeToUI32(unsigned long * result, const unsigned char * data, const unsigned int startBit, const unsigned int length)2250 {2251 if (length>32) {2252 *result = 0;2253 return false;2254 }2255 2256 if (length == 32) {2257 // only work if length == 322258 unsigned char c1, c2, c3, c4;2259 c4 = data[startBit>>3]; // LSB2260 c3 = data[(startBit>>3) + 1];2261 c2 = data[(startBit>>3) + 2];2262 c1 = data[(startBit>>3) + 3]; // MSB2263 2264 *result = (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;2265 return true;2266 } else {2267 // todo2268 *result = 0;2269 return false;2270 }2271 }2272 2273 //////////////////////////////////////////////////////////////////////////2274 // Little Endian - Intel coding2275 // gerer le signe2276 // gerer le depassement sur plus de 4 octets : necessite la fonction de decodage 64 bits2277 // verifier le depassement avant cette ligne : if (length == 32)2278 inline bool iDecodeToI32(long * result, const unsigned char * data, const unsigned int startBit, const unsigned int length)2279 {2280 if (length>32) {2281 *result = 0;2282 return false;2283 }2284 2285 if (length == 32) {2286 // only work if length == 322287 unsigned char c1, c2, c3, c4;2288 c4 = data[startBit>>3]; // LSB2289 c3 = data[(startBit>>3) + 1];2290 c2 = data[(startBit>>3) + 2];2291 c1 = data[(startBit>>3) + 3]; // MSB2292 2293 *result = (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;2294 return true;2295 } else {2296 // todo2297 *result = 0;2298 return false;2299 }2300 }2301 2302 //////////////////////////////////////////////////////////////////////////2303 // Little Endian - Intel coding2304 // gerer le depassement sur plus de 2 octets2305 // verifier le depassement avant cette ligne : if (length == 16)2306 inline bool iDecodeToUI16(unsigned short * result, const unsigned char * data, const unsigned int startBit, const unsigned int length)2307 {2308 if (length > 16) {2309 *result = 0;2310 return false;2311 }2312 2313 if (length == 16) {2314 // only work if length == 162315 unsigned char LSB, MSB;2316 LSB = data[startBit>>3];2317 MSB = data[(startBit>>3) + 1];2318 2319 *result = (MSB << 8) + LSB;2320 return true;2321 } else {2322 // TODO2323 *result = 0;2324 return false;2325 }2326 }2327 2328 //////////////////////////////////////////////////////////////////////////2329 // Little Endian - Intel coding2330 // gerer le signe2331 // gerer le depassement sur plus de 2 octets2332 // verifier le depassement avant cette ligne : if (length == 16)2333 // manque le decalage2334 inline bool iDecodeToI16(short * result, const unsigned char * data, const unsigned int startBit, const unsigned int length)2335 {2336 if (length > 16) {2337 *result = 0;2338 return false;2339 }2340 2341 if (length == 16) {2342 // only work if length == 162343 unsigned char LSB, MSB;2344 LSB = data[startBit>>3];2345 MSB = data[(startBit>>3) + 1];2346 2347 *result = (MSB << 8) + LSB;2348 return true;2349 } else {2350 // todo2351 *result = 0;2352 return false;2353 }2354 }2355 2356 //////////////////////////////////////////////////////////////////////////2357 // Little Endian - Intel coding2358 // OK2359 inline bool iDecodeToUI8(unsigned char * result,const unsigned char * data, const unsigned int startBit, const unsigned int length)2360 {2361 if (length > 8) {2362 *result = 0;2363 return false;2364 }2365 2366 // verify that the frame is not spread in 2 bytes2367 if (iSpreadOnMoreThan1Byte(startBit, length)) {2368 // decode in a 16-bit integer2369 unsigned short temp;2370 if ( iDecodeToUI16(&temp, data, startBit, length) ) {2371 // and cast in an 8 bit integer2372 *result = (unsigned char) temp;2373 return true;2374 } else {2375 *result = 0;2376 return true;2377 }2378 } else {2379 // ok data is stored at most in 1 byte2380 unsigned char c;2381 c = data[startBit>>3];2382 c >>= (startBit & 0x07);2383 *result = c & getMask8( length );2384 2385 return true;2386 }2387 }2388 2389 //////////////////////////////////////////////////////////////////////////2390 // Little Endian - Intel coding2391 // OK2392 inline bool iDecodeToI8(char * result, const unsigned char * data, const unsigned int startBit, const unsigned int length)2393 {2394 if (length > 8) {2395 *result = 0;2396 return false;2397 }2398 2399 // verify that the frame is not spread in 2 bytes2400 if (iSpreadOnMoreThan1Byte(startBit, length)) {2401 // decode in a 16-bit integer2402 short temp;2403 if ( iDecodeToI16(&temp, data, startBit, length) ) {2404 // and cast in an 8 bit integer2405 *result = static_cast<char>(temp);2406 return true;2407 } else {2408 *result = 0;2409 return true;2410 }2411 } else {2412 // ok data is stored at most in 1 byte2413 char c;2414 c = data[startBit>>3]; // >>3 <=> div 82415 c >>= (startBit & 0x07); // &0x07 <=> modulo 82416 c = c & getMask8( length );2417 if (c & getShiftMask8(length-1,1))2418 // do the two's complement to get the signed value if the msb=12419 *result = - ( ( (~c) + 1 ) & getMask8(length) );2420 else2421 *result = c;2422 2423 return true;2424 }2425 }2426 2427 //////////////////////////////////////////////////////////////////////////2428 // Big Endian - Motorola coding2429 // ok2430 inline bool mobileyemDecodeToUI16(unsigned short * result, const unsigned char * data, const unsigned int startBit, const unsigned int length)2431 {2432 if (length>16){ *result = 0; return false; }2433 2434 // verify that the frame is not spread in 3 bytes2435 /* if (mSpreadOnMoreThan2Bytes(startBit, length))2436 {2437 // decode in a 32-bits integer2438 unsigned long temp;2439 if (mDecodeToUI32(&temp,data,startBit,length))2440 {2441 *result = static_cast<unsigned short>(temp);2442 return true;2443 } else {2444 *result = 0;2445 return false;2446 }2447 }2448 else2449 {*/2450 if (length == 16) {2451 // only work if data are correctly byte-aligned2452 unsigned char LSB, MSB;2453 MSB = data[startBit>>3];2454 LSB = data[(startBit>>3) + 1];2455 2456 *result = ( (MSB << 8) + LSB );2457 return true;2458 } else {2459 unsigned char LSB, MSB, maskMSB, maskLSB, nbBitsMSB, nbBitsLSB;2460 nbBitsMSB = (startBit & 0x07) + 1; // the number of bits contained in the MSB2461 nbBitsLSB = static_cast<unsigned char>(length - nbBitsMSB);2462 maskMSB = getMask8( nbBitsMSB );2463 maskLSB = getShiftMask8(8 - nbBitsLSB , nbBitsLSB);2464 MSB = data[startBit>>3] & maskMSB;2465 LSB = data[(startBit>>3) + 1] & maskLSB;2466 2467 *result = ( (MSB << 8) + LSB ) >> (8 + (startBit&0x07) + 1 - length);2468 return true;2469 }2470 //}2471 }2472 2473 //////////////////////////////////////////////////////////////////////////2474 // Big Endian - Motorola coding2475 // ok2476 inline bool mobileyemDecodeToI16(short *result, const unsigned char * data, const unsigned int startBit, const unsigned int length)2477 {2478 if (length>16) { *result = 0; return false; }2479 2480 // verify that the frame is not spread in 3 bytes2481 /* if (mSpreadOnMoreThan2Bytes(startBit, length))2482 {2483 // decode in a 32-bits integer2484 long temp;2485 if (mDecodeToI32(&temp, data, startBit, length)) {2486 *result = (short)temp;2487 return true;2488 } else {2489 *result = 0;2490 return false;2491 }2492 }2493 else // ok data are stored at most in 2 bytes2494 {*/2495 if (length == 16)2496 {2497 // only work if data are correctly byte-aligned2498 unsigned char LSB, MSB;2499 MSB = data[startBit>>3];2500 LSB = data[(startBit>>3) + 1];2501 short temp = (MSB << 8) + LSB;2502 2503 // Attention : a-t on besoin d'appliquer le signe ?2504 // n'est-il pas deja inclu dans la donn�e comme elle est correctement align�e sur l'octet ?2505 if (temp & 0x8000) {2506 // do the two's complement to get the signed value2507 *result = - ( ( (~temp) + 1 ) & 0xFFFF );2508 } else {2509 *result = temp;2510 }2511 return true;2512 } else {2513 unsigned char LSB, MSB, maskMSB, maskLSB, nbBitsMSB, nbBitsLSB;2514 nbBitsMSB = (startBit & 0x07)+1; // the number of bits contained in the MSB2515 nbBitsLSB = static_cast<unsigned char>(length - nbBitsMSB);2516 maskMSB = getMask8( nbBitsMSB );2517 maskLSB = getShiftMask8(8 - nbBitsLSB , nbBitsLSB);2518 MSB = data[startBit>>3] & maskMSB;2519 LSB = data[(startBit>>3) + 1] & maskLSB;2520 // assign the MSB and LSB char in the short integer value and right-shift2521 // to place the lsb to the bit 02522 unsigned short temp = ( (MSB << 8) + LSB ) >> (8 + (startBit&0x07) + 1 - length);2523 if (temp & getShiftMask16(length-1,1))2524 // do the two's complement to get the signed value if the msb=12525 *result = -1 * ( ( (~temp) + 1 ) & getMask16(length) );2526 else2527 *result = temp;2528 return true;2529 }2530 //}2531 }2532 2533 //////////////////////////////////////////////////////////////////////////2534 // Big Endian - Motorola coding2535 // OK2536 inline bool mobileyemDecodeToUI8(unsigned char * result, const unsigned char * data, const unsigned int startBit, const unsigned int length)2537 {2538 if (length>8) { *result = 0; return false; }2539 2540 // verify that the frame is not spread in 2 bytes2541 if (mSpreadOnMoreThan1Byte(startBit, length))2542 {2543 // decode in a 16-bit integer2544 unsigned short temp;2545 if ( mDecodeToUI16(&temp, data, startBit, length) ) {2546 *result = static_cast<unsigned char>(temp);2547 return true;2548 } else {2549 *result = 0;2550 return true;2551 }2552 }2553 else // ok data is stored at most in 1 byte2554 {2555 unsigned char c;2556 c = data[startBit>>3]; // >>3 <=> div 82557 c >>= (8-((startBit & 0x07)+1)); // &0x07 <=> modulo 82558 *result = c & getMask8( length );2559 2560 return true;2561 }2562 }2563 2564 //////////////////////////////////////////////////////////////////////////2565 // Big Endian - Motorola coding2566 // OK2567 inline bool mobileyemDecodeToI8(char * result, const unsigned char * data, const unsigned int startBit, const unsigned int length)2568 {2569 if (length>8) { *result = 0; return false; }2570 2571 // verify that the frame is not spread in 2 bytes2572 if (mSpreadOnMoreThan1Byte(startBit, length))2573 {2574 // decode in a 16-bit integer2575 short temp;2576 if ( mDecodeToI16(&temp, data, startBit, length) ) {2577 *result = static_cast<char>(temp);2578 return true;2579 } else {2580 *result = 0;2581 return true;2582 }2583 }2584 else // ok data is stored at most in 1 byte2585 {2586 char c;2587 c = data[startBit>>3];2588 c >>= (8-((startBit & 0x07)+1));2589 c = c & getMask8( length );2590 2591 // a-t-on besoin d'appliquer le signe dans le cas ou length = 82592 // la donnee est correctement alignee sur l'octet donc le signe est peut-etre compris2593 if (c & getShiftMask8(length-1,1))2594 // do the two's complement to get the signed value if the msb=12595 *result = - ( ( (~c) + 1 ) & getMask8(length) );2596 else2597 *result = c;2598 return true;2599 }2600 }2601 2602 //////////////////////////////////////////////////////////////////////////2603 // Little Endian - Intel coding2604 // OK, mais gerer le depassement sur plus de 2 octets2605 inline bool mobileyeiDecodeToUI16(unsigned short * result, const unsigned char * data, const unsigned int startBit, const unsigned int length)2606 {2607 if (length>16) {2608 *result = 0;2609 return false;2610 }2611 2612 if (length == 16) {2613 // only work if length == 162614 unsigned char LSB, MSB;2615 LSB = data[startBit>>3];2616 MSB = data[(startBit>>3) + 1];2617 2618 *result = (unsigned short)((MSB << 8) + LSB);2619 return true;2620 } else {2621 unsigned char LSB, MSB, maskMSB, maskLSB, nbBitsMSB, nbBitsLSB;2622 2623 nbBitsLSB = static_cast<unsigned char>(8*((startBit>>3) + 1) - startBit);2624 nbBitsMSB = static_cast<unsigned char>(length - nbBitsLSB);2625 maskLSB = getShiftMask8(8 - nbBitsLSB , nbBitsLSB);2626 maskMSB = getMask8( nbBitsMSB );2627 LSB = data[startBit>>3] & maskLSB;2628 MSB = data[(startBit>>3) + 1] & maskMSB;2629 2630 *result = (unsigned short)((MSB<<8) + (LSB>>(startBit-8*(startBit>>3))));2631 2632 return true;2633 }2634 }2635 2636 //////////////////////////////////////////////////////////////////////////2637 // Little Endian - Intel coding2638 // gerer le signe2639 // gerer le depassement sur plus de 2 octets2640 // verifier le depassement avant cette ligne : if (length == 16)2641 // manque le decalage2642 inline bool mobileyeiDecodeToI16(short * result, const unsigned char * data, const unsigned int startBit, const unsigned int length)2643 {2644 if (length > 16) {2645 *result = 0;2646 return false;2647 }2648 2649 if (length == 16) {2650 // only work if length == 162651 unsigned char LSB, MSB;2652 LSB = data[startBit>>3];2653 MSB = data[(startBit>>3) + 1];2654 2655 short temp = (MSB << 8) + LSB;2656 2657 if (temp & 0x8000) {2658 // do the two's complement to get the signed value2659 *result = - ( ( (~temp) + 1 ) & 0xFFFF );2660 } else {2661 *result = temp;2662 }2663 return true;2664 } else {2665 unsigned char LSB, MSB, maskMSB, maskLSB, nbBitsMSB, nbBitsLSB;2666 2667 nbBitsLSB = static_cast<unsigned char>(8*((startBit>>3) + 1) - startBit);2668 nbBitsMSB = static_cast<unsigned char>(length - nbBitsLSB);2669 maskLSB = getShiftMask8(8 - nbBitsLSB , nbBitsLSB);2670 maskMSB = getMask8( nbBitsMSB );2671 LSB = data[startBit>>3] & maskLSB;2672 MSB = data[(startBit>>3) + 1] & maskMSB;2673 2674 unsigned short temp = (unsigned short)((MSB<<8) + (LSB>>(startBit-8*(startBit>>3))));2675 2676 if (temp & getShiftMask16(length-1,1))2677 // do the two's complement to get the signed value if the msb=12678 *result = -1 * ( ( (~temp) + 1 ) & getMask16(length) );2679 else2680 *result = temp;2681 return true;2682 }2683 }2684 2685 //////////////////////////////////////////////////////////////////////////2686 // Little Endian - Intel coding2687 // OK2688 inline bool mobileyeiDecodeToUI8(unsigned char * result,const unsigned char * data, const unsigned int startBit, const unsigned int length)2689 {2690 if (length>8) { *result = 0; return false; }2691 2692 // verify that the frame is not spread in 2 bytes2693 if (iSpreadOnMoreThan1Byte(startBit, length))2694 {2695 // decode in a 16-bit integer2696 unsigned short temp;2697 if ( iDecodeToUI16(&temp, data, startBit, length) )2698 {2699 // and cast in an 8 bit integer2700 *result = (unsigned char) temp;2701 return true;2702 }2703 else2704 {2705 *result = 0;2706 return true;2707 }2708 }2709 else // ok data is stored at most in 1 byte2710 {2711 unsigned char c;2712 c = data[startBit>>3];2713 c >>= (startBit & 0x07);2714 *result = c & getMask8( length );2715 2716 return true;2717 }2718 2719 }2720 2721 //////////////////////////////////////////////////////////////////////////2722 // Little Endian - Intel coding2723 // OK2724 inline bool mobileyeiDecodeToI8(char * result, const unsigned char * data, const unsigned int startBit, const unsigned int length)2725 {2726 if (length > 8) {2727 *result = 0;2728 return false;2729 }2730 2731 // verify that the frame is not spread in 2 bytes2732 if (iSpreadOnMoreThan1Byte(startBit, length)) {2733 // decode in a 16-bit integer2734 short temp;2735 if ( iDecodeToI16(&temp, data, startBit, length) ) {2736 // and cast in an 8 bit integer2737 *result = (char)(temp);2738 return true;2739 } else {2740 *result = 0;2741 return true;2742 }2743 } else {2744 // ok data is stored at most in 1 byte2745 char c;2746 c = data[startBit>>3]; // >>3 <=> div 82747 c >>= (startBit & 0x07); // &0x07 <=> modulo 82748 c = c & getMask8( length );2749 if (c & getShiftMask8(length-1,1))2750 // do the two's complement to get the signed value if the msb=12751 *result = - ( ( (~c) + 1 ) & getMask8(length) );2752 else2753 *result = c;2754 2755 return true;2756 }2757 }2758 2759 inline int mobileyeDecodeCAN(unsigned char *data,unsigned char bigEndian,unsigned int startBit,unsigned int length,unsigned char Signed)2760 {2761 int value;2762 unsigned short ustemp16 = 0;2763 unsigned char uctemp8 = 0;2764 short stemp16 = 0;2765 char ctemp8 = 0;2766 2767 if(bigEndian) {2768 if(length>8){2769 if(Signed){mobileyemDecodeToI16(&stemp16,data, startBit, length);value = stemp16;}2770 else {mobileyemDecodeToUI16(&ustemp16,data, startBit, length);value = ustemp16;}}2771 else if(Signed) {mobileyemDecodeToI8(&ctemp8,data, startBit, length);value = ctemp8;}2772 else {mobileyemDecodeToUI8(&uctemp8,data, startBit, length);value = uctemp8;}2773 } else {2774 if(length>8){2775 if(Signed) {mobileyeiDecodeToI16(&stemp16,data, startBit, length);value = stemp16;}2776 else {mobileyeiDecodeToUI16(&ustemp16,data, startBit, length);value = ustemp16;}}2777 else if(Signed) {mobileyeiDecodeToI8(&ctemp8,data, startBit, length);value = ctemp8;}2778 else {mobileyeiDecodeToUI8(&uctemp8,data, startBit, length);value = uctemp8;}2779 }2780 2781 return value;2782 }2783 2784 #ifdef __cplusplus2785 }2786 #endif // __cplusplus2787 2788 #endif // BINARYDECODER_H2789 >>>>>>> .r303
Note:
See TracChangeset
for help on using the changeset viewer.