📄 main.c
字号:
/****************************************************************************
Copyright (C) Cambridge Silicon Radio Ltd. 2005-2006
Part of BlueLab 3.5.2-release
FILE NAME
hid_keyboard.c
DESCRIPTION
This file contains the main HID keyboard application.
NOTES
*/
/****************************************************************************
Header files
*/
#include "hid_keyboard.h"
#include "hid_keyboard_sm.h"
#include "hid_keyboard_aux.h"
#include "hid_keyboard_auth.h"
#include "hid_keyboard_events.h"
#include "hid_keyboard_service_record.h"
#include <boot.h>
/*
#ifndef HW_CASIRA_UART
#define HW_CASIRA_UART
#endif*/
/* DIACs that this device will respond to */
static const uint32 app_hid_diac[] =
{
0x9E8B00, 0x9e8b33
};
/* Connection library messages application would like */
static const msg_filter app_msg_filter = {msg_group_mode_change};
/* Application SDP record configuration */
static const hid_config app_keyboard_config =
{
sizeof(hid_keyboard_service_record),
hid_keyboard_service_record
};
/* Application power table */
#define APP_POWER_TABLE_ENTRIES (sizeof(app_keyboard_power_table) / sizeof(lp_power_table))
static const lp_power_table app_keyboard_power_table[]=
{
/* mode, min_interval, max_interval, attempt, timeout, duration */
{lp_sniff, 20, 52, 1, 1, 1},
{lp_sniff, 54, 162, 1, 16, 30},
{lp_sniff, 164, 402, 1, 16, 600},
{lp_sniff, 404, 802, 1, 16, 0}
};
/* Application HID connection configuration */
static const hid_connection_config app_keyboard_connection_config =
{
APP_POWER_TABLE_ENTRIES, app_keyboard_power_table, /* Power table */
11250, /* Latency (11.25ms) */
FALSE, /* Don't use Guaranteed QOS for interrupt channel */
};
const uint8 Button_Data[9][9] = {
{0x01, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x01, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x01, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x01, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x01, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x01, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x01, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x01, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
};
#ifdef HW_DEV_1163A
/* Include key code lookup table */
#include "hid_keyboard_key_codes.h"
/* Matrix sensor configuration */
HID_MATRIX_CONFIG app_hidio_config =
{
sizeof(HID_MATRIX_CONFIG),
18, 8, /* Rows, Columns */
app_keyboard_key_codes /* Key code lookup table */
};
#endif
/*************************************************************************
NAME
appHandleUnexpected
DESCRIPTION
Called when an unexpected message is received.
RETURNS
void
*/
static void appHandleUnexpected(appTaskData *theApp, MessageId id)
{
MAIN_PRINT(("appHandleUnexpected, id = %x\n", id));
}
/*************************************************************************
NAME
appHandleInternalIdleTimeoutInd
DESCRIPTION
Called when the idle timer has expired, enters the local disconnecting
state. This will automatically release the HID connection.
RETURNS
void
*/
static void appHandleInternalIdleTimeoutInd(appTaskData *theApp)
{
appSetState(theApp, appCabledDisconnecting);
}
/*************************************************************************
NAME
appHandleInternalPinCodeTimeoutInd
DESCRIPTION
This function is called when pin code entry has timed out. The
authentication is stopped and the host we are trying to authenticate
with is deleted from the trusted device list.
RETURNS
void
*/
static void appHandleInternalPinCodeTimeoutInd(appTaskData *theApp)
{
MAIN_PRINT(("appHandleInternalPinCodeTimeoutInd\n"));
/* Stop pin code entry */
appStopPinCodeEntry(theApp, FALSE);
/* Delete device from trusted device list */
ConnectionSmDeleteAuthDevice(&theApp->auth_bd_addr);
}
/*************************************************************************
NAME
appHandleInternalDiscoverableTimeoutInd
DESCRIPTION
This function is called when discovery mode has timed out.
RETURNS
void
*/
static void appHandleInternalDiscoverableTimeoutInd(appTaskData *theApp)
{
appSetState(theApp, appIdle);
}
/*************************************************************************
NAME
appHandleInternalUnplugIndDefer
DESCRIPTION
Called to handle the unplug button when the application is not in a
state to handle it immediately. Just sets a flag.
RETURNS
void
*/
static void appHandleInternalUnplugIndDefer(appTaskData *theApp)
{
/* Set unplug flag, we'll handle it later */
theApp->unplug_pending = TRUE;
}
/*************************************************************************
NAME
appHandleInternalUnplugIndConnected
DESCRIPTION
Called when the unplug button is pressed and we are in the connected
state. Sends a virtual unplug request to the host.
RETURNS
void
*/
static void appHandleInternalUnplugIndConnected(appTaskData *theApp)
{
/* Set unplug flag, we'll handle it later */
theApp->unplug_pending = TRUE;
/* Start an unplug timer */
MessageSendLater(&theApp->task, APP_INTERNAL_UNPLUG_TIMEOUT_IND, 0, D_SEC(APP_UNPLUG_TIMEOUT));
/* Send virtual unplug to host */
HidControl(theApp->hid, hid_control_op_unplug);
}
/*************************************************************************
NAME
appHandleInternalUnplugIndDisconnected
DESCRIPTION
Called when the connect button is pressed and we are in the connected
state.
RETURNS
void
*/
static void appHandleInternalUnplugIndDisconnected(appTaskData *theApp)
{
MAIN_PRINT(("appHandleInternalUnplugIndDisconnected\n"));
/* Set unplug flag, we'll handle it later */
theApp->unplug_pending = TRUE;
/* Move to discoverable state */
appSetState(theApp, appDiscoverable);
}
/*************************************************************************
NAME
appHandleInternalUnplugIndDiscoverable
DESCRIPTION
Called when the connect button is pressed and we are in the
discoverable state.
RETURNS
void
*/
static void appHandleInternalUnplugIndDiscoverable(appTaskData *theApp)
{
MAIN_PRINT(("appHandleInternalUnplugIndDiscoverable\n"));
/* Move to discoverable state */
appSetState(theApp, appDiscoverable);
}
/*************************************************************************
NAME
appHandleInternalUnplugTimeoutInd
DESCRIPTION
Called when the virtual unplug operation has timeout, moves to
disconnecting state.
RETURNS
void
*/
static void appHandleInternalUnplugTimeoutInd(appTaskData *theApp)
{
MAIN_PRINT(("appHandleInternalUnplugTimeoutInd\n"));
/* Move to disconnecting state */
appSetState(theApp, appCabledDisconnecting);
}
/*************************************************************************
NAME
appHandleClInitConfirm
DESCRIPTION
Called once the connection library has been initialised. Starts off
HID library initialisation.
RETURNS
void
*/
static void appHandleClInitConfirm(appTaskData *theApp, CL_INIT_CFM_T *cfm)
{
MAIN_PRINT(("appHandleClInitConfirm\n"));
/* Connection Library initialisation was a success */
if (cfm->status == success)
{
/* Write class of device */
ConnectionWriteClassOfDevice(HID_MAJOR_DEVICE_CLASS | HID_MINOR_KEYBOARD);
/* Turn on security */
ConnectionSmSetSecurityMode(&theApp->task, sec_mode0_off, hci_enc_mode_off);
ConnectionSmRegisterIncomingService(protocol_l2cap, 0, secl_none);
/* Response to HID DIAC */
ConnectionWriteInquiryAccessCode(&theApp->task, app_hid_diac, 2);
/* Allow SDP record browsing */
ConnectionSmSetSdpSecurityIn(TRUE);
/* Initialise the HID library */
HidInit(&theApp->task, &app_keyboard_config);
}
else
Panic();
}
/*************************************************************************
NAME
appHandleClSmGetMruAttributeConfirm
DESCRIPTION
Called when the connection library passes the application information
on a trusted device, kicks off a HID connection.
RETURNS
void
*/
static void appHandleClSmGetIndexedAttributeConfirm(appTaskData *theApp, CL_SM_GET_INDEXED_ATTRIBUTE_CFM_T *cfm)
{
MAIN_PRINT(("appHandleClSmGetIndexedAttributeConfirm\n"));
if (cfm->status == success)
{
MAIN_PRINT(("Attempt to connect to %d,%d,%ld\n", cfm->bd_addr.nap, cfm->bd_addr.uap, cfm->bd_addr.lap));
/* Store Bluetooth address of host */
theApp->host_bd_addr = cfm->bd_addr;
/* Attempt to connect to host */
HidConnect(theApp->hid_lib, &theApp->task, &theApp->host_bd_addr, &app_keyboard_connection_config);
}
else
{
/* Check if we have paired with any devices */
if (theApp->trusted_device_index)
{
MAIN_PRINT(("Failed to connect\n"));
/* Reset device index */
theApp->trusted_device_index = 0;
/* Move to disconnected state */
appSetState(theApp, appCabledDisconnected);
}
else
{
MAIN_PRINT(("No devices, making discoverable\n"));
/* Make device discoverable */
appSetState(theApp, appDiscoverable);
}
}
}
/*************************************************************************
NAME
appHandleClSmPinCodeIndication
DESCRIPTION
Called when the connection library requires a pin code from the user.
RETURNS
void
*/
static void appHandleClSmPinCodeIndication(appTaskData *theApp, CL_SM_PIN_CODE_IND_T *ind)
{
MAIN_PRINT(("appHandleClSmPinCodeIndication\n"));
/* Store host bluetooth address */
theApp->auth_bd_addr = ind->bd_addr;
/* Start pin code entry */
/*#ifdef APP_FIXED_PIN
ConnectionSmPinCodeResponse(&theApp->auth_bd_addr, 4, (unsigned char *)"8888");
#else
appStartPinCodeEntry(theApp);
#endif*/
if(theApp->Dyn_Fix_Pin_Code)
ConnectionSmPinCodeResponse(&theApp->auth_bd_addr, 4, (unsigned char *)"8888");
else
appStartPinCodeEntry(theApp);
}
/*************************************************************************
NAME
appRejectClSmPinCodeIndication
DESCRIPTION
Called when the connection library requires a pin code, and we are not
discoverable.
RETURNS
void
*/
static void appRejectClSmPinCodeIndication(appTaskData *theApp, CL_SM_PIN_CODE_IND_T *ind)
{
MAIN_PRINT(("appRejectClSmPinCodeIndication\n"));
/* Send no pin code */
ConnectionSmPinCodeResponse(&theApp->auth_bd_addr, 0, (unsigned char *)"");
}
/*************************************************************************
NAME
appHandleClSmAuthenticateConfirm
DESCRIPTION
Handles authentication confirmation, if successful authentication
state is set as trusted, the host is actually mark as trusted only
once the initial connection has been setup successfully.
RETURNS
void
*/
static void appHandleClSmAuthenticateConfirm(appTaskData *theApp, CL_SM_AUTHENTICATE_CFM_T *cfm)
{
MAIN_PRINT(("appHandleClSmAuthenticateConfirm\n"));
/* Check if authentication was successful */
if (cfm->status == auth_status_success)
{
MAIN_PRINT(("Authentication successful\n"));
/* Stop pin code entry */
appStopPinCodeEntry(theApp, TRUE);
}
else
{
MAIN_PRINT(("Authentication failed\n"));
/* Stop pin code entry */
appStopPinCodeEntry(theApp, FALSE);
}
}
/*************************************************************************
NAME
appHandleClSmAuthenticateConfirm
DESCRIPTION
Handles authentication confirmation, if successful authentication
state is set as trusted, the host is actually mark as trusted only
once the initial connection has been setup successfully.
RETURNS
void
*/
static void appHandleClSmAuthenticateConfirmDiscoverableConnected(appTaskData *theApp, CL_SM_AUTHENTICATE_CFM_T *cfm)
{
MAIN_PRINT(("appHandleClSmAuthenticateConfirmDiscoverableConnected\n"));
/* Check if authentication was successful */
if (cfm->status == auth_status_success)
{
MAIN_PRINT(("Authentication successful\n"));
/* Stop pin code entry */
appStopPinCodeEntry(theApp, TRUE);
/* Connecting successful, now set trust level */
ConnectionSmSetTrustLevel(&theApp->auth_bd_addr, TRUE);
/* Move to cabled connected state */
appSetState(theApp, appCabledConnected);
}
else
{
MAIN_PRINT(("Authentication failed\n"));
/* Stop pin code entry */
appStopPinCodeEntry(theApp, FALSE);
/* Move to discoverable state */
appSetState(theApp, appDiscoverable);
}
}
/*************************************************************************
NAME
appHandleClSmAuthoriseInd
DESCRIPTION
Handles incoming connection authorisation, if connection is from
host we have just authenticated with the connection is accepted,
otherwise it is rejected.
RETURNS
void
*/
static void appHandleClSmAuthoriseInd(appTaskData *theApp, CL_SM_AUTHORISE_IND_T *ind)
{
/* Check if connection is from authorised host */
if (BdaddrIsSame(&theApp->auth_bd_addr, &ind->bd_addr) && (ind->protocol_id == protocol_l2cap))
{
MAIN_PRINT(("appHandleClSmAuthoriseInd, accept\n"));
/* Accept connection */
ConnectionSmAuthoriseResponse(&ind->bd_addr, ind->protocol_id, ind->channel, ind->incoming, TRUE);
}
else
{
MAIN_PRINT(("appHandleClSmAuthoriseInd, reject\n"));
/* Reject connection */
ConnectionSmAuthoriseResponse(&ind->bd_addr, ind->protocol_id, ind->channel, ind->incoming, FALSE);
}
}
/*************************************************************************
NAME
appRejectClSmAuthoriseInd
DESCRIPTION
Rejects incoming connection.
RETURNS
void
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -