예제 #1
0
파일: main.c 프로젝트: olatief/Headstage
void main(void)
{
  bool send_crypt_data = false;
  bool tx_success = false;
  gzp_id_req_res_t id_req_status;
  uint8_t payload[GZLL_MAX_PAYLOAD_LENGTH];
 
  mcu_init();
  
  // Initialize Gazell Link Layer
  gzll_init();
  
  // Initialize Gazell Pairing Library
  gzp_init();

  EA = 1;

  while(true)
  {      
    payload[0] = ~P0;
    
    // Send every other packet as encrypted data  
    if(send_crypt_data)
    {
      // Send encrypted packet using the Gazell pairing library
      tx_success = gzp_crypt_data_send(payload, GZP_ENCRYPTED_USER_DATA_MAX_LENGTH);
    }
    else
    {
      // Send packet as plaintext on pipe 2
      gzll_tx_data(payload, GZLL_MAX_FW_PAYLOAD_LENGTH, 2);   
      while(gzll_get_state() != GZLL_IDLE)
      ;
      tx_success = gzll_tx_success();
    }
    send_crypt_data = !send_crypt_data;

    // If data transfer failed
    if(!tx_success)
    {
      // Send "system address request". Needed for sending any user data to Host.
      gzp_address_req_send();
      
      // Send "Host ID request". Needed for sending encrypted user data to host.
      id_req_status = gzp_id_req_send();
    }

    // If waiting for Host to grant or reject ID request
    if(id_req_status == GZP_ID_RESP_PENDING)
    {
      // Send new ID request for fetching response
      id_req_status = gzp_id_req_send();
    }
  }  
}
static void read_keyboard_and_send(void)
{
    uint8_t keyboard_packet[NRFR_KEYBOARD_PACKET_LENGTH];

    // "Scan" keyboard.
    keyboard_get_non_empty_packet(keyboard_packet);

    // Send address request if required.
    if(!host_id_received  && !system_addr_received)
    {
        system_addr_received = gzp_address_req_send();
    }

    /* Send Host ID request if required. This may take a few attempts
     * as the Host may require some time to generate the Host ID. */
    if(!host_id_received  && system_addr_received )
    {
        while(send_host_id_req() == GZP_ID_RESP_PENDING);
    }

    /* After receiving the Host ID we send one packet in order
     * to update the dynamic key. 
     */
    if(host_id_received && !dyn_key_ok)
    {
        bool keyboard_send_ok = true;
        keyboard_send_ok = gzp_crypt_data_send(keyboard_packet, NRFR_KEYBOARD_PACKET_LENGTH);
        
        if(!keyboard_send_ok)
        {
            host_id_received = false;
        }
        else
        {
            dyn_key_ok = true;
        }
    }
    
    /* If we have the Host ID and dynamic key we can transmit encrypted data.
     */
    if(host_id_received && dyn_key_ok)
    {
        bool keyboard_send_ok = true;
        keyboard_send_ok = gzp_crypt_data_send(keyboard_packet, NRFR_KEYBOARD_PACKET_LENGTH);
        if(keyboard_send_ok)
        {
           // Wait until button is depressed.
            while(nrf_gpio_pin_read(BUTTON_SEND_KEYBOARD_DATA) == 0)
            {}        
            // Send empty keyboard packet to release all keys.
            keyboard_get_empty_packet(keyboard_packet);
            keyboard_send_ok = gzp_crypt_data_send(keyboard_packet, NRFR_KEYBOARD_PACKET_LENGTH);
        }

        if(!keyboard_send_ok)
        {
            dyn_key_ok = false;
        }
    }

}