📄 app_dongle.c
字号:
/****************************************************************************
Copyright (C) Cambridge Silicon Radio Ltd. 2005-2008
Part of BlueLab 4.0.1-release
FILE NAME
app_dongle.c
DESCRIPTION
This file contains the HID dongle task.
NOTES
*/
/****************************************************************************
Header files
*/
#include "hid.h"
#include "app_usb.h"
#include "app_device.h"
#include "app_led.h"
#include "app_dongle_button.h"
#include <connection.h>
#include <message.h>
#include <vm.h>
#include <stream.h>
#include <bdaddr.h>
#include <stdio.h>
#include <source.h>
#include <string.h>
#include <assert.h>
#include <panic.h>
#include <stdlib.h>
#include <ps.h>
#ifdef APP_DEBUG
#include <stdio.h>
#define MAIN_PRINT(x) printf x
#else
#define MAIN_PRINT(x)
#endif
/*************************************************************************
NAME
appUpdatePageScanEnable
DESCRIPTION
This function is called to enable/disable page scan.
It checks to see if there any disconnected devices.
RETURNS
void
*/
static void appUpdatePageScanEnable(appDongleTaskData *theApp)
{
int dev;
bool page_scan_enable = FALSE;
/* Check if there are any device that may want to connect to us */
for (dev = 0; dev < APP_DONGLE_NUM_DEVICES; dev++)
{
appDeviceTaskData *theDevice = &theApp->dev_table[dev];
/* Check if device is not empty and not currently connected */
if (appDeviceIsDisconnected(theDevice))
page_scan_enable = TRUE;
}
MAIN_PRINT(("Scan Enable %d\n", page_scan_enable ? hci_scan_enable_page : hci_scan_enable_off));
/* Turn page scan enable on or off */
ConnectionWriteScanEnable(page_scan_enable ? hci_scan_enable_page : hci_scan_enable_off);
}
/*************************************************************************
NAME
appIsInquiryRequired
DESCRIPTION
This function is called to check if we need to do inquiry.
It checks if there any empty device slots in the table.
RETURNS
*/
static bool appIsInquiryRequired(appDongleTaskData *theApp, bool initial)
{
int dev;
int dev_empty_count = 0;
/* Check if we need to do inquiry */
for (dev = 0; dev < APP_DONGLE_NUM_DEVICES; dev++)
{
appDeviceTaskData *theDevice = &theApp->dev_table[dev];
if (appDeviceIsEmpty(theDevice, 0xFFFFFFFF))
dev_empty_count++;
}
return initial ? (dev_empty_count == APP_DONGLE_NUM_DEVICES) : (dev_empty_count > 0);
}
/*************************************************************************
NAME
appDongleInquiryEnter
DESCRIPTION
This function is called whenever the dongle enters inquiry scan mode.
RETURNS
void
*/
static void appDongleInquiryEnter(appDongleTaskData *theApp)
{
MAIN_PRINT(("appDongleInquiryEnter\n"));
/* Start flashing LED */
LedUpdate(&theApp->led, 250, 250, 0);
/* Start inquiry scan */
ConnectionInquire(&theApp->task, 0x9E8B00, 0, 23, HID_MAJOR_DEVICE_CLASS);
}
/*************************************************************************
NAME
appDongleInquiryExit
DESCRIPTION
This function is called whenever the dongle exits inquiry scan state.
Turns flashing LED off and cancels inquiry scan.
RETURNS
void
*/
static void appDongleInquiryExit(appDongleTaskData *theApp)
{
MAIN_PRINT(("appDongleInquiryExit\n"));
/* Turn LED off */
LedUpdate(&theApp->led, 0, -1, 0);
/* Stop inquiry scan */
ConnectionInquireCancel(&theApp->task);
}
/*************************************************************************
NAME
appDongleActiveExit
DESCRIPTION
This function is called whenever the dongle enters active state.
RETURNS
void
*/
static void appDongleActiveExit(appDongleTaskData *theApp)
{
MAIN_PRINT(("appDongleActiveExit\n"));
}
/*************************************************************************
NAME
appGetState
DESCRIPTION
Returns current state.
RETURNS
appDongleState - Current state.
*/
static appDongleState appGetState(appDongleTaskData *theApp)
{
return theApp->state;
}
/*************************************************************************
NAME
appSetState
DESCRIPTION
This function sets the new state, it handles calling any exit function
for the old state and any entry function for the new state.
RETURNS
void
*/
static void appSetState(appDongleTaskData *theApp, appDongleState state)
{
if (state != theApp->state)
{
/* Handle exiting old state */
switch (theApp->state)
{
case appDongleInquiry:
appDongleInquiryExit(theApp);
break;
case appDongleActive:
appDongleActiveExit(theApp);
default:
break;
}
/* Set new state */
theApp->state = state;
/* Handle entering new state */
switch (theApp->state)
{
case appDongleInquiry:
appDongleInquiryEnter(theApp);
break;
default:
break;
}
}
}
/*************************************************************************
NAME
appHandleUnexpected
DESCRIPTION
Called when an unexpected message is received.
RETURNS
void
*/
static void appHandleUnexpected(appDongleTaskData *theApp, MessageId id)
{
MAIN_PRINT(("appHandleUnexpected, %x\n", id));
}
/*************************************************************************
NAME
appHandleClInitCfm
DESCRIPTION
This function is called when the Connection library has initialised.
If initialisation was successful then the HID library is initialised.
RETURNS
void
*/
static void appHandleClInitCfm(appDongleTaskData *theApp, CL_INIT_CFM_T *cfm)
{
MAIN_PRINT(("appHandleClInitCfm\n"));
/* Connection Library initialisation was a success, initialise the HID library */
if (cfm->status == success)
HidInit(&theApp->task, NULL);
else
Panic();
}
/*************************************************************************
NAME
appHandleHidInitCfm
DESCRIPTION
This function is called when the HID library has initialised.
If initialisation was successful then the Connection library security
is configured, and the HID devices are checked to see if there is a
need to do an inquiry scan.
RETURNS
void
*/
static void appHandleHidInitCfm(appDongleTaskData *theApp, HID_INIT_CFM_T *cfm)
{
MAIN_PRINT(("appHandleHidInitCfm\n"));
/* Check for hid_init_success */
if (cfm->status == hid_init_success)
{
/* Store handle to HID library */
theApp->hid_lib = cfm->hid_lib;
/* Page scan for 100 slots out of every 150 */
ConnectionWritePagescanActivity(150, 100);
/* Configure security */
ConnectionSmSetSecurityMode(&theApp->task, sec_mode0_off, hci_enc_mode_off);
/* Check if we need to this host connectable (page scan) */
appUpdatePageScanEnable(theApp);
/* Move to inquiry or active state */
appSetState(theApp, appIsInquiryRequired(theApp, TRUE) ? appDongleInquiry : appDongleActive);
}
else
Panic();
}
/*************************************************************************
NAME
appHandleHidConnectInd
DESCRIPTION
Handles incomming HID connection, searches device table, if match found
send message to device task, the device task will then handle the
incomming connection. If no match is found then the connection is
rejected
RETURNS
void
*/
static void appHandleHidConnectInd(appDongleTaskData *theApp, HID_CONNECT_IND_T *ind)
{
int dev;
MAIN_PRINT(("appHandleHidConnectInd\n"));
/* Look for matching device */
for (dev = 0; dev < APP_DONGLE_NUM_DEVICES; dev++)
{
appDeviceTaskData *theDevice = &theApp->dev_table[dev];
if (appDeviceIsBdaddr(theDevice, &ind->bd_addr))
{
/* Pass HID_CONNECT_IND to device task */
appDeviceConnectIndication(theDevice, ind->hid);
return;
}
}
/* If we get here then no device wants this connection so just reject it */
HidConnectResponse(ind->hid, &theApp->task, FALSE, NULL);
}
/*************************************************************************
NAME
appRejectHidConnectInd
DESCRIPTION
Rejects incoming HID connection, called when dongle is not in a state to
handle a HID connection.
RETURNS
void
*/
static void appRejectHidConnectInd(appDongleTaskData *theApp, HID_CONNECT_IND_T *ind)
{
/* Reject connection */
HidConnectResponse(ind->hid, &theApp->task, FALSE, NULL);
}
/*************************************************************************
NAME
appHandleClDmInquireResult
DESCRIPTION
This function handles inquiry results.
RETURNS
void
*/
static void appHandleClDmInquireResult(appDongleTaskData *theApp, CL_DM_INQUIRE_RESULT_T *res)
{
MAIN_PRINT(("appHandleClDmInquireResult\n"));
/* Check if we have a result */
if (res->status == inquiry_status_result)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -