⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hci.c

📁 蓝牙协议源代码 bluetooth stack for lwip
💻 C
📖 第 1 页 / 共 4 页
字号:
  ((u8_t *)p->payload)[4] = lap & 0xFF;
  ((u8_t *)p->payload)[5] = lap >> 8;
  ((u8_t *)p->payload)[6] = lap >> 16;
  //memcpy(((u8_t *)p->payload)+4, inqres->cod, 3);
  ((u8_t *)p->payload)[7] = inq_len;
  ((u8_t *)p->payload)[8] = num_resp;
  phybusif_output(p, p->tot_len);
  pbuf_free(p);
  
  return ERR_OK;
}
/*-----------------------------------------------------------------------------------*/
/* hci_disconnect():
 *
 * Used to terminate an existing connection.
 */
 /*-----------------------------------------------------------------------------------*/
err_t
hci_disconnect(struct bd_addr *bdaddr, u8_t reason)
{
  struct pbuf *p;
  struct hci_link *link;

  link = hci_get_link(bdaddr);

  if(link == NULL) {
    LWIP_DEBUGF(HCI_DEBUG, ("hci_disconnect: Connection does not exist\n"));
    return ERR_CONN; /* Connection does not exist */ 
  }
  if((p = pbuf_alloc(PBUF_RAW, HCI_DISCONN_PLEN, PBUF_RAM)) == NULL) {
    LWIP_DEBUGF(HCI_DEBUG, ("hci_disconnect: Could not allocate memory for pbuf\n"));
    return ERR_MEM; /* Could not allocate memory for pbuf */
  }
  /* Assembling command packet */
  p = hci_cmd_ass(p, HCI_DISCONN_OCF, HCI_LINK_CTRL_OGF, HCI_DISCONN_PLEN);

  /* Assembling cmd prameters */
  ((u16_t *)p->payload)[2] = link->conhdl;
  ((u8_t *)p->payload)[6] = reason;
  phybusif_output(p, p->tot_len);
  pbuf_free(p);

  return ERR_OK;
}
/*-----------------------------------------------------------------------------------*/
/* hci_reject_connection_request():
 *
 * Used to decline a new incoming connection request.
 */
 /*-----------------------------------------------------------------------------------*/
err_t
hci_reject_connection_request(struct bd_addr *bdaddr, u8_t reason)
{
  struct pbuf *p;
  if((p = pbuf_alloc(PBUF_RAW, HCI_REJECT_CONN_REQ_PLEN, PBUF_RAM)) == NULL) {
    LWIP_DEBUGF(HCI_DEBUG, ("hci_reject_connection_request: Could not allocate memory for pbuf\n"));
    return ERR_MEM;
  }
  /* Assembling command packet */
  p = hci_cmd_ass(p, HCI_REJECT_CONN_REQ_OCF, HCI_LINK_CTRL_OGF, HCI_REJECT_CONN_REQ_PLEN);
  /* Assembling cmd prameters */
  memcpy(((u8_t *)p->payload) + 4, bdaddr->addr, 6);
  ((u8_t *)p->payload)[10] = reason;
  phybusif_output(p, p->tot_len);
  pbuf_free(p);

  return ERR_OK;
}
/*-----------------------------------------------------------------------------------*/
/* hci_pin_code_request_reply():
 *
 * Used to reply to a PIN Code Request event from the Host Controller and specifies 
 * the PIN code to use for a connection.
 */
 /*-----------------------------------------------------------------------------------*/
err_t
hci_pin_code_request_reply(struct bd_addr *bdaddr, u8_t pinlen, u8_t *pincode)
{
  struct pbuf *p;
  
  if((p = pbuf_alloc(PBUF_RAW, HCI_PIN_CODE_REQ_REP_PLEN, PBUF_RAM)) == NULL) {
    LWIP_DEBUGF(HCI_DEBUG, ("hci_pin_code_request_reply: Could not allocate memory for pbuf\n"));
    return ERR_MEM;
  }
  
  /* Reset buffer content just to make sure */
  memset((u8_t *)p->payload, 0, HCI_PIN_CODE_REQ_REP_PLEN);
  
  /* Assembling command packet */
  p = hci_cmd_ass(p, HCI_PIN_CODE_REQ_REP, HCI_LINK_CTRL_OGF, HCI_PIN_CODE_REQ_REP_PLEN);
  /* Assembling cmd prameters */
  memcpy(((u8_t *)p->payload) + 4, bdaddr->addr, 6);
  ((u8_t *)p->payload)[10] = pinlen;
  memcpy(((u8_t *)p->payload) + 11, pincode, pinlen);
  
  phybusif_output(p, p->tot_len);
  pbuf_free(p);

  return ERR_OK;
}
/*-----------------------------------------------------------------------------------*/
/* hci_pin_code_request_neg_reply():
 *
 * Used to reply to a PIN Code Request event from the Host Controller when the Host 
 * cannot specify a PIN code to use for a connection.
 */
 /*-----------------------------------------------------------------------------------*/
err_t
hci_pin_code_request_neg_reply(struct bd_addr *bdaddr)
{
  struct pbuf *p;
  if((p = pbuf_alloc(PBUF_RAW, HCI_PIN_CODE_REQ_NEG_REP_PLEN, PBUF_RAM)) == NULL) {
    LWIP_DEBUGF(HCI_DEBUG, ("hci_pin_code_request_neg_reply: Could not allocate memory for pbuf\n"));
    return ERR_MEM;
  }
  /* Assembling command packet */
  p = hci_cmd_ass(p, HCI_PIN_CODE_REQ_NEG_REP, HCI_LINK_CTRL_OGF, HCI_PIN_CODE_REQ_NEG_REP_PLEN);
  /* Assembling cmd prameters */
  memcpy(((u8_t *)p->payload)+4, bdaddr->addr, 6);
  phybusif_output(p, p->tot_len);
  pbuf_free(p);

  return ERR_OK;
}
/*-----------------------------------------------------------------------------------*/
/* hci_sniff_mode():
 *
 * Sets an ACL connection to low power Sniff mode.
 */
/*-----------------------------------------------------------------------------------*/
err_t
hci_sniff_mode(struct bd_addr *bdaddr, u16_t max_interval, u16_t min_interval, 
	       u16_t attempt, u16_t timeout)
{
  struct pbuf *p;
  struct hci_link *link;

  /* Check if an ACL connection exists */ 
  link = hci_get_link(bdaddr);

  if(link == NULL) {
    LWIP_DEBUGF(HCI_DEBUG, ("hci_sniff_mode: ACL connection does not exist\n"));
    return ERR_CONN;
  }

  if((p = pbuf_alloc(PBUF_TRANSPORT, HCI_SNIFF_PLEN, PBUF_RAM)) == NULL) { /* Alloc len of packet */
    LWIP_DEBUGF(HCI_DEBUG, ("hci_sniff_mode: Could not allocate memory for pbuf\n"));
    return ERR_MEM;
  }

  /* Assembling command packet */
  p = hci_cmd_ass(p, HCI_SNIFF_MODE, HCI_LINK_POLICY, HCI_SNIFF_PLEN);
  /* Assembling cmd prameters */
  ((u16_t *)p->payload)[2] = link->conhdl;
  ((u16_t *)p->payload)[3] = max_interval;
  ((u16_t *)p->payload)[4] = min_interval;
  ((u16_t *)p->payload)[5] = attempt;
  ((u16_t *)p->payload)[6] = timeout;

  phybusif_output(p, p->tot_len);
  pbuf_free(p);
  return ERR_OK;
}
/*-----------------------------------------------------------------------------------*/
/* hci_write_link_policy_settings():
 *
 * Control the modes (park, sniff, hold) that an ACL connection can take.
 *
 */
/*-----------------------------------------------------------------------------------*/
err_t
hci_write_link_policy_settings(struct bd_addr *bdaddr, u16_t link_policy)
{
  struct pbuf *p;
  struct hci_link *link;

  /* Check if an ACL connection exists */ 
  link = hci_get_link(bdaddr);

  if(link == NULL) {
    LWIP_DEBUGF(HCI_DEBUG, ("hci_write_link_policy_settings: ACL connection does not exist\n"));
    return ERR_CONN;
  }

  if( (p = pbuf_alloc(PBUF_TRANSPORT, HCI_W_LINK_POLICY_PLEN, PBUF_RAM)) == NULL) { /* Alloc len of packet */
    LWIP_DEBUGF(HCI_DEBUG, ("hci_write_link_policy_settings: Could not allocate memory for pbuf\n"));
    return ERR_MEM;
  }
  /* Assembling command packet */
  p = hci_cmd_ass(p, HCI_W_LINK_POLICY, HCI_LINK_POLICY, HCI_W_LINK_POLICY_PLEN);

  /* Assembling cmd prameters */
  ((u16_t *)p->payload)[2] = link->conhdl;
  ((u16_t *)p->payload)[3] = link_policy;
  phybusif_output(p, p->tot_len);
  pbuf_free(p);
  return ERR_OK;
}
/*-----------------------------------------------------------------------------------*/
/* hci_reset():
 *
 * Reset the Bluetooth host controller, link manager, and radio module.
 */
 /*-----------------------------------------------------------------------------------*/
err_t
hci_reset(void)
{
  struct pbuf *p;
  if((p = pbuf_alloc(PBUF_RAW, HCI_RESET_PLEN, PBUF_RAM)) == NULL) {
   LWIP_DEBUGF(HCI_DEBUG, ("hci_reset: Could not allocate memory for pbuf\n")); 
    return ERR_MEM;
  } 
  /* Assembling command packet */
  p = hci_cmd_ass(p, HCI_RESET_OCF, HCI_HC_BB_OGF, HCI_RESET_PLEN);
  /* Assembling cmd prameters */
  phybusif_output(p, p->tot_len);
  pbuf_free(p);

  return ERR_OK;
}
/*-----------------------------------------------------------------------------------*/
/* hci_set_event_filter():
 *
 * Used by the host to specify different event filters.
 */
 /*-----------------------------------------------------------------------------------*/
err_t
hci_set_event_filter(u8_t filter_type, u8_t filter_cond_type, u8_t* cond)
{
  u8_t cond_len = 0x00;
  struct pbuf *p;
  switch(filter_type) {
  case 0x00:
    LWIP_DEBUGF(HCI_DEBUG, ("hci_set_event_filter: Clearing all filters\n"));
    cond_len = 0x00;
    break;
  case 0x01:
    switch(filter_cond_type) {
    case 0x00:
      cond_len = 0x00;
      break;
    case 0x01:
      cond_len = 0x06;
      break;
    case 0x02:
      cond_len = 0x06;
      break;
    default:
      LWIP_DEBUGF(HCI_DEBUG, ("hci_set_event_filter: Entered an unspecified filter condition type!\n"));
      break;
    }
    break;
  case 0x02:
    switch(filter_cond_type) {
    case 0x00:
      cond_len = 0x01;
      break;
    case 0x01:
      cond_len = 0x07;
      break;
    case 0x02:
      cond_len = 0x07;
      break;
    default:
      LWIP_DEBUGF(HCI_DEBUG, ("hci_set_event_filter: Entered an unspecified filter condition type!\n"));
      break;
    }
    break;
  default:
    LWIP_DEBUGF(HCI_DEBUG, ("hci_set_event_filter: Entered an unspecified filter type!\n"));
    break;
  }
  if((p = pbuf_alloc(PBUF_RAW, HCI_SET_EV_FILTER_PLEN+cond_len, PBUF_RAM)) == NULL) {
    LWIP_DEBUGF(HCI_DEBUG, ("hci_set_event_filter: Could not allocate memory for pbuf\n"));
    return ERR_MEM;
  }
  /* Assembling command packet */
  p = hci_cmd_ass(p, HCI_SET_EV_FILTER_OCF, HCI_HC_BB_OGF, HCI_SET_EV_FILTER_PLEN+cond_len);
  ((u8_t *)p->payload)[4] = filter_type;
  ((u8_t *)p->payload)[5] = filter_cond_type;
  /* Assembling cmd prameters */
  if(cond_len) {
    memcpy(((u8_t *)p->payload)+6, cond, cond_len);
  }
  phybusif_output(p, p->tot_len);
  pbuf_free(p);
  return ERR_OK;
}
/*-----------------------------------------------------------------------------------*/
/* hci_write_stored_link_key():
 *
 * Writes a link key to be stored in the Bluetooth host controller.
 */
 /*-----------------------------------------------------------------------------------*/
