miguel747

Secret-KnockLock.h

Dec 16th, 2013
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.03 KB | None | 0 0
  1. /*
  2.  * secret_knock_lib.c
  3.  *
  4.  *  Created on: 03/12/2013
  5.  *      Author: Filipe Ribeiro
  6.  */
  7.  
  8. #include "secret_knock_lib.h"
  9.  
  10. unsigned char recordMode;
  11. unsigned char stupidUser;
  12. unsigned long int elapseTime;
  13. unsigned long int knockBuffer[KNOCK_MAX_SEQ];
  14. unsigned long int passwordBuffer[KNOCK_MAX_SEQ];
  15. unsigned char passwordLength;
  16. unsigned char knockIndex;
  17.  
  18. /*
  19.  *  portInit
  20.  *
  21.  *  Initialize ports
  22.  *
  23.  */
  24.  
  25. void portInit(void)
  26. {
  27.     //  Port 1
  28.     P1DIR = PORT1_DIR;
  29.     P1REN = PUSH_BUTTON;
  30.     P1OUT = PUSH_BUTTON;
  31.     P1IE = PUSH_BUTTON + PIEZO_PIN;
  32.     P1IES = PUSH_BUTTON;
  33.     P1IFG = 0x00;
  34.  
  35.     //  Port 2
  36.     P2DIR = PORT2_DIR;
  37. }
  38.  
  39. /*
  40.  *  timerAInit
  41.  *
  42.  *  Initialize timer to work in up mode with SMCLK clock source
  43.  *
  44.  */
  45.  
  46. void timerAInit(unsigned char timerAMask)
  47. {
  48.  
  49.     if(timerAMask & TIMER_A_0)
  50.     {
  51.         TA0CCR0 = 1000;
  52.         TA0CTL = TASSEL_2+MC_1;
  53.     }
  54.  
  55.     if(timerAMask & TIMER_A_1)
  56.     {
  57.         TA1CCTL0 = CCIE;
  58.         TA1CCR0 = 1000;
  59.         TA1CTL = TASSEL_2+MC_1;
  60.     }
  61. }
  62.  
  63. /*
  64.  *  knockReset
  65.  *
  66.  *  Reset knockIndex and clear knockBuffer.
  67.  *
  68.  */
  69.  
  70. void knockReset(void){
  71.  
  72.     unsigned char i;
  73.  
  74.     knockIndex = 0;
  75.  
  76.     for(i=0;i<KNOCK_MAX_SEQ;i++)
  77.         knockBuffer[i]=0;
  78. }
  79.  
  80. /*
  81.  *  knockNormalize
  82.  *
  83.  *  In order to compare sequences with different frequencies all sequences
  84.  *  are normalized before storing new passwords and comparing to the existing
  85.  *  one.
  86.  *
  87.  *  Normalization is done by dividing all intervals by the highest-valued one.
  88.  *  Then, all values are multiplied by a standard constant so calculations
  89.  *  can be performed only using integers.
  90.  *
  91.  */
  92.  
  93. void knockNormalize(void)
  94. {
  95.     unsigned char i;
  96.     unsigned long int maxInterval = 0;
  97.  
  98.     //  Find maximum interval
  99.     for(i=1;i<knockIndex;i++)
  100.         if(knockBuffer[i]>maxInterval)
  101.             maxInterval=knockBuffer[i];
  102.     if(maxInterval == 0)
  103.         return ;
  104.     //  Normalize intervals and record secret knock
  105.     for(i=1;i<knockIndex;i++){
  106.         knockBuffer[i-1] = knockBuffer[i]*KNOCK_NORM_INTERVAL;
  107.         knockBuffer[i-1] /= maxInterval;
  108.     }
  109.     knockIndex--;
  110. }
  111.  
  112. /*
  113.  *  passwordRecord
  114.  *
  115.  *  Store the normalized sequence in the password vector
  116.  *
  117.  */
  118.  
  119. void passwordRecord(void)
  120. {
  121.     unsigned char i;
  122.     knockNormalize();
  123.     for(i=0;i<knockIndex;i++)
  124.         passwordBuffer[i] = knockBuffer[i];
  125.     passwordLength = knockIndex;
  126. }
  127.  
  128. /*
  129.  *  passwordCheck
  130.  *
  131.  *  Verify if the sequence tried is the password
  132.  *  Both sequence and password are already normalized, so two types of
  133.  *  inaccuracy are verified:
  134.  *      1 knockError: The difference between each knock of the sequence and the
  135.  *      corresponding knock in the password. Errors cannot exceed the threshold
  136.  *      (KNOCK_TOLERANCE).
  137.  *      2 totalError: The sum of all knock errors. totalError cannot exceed the
  138.  *      threshold (TOTAL_TOLERANCE).
  139.  *
  140.  */
  141.  
  142. unsigned char passwordCheck(void)
  143. {
  144.     unsigned char i;
  145.     unsigned long int knockError;
  146.     unsigned int totalError;
  147.     int t;
  148.  
  149.     knockNormalize();
  150.  
  151.     if(knockIndex != passwordLength)
  152.         return KNOCK_NOT_OK;
  153.  
  154.     totalError = 0;
  155.  
  156.     //  Validate knock
  157.     for(i=0;i<passwordLength;i++)
  158.     {
  159.         if(knockBuffer[i] > passwordBuffer[i])
  160.             knockError = knockBuffer[i]-passwordBuffer[i];
  161.         else
  162.             knockError = passwordBuffer[i] - knockBuffer[i];
  163.  
  164.         if(knockError> KNOCK_TOLERANCE)
  165.             return KNOCK_NOT_OK;
  166.  
  167.         totalError += knockError;
  168.     }
  169.  
  170.     //  In case this if statement is ignored alter the optimization configurations: project -> properties -> build -> msp430 compiler -> optimization -> optimization level = 1
  171.     if(totalError > TOTAL_TOLERANCE)
  172.         return KNOCK_NOT_OK;
  173.  
  174.     return KNOCK_OK;
  175. }
  176.  
  177. /*
  178.  *  knockInit
  179.  *
  180.  *  Reset all program flags and password buffer
  181.  *  Initialize ports and timer
  182.  *
  183.  */
  184.  
  185. void knockInit(void)
  186. {
  187.     unsigned char i;
  188.     //  Reset record mode
  189.     recordMode = RECORD_MODE_OFF;
  190.     //  Reset elapse time counter
  191.     elapseTime = 0;
  192.     //  Set index to beginning of the buffer
  193.     knockIndex = 0;
  194.     //  Reset blocking flag
  195.     stupidUser = 0;
  196.     //  Reset knock code
  197.     for(i=0;i<KNOCK_MAX_SEQ;i++)
  198.         passwordBuffer[i] = 1;
  199.     //  Reset knock code length
  200.     passwordLength = KNOCK_MAX_SEQ;
  201.  
  202.     portInit();
  203.     timerAInit(TIMER_A_1);
  204. }
Advertisement
Add Comment
Please, Sign In to add comment