Advertisement
Guest User

Sketch keys all without bluetooth

a guest
Jan 19th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.59 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2. #include <OneWire.h>
  3. #include <string.h>
  4. #define pin 10                // Pin 10 on Arduino
  5. #define port_speed 9600       // Port speed (bit / sec)
  6. #define led_delay_time 1000   // Delay time using for led blinking
  7. #define _waiting_time 20000    // Default waiting time for the key applying
  8. #define _delay_time 1000       // Default delay time between waiting the key
  9.  
  10. const int STORAGE_SIZE = 5;
  11. struct KeysStorage{
  12.  
  13.   byte key_for_writing[8], last_key[8];
  14.  
  15.   byte storage[STORAGE_SIZE][8];
  16.   String titles[8];
  17.  
  18.   KeysStorage(){
  19.     clear_storage();
  20.   };
  21.  
  22.   bool set(short n, byte key[], String title){
  23.     if (n >= 0 && n < STORAGE_SIZE){
  24.       for (short i = 0; i < 8; i++){
  25.         storage[n][i] = key[i];
  26.       }
  27.       titles[n] = title;
  28.       return true;
  29.     }
  30.     return false;
  31.   };
  32.  
  33.   String get_key_string(short n){
  34.     if (n >= 0 && n < STORAGE_SIZE){
  35.       return "<" + titles[n] + "> (" + key_as_string(storage[n]) + ")";
  36.     }
  37.     return "Error: incorrect number of the key";
  38.   }
  39.  
  40.   void clear_storage(){
  41.     for (short i = 0; i < STORAGE_SIZE; i++){
  42.       for (short j = 0; j < 8; j++){
  43.         storage[i][j] = 0;
  44.       }
  45.       titles[i] = "Unknown";
  46.     }
  47.   }
  48.  
  49.   String str_key = "";
  50.   String key_as_string(byte key[]){
  51.    
  52.     str_key = "";
  53.     // Building key as String
  54.     for (int i = 0; i < 8; i++){
  55.       str_key += String(key[i], HEX) + (i == 7 ? "": ":");
  56.     }
  57.    
  58.     return str_key;
  59.   }
  60.  
  61.   String build_key_string(byte key[]){
  62.  
  63.     String key_title = "Unknown";
  64.     bool is_key_storing;
  65.    
  66.     for (int i = 0; i < STORAGE_SIZE; i++){
  67.      
  68.       is_key_storing = true;
  69.       for (int j = 0; j < 8; j++){
  70.         if (storage[i][j] != key[j])
  71.           is_key_storing = false;
  72.       }
  73.      
  74.       if (is_key_storing){
  75.         key_title = titles[i];
  76.         break;
  77.       }
  78.      
  79.     }
  80.  
  81.     return "<" + key_title + "> (" + key_as_string(key) + ")";
  82.   }
  83.  
  84.   String get_storage_content(){
  85.    
  86.     String result = "";
  87.    
  88.     for (int i = 0; i < STORAGE_SIZE; i++){
  89.       result += ("<" + titles[i] + "> (" + key_as_string(storage[i]) + ")\n");
  90.     }
  91.     return result;// "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; 200/37
  92.   }
  93.  
  94.   bool set_for_writing(short n){
  95.     if (!is_empty(n)){
  96.       for (short i = 0; i < 8; i++){
  97.         key_for_writing[i] = storage[n][i];
  98.       }
  99.       return true;
  100.     }
  101.     return false;
  102.   }
  103.  
  104.   bool is_empty(short n){
  105.     if (n >= 0 && n < STORAGE_SIZE){
  106.       for (short i = 0; i < 8; i++){
  107.         if (storage[n][i] != 0){
  108.           return false;
  109.         }
  110.       }
  111.     }
  112.     return true;
  113.   }
  114.  
  115.   bool is_last_key_in_writing_buffer(){
  116.     for (short i = 0; i < 8; i++){
  117.       if (key_for_writing[i] != last_key[i]) return false;
  118.     }
  119.     return true;
  120.   }
  121.  
  122.   void save_last_to_writing_buffer(){
  123.     for (short i = 0; i < 8; i++){
  124.       key_for_writing[i] = last_key[i];
  125.     }
  126.   }
  127.  
  128. };
  129.  
  130. enum State {
  131.   BASE,
  132.   COPY,
  133.   SET,
  134.   WRITE
  135. };
  136.  
  137. // State
  138. State state = BASE;
  139.  
  140. // Keys
  141. OneWire ibutton (pin);
  142. KeysStorage keys_storage;
  143. String temp_key_description;
  144.  
  145. // LED
  146. const int LED = 13;
  147. bool led_active = true;
  148.  
  149. // IO
  150. char in_bytes[100];
  151. char* p_buffer;
  152. byte bytes_counter;
  153.  
  154.  
  155. bool always_false(){
  156.   return false;
  157. }
  158.  
  159. // Repeat <function> until it returns True, or time is over
  160. template<typename Fun> bool repeat_for(Fun function=always_false, int repeat_time=_waiting_time, int delay_time=_delay_time){
  161.  
  162.   int executing_time = 0;
  163.   bool func_result = false;
  164.  
  165.   while (executing_time <= repeat_time && !(func_result = function())){
  166.  
  167.     // Monitoring serial input for the "reset" command
  168.     if (Serial.available()){
  169.  
  170.       read_input();
  171.       if (String(in_bytes).equalsIgnoreCase("reset")){
  172.         return false;
  173.       }
  174.       if (state == SET){
  175.         // Terminate the function with 'true' state when state == SET
  176.         return true;
  177.       }
  178.     }
  179.  
  180.     // Delay and increase time counter
  181.     delay(delay_time);
  182.     executing_time += delay_time;
  183.   }
  184.  
  185.   return func_result;
  186. }
  187.  
  188. // Create the key from the input buffer and put it to the buffer for writing
  189. void parse_key_from_input_buffer(){
  190.  
  191.   if (!in_bytes) return;
  192.  
  193.   byte hex_ascii[2];
  194.   int current_symbol = 0;
  195.  
  196.   for (int i = 0; i < 8; i++){
  197.     hex_ascii[0] = in_bytes[current_symbol++]; hex_ascii[1] = in_bytes[current_symbol++];
  198.     keys_storage.key_for_writing[i] = ascii_to_byte(hex_ascii, 2, 16);
  199.     current_symbol++;  // skip the delimiter
  200.   }
  201.  
  202. }
  203.  
  204. // Bytes in hex_ascii must be in the direct order
  205. char ascii_to_byte(char ascii[], byte len, byte base){
  206.   char result = 0, c, digit = 0;
  207.   for (int i = 0; i < len; i++){
  208.    
  209.     c = ascii[i];
  210.     if (c >= 48 && c <= 57) digit = c - 48;
  211.     else if (c >= 65 && c <= 70) digit = c - 65 + 0xA;
  212.     else if (c >= 97 && c <= 102) digit = c - 97 + 0xA;
  213.    
  214.     for (int j = 0; j < len - 1 - i; j++){
  215.       digit *= base;
  216.     }
  217. //    digit = digit << (4 * (len - 1 - i)); // shift by corresponding count of bytes
  218.  
  219.     result += digit;
  220.   }
  221.  
  222.   return result;
  223. }
  224. // Arduino functions
  225. //============================================//
  226.  
  227. void setup(){
  228.  
  229.   Serial.begin(port_speed);
  230.   pinMode(LED, OUTPUT);
  231.   digitalWrite(LED, HIGH);
  232.  
  233. }
  234.  
  235. void loop(){
  236.  
  237.   // Read from Bluetooth
  238.   if (Serial.available())
  239.   {
  240.    
  241.     read_input();
  242.     process_input();
  243.  
  244.     reset_state();
  245.   } else {
  246.     standing_led_control();
  247.   }
  248. }
  249.  
  250. void read_input(){
  251.  
  252.   bytes_counter = 0;
  253.  
  254.   while (Serial.available()){
  255.     in_bytes[bytes_counter++] = Serial.read();
  256.     delay(10);
  257.   }
  258.  
  259.   in_bytes[bytes_counter++] = '\0';
  260. //  Serial.println("Buffer content after the reading " + String(bytes_counter) + " bytes: " + String(in_bytes));
  261.  
  262. }
  263.  
  264. void process_input(){
  265.  
  266.   p_buffer = strtok(in_bytes, " ");
  267. //  String last_error = "";
  268.   bool last_error = true;
  269.   String command = String(p_buffer);
  270.  
  271.   if (command.equalsIgnoreCase("reset")){
  272.     reset_state();
  273.     Serial.println("Reseted successfully");
  274.   } else
  275.   if (state == BASE){
  276.    
  277.     if (command.equalsIgnoreCase("copy")){
  278.       last_error = process_copy();
  279.     } else
  280.    
  281.     if (command.equalsIgnoreCase("storage")){
  282.       Serial.println("STORAGE CONTENT:\n" + keys_storage.get_storage_content());
  283.     } else
  284.  
  285.     if (command.equalsIgnoreCase("clear")){
  286.       keys_storage.clear_storage();
  287.       Serial.println("Storage has been cleaned");
  288.     } else
  289.  
  290.     if (command.equalsIgnoreCase("set")){
  291.       last_error = process_set();
  292.     } else
  293.  
  294.     if (command.equalsIgnoreCase("write")){
  295.       last_error = process_write();
  296.     }
  297.  
  298.     if (!last_error){
  299.       Serial.println("ERROR: undescribed error occured");
  300.     }
  301.   }
  302.  
  303.   reset_state();
  304. }
  305.  
  306. void reset_state(){
  307.   state = BASE;
  308. }
  309.  
  310. //char error_incorrect_command_write[100] = "Incorrect call of 'write' command: you should use 'write <number of a cell> <number of copies>'";
  311. bool process_write(){
  312.  
  313.   // Read number of a cell
  314.   p_buffer = strtok(NULL, " ");
  315.   int cell_number = 0, copies_count = 0;
  316.   if (p_buffer && (cell_number = String(p_buffer).toInt()) == 0) {
  317. //    return error_incorrect_command_write;
  318.     return false;
  319.   }
  320.   if (cell_number <= 0 || cell_number > STORAGE_SIZE){
  321. //    return "Incorrect cell number";
  322.     return false;
  323.   }
  324.  
  325.   // Read count of copies
  326.   p_buffer = strtok(NULL, " ");
  327.   if (p_buffer && (copies_count = String(p_buffer).toInt()) == 0) {
  328. //    return error_incorrect_command_write;
  329.     return false;
  330.   }
  331.   if (copies_count < 0){
  332. //    return "Incorrect copies number";
  333.     return false;
  334.   }
  335.   if (!copies_count){
  336.     copies_count++;
  337.   }
  338.  
  339.   // Copy selected key to temp_storage from where it will be writing
  340.   if (!keys_storage.set_for_writing(cell_number - 1)){
  341. //    return "Selected cell #" + String(cell_number) + " is empty.";
  342.     return false;
  343.   }
  344.  
  345.   state = WRITE;
  346.   Serial.println("Start writing " + String(copies_count) + " copies of the key #" + String(cell_number) + " " + keys_storage.get_key_string(cell_number - 1) + "\n");
  347.  
  348.   for (short i = 0; i < copies_count; i++){
  349.     Serial.println("Please, apply #" + String(i + 1) + "-th slave key");
  350.     if (!repeat_for(write_key, _waiting_time, _delay_time)){
  351. //      return String("Failed writing the key #" + String(i + 1));
  352.       return false;
  353.     } else {
  354.       Serial.println("Key " + keys_storage.build_key_string(keys_storage.last_key) + " rewrited");
  355.     }
  356.   }
  357.  
  358. //  return "";
  359.   return true;
  360. }
  361.  
  362. bool process_set(){
  363.  
  364.   // Read number of the cell for setting
  365.   p_buffer = strtok(NULL, " ");
  366.   int cell_number = 0;
  367.   if (p_buffer && (cell_number = String(p_buffer).toInt()) == 0 || !p_buffer) {
  368. //    return "Incorrect call of the 'set' command: you should use 'set <number of the cell>'";
  369.     return false;
  370.   }
  371.   if (cell_number <= 0 || cell_number > STORAGE_SIZE){
  372. //    return "Incorrect cell number";
  373.     return false;
  374.   }
  375.  
  376.   state = SET;
  377.   Serial.println("Please, send the key value in format 'FF:FF:FF:FF:FF:FF:FF:FF'");
  378.  
  379.   // If a key was received
  380.   if (repeat_for(always_false, _waiting_time, _delay_time)){
  381.  
  382.     //Put key to temp_storage
  383.     parse_key_from_input_buffer();
  384.     Serial.println("Now, please, send me short description of the key");
  385.    
  386.     if (repeat_for(always_false, _waiting_time, _delay_time)){
  387.       temp_key_description = String(in_bytes);
  388.      
  389.       if (keys_storage.set(cell_number - 1, keys_storage.key_for_writing, temp_key_description)){
  390.         Serial.println("New value successfully setted up.\nSTORAGE CONTENT:\n" + keys_storage.get_storage_content());
  391.       } else {
  392. //        return "Incorrect cell number";
  393.         return false;
  394.       }
  395.     } else {
  396. //      return "Waiting time for the description is exceeded";
  397.       return false;
  398.     }
  399.    
  400.   } else {
  401. //    return "Waiting time for the key is exceeded";
  402.     return false;
  403.   }
  404.  
  405. //  return "";
  406.   return true;
  407. }
  408.  
  409. bool process_copy(){
  410.  
  411.   p_buffer = strtok(NULL, " ");
  412.   int copy_times = 0;
  413.   if (p_buffer && (copy_times = String(p_buffer).toInt()) == 0) {
  414. //    return "Incorrect call of the copy command: you should use 'copy <number greater than 0>' or 'copy' instead";
  415.     return false;
  416.   }
  417.  
  418.   state = COPY;
  419.   if (copy_times == 0)
  420.     copy_times = 1;
  421.    
  422.   Serial.println("Start of copying " + String(copy_times) + " times. Waiting " + String(_waiting_time / 1000) + " seconds for the master key...");
  423.  
  424.   // Key has been applied
  425.   if (repeat_for(read_key, _waiting_time, _delay_time)){
  426.    
  427.     Serial.println("Master-key " + keys_storage.build_key_string(keys_storage.key_for_writing));
  428.    
  429.     for (int i = 0; i < copy_times; i++){
  430.      
  431.       Serial.println("Please, apply #" + String(i + 1) + "-th slave key");
  432.      
  433.       if (!repeat_for(write_key, _waiting_time, _delay_time)){
  434. //        return String("Failed writing the key #" + String(i + 1));
  435.         return false;
  436.       } else {
  437.         Serial.println("Key " + keys_storage.build_key_string(keys_storage.last_key) + " rewrited");
  438.       }
  439.      
  440.     }
  441.    
  442.   } else {
  443. //    return "Master key wasn't applied in 10 seconds";
  444.     return false;
  445.   }
  446.  
  447. //  return "";
  448.   return true;
  449. }
  450.  
  451. void standing_led_control(){
  452.  
  453.   // Try to read the key
  454.   if (_read_key()){
  455.    
  456.     // Set active LED
  457.     set_led(true);
  458.  
  459.     // Print time and current buffer value
  460.     Serial.println(String(millis()/1000) + "> " + keys_storage.build_key_string(keys_storage.last_key));
  461.    
  462.   } else {
  463.     // Switch LED off and delay
  464.     set_led(false);
  465.   }
  466.  
  467.   delay(500);
  468. }
  469. // Auxiliary functions
  470. //============================================//
  471.  
  472. void set_led(bool state){
  473.   if (state == led_active) return;
  474.  
  475.   if (state){
  476.     digitalWrite(LED, HIGH);
  477.   } else {
  478.     digitalWrite(LED, LOW);
  479.   }
  480.   led_active = state;
  481.  
  482. }
  483.  
  484. bool read_key(){
  485.   bool key_exists = _read_key();
  486.  
  487.   if (key_exists){
  488.     keys_storage.save_last_to_writing_buffer();
  489.     set_led(false);
  490.   } else {
  491.     set_led(!led_active);
  492.   }
  493.  
  494.   return key_exists;
  495. }
  496.  
  497. bool write_key(){
  498.   bool key_exists = _read_key() && (!keys_storage.is_last_key_in_writing_buffer());
  499.  
  500.   if (key_exists){
  501.     _write_key(keys_storage.key_for_writing);
  502.     set_led(false);
  503.   } else {
  504.     set_led(!led_active);
  505.   }
  506.  
  507.   return key_exists;
  508. }
  509.  
  510. bool _read_key(){
  511.  
  512.   bool key_exists = ibutton.search(keys_storage.last_key);
  513.  
  514.   if (!key_exists){
  515.     ibutton.reset_search();
  516.   }
  517.  
  518.   return key_exists;
  519. }
  520.  
  521.  
  522. void _write_key(byte* key){
  523.  
  524.     ibutton.skip(); ibutton.reset(); ibutton.write(0x33);
  525.  
  526.     // Send preparing commands
  527.     ibutton.skip();
  528.     ibutton.reset();
  529.     ibutton.write(0xD1);  // send 0xD1
  530.     digitalWrite(10, LOW); pinMode(10, OUTPUT); delayMicroseconds(60);  // send logical 0
  531.     pinMode(10, INPUT); digitalWrite(10, HIGH); delay(10);
  532.  
  533.     ibutton.skip();
  534.     ibutton.reset();
  535.     ibutton.write(0xD5);
  536.  
  537.     // Write the key by bytes
  538.     for (byte x = 0; x < 8; x++){
  539.       writeByte(key[x]);
  540.     }
  541.  
  542.     // Send end commands
  543.     ibutton.reset();
  544.     ibutton.write(0xD1);  // send 0xD1
  545.     digitalWrite(10, LOW); pinMode(10, OUTPUT); delayMicroseconds(10);  // send logical 1
  546.     pinMode(10, INPUT); digitalWrite(10, HIGH); delay(10);
  547.  
  548. }
  549.  
  550. int writeByte(byte data){
  551.   int data_bit;
  552.   for(data_bit=0; data_bit<8; data_bit++){
  553.     if (data & 1){
  554.       digitalWrite(pin, LOW); pinMode(pin, OUTPUT);
  555.       delayMicroseconds(60);
  556.       pinMode(pin, INPUT); digitalWrite(pin, HIGH);
  557.       delay(10);
  558.     } else {
  559.       digitalWrite(pin, LOW); pinMode(pin, OUTPUT);
  560.       pinMode(pin, INPUT); digitalWrite(pin, HIGH);
  561.       delay(10);
  562.     }
  563.     data = data >> 1;
  564.   }
  565.   return 0;
  566. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement