Guest User

Token Bluetooth

a guest
Dec 15th, 2014
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 30.93 KB | None | 0 0
  1. /* Copyright (c) 2014, Nordic Semiconductor ASA
  2.  *
  3.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  4.  * of this software and associated documentation files (the "Software"), to deal
  5.  * in the Software without restriction, including without limitation the rights
  6.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7.  * copies of the Software, and to permit persons to whom the Software is
  8.  * furnished to do so, subject to the following conditions:
  9.  *
  10.  * The above copyright notice and this permission notice shall be included in all
  11.  * copies or substantial portions of the Software.
  12.  *
  13.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19.  * SOFTWARE.
  20.  */
  21.  
  22. /**
  23.  * HID (Human Interface Device) template
  24.  */
  25.  
  26. /** @defgroup HID_keyboard_2_bonds_template project
  27. @{
  28. @ingroup projects
  29. @brief HID Keyboard project that can be used as a template for new projects.
  30.  
  31. @details
  32.  
  33. IMPORTANT: This example still is not compatible with CHIPKIT
  34.  
  35. This project is a firmware template for new HID keyboard projects which need to use 2 bonds.
  36. The project will run correctly in its current state.
  37. This will show the Arduino board as a HID Keybaord to the Win 8.
  38. After HID Keyboard has been bonded with Win 8.
  39. The letter 'A' is sent to the Win 8 every 4 seconds.
  40. With this project you have a starting point for adding your own application functionality.
  41.  
  42. The following instructions describe the steps to be made on the Windows PC:
  43.  
  44.  -# Install the Master Control Panel on your computer. Connect the Master Emulator
  45.     (nRF2739) and make sure the hardware drivers are installed.
  46.  
  47.  -# Alternatively you should be able to get the board to work directly with a Win 8/Win RT PC after adding
  48.  the required buttons for I/O.
  49.  
  50. Note:
  51. Pin #6 on Arduino -> PAIRING CLEAR pin: Connect to 3.3v to clear the pairing
  52. Pin #2 on Arduino -> Add new bond pin: Connect to 3.3v to add a new bond.
  53.  
  54. The bonding information is stored in the EEPROM of the ATmega328 in the Arduino UNO
  55.  
  56. The setup() and the loop() functions are the equvivlent of main() .
  57.  
  58.  */
  59. #include <SPI.h>
  60. #include "services.h"
  61.  
  62. #include <lib_aci.h>
  63. #include "aci_setup.h"
  64. #include "EEPROM.h"
  65.  
  66. #ifdef SERVICES_PIPE_TYPE_MAPPING_CONTENT
  67.     static services_pipe_type_mapping_t
  68.         services_pipe_type_mapping[NUMBER_OF_PIPES] = SERVICES_PIPE_TYPE_MAPPING_CONTENT;
  69. #else
  70.     #define NUMBER_OF_PIPES 0
  71.     static services_pipe_type_mapping_t * services_pipe_type_mapping = NULL;
  72. #endif
  73.  
  74. static hal_aci_data_t setup_msgs[NB_SETUP_MESSAGES] PROGMEM = SETUP_MESSAGES_CONTENT;
  75.  
  76. #define BYTES_PER_BOND_IN_HEADER       2
  77. #define BONDS_MAX                      1
  78. #define BOND_DOES_NOT_EXIST_AT_INDEX   0xF0
  79.  
  80. #define BONDING_PIN 4  
  81. #define PAIRING_CLEAR_PIN 6
  82.  
  83.  
  84. // aci_struct that will contain
  85. // total initial credits
  86. // current credit
  87. // current state of the aci (setup/standby/active/sleep)
  88. // open remote pipe pending
  89. // close remote pipe pending
  90. // Current pipe available bitmap
  91. // Current pipe closed bitmap
  92. // Current connection interval, slave latency and link supervision timeout
  93. // Current State of the the GATT client (Service Discovery)
  94. // Status of the bond (R) Peer address
  95. static struct aci_state_t aci_state;
  96. static hal_aci_evt_t aci_data;
  97. static hal_aci_data_t aci_cmd;
  98.  
  99. /*
  100. We will store the bonding info for the nRF8001 in the MCU to recover from a power loss situation
  101. */
  102. static uint8_t bond_number          = 0;
  103. static uint8_t current_bond_index   = 0xFF;
  104. static uint16_t eeprom_write_offset = BONDS_MAX * BYTES_PER_BOND_IN_HEADER;
  105.  
  106. /*
  107. We will do the timing change for the link only once
  108. */
  109. static bool timing_change_done = false;
  110. static bool disconnect_started = false;
  111.  
  112. /*
  113. Variables used for the timer on the AVR
  114. */
  115. volatile uint8_t timer1_f = 0;
  116.  
  117. /*
  118. The keyboard report is 8 bytes
  119. 0 Modifier keys
  120. 1 Reserved
  121. 2 Keycode 1
  122. 3 Keycode 2
  123. 4 Keycode 3
  124. 5 Keycode 4
  125. 6 Keycode 5
  126. 7 Keycode 6
  127. */
  128. //uint8_t keypressA[8]={ 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00 };
  129.  
  130. /*** FUNC
  131.  
  132. Name:           Timer1start
  133.  
  134. Function:       Start timer 1 to interrupt periodically. Call this from
  135.                 the Arduino setup() function.
  136.  
  137. Description:    The pre-scaler and the timer count divide the timer-counter
  138.                 clock frequency to give a timer overflow interrupt rate:
  139.  
  140.                 Interrupt rate =  16MHz / (prescaler * (255 - TCNT2))
  141.  
  142.         TCCR2B[b2:0]   Prescaler    Freq [KHz], Period [usec] after prescale
  143.           0x0            (TC stopped)     0         0
  144.           0x1                1        16000.        0.0625
  145.           0x2                8         2000.        0.500
  146.           0x3               32          500.        2.000
  147.           0x4               64          250.        4.000
  148.           0x5              128          125.        8.000
  149.           0x6              256           62.5      16.000
  150.           0x7             1024           15.625    64.000
  151.  
  152.  
  153. Parameters: void
  154.  
  155. Returns:    void
  156.  
  157. FUNC ***/
  158.  
  159. //void Timer1start()
  160. //{
  161.   //// Setup Timer1 overflow to fire every 4000ms
  162.   ////   period [sec] = (1 / f_clock [sec]) * prescale * (count)
  163.   ////                  (1/16000000)  * 1024 * (count) = 4000 ms
  164. //
  165. //
  166.   //TCCR1B  = 0x00;        // Disable Timer1 while we set it up
  167. //
  168.   //TCNT1H  = 11;          // Approx 4000ms when prescaler is set to 1024
  169.   //TCNT1L  = 0;
  170.   //TIFR1   = 0x00;        // Timer1 INT Flag Reg: Clear Timer Overflow Flag
  171.   //TIMSK1  = 0x01;        // Timer1 INT Reg: Timer1 Overflow Interrupt Enable
  172.   //TCCR1A  = 0x00;        // Timer1 Control Reg A: Wave Gen Mode normal
  173.   //TCCR1B  = 0x05;        // Timer1 Control Reg B: Timer Prescaler set to 1024
  174. //}
  175.  
  176. //void Timer1stop()
  177. //{
  178.   //TCCR1B = 0x00;
  179.   //TIMSK1 = 0x00;
  180. //}
  181.  
  182. /*** FUNC
  183.  
  184. Name:       Timer1 ISR
  185.  
  186. Function:   Handles the Timer1-overflow interrupt
  187.  
  188. FUNC ***/
  189.  
  190. //ISR(TIMER1_OVF_vect)
  191. //{
  192.   //if (0 == timer1_f)
  193.   //{
  194.     //timer1_f = 1;
  195.   //}
  196. //
  197.   //TCNT1H = 11;    // Approx 4000 ms - Reload
  198.   //TCNT1L = 0;
  199.   //TIFR1  = 0x00;    // timer1 int flag reg: clear timer overflow flag
  200. //};
  201.  
  202. /* Define how assert should function in the BLE library */
  203. void __ble_assert(const char *file, uint16_t line)
  204. {
  205.   Serial.print("ERROR ");
  206.   Serial.print(file);
  207.   Serial.print(": ");
  208.   Serial.print(line);
  209.   Serial.print("\n");
  210.   while(1);
  211. }
  212.  
  213. /*************NOTE**********
  214. Scroll to the end of the file and read the loop() and setup() functions.
  215. The loop/setup functions is the equivalent of the main() function
  216. */
  217.  
  218. /*
  219. Read the Dymamic data from the EEPROM and send then as ACI Write Dynamic Data to the nRF8001
  220. This will restore the nRF8001 to the situation when the Dynamic Data was Read out
  221.  
  222. Header in the EEPROM:
  223. 2 bytes are used in the header for each master the nRF8001 has bonded with.
  224. The code supports 2 masters so the header uses 4 bytes.
  225.  
  226. Byte 0:
  227. Bit 0  :Valid
  228. Bit 1-7:Number of messages for restore
  229. Byte 1:
  230. Bit 0-7: Offset in EEPROM bytes to the ACI msg stored
  231. Byte 2:
  232. Bit 0  :Valid
  233. Bit 1-7:Number of messages for restore
  234. Byte 3:
  235. Bit 0-7: Offset in EEPROM bytes to the ACI msg stored
  236.  
  237. bond_index : The index of the stored bonds in the EEPROM. Value: 0 or 1, when max 2 bonds can be stored.
  238. */
  239. aci_status_code_t bond_data_restore(aci_state_t *aci_stat, uint8_t bond_index)
  240. {
  241.   aci_evt_t *aci_evt;
  242.   uint8_t eeprom_status       = 0;
  243.   uint16_t eeprom_offset_read = 0;
  244.   uint8_t write_dyn_num_msgs  = 0;
  245.   uint8_t len =0;
  246.  
  247.   eeprom_status      = EEPROM.read(bond_index * BYTES_PER_BOND_IN_HEADER);
  248.   if (eeprom_status == 0xFF)
  249.   {
  250.     return (aci_status_code_t)BOND_DOES_NOT_EXIST_AT_INDEX;
  251.   }
  252.  
  253.   eeprom_offset_read = EEPROM.read((bond_index * BYTES_PER_BOND_IN_HEADER) + 1); //Offset from the start of the EEPROM
  254.   //Since the size of the header is 4 bytes for 2 bonds. The first bond is stored from location 4 and the offset is 4.
  255.  
  256.   //Print the EEPROM header of 4 bytes
  257.   Serial.println(F("EEPROM Header (4 bytes):"));
  258.   Serial.println(EEPROM.read(0), HEX);
  259.   Serial.println(EEPROM.read(1), HEX);
  260.   Serial.println(EEPROM.read(2), HEX);
  261.   Serial.println(EEPROM.read(3), HEX);
  262.   Serial.println(F("--"));
  263.  
  264.   // Get the number of messages to write for the eeprom_status
  265.   write_dyn_num_msgs = eeprom_status & 0x7F;
  266.  
  267.   //Read from the EEPROM
  268.   while(1)
  269.   {
  270.     len = EEPROM.read(eeprom_offset_read);
  271.  
  272.     eeprom_offset_read++;
  273.     aci_cmd.buffer[0] = len;
  274.  
  275.     for (uint8_t i=1; i<=len; i++)
  276.     {
  277.       aci_cmd.buffer[i] = EEPROM.read(eeprom_offset_read);
  278.       eeprom_offset_read++;
  279.     }
  280.  
  281.     //@todo : Fix this bug that is changing this byte
  282.     aci_cmd.buffer[1] = ACI_CMD_WRITE_DYNAMIC_DATA;
  283.  
  284.     //Send the ACI Write Dynamic Data
  285.     if (!hal_aci_tl_send(&aci_cmd))
  286.     {
  287.       Serial.println(F("bond_data_restore: Cmd Q Full"));
  288.       return ACI_STATUS_ERROR_INTERNAL;
  289.     }
  290.  
  291.     //Spin in the while loop waiting for an event
  292.     while (1)
  293.     {
  294.       if (lib_aci_event_get(aci_stat, &aci_data))
  295.       {
  296.         aci_evt = &aci_data.evt;
  297.  
  298.         if (ACI_EVT_CMD_RSP != aci_evt->evt_opcode)
  299.         {
  300.           //Got something other than a command response evt -> Error
  301.           Serial.print(F("bond_data_restore: Expected cmd rsp evt. Got: 0x"));
  302.           Serial.println(aci_evt->evt_opcode, HEX);
  303.           return ACI_STATUS_ERROR_INTERNAL;
  304.         }
  305.         else
  306.         {
  307.           write_dyn_num_msgs--;
  308.  
  309.           //ACI Evt Command Response
  310.           if (ACI_STATUS_TRANSACTION_COMPLETE == aci_evt->params.cmd_rsp.cmd_status)
  311.           {
  312.             return ACI_STATUS_TRANSACTION_COMPLETE;
  313.           }
  314.           if (0 >= write_dyn_num_msgs)
  315.           {
  316.             //should have returned earlier
  317.             return ACI_STATUS_ERROR_INTERNAL;
  318.           }
  319.           if (ACI_STATUS_TRANSACTION_CONTINUE == aci_evt->params.cmd_rsp.cmd_status)
  320.           {
  321.             //break and write the next ACI Write Dynamic Data
  322.             break;
  323.           }
  324.         }
  325.       }
  326.     }
  327.   }
  328. }
  329.  
  330.  
  331. /*
  332. This function is specific to the atmega328
  333. @params ACI Command Response Evt received from the Read Dynmaic Data
  334. Uses the global   static int eeprom_write_offset to track the EEPROM write offset
  335. */
  336. void bond_data_store(aci_evt_t *evt)
  337. {
  338.   //Write it to non-volatile storage
  339.   EEPROM.write( eeprom_write_offset, evt->len -2 );
  340.   eeprom_write_offset++;
  341.  
  342.   EEPROM.write( eeprom_write_offset, ACI_CMD_WRITE_DYNAMIC_DATA);
  343.   eeprom_write_offset++;
  344.  
  345.   for (uint8_t i=0; i< (evt->len-3); i++)
  346.   {
  347.     EEPROM.write( eeprom_write_offset, evt->params.cmd_rsp.params.padding[i]);
  348.     eeprom_write_offset++;
  349.   }
  350. }
  351.  
  352. bool bond_data_read_store(aci_state_t *aci_stat, uint8_t new_bond_index)
  353. {
  354.   /*
  355.   The size of the dynamic data for a specific Bluetooth Low Energy configuration
  356.   is present in the ublue_setup.gen.out.txt generated by the nRFgo studio as "dynamic data size".
  357.   */
  358.   bool status = false;
  359.   aci_evt_t * aci_evt = NULL;
  360.   uint8_t read_dyn_num_msgs = 0;
  361.  
  362.   //Start reading the dynamic data
  363.   lib_aci_read_dynamic_data();
  364.   read_dyn_num_msgs++;
  365.  
  366.   while (1)
  367.   {
  368.     if (true == lib_aci_event_get(aci_stat, &aci_data))
  369.     {
  370.       aci_evt = &aci_data.evt;
  371.  
  372.       if (ACI_EVT_CMD_RSP != aci_evt->evt_opcode )
  373.       {
  374.         //Got something other than a command response evt -> Error
  375.         status = false;
  376.         break;
  377.       }
  378.  
  379.       if (ACI_STATUS_TRANSACTION_COMPLETE == aci_evt->params.cmd_rsp.cmd_status)
  380.       {
  381.         //Store the contents of the command response event in the EEPROM
  382.         //(len, cmd, seq-no, data) : cmd ->Write Dynamic Data so it can be used directly
  383.         bond_data_store(aci_evt);
  384.  
  385.         //Set the flag in the EEPROM that the contents of the EEPROM is valid
  386.         EEPROM.write(new_bond_index * BYTES_PER_BOND_IN_HEADER, 0x80|read_dyn_num_msgs );
  387.         //Set the offset for the next bond as well
  388.         EEPROM.write(((new_bond_index+1) * BYTES_PER_BOND_IN_HEADER)+1, eeprom_write_offset);
  389.  
  390.         //Finished with reading the dynamic data
  391.         status = true;
  392.  
  393.         break;
  394.       }
  395.  
  396.       if (!(ACI_STATUS_TRANSACTION_CONTINUE == aci_evt->params.cmd_rsp.cmd_status))
  397.       {
  398.         //We failed the read dymanic data
  399.         //Set the flag in the EEPROM that the contents of the EEPROM is invalid
  400.         EEPROM.write(new_bond_index * BYTES_PER_BOND_IN_HEADER, 0xFF);
  401.  
  402.         status = false;
  403.         break;
  404.       }
  405.       else
  406.       {
  407.         //Store the contents of the command response event in the EEPROM
  408.         // (len, cmd, seq-no, data) : cmd ->Write Dynamic Data so it can be used directly when re-storing the dynamic data
  409.         bond_data_store(aci_evt);
  410.  
  411.         //Read the next dynamic data message
  412.         lib_aci_read_dynamic_data();
  413.         read_dyn_num_msgs++;
  414.       }
  415.  
  416.     }
  417.   }
  418.   return status;
  419. }
  420.  
  421. //Process all ACI events here
  422. void aci_loop()
  423. {
  424.   static bool setup_required = false;
  425.  
  426.   // We enter the if statement only when there is a ACI event available to be processed
  427.   if (lib_aci_event_get(&aci_state, &aci_data))
  428.   {
  429.     aci_evt_t * aci_evt;
  430.     aci_evt = &aci_data.evt;
  431.  
  432.     switch(aci_evt->evt_opcode)
  433.     {
  434.         case ACI_EVT_DEVICE_STARTED:
  435.         {
  436.           aci_state.data_credit_total = aci_evt->params.device_started.credit_available;
  437.           switch(aci_evt->params.device_started.device_mode)
  438.           {
  439.             case ACI_DEVICE_SETUP:
  440.               /**
  441.               When the device is in the setup mode
  442.               */
  443.               aci_state.device_state = ACI_DEVICE_SETUP;
  444.               Serial.println(F("Evt Device Started: Setup"));
  445.               setup_required = true;
  446.               break;
  447.  
  448.             case ACI_DEVICE_STANDBY:
  449.               Serial.println(F("Evt Device Started: Standby"));
  450.               if (aci_evt->params.device_started.hw_error)
  451.               {
  452.                 delay(20); //Magic number used to make sure the HW error event is handled correctly.
  453.               }
  454.               else
  455.               {
  456.                 //Manage the bond in EEPROM of the AVR
  457.                 {
  458.                   uint8_t eeprom_status = 0;
  459.  
  460.                   //Reset the number of bonds and count from the EEPROM header
  461.                   bond_number = 0;
  462.                   eeprom_status = EEPROM.read(0);
  463.                   if (eeprom_status != 0xFF)
  464.                   {
  465.                     bond_number++;
  466.                     //Update the write offset to the correct number
  467.                     eeprom_write_offset = EEPROM.read(bond_number * BYTES_PER_BOND_IN_HEADER + 1);
  468.                     if (EEPROM.read(2) != 0xFF)
  469.                     {
  470.                       bond_number++;
  471.                       //This is needed only if we wanted to add a 3rd bond
  472.                       eeprom_write_offset = EEPROM.read(bond_number * BYTES_PER_BOND_IN_HEADER + 1);
  473.                     }
  474.                     //bond 0
  475.                     current_bond_index = 0;
  476.                     Serial.println(F("Previous Bond present. Restoring bond 0"));
  477.                     //We must have lost power and restarted and must restore the bonding infromation using the ACI Write Dynamic Data
  478.                     if (ACI_STATUS_TRANSACTION_COMPLETE == bond_data_restore(&aci_state, current_bond_index))
  479.                     {
  480.                       Serial.println(F("Bond 0 restored successfully"));
  481.                       current_bond_index++;
  482.                     }
  483.                     else
  484.                     {
  485.                       Serial.println(F("Bond 0 restore failed"));
  486.                     }
  487.                   }
  488.                 }
  489.  
  490.                 // Start bonding if no bonds are available
  491.                 if (bond_number == 0)
  492.                 {
  493.                   lib_aci_bond(180/* in seconds */, 0x0050 /* advertising interval 50ms*/);
  494.                   Serial.println(F("Advertising started : Waiting to be connected and bonded"));
  495.                 }
  496.                 else
  497.                 {
  498.                   //connect to an already bonded device
  499.                   //Use lib_aci_direct_connect for faster re-connections
  500.                   lib_aci_connect(10/* in seconds */, 0x0020 /* advertising interval 20ms*/);
  501.                   Serial.println(F("Already bonded : Advertising started : Waiting to be connected"));
  502.                 }
  503.               }
  504.               break;
  505.           }
  506.         }
  507.           break; //ACI Device Started Event
  508.  
  509.       case ACI_EVT_CMD_RSP:
  510.         //If an ACI command response event comes with an error -> stop
  511.         if ((ACI_STATUS_SUCCESS != aci_evt->params.cmd_rsp.cmd_status )
  512.             && (ACI_CMD_READ_DYNAMIC_DATA  != aci_evt->params.cmd_rsp.cmd_opcode)
  513.             && (ACI_CMD_WRITE_DYNAMIC_DATA != aci_evt->params.cmd_rsp.cmd_opcode))
  514.         {
  515.           //ACI ReadDynamicData and ACI WriteDynamicData will have status codes of
  516.           //TRANSACTION_CONTINUE and TRANSACTION_COMPLETE
  517.           //all other ACI commands will have status code of ACI_STATUS_SCUCCESS for a successful command
  518.  
  519.           Serial.print(F("ACI Command "));
  520.           Serial.println(aci_evt->params.cmd_rsp.cmd_opcode, HEX);
  521.           Serial.print(F("Evt Cmd respone: Status "));
  522.           Serial.println(aci_evt->params.cmd_rsp.cmd_status, HEX);
  523.         }
  524.         //if (ACI_CMD_GET_DEVICE_VERSION == aci_evt->params.cmd_rsp.cmd_opcode)
  525.         //{
  526.           //lib_aci_set_local_data(&aci_state, PIPE_DEVICE_INFORMATION_HARDWARE_REVISION_STRING_SET,
  527.                //(uint8_t *)&(aci_evt->params.cmd_rsp.params.get_device_version),
  528.                    //sizeof(aci_evt_cmd_rsp_params_get_device_version_t));
  529.         //}
  530.         break;
  531.  
  532.       case ACI_EVT_CONNECTED:
  533.         /*
  534.         reset the credit available when the link gets connected
  535.         */
  536.         aci_state.data_credit_available = aci_state.data_credit_total;
  537.         Serial.println(F("Evt Connected"));
  538.         /*
  539.          Get the Device Version of the nRF8001 and place it in the
  540.          Hardware Revision String Characteristic of the Device Info. GATT Service
  541.          */
  542.         lib_aci_device_version();
  543.         timing_change_done = false;
  544.         //Timer1stop();
  545.         break;
  546.  
  547.       //case ACI_EVT_PIPE_STATUS:
  548.         //Serial.println(F("Evt Pipe Status"));
  549.         //if (lib_aci_is_pipe_available(&aci_state, PIPE_HID_SERVICE_HID_REPORT_TX)
  550.             //&& (false == timing_change_done))
  551.         //{
  552.           //lib_aci_change_timing_GAP_PPCP(); //Uses the GAP preferred timing as put in the nRFGo studio xml file-> See also in services.h
  553.           //timing_change_done = true;
  554.           //Serial.println(F("Timer started"));
  555.           ////Timer1start();
  556.         //}
  557.         //break;
  558.  
  559.       case ACI_EVT_TIMING:
  560.         Serial.print(F("Timing change received conn Interval: 0x"));
  561.         Serial.println(aci_evt->params.timing.conn_rf_interval, HEX);
  562.         break;
  563.  
  564.       case ACI_EVT_DATA_CREDIT:
  565.         /**
  566.         Bluetooth Radio ack received from the peer radio for the data packet sent.
  567.         Multiple data packets can be acked in a single aci data credit event.
  568.         */
  569.         aci_state.data_credit_available = aci_state.data_credit_available + aci_evt->params.data_credit.credit;
  570.         break;
  571.  
  572.       //case ACI_EVT_PIPE_ERROR:
  573.         ////See the appendix in the nRF8001 Product Specication for details on the error codes
  574.         //Serial.print(F("ACI Evt Pipe Error: Pipe #:"));
  575.         //Serial.print(aci_evt->params.pipe_error.pipe_number, DEC);
  576.         //Serial.print(F("  Pipe Error Code: 0x"));
  577.         //Serial.println(aci_evt->params.pipe_error.error_code, HEX);
  578. //
  579.         ////Increment the credit available as the data packet was not sent.
  580.         ////The pipe error also represents the Attribute protocol Error Response sent from the peer and that should not be counted
  581.         ////for the credit.
  582.         //if (ACI_STATUS_ERROR_PEER_ATT_ERROR != aci_evt->params.pipe_error.error_code)
  583.         //{
  584.           //aci_state.data_credit_available++;
  585.         //}
  586.         //break;
  587.  
  588.       case ACI_EVT_BOND_STATUS:
  589.         Serial.println(F("Evt Bond Status"));
  590.         aci_state.bonded = aci_evt->params.bond_status.status_code;
  591.         break;
  592.  
  593.       case ACI_EVT_DISCONNECTED:
  594.         //Stop the timer
  595.         //Timer1stop();
  596.         Serial.println(F("Disconnected/timed out"));
  597.         /**
  598.         Advertise again if the advertising timed out.
  599.         */
  600.         if(ACI_STATUS_ERROR_ADVT_TIMEOUT == aci_evt->params.disconnected.aci_status)
  601.         {
  602.           aci_status_code_t bond_restore_status;
  603.  
  604.           Serial.print(F("Previous Bond present. Restoring bond "));
  605.  
  606.           //We must have lost power and restarted and must restore the bonding infromation using the ACI Write Dynamic Data
  607.           //Switch to the next bond
  608.           Serial.println(current_bond_index);
  609.           if (current_bond_index >= BONDS_MAX)
  610.           {
  611.             current_bond_index = 0;
  612.           }
  613.           bond_restore_status = bond_data_restore(&aci_state, current_bond_index);
  614.           current_bond_index++;
  615.  
  616.           if (ACI_STATUS_TRANSACTION_COMPLETE == bond_restore_status)
  617.           {
  618.             Serial.println(F("Bond restored successfully"));
  619.           }
  620.           if (BOND_DOES_NOT_EXIST_AT_INDEX == bond_restore_status)
  621.           {
  622.             Serial.println(F("Bond does not exist"));
  623.             current_bond_index = 0;
  624.           }
  625.         }
  626.         else
  627.         {
  628.           if (ACI_BOND_STATUS_SUCCESS == aci_state.bonded) //Were bonded in the just disconnected connection.
  629.           {
  630.             aci_state.bonded = ACI_BOND_STATUS_FAILED;
  631.             //Store away the dynamic data of the nRF8001 in the Flash or EEPROM of the MCU
  632.             // so we can restore the bond information of the nRF8001 in the event of power loss
  633.             //Update the header offset with the previous value of the eeprom_write_offset
  634.             EEPROM.write((bond_number * BYTES_PER_BOND_IN_HEADER)+ 1, eeprom_write_offset );
  635.  
  636.             Serial.println(F("eeprom_write_offset"));
  637.             Serial.println(eeprom_write_offset);
  638.             Serial.println(F("bond_number"));
  639.             Serial.println(bond_number);
  640.  
  641.  
  642.             // pass the bond_number before increment to use it as an index
  643.             if (bond_data_read_store(&aci_state, bond_number))
  644.             {
  645.               bond_number++;
  646.               Serial.println(F("Dynamic Data read and stored successfully"));
  647.             }
  648.           }
  649.         }
  650.  
  651.         //If a new bond is to be added, PIN 2 must tbe high or no bond exists
  652.         if (0x01 == digitalRead(BONDING_PIN) || (0 == bond_number))
  653.         {
  654.           //Start bonding
  655.           //Less than 2 bonds. We can add one more
  656.           if (bond_number < BONDS_MAX)
  657.           {
  658.             // Previous bonding failed. Try to bond again.
  659.             lib_aci_bond(180/* in seconds */, 0x0050 /* advertising interval 50ms*/);
  660.             Serial.println(F("Advertising started : Waiting to be connected and bonded for a new bond to be added"));
  661.           }
  662.         }
  663.         else
  664.         {
  665.           //connect to an already bonded device
  666.           //Use lib_aci_direct_connect for faster re-connections (advertising interval of 3.75 ms is used for directed advertising)
  667.           lib_aci_connect(10/* in seconds */, 0x0020 /* advertising interval 20ms*/);
  668.           Serial.println(F("Already bonded : Advertising started : Waiting to be connected"));
  669.         }
  670.         break;
  671.  
  672.       //case ACI_EVT_DATA_RECEIVED:
  673.         //Serial.print(F("Pipe #: 0x"));
  674.         //Serial.print(aci_evt->params.data_received.rx_data.pipe_number, HEX);
  675.         //{
  676.           //int i;
  677.           //Serial.print(F(" Data(Hex) : "));
  678.           //for(i=0; i<aci_evt->len - 2; i++)
  679.           //{
  680.             //Serial.print(aci_evt->params.data_received.rx_data.aci_data[i], HEX);
  681.             //Serial.print(F(" "));
  682.           //}
  683.         //}
  684.         //Serial.println(F(""));
  685.         //break;
  686.  
  687.       case ACI_EVT_HW_ERROR:
  688.         Serial.print(F("HW error: "));
  689.         Serial.println(aci_evt->params.hw_error.line_num, DEC);
  690.  
  691.         for(uint8_t counter = 0; counter <= (aci_evt->len - 3); counter++)
  692.         {
  693.           Serial.write(aci_evt->params.hw_error.file_name[counter]); //uint8_t file_name[20];
  694.         }
  695.         Serial.println();
  696.  
  697.         //Manage the bond in EEPROM of the AVR
  698.         {
  699.           uint8_t eeprom_status = 0;
  700.  
  701.           //Reset the number of bonds and count from the EEPROM header
  702.           bond_number = 0;
  703.           eeprom_status = EEPROM.read(0);
  704.           if (eeprom_status != 0xFF)
  705.           {
  706.             bond_number++;
  707.             //Update the write offset to the correct number
  708.             eeprom_write_offset = EEPROM.read(bond_number * BYTES_PER_BOND_IN_HEADER + 1);
  709.             if (EEPROM.read(2) != 0xFF)
  710.             {
  711.               bond_number++;
  712.               //This is needed only if we wanted to add a 3rd bond
  713.               eeprom_write_offset = EEPROM.read(bond_number * BYTES_PER_BOND_IN_HEADER + 1);
  714.             }
  715.             //bond 0
  716.             current_bond_index = 0;
  717.             Serial.println(F("Previous Bond present. Restoring bond 0"));
  718.             //We must have lost power and restarted and must restore the bonding infromation using the ACI Write Dynamic Data
  719.             if (ACI_STATUS_TRANSACTION_COMPLETE == bond_data_restore(&aci_state, current_bond_index))
  720.             {
  721.               Serial.println(F("Bond 0 restored successfully"));
  722.               current_bond_index++;
  723.             }
  724.             else
  725.             {
  726.               Serial.println(F("Bond 0 restore failed"));
  727.             }
  728.           }
  729.         }
  730.  
  731.         // Start bonding if no bonds are available
  732.         if (bond_number == 0)
  733.         {
  734.           lib_aci_bond(180/* in seconds */, 0x0050 /* advertising interval 50ms*/);
  735.           Serial.println(F("Advertising started : Waiting to be connected and bonded"));
  736.         }
  737.         else
  738.         {
  739.           //connect to an already bonded device
  740.           //Use lib_aci_direct_connect for faster re-connections
  741.           lib_aci_connect(10/* in seconds */, 0x0020 /* advertising interval 20ms*/);
  742.           Serial.println(F("Already bonded : Advertising started : Waiting to be connected"));
  743.         }
  744.       break;
  745.     }
  746.   }
  747.   else
  748.   {
  749.     //Serial.println(F("No ACI Events available"));
  750.     // No event in the ACI Event queue
  751.     // Arduino can go to sleep now
  752.     // Wakeup from sleep from the RDYN line
  753.   }
  754.  
  755.   /* setup_required is set to true when the device starts up and enters setup mode.
  756.    * It indicates that do_aci_setup() should be called. The flag should be cleared if
  757.    * do_aci_setup() returns ACI_STATUS_TRANSACTION_COMPLETE.
  758.    */
  759.   if(setup_required)
  760.   {
  761.     if (SETUP_SUCCESS == do_aci_setup(&aci_state))
  762.     {
  763.       setup_required = false;
  764.     }
  765.   }
  766. }
  767.  
  768. /*
  769. This is called only once after a reset of the AVR
  770. */
  771. void setup(void)
  772. {
  773.   Serial.begin(115200);
  774.   //Wait until the serial port is available (useful only for the Leonardo)
  775.   //As the Leonardo board is not reseted every time you open the Serial Monitor
  776.   #if defined (__AVR_ATmega32U4__)
  777.     while(!Serial)
  778.     {}
  779.     delay(5000);  //5 seconds delay for enabling to see the start up comments on the serial board
  780.   #elif defined(__PIC32MX__)
  781.     delay(1000);
  782.   #endif
  783.  
  784.   Serial.println(F("Arduino setup"));
  785.  
  786.   /**
  787.   Point ACI data structures to the the setup data that the nRFgo studio generated for the nRF8001
  788.   */
  789.   if (NULL != services_pipe_type_mapping)
  790.   {
  791.     aci_state.aci_setup_info.services_pipe_type_mapping = &services_pipe_type_mapping[0];
  792.   }
  793.   else
  794.   {
  795.     aci_state.aci_setup_info.services_pipe_type_mapping = NULL;
  796.   }
  797.   aci_state.aci_setup_info.number_of_pipes    = NUMBER_OF_PIPES;
  798.   aci_state.aci_setup_info.setup_msgs         = setup_msgs;
  799.   aci_state.aci_setup_info.num_setup_msgs     = NB_SETUP_MESSAGES;
  800.  
  801.   /*
  802.   Tell the ACI library, the MCU to nRF8001 pin connections.
  803.   The Active pin is optional and can be marked UNUSED
  804.   */
  805.   aci_state.aci_pins.board_name = BOARD_DEFAULT; //See board.h for details REDBEARLAB_SHIELD_V1_1
  806.   aci_state.aci_pins.reqn_pin   = 10; //9; // alterei pro nano e bluefruit LE tutorial
  807.   aci_state.aci_pins.rdyn_pin   = 2;  //8; // alterei pro nano e bluefruit LE tutorial
  808.   aci_state.aci_pins.mosi_pin   = MOSI;
  809.   aci_state.aci_pins.miso_pin   = MISO;
  810.   aci_state.aci_pins.sck_pin    = SCK;
  811.  
  812.   aci_state.aci_pins.spi_clock_divider      = SPI_CLOCK_DIV8;//SPI_CLOCK_DIV8  = 2MHz SPI speed
  813.                                                              //SPI_CLOCK_DIV16 = 1MHz SPI speed
  814.  
  815.   aci_state.aci_pins.reset_pin              = 9; //4; // alterei pro nano e bluefruit LE tutorial
  816.   aci_state.aci_pins.active_pin             = UNUSED;
  817.   aci_state.aci_pins.optional_chip_sel_pin  = UNUSED;
  818.  
  819.   aci_state.aci_pins.interface_is_interrupt = false;
  820.   aci_state.aci_pins.interrupt_number       = 1;
  821.  
  822.   /** We reset the nRF8001 here by toggling the RESET line connected to the nRF8001
  823.    *  and initialize the data structures required to setup the nRF8001
  824.    */
  825.   //The second parameter is for turning debug printing on for the ACI Commands and Events so they be printed on the Serial
  826.   lib_aci_init(&aci_state, false);
  827.  
  828.   //Initialize the state variables
  829.   aci_state.bonded   = ACI_BOND_STATUS_FAILED;
  830.   disconnect_started = false;
  831.  
  832.   pinMode(BONDING_PIN, INPUT); //Pino 4...troquei pra ficar igual ao bluefruit //Pin #2 on Arduino -> Bonding pin: Connect to 3.3v to start bonding
  833.   pinMode(PAIRING_CLEAR_PIN, INPUT); //Pin #6 on Arduino -> PAIRING CLEAR pin: Connect to 3.3v to clear the stored bonding information
  834.  
  835.   if (0x01 == digitalRead(PAIRING_CLEAR_PIN))
  836.   {
  837.     //Clear the pairing
  838.     Serial.println(F("Pairing cleared. Remove the wire on Pin 6 and reset the board for normal operation."));
  839.     //Address. Value
  840.     EEPROM.write(0, 0xFF);
  841.     EEPROM.write(1, 0xFF);
  842.     EEPROM.write(2, 0xFF);
  843.     EEPROM.write(3, 0xFF);
  844.     while(1) {};
  845.   }
  846. }
  847.  
  848. /* This is like a main() { while(1) { loop() } }
  849. */
  850. void loop(void)
  851. {
  852.  
  853.   aci_loop();
  854.  
  855.   /*
  856.   Method for sending HID Reports
  857.   */
  858.   //if (lib_aci_is_pipe_available(&aci_state, PIPE_HID_SERVICE_HID_REPORT_TX)
  859.       //&& (aci_state.data_credit_available == 2)
  860.       //&& (1 == timer1_f) )
  861.   //{
  862.     //timer1_f = 0;
  863.     //keypressA[2] = 0x04;
  864.     //lib_aci_send_data(PIPE_HID_SERVICE_HID_REPORT_TX, &keypressA[0], 8);
  865.     //aci_state.data_credit_available--;
  866.     //keypressA[2] = 0x00;
  867.     //lib_aci_send_data(PIPE_HID_SERVICE_HID_REPORT_TX, &keypressA[0], 8);
  868.     //aci_state.data_credit_available--;
  869.   //}
  870.  
  871.   if (0x01 == digitalRead(BONDING_PIN) && (disconnect_started == false))
  872.   {
  873.     //Disconnect the link by ACI Disconnect
  874.     Serial.println(F("Disconnect Started"));
  875.     lib_aci_disconnect(&aci_state, ACI_REASON_TERMINATE);
  876.     disconnect_started = true;
  877.   }
  878.  
  879. }
Advertisement
Add Comment
Please, Sign In to add comment