Advertisement
Guest User

Untitled

a guest
May 27th, 2015
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.50 KB | None | 0 0
  1. /*
  2.  * DatakeyToken.h
  3.  *
  4.  *  Created on: Mar 15, 2014
  5.  *      Author: swindle
  6.  */
  7.  
  8. #ifndef DATAKEYTOKEN_H_
  9. #define DATAKEYTOKEN_H_
  10.  
  11. #include <iostream>
  12. #include <vector>
  13. #include <string>
  14. #include <cstdio>
  15. #include <linux/i2c-dev.h>
  16. #include <sys/ioctl.h>
  17. #include <fcntl.h>
  18. #include <cstring>
  19. #include <unistd.h>
  20. #include <stdint.h>
  21. #include <ctype.h>
  22. #include <iomanip>
  23. #include <iterator>
  24. #include <algorithm>
  25. #include <sstream>
  26. #include <fstream>
  27.  
  28. //#define MAX_BUS 0x1FFF
  29.  
  30. ///////////////////////////////////////////////////////////////////////////
  31. //
  32. //  Preprocessor Defines :
  33. //  The first 2 of these can be found by using i2ctools installed
  34. //  in most linux distributions.  Using "i2cdetect -l" will list
  35. //  the buses available for use on the system. "i2cdetect -y -r 1"
  36. //  ( where 1 is the bus where your device is located) will give you
  37. //  the address of the device. Using the output from "i2cdetect -y -r 1"
  38. //  we can see that we have a device in 0x50 below. This happens to be
  39. //  the token and this should match the information provided on the data
  40. //  sheet, but here we have conformation that the device is present.
  41. //
  42. //       0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
  43. //   00:          -- -- -- -- -- -- -- -- -- -- -- -- --
  44. //   10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  45. //   20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  46. //   30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  47. //   40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  48. //   50: 50 -- -- -- UU UU UU UU -- -- -- -- -- -- -- --
  49. //   60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  50. //   70: -- -- -- -- -- -- -- --
  51. //
  52. //  I2C_BUS    the bus which the token is placed confirmed by i2cdetect.
  53. //  TOKEN_BUS_ADDRESS   memory address of token.  This is on the data sheet
  54. //              but can also be deduced via i2cdetect.
  55. //
  56. ////////////////////////////////////////////////////////////////////////////
  57.  
  58. #define I2C_BUS 1
  59. #define TOKEN_BUS_ADDRESS 0x50
  60.  
  61. ////////////////////////////////////////////////////////////////////////////
  62. //
  63. //   The following defines are relevent only to the specific token type
  64. //   the datakey token ISK.  And can be found in the datakey specifications
  65. //   sheet.  Hex is easier to use for all values instead of mixing them up,
  66. //   at least for me).
  67. //
  68. //   TOKEN_I2C_BUFFER   is the page size of the line (e.g. 32 characters for 0x20)
  69. //   TOKEN_MIN_RANGE    is the start address of where data can be placed.  Factoid:
  70. //                      Some I2C devices can start at higer ranges.
  71. //   TOKEN_MAX_RANGE    is the max range of the token and the last available storage
  72. //                      byte.
  73. //   TOKEN_MAX_PAGE     is defined by dividing the TOKEN_MAX_RANGE / TOKEN_I2C_BUFFER
  74. //                      all loops based on this number must start begin at 0 for full
  75. //                      range.
  76. //   TOKEN_PROGRESS     is the percentage of 1 action over the TOKEN_MAX_RANGE
  77. //
  78. //
  79. //
  80. //////////////////////////////////////////////////////////////////////////////
  81.  
  82. #define TOKEN_I2C_BUFFER    0x20
  83. #define TOKEN_MIN_RANGE     0x0000
  84. #define TOKEN_MAX_RANGE     0x1FFF
  85. #define TOKEN_MAX_PAGE      (  TOKEN_MAX_RANGE / TOKEN_I2C_BUFFER  )
  86. #define TOKEN_PROGRESS      (float)( 1.0 / TOKEN_MAX_RANGE )
  87.  
  88. using namespace std;
  89.  
  90. class DatakeyToken {
  91. public:
  92.     DatakeyToken();
  93.     virtual ~DatakeyToken();
  94.  
  95.     ///////////////////////////////////////////////////////////////////////////////////
  96.     //
  97.     //   The DatakeToken header is arranged to give user access to controllable members
  98.     //   but keep the workhorse members private. Example: the readTheToken member
  99.     //   can control 3 diffrent private members; tokenDisplayAll(), tokenDisplayLine(),
  100.     //   and tokenDisplayPrintable(). This allows for the minimum of public members.
  101.     //   Greater detail is in cpp file.
  102.     //
  103.     ///////////////////////////////////////////////////////////////////////////////////
  104.  
  105.  
  106.     int readTheToken( char , unsigned int );        //  displays data from token
  107.     int formatTheToken( bool );                     //  formats the token with 0xff
  108.     int writeToToken( char*, char, unsigned int );  //  writes data to the token
  109.     int eraseLine( unsigned int );                  //  erases a line of the token
  110.     long long int checksumToken();                  //  creates a checksum of the token
  111.     int tokenCopy( int, int );                      //  copies the token to another token
  112.     int copyFileToToken( string );                  //  copies file to token ( inop )
  113.  
  114. private:
  115.  
  116.     ///////////////////////////////////////////////////////////////////////////
  117.     //  defines of private class variables
  118.     ///////////////////////////////////////////////////////////////////////////
  119.  
  120.     int file;                                       // identifies the opened token
  121.     vector<string> tokenVec;                        // vector that all token data is read into
  122.     char dataBuffer[TOKEN_BUS_ADDRESS];             // size of each buffer ( 32 bytes )
  123.  
  124.     ////////////////////////////////////////////////////////////////////////////
  125.     // defines private functions of class. Greater detail of each is in cpp file
  126.     /////////////////////////////////////////////////////////////////////////////
  127.  
  128.     void tokenDisplayAll();                         //  displays all characters in token
  129.     void tokenDisplayLine( unsigned int );          //  displays single line of token
  130.     void tokenDisplayPrintable();                   //  displays only printable characters
  131.     int tokenFormat();                              //  formats the token
  132.     int tokenOpen();                                //  opens the token for read/write
  133.     int tokenRead();                                //  places token data in tokenVec vector
  134.     void tokenClose();                              //  closes the token
  135.     int tokenWrite( char*, char, unsigned int );    //  writes to data to token
  136.  
  137. };
  138.  
  139. #endif /* DATAKEYTOKEN_H_ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement