📄 main.c
字号:
/* Copyright (C) Cambridge Silicon Radio Ltd. 2005-2006 */
/* Part of BlueLab 3.4.2-release */
#include "spp_dev_private.h"
#include "spp_dev_init.h"
#include "spp_dev_inquire.h"
#include "spp_dev_auth.h"
#include "spp_dev_led.h"
#include <connection.h>
#include <panic.h>
#include <stdio.h>
#include <stream.h>
#include <pio.h>
/*#define DEBUG_ENABLED*/
#ifdef DEBUG_ENABLED
#define DEBUG(x) {printf x;}
#else
#define DEBUG(x)
#endif
static sppTaskData theSppApp;
typedef struct
{
TaskData task;
} PioState;
/*************************************************************************
NAME
sppAppTask
DESCRIPTION
Returns the spp app's main task.
RETURNS
Task
*/
Task getAppTask(void)
{
return &theSppApp.task;
}
/*************************************************************************
NAME
unhandledSppState
DESCRIPTION
This function is called when a message arrives and the Spp app is
in an unexpected state.
RETURNS
*/
static void unhandledSppState(sppDevState state, MessageId id)
{
DEBUG(("SPP current state %d message id 0x%x\n", state, id));
}
/*************************************************************************
NAME
setSppState
DESCRIPTION
Set the SPP State to the specified state
RETURNS
*/
void setSppState(const sppDevState state)
{
DEBUG(("SPP State - C=%d N=%d\n",theSppApp.spp_state, state));
theSppApp.spp_state = state;
sppDevUpdateLed(state);
}
static void pioHandler(Task task, MessageId id, Message data)
{
const MessagePioChanged *m = (const MessagePioChanged *)data;
switch(id)
{
case MESSAGE_PIO_CHANGED:
if (m->state == BUTTON_RESET)
{
DEBUG(("Button pressed\n"));
switch(theSppApp.spp_state)
{
case sppDevInitialising:
return;
break;
case sppDevReady:
case sppDevPairable:
case sppDevConnecting:
sppDevInquire(&theSppApp);
break;
case sppDevConnected:
SppDisconnect(theSppApp.spp);
break;
}
}
default:
break;
}
}
static void PioInit(PioState* state)
{
state->task.handler = pioHandler;
MessagePioTask(&state->task);
PioDebounce(BUTTON_RESET, 1, 0);
}
/* Task handler function */
static void app_handler(Task task, MessageId id, Message message)
{
sppDevState state = theSppApp.spp_state;
switch(id)
{
case CL_INIT_CFM:
DEBUG(("CL_INIT_CFM\n"));
if(((CL_INIT_CFM_T*)message)->status == success)
/* Connection Library initialisation was a success */
sppDevInit();
else
Panic();
break;
case SPP_INIT_CFM:
DEBUG(("SPP_INIT_CFM\n"));
switch(state)
{
case sppDevInitialising:
/* Check for spp_init_success. What do we do if it failed? */
if (((SPP_INIT_CFM_T *) message)->status == spp_init_success)
{
setSppState(sppDevReady);
theSppApp.spp = ((SPP_INIT_CFM_T *) message)->spp;
sppDevInquire(&theSppApp);
}
break;
case sppDevReady:
case sppDevPairable:
case sppDevConnecting:
case sppDevConnected:
default:
unhandledSppState(state, id);
break;
}
break;
case SPP_CONNECT_CFM:
DEBUG(("SPP_CONNECT_CFM result = %d\n",((SPP_CONNECT_CFM_T*)message)->status));
switch(state)
{
case sppDevConnecting:
/* Connect cfm, but must check status as connection may have failed */
if (((SPP_CONNECT_CFM_T*)message)->status == rfcomm_connect_success)
{
/* Connection Success */
DEBUG(("Device connected...\n"));
/* Connect Uart to Rfcomm */
(void) StreamConnect(StreamUartSource(), ((SPP_CONNECT_CFM_T*)message)->sink);
(void) StreamConnect(StreamSourceFromSink(((SPP_CONNECT_CFM_T*)message)->sink), StreamUartSink());
setSppState(sppDevConnected);
ConnectionWriteScanEnable(hci_scan_enable_off);
(void) MessageCancelFirst(&theSppApp.task, SPP_DEV_INQUIRY_TIMEOUT_IND);
}
else
{
/* Connection failed */
setSppState(sppDevPairable);
DEBUG(("Connection failed\n"));
}
break;
case sppDevPairable:
/* Connect cfm, but must check status as connection may have failed */
if (((SPP_CONNECT_CFM_T*)message)->status == rfcomm_connect_success)
{
/* Device has been reset to pairable mode. Disconnect from current device */
SppDisconnect(theSppApp.spp);
}
break;
case sppDevInitialising:
case sppDevReady:
case sppDevConnected:
default:
unhandledSppState(state, id);
break;
}
break;
case SPP_CONNECT_IND:
DEBUG(("SPP_CONNECT_IND\n"));
switch(state)
{
case sppDevPairable:
/* Received command that a device is trying to connect. Send response. */
sppDevAuthoriseConnectInd(&theSppApp,(SPP_CONNECT_IND_T*)message);
setSppState(sppDevConnecting);
break;
case sppDevInitialising:
case sppDevConnecting:
case sppDevReady:
case sppDevConnected:
default:
unhandledSppState(state, id);
break;
}
break;
case SPP_DISCONNECT_IND:
DEBUG(("SPP_DISCONNECT_IND\n"));
/* Disconnect message has arrived */
switch(state)
{
case sppDevConnected:
sppDevInquire(&theSppApp);
break;
case sppDevInitialising:
case sppDevPairable:
case sppDevConnecting:
case sppDevReady:
default:
unhandledSppState(state, id);
break;
}
break;
case SPP_DEV_INQUIRY_TIMEOUT_IND:
DEBUG(("SPP_DEV_INQUIRY_TIMEOUT_IND\n"));
switch(state)
{
case sppDevPairable:
/* Inquiry mode timed out */
ConnectionWriteScanEnable(hci_scan_enable_off);
setSppState(sppDevReady);
break;
case sppDevConnected:
case sppDevInitialising:
case sppDevConnecting:
case sppDevReady:
default:
unhandledSppState(state, id);
break;
}
break;
case SPP_DEV_UDPATE_LED:
sppDevUpdateLed(theSppApp.spp_state);
break;
case CL_SM_PIN_CODE_IND:
DEBUG(("CL_SM_PIN_CODE_IND\n"));
sppDevHandlePinCodeRequest((CL_SM_PIN_CODE_IND_T *) message);
break;
case CL_SM_AUTHORISE_IND:
DEBUG(("CL_SM_PIN_CODE_IND\n"));
sppDevAuthoriseResponse((CL_SM_AUTHORISE_IND_T*) message);
break;
case CL_SM_AUTHENTICATE_CFM:
DEBUG(("CL_SM_AUTHENTICATE_CFM\n"));
sppDevSetTrustLevel((CL_SM_AUTHENTICATE_CFM_T*)message);
break;
case CL_DM_LINK_POLICY_IND:
DEBUG(("CL_DM_LINK_POLICY_IND\n"));
break;
default:
/* An unexpected message has arrived - must handle it */
DEBUG(("main app - msg type not yet handled 0x%x\n", id));
break;
}
}
int main(void)
{
PioState pio_state;
DEBUG(("Main Started...\n"));
#ifndef NO_UART_CHECK
/* Make sure Uart has been successfully initialised before running */
if (StreamUartSource())
#endif
{
/* Set up task 1 handler */
theSppApp.task.handler = app_handler;
setSppState(sppDevInitialising);
PioInit(&pio_state);
sppDevLedInit();
/* Init the Connection Manager */
ConnectionInit(&theSppApp.task);
/* Start the message scheduler loop */
MessageLoop();
}
/* Will never get here! */
DEBUG(("Main Ended!\n"));
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -