| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833 |
- /********************************** (C) COPYRIGHT *******************************
- * File Name : peripheral.C
- * Author : WCH
- * Version : V1.0
- * Date : 2018/12/10
- * Description : Peripheral slave multi-connection application,
- * initialize broadcast connection parameters, then broadcast,
- * after connecting to the host, request to update connection parameters,
- * and transmit data through custom services
- *********************************************************************************
- * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
- * Attention: This software (modified or not) and binary are used for
- * microcontroller manufactured by Nanjing Qinheng Microelectronics.
- *******************************************************************************/
- /*********************************************************************
- * INCLUDES
- */
- #include "CONFIG.h"
- #include "devinfoservice.h"
- #include "gattprofile.h"
- #include "peripheral.h"
- /*********************************************************************
- * MACROS
- */
- /*********************************************************************
- * CONSTANTS
- */
- // How often to perform periodic event
- #define SBP_PERIODIC_EVT_PERIOD 1600
- // How often to perform read rssi event
- #define SBP_READ_RSSI_EVT_PERIOD 3200
- // Parameter update delay
- #define SBP_PARAM_UPDATE_DELAY 6400
- // PHY update delay
- #define SBP_PHY_UPDATE_DELAY 2400
- // What is the advertising interval when device is discoverable (units of 625us, 80=50ms)
- #define DEFAULT_ADVERTISING_INTERVAL 80
- // Limited discoverable mode advertises for 30.72s, and then stops
- // General discoverable mode advertises indefinitely
- #define DEFAULT_DISCOVERABLE_MODE GAP_ADTYPE_FLAGS_GENERAL
- // Minimum connection interval (units of 1.25ms, 6=7.5ms)
- #define DEFAULT_DESIRED_MIN_CONN_INTERVAL 6
- // Maximum connection interval (units of 1.25ms, 100=125ms)
- #define DEFAULT_DESIRED_MAX_CONN_INTERVAL 1000
- // Slave latency to use parameter update
- #define DEFAULT_DESIRED_SLAVE_LATENCY 0
- // Supervision timeout value (units of 10ms, 100=1s)
- #define DEFAULT_DESIRED_CONN_TIMEOUT 100
- // Company Identifier: WCH
- #define WCH_COMPANY_ID 0x07D7
- /*********************************************************************
- * TYPEDEFS
- */
- /*********************************************************************
- * GLOBAL VARIABLES
- */
- /*********************************************************************
- * EXTERNAL VARIABLES
- */
- /*********************************************************************
- * EXTERNAL FUNCTIONS
- */
- /*********************************************************************
- * LOCAL VARIABLES
- */
- static uint8_t Peripheral_TaskID = INVALID_TASK_ID; // Task ID for internal task/event processing
- // GAP - SCAN RSP data (max size = 31 bytes)
- static uint8_t scanRspData[] = {
- // complete name
- 0x08, // length of this data
- GAP_ADTYPE_LOCAL_NAME_COMPLETE,
- 'K', 'E', 'Y', 'B', 'A', 'S', 'E',
- // connection interval range
- 0x05, // length of this data
- GAP_ADTYPE_SLAVE_CONN_INTERVAL_RANGE,
- LO_UINT16(DEFAULT_DESIRED_MIN_CONN_INTERVAL), // 100ms
- HI_UINT16(DEFAULT_DESIRED_MIN_CONN_INTERVAL),
- LO_UINT16(DEFAULT_DESIRED_MAX_CONN_INTERVAL), // 1s
- HI_UINT16(DEFAULT_DESIRED_MAX_CONN_INTERVAL),
- // Tx power level
- 0x02, // length of this data
- GAP_ADTYPE_POWER_LEVEL,
- 0 // 0dBm
- };
- // GAP - Advertisement data (max size = 31 bytes, though this is
- // best kept short to conserve power while advertising)
- static uint8_t advertData[] = {
- // Flags; this sets the device to use limited discoverable
- // mode (advertises for 30 seconds at a time) instead of general
- // discoverable mode (advertises indefinitely)
- 0x02, // length of this data
- GAP_ADTYPE_FLAGS,
- DEFAULT_DISCOVERABLE_MODE | GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED,
- // service UUID, to notify central devices what services are included
- // in this peripheral
- 0x03, // length of this data
- GAP_ADTYPE_16BIT_MORE, // some of the UUID's, but not all
- LO_UINT16(SIMPLEPROFILE_SERV_UUID),
- HI_UINT16(SIMPLEPROFILE_SERV_UUID),
- //指向制造商自定义的数据
- 0x0D,
- GAP_ADTYPE_MANUFACTURER_SPECIFIC,
- 0xD7, 0x07, 0xFF, 0x01,
- SOFTWARE_VERSION, 0x01,
- 0xFF, 0xFF, 0xFF,
- 0xFF, 0xFF, 0xFF
- };
- // GAP GATT Attributes
- static uint8_t attDeviceName[GAP_DEVICE_NAME_LEN] = "KEYBASE";
- // Connection item list
- static peripheralConnItem_t peripheralConnList;
- static uint8_t peripheralMTU = ATT_MTU_SIZE;
- static download_ble_data_func download_ble_data = NULL;
- static download_ble_data_func download_ble_file = NULL;
- static ble_connect_func ble_connect = NULL;
- /*********************************************************************
- * LOCAL FUNCTIONS
- */
- static void Peripheral_ProcessTMOSMsg(tmos_event_hdr_t *pMsg);
- static void peripheralStateNotificationCB(gapRole_States_t newState, gapRoleEvent_t *pEvent);
- static void performPeriodicTask(void);
- static void simpleProfileChangeCB(uint8_t paramID, uint8_t *pValue, uint16_t len);
- static void peripheralParamUpdateCB(uint16_t connHandle, uint16_t connInterval,
- uint16_t connSlaveLatency, uint16_t connTimeout);
- static void peripheralInitConnItem(peripheralConnItem_t *peripheralConnList);
- static void peripheralRssiCB(uint16_t connHandle, int8_t rssi);
- static void peripheralChar4Notify(uint8_t *pValue, uint16_t len);
- /*********************************************************************
- * PROFILE CALLBACKS
- */
- // GAP Role Callbacks
- static gapRolesCBs_t Peripheral_PeripheralCBs = {
- peripheralStateNotificationCB, // Profile State Change Callbacks
- peripheralRssiCB, // When a valid RSSI is read from controller (not used by application)
- peripheralParamUpdateCB
- };
- // Broadcast Callbacks
- static gapRolesBroadcasterCBs_t Broadcaster_BroadcasterCBs = {
- NULL, // Not used in peripheral role
- NULL // Receive scan request callback
- };
- // GAP Bond Manager Callbacks
- static gapBondCBs_t Peripheral_BondMgrCBs = {
- NULL, // Passcode callback (not used by application)
- NULL, // Pairing / Bonding state Callback (not used by application)
- NULL // oob callback
- };
- // Simple GATT Profile Callbacks
- static simpleProfileCBs_t Peripheral_SimpleProfileCBs = {
- simpleProfileChangeCB // Characteristic value change callback
- };
- /*********************************************************************
- * PUBLIC FUNCTIONS
- */
- /*********************************************************************
- * @fn Peripheral_Init
- *
- * @brief Initialization function for the Peripheral App Task.
- * This is called during initialization and should contain
- * any application specific initialization (ie. hardware
- * initialization/setup, table initialization, power up
- * notificaiton ... ).
- *
- * @param task_id - the ID assigned by TMOS. This ID should be
- * used to send messages and set timers.
- *
- * @return none
- */
- void Peripheral_Init(void)
- {
- Peripheral_TaskID = TMOS_ProcessEventRegister(Peripheral_ProcessEvent);
- // Setup the GAP Peripheral Role Profile
- {
- uint8_t initial_advertising_enable = TRUE;
- uint16_t desired_min_interval = DEFAULT_DESIRED_MIN_CONN_INTERVAL;
- uint16_t desired_max_interval = DEFAULT_DESIRED_MAX_CONN_INTERVAL;
- advertData[sizeof(advertData) - 6] = MacAddr[0];
- advertData[sizeof(advertData) - 5] = MacAddr[1];
- advertData[sizeof(advertData) - 4] = MacAddr[2];
- advertData[sizeof(advertData) - 3] = MacAddr[3];
- advertData[sizeof(advertData) - 2] = MacAddr[4];
- advertData[sizeof(advertData) - 1] = MacAddr[5];
- // Set the GAP Role Parameters
- GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &initial_advertising_enable);
- GAPRole_SetParameter(GAPROLE_SCAN_RSP_DATA, sizeof(scanRspData), scanRspData);
- GAPRole_SetParameter(GAPROLE_ADVERT_DATA, sizeof(advertData), advertData);
- GAPRole_SetParameter(GAPROLE_MIN_CONN_INTERVAL, sizeof(uint16_t), &desired_min_interval);
- GAPRole_SetParameter(GAPROLE_MAX_CONN_INTERVAL, sizeof(uint16_t), &desired_max_interval);
- }
- // Set the GAP Characteristics
- GGS_SetParameter(GGS_DEVICE_NAME_ATT, GAP_DEVICE_NAME_LEN, attDeviceName);
- {
- uint16_t advInt = DEFAULT_ADVERTISING_INTERVAL;
- // Set advertising interval
- GAP_SetParamValue(TGAP_DISC_ADV_INT_MIN, advInt);
- GAP_SetParamValue(TGAP_DISC_ADV_INT_MAX, advInt);
- // Enable scan req notify
- GAP_SetParamValue(TGAP_ADV_SCAN_REQ_NOTIFY, ENABLE);
- }
- // Setup the GAP Bond Manager
- {
- uint32_t passkey = 0; // passkey "000000"
- uint8_t pairMode = GAPBOND_PAIRING_MODE_NO_PAIRING;//GAPBOND_PAIRING_MODE_WAIT_FOR_REQ;
- uint8_t mitm = TRUE;
- uint8_t bonding = FALSE;
- uint8_t ioCap = GAPBOND_IO_CAP_DISPLAY_ONLY;
- GAPBondMgr_SetParameter(GAPBOND_PERI_DEFAULT_PASSCODE, sizeof(uint32_t), &passkey);
- GAPBondMgr_SetParameter(GAPBOND_PERI_PAIRING_MODE, sizeof(uint8_t), &pairMode);
- GAPBondMgr_SetParameter(GAPBOND_PERI_MITM_PROTECTION, sizeof(uint8_t), &mitm);
- GAPBondMgr_SetParameter(GAPBOND_PERI_IO_CAPABILITIES, sizeof(uint8_t), &ioCap);
- GAPBondMgr_SetParameter(GAPBOND_PERI_BONDING_ENABLED, sizeof(uint8_t), &bonding);
- }
- // Initialize GATT attributes
- GGS_AddService(GATT_ALL_SERVICES); // GAP
- GATTServApp_AddService(GATT_ALL_SERVICES); // GATT attributes
- DevInfo_AddService(); // Device Information Service
- SimpleProfile_AddService(GATT_ALL_SERVICES); // Simple GATT Profile
- // Setup the SimpleProfile Characteristic Values
- {
- uint8_t charValue1[SIMPLEPROFILE_CHAR1_LEN] = {1};
- uint8_t charValue2[SIMPLEPROFILE_CHAR2_LEN] = {0};
- uint8_t charValue3[SIMPLEPROFILE_CHAR3_LEN] = {0};
- uint8_t charValue4[SIMPLEPROFILE_CHAR4_LEN] = {0};
- #if(DEBUG == DEBUG_UART_BLE)
- uint8_t charValue5[SIMPLEPROFILE_CHAR5_LEN] = {1, 2, 3, 4, 5};
- #endif
- SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR1, SIMPLEPROFILE_CHAR1_LEN, charValue1);
- SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR2, SIMPLEPROFILE_CHAR2_LEN, charValue2);
- SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR3, SIMPLEPROFILE_CHAR3_LEN, charValue3);
- SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR4, SIMPLEPROFILE_CHAR4_LEN, charValue4);
- #if(DEBUG == DEBUG_UART_BLE)
- SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR5, SIMPLEPROFILE_CHAR5_LEN, charValue5);
- #endif
- }
- // Init Connection Item
- peripheralInitConnItem(&peripheralConnList);
- // Register callback with SimpleGATTprofile
- SimpleProfile_RegisterAppCBs(&Peripheral_SimpleProfileCBs);
- // Register receive scan request callback
- GAPRole_BroadcasterSetCB(&Broadcaster_BroadcasterCBs);
- // Setup a delayed profile startup
- tmos_set_event(Peripheral_TaskID, SBP_START_DEVICE_EVT);
- }
- /*********************************************************************
- * @fn peripheralInitConnItem
- *
- * @brief Init Connection Item
- *
- * @param peripheralConnList -
- *
- * @return NULL
- */
- static void peripheralInitConnItem(peripheralConnItem_t *peripheralConnList)
- {
- peripheralConnList->connHandle = GAP_CONNHANDLE_INIT;
- peripheralConnList->connInterval = 0;
- peripheralConnList->connSlaveLatency = 0;
- peripheralConnList->connTimeout = 0;
- }
- /*********************************************************************
- * @fn Peripheral_ProcessEvent
- *
- * @brief Peripheral Application Task event processor. This function
- * is called to process all events for the task. Events
- * include timers, messages and any other user defined events.
- *
- * @param task_id - The TMOS assigned task ID.
- * @param events - events to process. This is a bit map and can
- * contain more than one event.
- *
- * @return events not processed
- */
- uint16_t Peripheral_ProcessEvent(uint8_t task_id, uint16_t events)
- {
- // VOID task_id; // TMOS required parameter that isn't used in this function
- if(events & SYS_EVENT_MSG)
- {
- uint8_t *pMsg;
- if((pMsg = tmos_msg_receive(Peripheral_TaskID)) != NULL)
- {
- Peripheral_ProcessTMOSMsg((tmos_event_hdr_t *)pMsg);
- // Release the TMOS message
- tmos_msg_deallocate(pMsg);
- }
- // return unprocessed events
- return (events ^ SYS_EVENT_MSG);
- }
- if(events & SBP_START_DEVICE_EVT)
- {
- // Start the Device
- GAPRole_PeripheralStartDevice(Peripheral_TaskID, &Peripheral_BondMgrCBs, &Peripheral_PeripheralCBs);
- return (events ^ SBP_START_DEVICE_EVT);
- }
- if(events & SBP_PERIODIC_EVT)
- {
- // Restart timer
- if(SBP_PERIODIC_EVT_PERIOD)
- {
- tmos_start_task(Peripheral_TaskID, SBP_PERIODIC_EVT, SBP_PERIODIC_EVT_PERIOD);
- }
- // Perform periodic application task
- performPeriodicTask();
- return (events ^ SBP_PERIODIC_EVT);
- }
- if(events & SBP_PARAM_UPDATE_EVT)
- {
- // Send connect param update request
- GAPRole_PeripheralConnParamUpdateReq(peripheralConnList.connHandle,
- DEFAULT_DESIRED_MIN_CONN_INTERVAL,
- DEFAULT_DESIRED_MAX_CONN_INTERVAL,
- DEFAULT_DESIRED_SLAVE_LATENCY,
- DEFAULT_DESIRED_CONN_TIMEOUT,
- Peripheral_TaskID);
- return (events ^ SBP_PARAM_UPDATE_EVT);
- }
- if(events & SBP_PHY_UPDATE_EVT)
- {
- // start phy update
- PRINT("PHY Update %x...\n", GAPRole_UpdatePHY(peripheralConnList.connHandle, 0, GAP_PHY_BIT_LE_2M,
- GAP_PHY_BIT_LE_2M, 0));
- return (events ^ SBP_PHY_UPDATE_EVT);
- }
- if(events & SBP_READ_RSSI_EVT)
- {
- GAPRole_ReadRssiCmd(peripheralConnList.connHandle);
- tmos_start_task(Peripheral_TaskID, SBP_READ_RSSI_EVT, SBP_READ_RSSI_EVT_PERIOD);
- return (events ^ SBP_READ_RSSI_EVT);
- }
- // Discard unknown events
- return 0;
- }
- /*********************************************************************
- * @fn Peripheral_ProcessGAPMsg
- *
- * @brief Process an incoming task message.
- *
- * @param pMsg - message to process
- *
- * @return none
- */
- static void Peripheral_ProcessGAPMsg(gapRoleEvent_t *pEvent)
- {
- switch(pEvent->gap.opcode)
- {
- case GAP_SCAN_REQUEST_EVENT:
- {
- // PRINT("Receive scan req from %x %x %x %x %x %x ..\n", pEvent->scanReqEvt.scannerAddr[0],
- // pEvent->scanReqEvt.scannerAddr[1], pEvent->scanReqEvt.scannerAddr[2], pEvent->scanReqEvt.scannerAddr[3],
- // pEvent->scanReqEvt.scannerAddr[4], pEvent->scanReqEvt.scannerAddr[5]);
- break;
- }
- case GAP_PHY_UPDATE_EVENT:
- {
- PRINT("Phy update Rx:%x Tx:%x ..\n", pEvent->linkPhyUpdate.connRxPHYS, pEvent->linkPhyUpdate.connTxPHYS);
- break;
- }
- default:
- break;
- }
- }
- /*********************************************************************
- * @fn Peripheral_ProcessTMOSMsg
- *
- * @brief Process an incoming task message.
- *
- * @param pMsg - message to process
- *
- * @return none
- */
- static void Peripheral_ProcessTMOSMsg(tmos_event_hdr_t *pMsg)
- {
- switch(pMsg->event)
- {
- case GAP_MSG_EVENT:
- {
- Peripheral_ProcessGAPMsg((gapRoleEvent_t *)pMsg);
- break;
- }
- case GATT_MSG_EVENT:
- {
- gattMsgEvent_t *pMsgEvent;
- pMsgEvent = (gattMsgEvent_t *)pMsg;
- if(pMsgEvent->method == ATT_MTU_UPDATED_EVENT)
- {
- peripheralMTU = pMsgEvent->msg.exchangeMTUReq.clientRxMTU;
- PRINT("mtu exchange: %d\n", pMsgEvent->msg.exchangeMTUReq.clientRxMTU);
- }
- break;
- }
- default:
- break;
- }
- }
- /*********************************************************************
- * @fn Peripheral_LinkEstablished
- *
- * @brief Process link established.
- *
- * @param pEvent - event to process
- *
- * @return none
- */
- static void Peripheral_LinkEstablished(gapRoleEvent_t *pEvent)
- {
- gapEstLinkReqEvent_t *event = (gapEstLinkReqEvent_t *)pEvent;
- // See if already connected
- if(peripheralConnList.connHandle != GAP_CONNHANDLE_INIT)
- {
- GAPRole_TerminateLink(pEvent->linkCmpl.connectionHandle);
- PRINT("Connection max...\n");
- }
- else
- {
- peripheralConnList.connHandle = event->connectionHandle;
- peripheralConnList.connInterval = event->connInterval;
- peripheralConnList.connSlaveLatency = event->connLatency;
- peripheralConnList.connTimeout = event->connTimeout;
- // Set timer for periodic event
- tmos_start_task(Peripheral_TaskID, SBP_PERIODIC_EVT, SBP_PERIODIC_EVT_PERIOD);
- // Set timer for param update event
- tmos_start_task(Peripheral_TaskID, SBP_PARAM_UPDATE_EVT, SBP_PARAM_UPDATE_DELAY);
- // Start read rssi
- tmos_start_task(Peripheral_TaskID, SBP_READ_RSSI_EVT, SBP_READ_RSSI_EVT_PERIOD);
- PRINT("Conn %x - Int %x \n", event->connectionHandle, event->connInterval);
- }
- }
- /*********************************************************************
- * @fn Peripheral_LinkTerminated
- *
- * @brief Process link terminated.
- *
- * @param pEvent - event to process
- *
- * @return none
- */
- static void Peripheral_LinkTerminated(gapRoleEvent_t *pEvent)
- {
- gapTerminateLinkEvent_t *event = (gapTerminateLinkEvent_t *)pEvent;
- if(event->connectionHandle == peripheralConnList.connHandle)
- {
- peripheralConnList.connHandle = GAP_CONNHANDLE_INIT;
- peripheralConnList.connInterval = 0;
- peripheralConnList.connSlaveLatency = 0;
- peripheralConnList.connTimeout = 0;
- tmos_stop_task(Peripheral_TaskID, SBP_PERIODIC_EVT);
- tmos_stop_task(Peripheral_TaskID, SBP_READ_RSSI_EVT);
- // Restart advertising
- {
- uint8_t advertising_enable = TRUE;
- GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &advertising_enable);
- }
- }
- else
- {
- PRINT("ERR..\n");
- }
- }
- /*********************************************************************
- * @fn peripheralRssiCB
- *
- * @brief RSSI callback.
- *
- * @param connHandle - connection handle
- * @param rssi - RSSI
- *
- * @return none
- */
- static void peripheralRssiCB(uint16_t connHandle, int8_t rssi)
- {
- // PRINT("RSSI -%d dB Conn %x \n", -rssi, connHandle);
- }
- /*********************************************************************
- * @fn peripheralParamUpdateCB
- *
- * @brief Parameter update complete callback
- *
- * @param connHandle - connect handle
- * connInterval - connect interval
- * connSlaveLatency - connect slave latency
- * connTimeout - connect timeout
- *
- * @return none
- */
- static void peripheralParamUpdateCB(uint16_t connHandle, uint16_t connInterval,
- uint16_t connSlaveLatency, uint16_t connTimeout)
- {
- if(connHandle == peripheralConnList.connHandle)
- {
- peripheralConnList.connInterval = connInterval;
- peripheralConnList.connSlaveLatency = connSlaveLatency;
- peripheralConnList.connTimeout = connTimeout;
- PRINT("Update %x - Int %x \n", connHandle, connInterval);
- }
- else
- {
- PRINT("ERR..\n");
- }
- }
- /*********************************************************************
- * @fn peripheralStateNotificationCB
- *
- * @brief Notification from the profile of a state change.
- *
- * @param newState - new state
- *
- * @return none
- */
- static void peripheralStateNotificationCB(gapRole_States_t newState, gapRoleEvent_t *pEvent)
- {
- switch(newState & GAPROLE_STATE_ADV_MASK)
- {
- case GAPROLE_STARTED:
- PRINT("Initialized..\n");
- break;
- case GAPROLE_ADVERTISING:
- if(pEvent->gap.opcode == GAP_LINK_TERMINATED_EVENT)
- {
- Peripheral_LinkTerminated(pEvent);
- PRINT("Disconnected.. Reason:%x\n", pEvent->linkTerminate.reason);
- PRINT("Advertising..\n");
- if(ble_connect != NULL)
- {
- ble_connect(false);
- }
- }
- else if(pEvent->gap.opcode == GAP_MAKE_DISCOVERABLE_DONE_EVENT)
- {
- PRINT("Advertising..\n");
- }
- break;
- case GAPROLE_CONNECTED:
- if(pEvent->gap.opcode == GAP_LINK_ESTABLISHED_EVENT)
- {
- Peripheral_LinkEstablished(pEvent);
- PRINT("Connected..\n");
- if(ble_connect != NULL)
- {
- ble_connect(true);
- }
- }
- break;
- case GAPROLE_CONNECTED_ADV:
- if(pEvent->gap.opcode == GAP_MAKE_DISCOVERABLE_DONE_EVENT)
- {
- PRINT("Connected Advertising..\n");
- }
- break;
- case GAPROLE_WAITING:
- if(pEvent->gap.opcode == GAP_END_DISCOVERABLE_DONE_EVENT)
- {
- PRINT("Waiting for advertising..\n");
- }
- else if(pEvent->gap.opcode == GAP_LINK_TERMINATED_EVENT)
- {
- Peripheral_LinkTerminated(pEvent);
- PRINT("Disconnected.. Reason:%x\n", pEvent->linkTerminate.reason);
- if(ble_connect != NULL)
- {
- ble_connect(false);
- }
- }
- else if(pEvent->gap.opcode == GAP_LINK_ESTABLISHED_EVENT)
- {
- if(pEvent->gap.hdr.status != SUCCESS)
- {
- PRINT("Waiting for advertising..\n");
- }
- else
- {
- PRINT("Error..\n");
- }
- }
- else
- {
- PRINT("Error..%x\n", pEvent->gap.opcode);
- }
- break;
- case GAPROLE_ERROR:
- PRINT("Error..\n");
- break;
- default:
- break;
- }
- }
- /*********************************************************************
- * @fn performPeriodicTask
- *
- * @brief Perform a periodic application task. This function gets
- * called every five seconds as a result of the SBP_PERIODIC_EVT
- * TMOS event. In this example, the value of the third
- * characteristic in the SimpleGATTProfile service is retrieved
- * from the profile, and then copied into the value of the
- * the fourth characteristic.
- *
- * @param none
- *
- * @return none
- */
- static void performPeriodicTask(void)
- {
- // uint8_t notiData[SIMPLEPROFILE_CHAR4_LEN] = {0x88};
- // peripheralChar4Notify(notiData, SIMPLEPROFILE_CHAR4_LEN);
- }
- /*********************************************************************
- * @fn peripheralChar4Notify
- *
- * @brief Prepare and send simpleProfileChar4 notification
- *
- * @param pValue - data to notify
- * len - length of data
- *
- * @return none
- */
- static void peripheralChar4Notify(uint8_t *pValue, uint16_t len)
- {
- attHandleValueNoti_t noti;
- if(len > (peripheralMTU - 3))
- {
- PRINT("Too large noti\n");
- return;
- }
- noti.len = len;
- noti.pValue = GATT_bm_alloc(peripheralConnList.connHandle, ATT_HANDLE_VALUE_NOTI, noti.len, NULL, 0);
- if(noti.pValue)
- {
- tmos_memcpy(noti.pValue, pValue, noti.len);
- if(simpleProfileChar4_Notify(peripheralConnList.connHandle, ¬i) != SUCCESS)
- {
- GATT_bm_free((gattMsg_t *)¬i, ATT_HANDLE_VALUE_NOTI);
- }
- }
- }
- #if(DEBUG == DEBUG_UART_BLE)
- /*********************************************************************
- * @fn peripheralChar5Notify
- *
- * @brief Prepare and send simpleProfileChar4 notification
- *
- * @param pValue - data to notify
- * len - length of data
- *
- * @return none
- */
- static void peripheralChar5Notify(uint8_t *pValue, uint16_t len)
- {
- attHandleValueNoti_t noti;
- if(len > (peripheralMTU - 3))
- {
- // PRINT("Too large noti\n");
- return;
- }
- noti.len = len;
- noti.pValue = GATT_bm_alloc(peripheralConnList.connHandle, ATT_HANDLE_VALUE_NOTI, noti.len, NULL, 0);
- if(noti.pValue)
- {
- tmos_memcpy(noti.pValue, pValue, noti.len);
- if(simpleProfileChar5_Notify(peripheralConnList.connHandle, ¬i) != SUCCESS)
- {
- GATT_bm_free((gattMsg_t *)¬i, ATT_HANDLE_VALUE_NOTI);
- }
- }
- }
- #endif
- void Jump_OTA(void);
- /*********************************************************************
- * @fn simpleProfileChangeCB
- *
- * @brief Callback from SimpleBLEProfile indicating a value change
- *
- * @param paramID - parameter ID of the value that was changed.
- * pValue - pointer to data that was changed
- * len - length of data
- *
- * @return none
- */
- static void simpleProfileChangeCB(uint8_t paramID, uint8_t *pValue, uint16_t len)
- {
- switch(paramID)
- {
- case SIMPLEPROFILE_CHAR1:
- {
- if(download_ble_file != NULL)
- {
- download_ble_file(pValue, len);
- }
- break;
- }
- case SIMPLEPROFILE_CHAR3:
- {
- if(download_ble_file != NULL)
- {
- download_ble_file(pValue, len);
- }
- break;
- }
- case SIMPLEPROFILE_CHAR4:
- {
- if(download_ble_data != NULL)
- {
- download_ble_data(pValue, len);
- }
- break;
- }
- default:
- // should not reach here!
- break;
- }
- }
- #if(DEBUG == DEBUG_UART_BLE)
- void debug_ble_tx_data(uint8_t *data, uint16_t length)
- {
- peripheralChar5Notify(data, length);
- }
- #endif
- void ble_data_send(uint8_t *data, int len)
- {
- peripheralChar4Notify(data, len);
- }
- void set_download_ble_data_func(download_ble_data_func func)
- {
- download_ble_data = func;
- }
- void set_download_ble_file_func(download_ble_data_func func)
- {
- download_ble_file = func;
- }
- void set_ble_connect_evtfunc(ble_connect_func func)
- {
- ble_connect = func;
- }
- /*********************************************************************
- *********************************************************************/
|