err_t
hci_write_stored_link_key(struct bd_addr *bdaddr, u8_t *link)
{
  struct pbuf *p;
  if((p = pbuf_alloc(PBUF_RAW, HCI_WRITE_STORED_LINK_KEY_PLEN, PBUF_RAM)) == NULL) {
    LWIP_DEBUGF(HCI_DEBUG, ("hci_write_stored_link_key: Could not allocate memory for pbuf\n"));
    return ERR_MEM;
  }
  /* Assembling command packet */
  p = hci_cmd_ass(p, HCI_WRITE_STORED_LINK_KEY, HCI_HC_BB_OGF, HCI_WRITE_STORED_LINK_KEY_PLEN);
  /* Assembling cmd prameters */
  ((u8_t *)p->payload)[4] = 0x01;
  memcpy(((u8_t *)p->payload) + 5, bdaddr->addr, 6);
  memcpy(((u8_t *)p->payload) + 11, link, 16);
  phybusif_output(p, p->tot_len);
  pbuf_free(p);

  return ERR_OK;
}
/*-----------------------------------------------------------------------------------*/
/* hci_change_local_name():
 *
 * Writes a link key to be stored in the Bluetooth host controller.
 */
 /*-----------------------------------------------------------------------------------*/
err_t
hci_change_local_name(u8_t *name, u8_t len)
{
  struct pbuf *p;
  if((p = pbuf_alloc(PBUF_RAW, HCI_CHANGE_LOCAL_NAME_PLEN + len, PBUF_RAM)) == NULL) {
    LWIP_DEBUGF(HCI_DEBUG, ("hci_change_local_name: Could not allocate memory for pbuf\n"));
    return ERR_MEM;
  }
  /* Assembling command packet */
  p = hci_cmd_ass(p, HCI_CHANGE_LOCAL_NAME, HCI_HC_BB_OGF, HCI_CHANGE_LOCAL_NAME_PLEN + len);
  /* Assembling cmd prameters */
  memcpy(((u8_t *)p->payload) + 4, name, len);
  phybusif_output(p, p->tot_len);
  pbuf_free(p);

  return ERR_OK;
}
/*-----------------------------------------------------------------------------------*/
/* hci_write_page_timeout():
 *
 * Define the amount of time a connection request will wait for the remote device
 * to respond before the local device returns a connection failure.
 */
 /*-----------------------------------------------------------------------------------*/
err_t
hci_write_page_timeout(u16_t page_timeout)
{
  struct pbuf *p;
  if((p = pbuf_alloc(PBUF_RAW, HCI_W_PAGE_TIMEOUT_PLEN, PBUF_RAM)) == NULL) {
    LWIP_DEBUGF(HCI_DEBUG, ("hci_write_page_timeout: Could not allocate memory for pbuf\n"));
    return ERR_MEM;
  } 
  /* Assembling command packet */
  p = hci_cmd_ass(p, HCI_W_PAGE_TIMEOUT_OCF, HCI_HC_BB_OGF, HCI_W_PAGE_TIMEOUT_PLEN);
  /* Assembling cmd prameters */
  ((u16_t *)p->payload)[2] = page_timeout;
  phybusif_output(p, p->tot_len);
  pbuf_free(p);

  return ERR_OK;
}
/*-----------------------------------------------------------------------------------*/
/* hci_write_scan_enable():
 *
 * Controls whether or not the Bluetooth device will periodically scan for page 
 * attempts and/or inquiry requests from other Bluetooth devices.
 */
 /*-----------------------------------------------------------------------------------*/
err_t
hci_write_scan_enable(u8_t scan_enable)
{
  struct pbuf *p;
  if((p = pbuf_alloc(PBUF_RAW, HCI_W_SCAN_EN_PLEN, PBUF_RAM)) == NULL) {
    LWIP_DEBUGF(HCI_DEBUG, ("hci_write_scan_enable: Could not allocate memory for pbuf\n"));
    return ERR_MEM;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -