summaryrefslogtreecommitdiffstats
path: root/arduino/src/pc.cpp
blob: 5c5841c82c010d63f0006e023449cbad6dbe38c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <Arduino.h>
#include <EEPROM.h>

#include "config.h"
#include "pc.h"
#include "emem.h"

/** Global vatiable that holds last message from PC. Lenght is plus one for zero termination. */
char pc_bytes[PC_MAX_BYTES+1];
/** Flag set when next scan should send mid. */
boolean pc_send_flag = false;

// from doorman.ino
extern unsigned char global_hash[];

/**
 * Function receives bytes from PC and parses them into message.
 * @return Returns true if message was received, false otherwise.
 */
boolean pc_parse(){
  static int pc_idx=0;
  char in = 0;	// for incoming serial data
  // send data only when you receive data:
  while (Serial.available() > 0) {
    //read the incoming byte:
    in = Serial.read();
    if (in=='$') {
      //Start
      pc_idx=0;
      pc_bytes[pc_idx]=in; 
      continue;
    } 
    if (in=='\n'){
#ifdef DEBUG      
      Serial.print("Got newline ");
      Serial.println(pc_idx);
#endif //DEBUG
      // what the fuck?
      if (pc_idx==PC_MAX_BYTES-2) {;
        pc_bytes[pc_idx+1]=in;
        pc_idx=0;
        return true;
      } else {
        pc_idx=0;
        continue;
      }
    }
    if (pc_idx<PC_MAX_BYTES-2){
      pc_idx+=1;
      pc_bytes[pc_idx]=in;
      continue;
    } else{
      pc_idx=0;
      continue;
    }     
  }
  return false;
}

/**
 * Prints hash given as blob of data.
 * Hash length is defined as EMEM_HASH_SIZE.
 * @param hash Given hash.
 */
void pc_print_hash(uint8_t* hash) {
  for (int ii=0; ii<EMEM_HASH_SIZE; ii++) {
    Serial.print("0123456789abcdef"[hash[ii]>>4]);
    Serial.print("0123456789abcdef"[hash[ii]&0xf]);
  }
}

/**
 * Sends message to PC.
 * @param code Message code.
 * @param data Message data.
 * @param id Message id.
 */
void pc_print(char code,unsigned int id,unsigned char * data) {
  unsigned long token = 0;
  unsigned int pin = 0;
  char buf[12];
  snprintf(buf,12,"$%c,%04x,",code,id);
  Serial.print(buf);
  pc_print_hash(data);
  snprintf(buf,12,",%08lx\n",token);
  Serial.print(buf); 
}

/**
 * Handles communication with PC.
 */
void pc_comm(){  
  if (pc_parse()){
#ifdef DEBUG    
    Serial.print("I received: ");
    for (int ii=0;ii<PC_MAX_BYTES;ii++) {
      Serial.print(pc_bytes[ii], HEX);
    }
#endif //DEBUG
    pc_bytes[PC_MAX_BYTES]=0;
    char code = 0;
    unsigned int id = 0;

    for (int ii=0;ii<EMEM_HASH_SIZE;ii++){
      global_hash[ii]=0;
    }
    unsigned long token = 0;
 
    //$a,0000,dfdd69691a7af8c141cef1ce69b5196b327f71f40b54da3c4e673283dcebd9b7,386855c3
    code=pc_bytes[1];
    char ids[5];
    for (int ii=0;ii<4;ii++){
      ids[ii]=pc_bytes[ii+3];
    }
    ids[4]='\0';
    sscanf(ids,"%d",&id);
    for (int ii=0;ii<32;ii++){
      char hex[3];
      hex[0]= pc_bytes[ii*2+8];
      hex[1]= pc_bytes[ii*2+8+1];
      hex[2]= 0;
      sscanf(hex,"%02x",global_hash+ii);
    }
    sscanf(pc_bytes+32*2+8+1,"%8lx",&token);

      
#ifdef DEBUG    
    Serial.print("CODE: ");
    Serial.print(code, HEX);
    Serial.print(", ID: ");
    Serial.print(id, HEX);
    Serial.print(", HASH: ");
    for (int ii=0; ii<EMEM_HASH_SIZE; ii++){
      Serial.print(global_hash[ii], HEX);
      Serial.print(',');
    }  
    Serial.print(" TOKEN: ");    
    Serial.print(token>>24 & 0x0ff, HEX);    
    Serial.print(',');
    Serial.print(token>>16 & 0x0ff, HEX);    
    Serial.print(',');
    Serial.print(token>>8 & 0x0ff, HEX);
    Serial.print(',');
    Serial.print(token & 0x0ff, HEX);
    Serial.println();    
    
          
#endif //DEBUG
    if (0){
#ifdef DEBUG    
      Serial.println("WRONG TOKEN");
#endif //DEBUG
      pc_print('E', id, global_hash);  
    } else if (pc_bytes[1]=='A' || pc_bytes[1]=='a'){
      //Add
      if (id == 0)
      {
        id = emem_find_free();
      }
      if (emem_find_data(global_hash)!=EMEM_INV_ADR){
        //Already have this data
#ifdef DEBUG
        Serial.println("ADD FAILED, USER EXISTS");
#endif 
        pc_print('E',id,global_hash);
      } else if (emem_set_record(id,global_hash)){
#ifdef DEBUG    
        Serial.println("ADD DONE");
#endif //DEBUG
        pc_print('C',id,global_hash);  
      } else {
#ifdef DEBUG    
        Serial.println("ADD FAILED");
#endif //DEBUG
        pc_print('E',id,global_hash);  
      }
    } else if (pc_bytes[1]=='R' || pc_bytes[1]=='r'){
//        //Revoke
      if (id==0) id=emem_find_data(global_hash);
      if (emem_del_record(id)){
#ifdef DEBUG    
        Serial.println("REVOKE DONE");  
#endif //DEBUG    
        pc_print('K',id,global_hash);
      } else {
#ifdef DEBUG    
        Serial.println("REVOKE FAILED");
#endif //DEBUG    
        pc_print('E',id,global_hash);
      }
    } else if (pc_bytes[1]=='Z' || pc_bytes[1]=='z'){
      //Zero eeprom       
      Serial.println("ZERO");
      for (int ii=0;ii<1024;ii++){
        EEPROM.write(ii,0);
        if (ii%10==0) {
          Serial.print(".");
        }
      }
      Serial.println();
      Serial.print("DONE");
      Serial.println();      
    } else if (pc_bytes[1]=='P' || pc_bytes[1]=='p'){
      emem_print(); 
    } else if (pc_bytes[1]=='G' || pc_bytes[1]=='g'){
       pc_send_flag=true; 
    } else {
#ifdef DEBUG      
      Serial.println("Unknown command");
#endif //DEBUG      
    }
  }
}