initial commit
This commit is contained in:
@@ -0,0 +1,232 @@
|
||||
//*****************************************************************************
|
||||
//
|
||||
// ancc_api.h
|
||||
//! @file
|
||||
//!
|
||||
//! @brief apple notification center service client.
|
||||
//
|
||||
//*****************************************************************************
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// Copyright (c) 2020, Ambiq Micro
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in the
|
||||
// documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// 3. Neither the name of the copyright holder nor the names of its
|
||||
// contributors may be used to endorse or promote products derived from this
|
||||
// software without specific prior written permission.
|
||||
//
|
||||
// Third party software included in this distribution is subject to the
|
||||
// additional license terms as defined in the /docs/licenses directory.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
// POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// This is part of revision 2.4.2 of the AmbiqSuite Development Package.
|
||||
//
|
||||
//*****************************************************************************
|
||||
|
||||
#ifndef ANCC_API_H
|
||||
#define ANCC_API_H
|
||||
|
||||
#include "att_api.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**************************************************************************************************
|
||||
Macros
|
||||
**************************************************************************************************/
|
||||
/* ANCS Service */ // 7905F431-B5CE-4E99-A40F-4B1E122D00D0
|
||||
#define ATT_UUID_ANCS_SERVICE 0xd0, 0x00, 0x2d, 0x12, 0x1e, 0x4b, 0x0f, 0xa4,\
|
||||
0x99, 0x4e, 0xce, 0xb5, 0x31, 0xf4, 0x05, 0x79
|
||||
/* ANCS characteristics */
|
||||
// 9FBF120D-6301-42D9-8C58-25E699A21DBD
|
||||
#define ATT_UUID_NOTIFICATION_SOURCE 0xbd, 0x1d, 0xa2, 0x99, 0xe6, 0x25, 0x58, 0x8c, \
|
||||
0xd9, 0x42, 0x01, 0x63, 0x0d, 0x12, 0xbf, 0x9f
|
||||
|
||||
//69D1D8F3-45E1-49A8-9821-9BBDFDAAD9D9
|
||||
#define ATT_UUID_CTRL_POINT 0xd9, 0xd9, 0xaa, 0xfd, 0xbd, 0x9b, 0x21, 0x98,\
|
||||
0xa8, 0x49, 0xe1, 0x45, 0xf3, 0xd8, 0xd1, 0x69
|
||||
|
||||
// 22EAC6E9-24D6-4BB5-BE44-B36ACE7C7BFB
|
||||
#define ATT_UUID_DATA_SOURCE 0xfb, 0x7b, 0x7c, 0xce, 0x6a, 0xb3, 0x44, 0xbe,\
|
||||
0xb5, 0x4b, 0xd6, 0x24, 0xe9, 0xc6, 0xea, 0x22
|
||||
|
||||
#define ANCC_LIST_ELEMENTS 64
|
||||
#define ANCC_APP_IDENTIFIER_SIZE_BYTES 64
|
||||
#define ANCC_ATTRI_BUFFER_SIZE_BYTES 512
|
||||
|
||||
/**@brief Category IDs for iOS notifications. */
|
||||
typedef enum
|
||||
{
|
||||
BLE_ANCS_CATEGORY_ID_OTHER, /**< The iOS notification belongs to the "other" category. */
|
||||
BLE_ANCS_CATEGORY_ID_INCOMING_CALL, /**< The iOS notification belongs to the "Incoming Call" category. */
|
||||
BLE_ANCS_CATEGORY_ID_MISSED_CALL, /**< The iOS notification belongs to the "Missed Call" category. */
|
||||
BLE_ANCS_CATEGORY_ID_VOICE_MAIL, /**< The iOS notification belongs to the "Voice Mail" category. */
|
||||
BLE_ANCS_CATEGORY_ID_SOCIAL, /**< The iOS notification belongs to the "Social" category. */
|
||||
BLE_ANCS_CATEGORY_ID_SCHEDULE, /**< The iOS notification belongs to the "Schedule" category. */
|
||||
BLE_ANCS_CATEGORY_ID_EMAIL, /**< The iOS notification belongs to the "E-mail" category. */
|
||||
BLE_ANCS_CATEGORY_ID_NEWS, /**< The iOS notification belongs to the "News" category. */
|
||||
BLE_ANCS_CATEGORY_ID_HEALTH_AND_FITNESS, /**< The iOS notification belongs to the "Health and Fitness" category. */
|
||||
BLE_ANCS_CATEGORY_ID_BUSINESS_AND_FINANCE, /**< The iOS notification belongs to the "Buisness and Finance" category. */
|
||||
BLE_ANCS_CATEGORY_ID_LOCATION, /**< The iOS notification belongs to the "Location" category. */
|
||||
BLE_ANCS_CATEGORY_ID_ENTERTAINMENT /**< The iOS notification belongs to the "Entertainment" category. */
|
||||
} ancc_category_id_values_t;
|
||||
|
||||
/**@brief Event IDs for iOS notifications. */
|
||||
typedef enum
|
||||
{
|
||||
BLE_ANCS_EVENT_ID_NOTIFICATION_ADDED, /**< The iOS notification was added. */
|
||||
BLE_ANCS_EVENT_ID_NOTIFICATION_MODIFIED, /**< The iOS notification was modified. */
|
||||
BLE_ANCS_EVENT_ID_NOTIFICATION_REMOVED /**< The iOS notification was removed. */
|
||||
} ancc_evt_id_values_t;
|
||||
|
||||
/**@brief Control point command IDs that the Notification Consumer can send to the Notification Provider. */
|
||||
typedef enum
|
||||
{
|
||||
BLE_ANCS_COMMAND_ID_GET_NOTIF_ATTRIBUTES, /**< Requests attributes to be sent from the NP to the NC for a given notification. */
|
||||
BLE_ANCS_COMMAND_ID_GET_APP_ATTRIBUTES, /**< Requests attributes to be sent from the NP to the NC for a given iOS App. */
|
||||
BLE_ANCS_COMMAND_ID_GET_PERFORM_NOTIF_ACTION, /**< Requests an action to be performed on a given notification, for example dismiss an alarm. */
|
||||
} ancc_command_id_values_t;
|
||||
|
||||
/**@brief IDs for iOS notification attributes. */
|
||||
typedef enum
|
||||
{
|
||||
BLE_ANCS_NOTIF_ATTR_ID_APP_IDENTIFIER, /**< Identifies that the attribute data is of an "App Identifier" type. */
|
||||
BLE_ANCS_NOTIF_ATTR_ID_TITLE, /**< Identifies that the attribute data is a "Title". Needs to be followed by a 2-bytes max length parameter*/
|
||||
BLE_ANCS_NOTIF_ATTR_ID_SUBTITLE, /**< Identifies that the attribute data is a "Subtitle". Needs to be followed by a 2-bytes max length parameter*/
|
||||
BLE_ANCS_NOTIF_ATTR_ID_MESSAGE, /**< Identifies that the attribute data is a "Message". Needs to be followed by a 2-bytes max length parameter*/
|
||||
BLE_ANCS_NOTIF_ATTR_ID_MESSAGE_SIZE, /**< Identifies that the attribute data is a "Message Size". */
|
||||
BLE_ANCS_NOTIF_ATTR_ID_DATE, /**< Identifies that the attribute data is a "Date". */
|
||||
BLE_ANCS_NOTIF_ATTR_ID_POSITIVE_ACTION_LABEL, /**< The notification has a "Positive action" that can be executed associated with it. */
|
||||
BLE_ANCS_NOTIF_ATTR_ID_NEGATIVE_ACTION_LABEL, /**< The notification has a "Negative action" that can be executed associated with it. */
|
||||
} ancc_notif_attr_id_values_t;
|
||||
|
||||
/**@brief ActionID values. */
|
||||
typedef enum
|
||||
{
|
||||
BLE_ANCS_NOTIF_ACTION_ID_POSITIVE,
|
||||
BLE_ANCS_NOTIF_ACTION_ID_NEGATIVE,
|
||||
} ancc_notif_action_id_values_t;
|
||||
|
||||
|
||||
/**@brief Typedef for iOS notifications. */
|
||||
typedef struct
|
||||
{
|
||||
uint8_t event_id; /**< This field informs the accessory whether the given iOS notification was added, modified, or removed. The enumerated values for this field are defined in EventID Values.. */
|
||||
uint8_t event_flags; /**< A bitmask whose set bits inform an NC of specificities with the iOS notification. */
|
||||
uint8_t category_id; /**< A numerical value providing a category in which the iOS notification can be classified. */
|
||||
uint8_t category_count; /**< The current number of active iOS notifications in the given category. */
|
||||
uint32_t notification_uid; /**< A 32-bit numerical value that is the unique identifier (UID) for the iOS notification. */
|
||||
bool_t noti_valid; /**< A flag to indicate whether the notification is still valid in the list. */
|
||||
} ancc_notif_t;
|
||||
|
||||
/*! ancs client enumeration of handle indexes of characteristics to be discovered */
|
||||
enum
|
||||
{
|
||||
ANCC_NOTIFICATION_SOURCE_HDL_IDX, // ANCC Notification Source Handle
|
||||
ANCC_NOTIFICATION_SOURCE_CCC_HDL_IDX, // ANCC Notification Source CCC Handle
|
||||
ANCC_CONTROL_POINT_HDL_IDX, // ANCC Control Point Handle
|
||||
ANCC_DATA_SOURCE_HDL_IDX, // ANCC Data Source Handle
|
||||
ANCC_DATA_SOURCE_CCC_HDL_IDX, // ANCC Data Source CCC Handle
|
||||
ANCC_HDL_LIST_LEN /*! Handle list length */
|
||||
};
|
||||
|
||||
/*! Configurable parameters */
|
||||
typedef struct
|
||||
{
|
||||
wsfTimerTicks_t period; /*! action timer expiration period in ms */
|
||||
} anccCfg_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
NOTI_ATTR_NEW_NOTIFICATION = 0,
|
||||
NOTI_ATTR_NEW_ATTRIBUTE,
|
||||
NOTI_ATTR_RECEIVING_ATTRIBUTE
|
||||
} enum_active_state_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint16_t handle; // handle to indicate the current active notification in the list
|
||||
enum_active_state_t attrState;
|
||||
uint16_t bufIndex;
|
||||
uint16_t parseIndex;
|
||||
uint16_t attrLength;
|
||||
uint8_t attrId;
|
||||
uint16_t attrCount;
|
||||
uint8_t commandId;
|
||||
uint32_t notiUid;
|
||||
uint8_t appId[ANCC_APP_IDENTIFIER_SIZE_BYTES];
|
||||
uint8_t attrDataBuf[ANCC_ATTRI_BUFFER_SIZE_BYTES];
|
||||
} active_notif_t;
|
||||
|
||||
/*! Application data reception callback */
|
||||
typedef void (*anccAttrRecvCback_t)(active_notif_t* pAttr);
|
||||
typedef void (*anccNotiCmplCback_t)(active_notif_t* pAttr, uint32_t notiUid);
|
||||
|
||||
/*! Application notify remove callback */
|
||||
typedef void (*anccNotiRemoveCback_t)(ancc_notif_t* pAttr);
|
||||
typedef struct
|
||||
{
|
||||
dmConnId_t connId;
|
||||
uint16_t* hdlList;
|
||||
anccCfg_t cfg;
|
||||
wsfTimer_t actionTimer; // perform actions with proper delay
|
||||
wsfTimer_t discoverTimer; // perform service discovery delay
|
||||
ancc_notif_t anccList[ANCC_LIST_ELEMENTS]; // buffer size = MAX_LIST_ELEMENTS*sizeof(ancc_notif_t)
|
||||
active_notif_t active;
|
||||
anccAttrRecvCback_t attrCback;
|
||||
anccNotiCmplCback_t notiCback;
|
||||
anccNotiRemoveCback_t rmvCback;
|
||||
} anccCb_t;
|
||||
|
||||
/**************************************************************************************************
|
||||
Function Prototypes
|
||||
**************************************************************************************************/
|
||||
// operation interfaces
|
||||
void AncsPerformNotiAction(uint16_t *pHdlList, uint32_t notiUid, ancc_notif_action_id_values_t actionId);
|
||||
void AncsGetAppAttribute(uint16_t *pHdlList, uint8_t* appId);
|
||||
void AnccGetNotificationAttribute(uint16_t *pHdlList, uint32_t notiUid);
|
||||
|
||||
// initialization interfaces
|
||||
void AnccInit(wsfHandlerId_t handlerId, anccCfg_t* cfg, uint8_t disctimer_event);
|
||||
void AnccCbackRegister(anccAttrRecvCback_t attrCback, anccNotiCmplCback_t notiCback, anccNotiRemoveCback_t rmvCback);
|
||||
|
||||
// app routines
|
||||
void AnccNtfValueUpdate(uint16_t *pHdlList, attEvt_t * pMsg, uint8_t actionTimerEvt);
|
||||
void AnccActionHandler(uint8_t actionTimerEvt);
|
||||
void AnccActionStop(void);
|
||||
void AnccActionStart(uint8_t timerEvt);
|
||||
void AnccConnClose(void);
|
||||
void AnccConnOpen(dmConnId_t connId, uint16_t* hdlList);
|
||||
void AnccSvcDiscover(dmConnId_t connId, uint16_t *pHdlList);
|
||||
bool_t AnccStartServiceDiscovery(void);
|
||||
|
||||
/* Global variable */
|
||||
extern anccCb_t anccCb;
|
||||
#ifdef __cplusplus
|
||||
};
|
||||
#endif
|
||||
|
||||
#endif /* ANCC_API_H */
|
||||
@@ -0,0 +1,709 @@
|
||||
//*****************************************************************************
|
||||
//
|
||||
// ancc_main.c
|
||||
//! @file
|
||||
//!
|
||||
//! @brief apple notification center service client.
|
||||
//
|
||||
//*****************************************************************************
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// Copyright (c) 2020, Ambiq Micro
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in the
|
||||
// documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// 3. Neither the name of the copyright holder nor the names of its
|
||||
// contributors may be used to endorse or promote products derived from this
|
||||
// software without specific prior written permission.
|
||||
//
|
||||
// Third party software included in this distribution is subject to the
|
||||
// additional license terms as defined in the /docs/licenses directory.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
// POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// This is part of revision 2.4.2 of the AmbiqSuite Development Package.
|
||||
//
|
||||
//*****************************************************************************
|
||||
|
||||
#include <string.h>
|
||||
#include "wsf_types.h"
|
||||
#include "wsf_assert.h"
|
||||
#include "bstream.h"
|
||||
#include "app_api.h"
|
||||
#include "ancc_api.h"
|
||||
|
||||
#include "am_util.h"
|
||||
|
||||
/**************************************************************************************************
|
||||
Local Variables
|
||||
**************************************************************************************************/
|
||||
|
||||
/*!
|
||||
* Apple Notification Center Service Client (ANCC)
|
||||
*/
|
||||
|
||||
/* UUIDs */
|
||||
static const uint8_t anccAncsSvcUuid[] = {ATT_UUID_ANCS_SERVICE}; /*! ANCS Service UUID */
|
||||
static const uint8_t anccNSChUuid[] = {ATT_UUID_NOTIFICATION_SOURCE}; /*! Notification Source UUID */
|
||||
static const uint8_t anccCPChUuid[] = {ATT_UUID_CTRL_POINT}; /*! control point UUID*/
|
||||
static const uint8_t anccDSChUuid[] = {ATT_UUID_DATA_SOURCE}; /*! data source UUID*/
|
||||
/* Characteristics for discovery */
|
||||
|
||||
#define DISCOVER_TIMER_DELAY (3000) // ms
|
||||
|
||||
/*! Proprietary data */
|
||||
static const attcDiscChar_t anccNSDat =
|
||||
{
|
||||
anccNSChUuid,
|
||||
ATTC_SET_REQUIRED | ATTC_SET_UUID_128
|
||||
};
|
||||
|
||||
/*! Proprietary data descriptor */ //notification source
|
||||
static const attcDiscChar_t anccNSdatCcc =
|
||||
{
|
||||
attCliChCfgUuid,
|
||||
ATTC_SET_REQUIRED | ATTC_SET_DESCRIPTOR
|
||||
};
|
||||
// control point
|
||||
static const attcDiscChar_t anccCtrlPoint =
|
||||
{
|
||||
anccCPChUuid,
|
||||
ATTC_SET_REQUIRED | ATTC_SET_UUID_128
|
||||
};
|
||||
|
||||
// data source
|
||||
static const attcDiscChar_t anccDataSrc =
|
||||
{
|
||||
anccDSChUuid,
|
||||
ATTC_SET_REQUIRED | ATTC_SET_UUID_128
|
||||
};
|
||||
/*! Proprietary data descriptor */ //data source
|
||||
static const attcDiscChar_t anccDataSrcCcc =
|
||||
{
|
||||
attCliChCfgUuid,
|
||||
ATTC_SET_REQUIRED | ATTC_SET_DESCRIPTOR
|
||||
};
|
||||
|
||||
/*! List of characteristics to be discovered; order matches handle index enumeration */
|
||||
static const attcDiscChar_t *anccSvcDiscCharList[] =
|
||||
{
|
||||
&anccNSDat, /*! Proprietary data */
|
||||
&anccNSdatCcc, /*! Proprietary data descriptor */
|
||||
&anccCtrlPoint, /*! Control point */
|
||||
&anccDataSrc, /*! data source */
|
||||
&anccDataSrcCcc /*! data source descriptor */
|
||||
};
|
||||
|
||||
/* sanity check: make sure handle list length matches characteristic list length */
|
||||
WSF_CT_ASSERT(ANCC_HDL_LIST_LEN == ((sizeof(anccSvcDiscCharList) / sizeof(attcDiscChar_t *))));
|
||||
|
||||
/* Global variable */
|
||||
anccCb_t anccCb;
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn AnccSvcDiscover
|
||||
*
|
||||
* \brief Perform service and characteristic discovery for ancs service.
|
||||
* Parameter pHdlList must point to an array of length ANCC_HDL_LIST_LEN.
|
||||
* If discovery is successful the handles of discovered characteristics and
|
||||
* descriptors will be set in pHdlList.
|
||||
*
|
||||
* \param connId Connection identifier.
|
||||
* \param pHdlList Characteristic handle list.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void AnccSvcDiscover(dmConnId_t connId, uint16_t *pHdlList)
|
||||
{
|
||||
AppDiscFindService(connId, ATT_128_UUID_LEN, (uint8_t *) anccAncsSvcUuid,
|
||||
ANCC_HDL_LIST_LEN, (attcDiscChar_t **) anccSvcDiscCharList, pHdlList);
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn AnccInit
|
||||
*
|
||||
* \brief Initialize the control variables of ancs client
|
||||
*
|
||||
* \param handlerId App handler id for timer operation.
|
||||
* \param cfg config variable.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void AnccInit(wsfHandlerId_t handlerId, anccCfg_t* cfg, uint8_t disctimer_event)
|
||||
{
|
||||
memset(anccCb.anccList, 0, ANCC_LIST_ELEMENTS * sizeof(ancc_notif_t));
|
||||
anccCb.cfg.period = cfg->period;
|
||||
anccCb.actionTimer.handlerId = handlerId;
|
||||
anccCb.discoverTimer.handlerId = handlerId;
|
||||
anccCb.discoverTimer.msg.event = disctimer_event;
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn AnccConnOpen
|
||||
*
|
||||
* \brief Update the key parameters for control variables when connection open.
|
||||
*
|
||||
* \param connId Connection identifier.
|
||||
* \param hdlList Characteristic handle list.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void AnccConnOpen(dmConnId_t connId, uint16_t* hdlList)
|
||||
{
|
||||
anccCb.connId = connId;
|
||||
anccCb.hdlList = hdlList;
|
||||
|
||||
WsfTimerStop(&anccCb.discoverTimer);
|
||||
WsfTimerStartMs(&anccCb.discoverTimer, DISCOVER_TIMER_DELAY);
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn AnccConnClose
|
||||
*
|
||||
* \brief Clear the key parameters for control variables when connection close.
|
||||
*
|
||||
* \param None.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void AnccConnClose(void)
|
||||
{
|
||||
WsfTimerStop(&anccCb.discoverTimer);
|
||||
anccCb.connId = DM_CONN_ID_NONE;
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn anccNoConnActive
|
||||
*
|
||||
* \brief Return TRUE if no connections with active measurements.
|
||||
*
|
||||
* \return TRUE if no connections active.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
static bool_t anccNoConnActive(void)
|
||||
{
|
||||
if (anccCb.connId != DM_CONN_ID_NONE)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn anccActionListPush
|
||||
*
|
||||
* \brief Return TRUE if element is added/updated to the list.
|
||||
*
|
||||
* \return TRUE if element is added/updated to the list.
|
||||
* FALSE if list is full.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
static bool_t anccActionListPush(ancc_notif_t* pElement)
|
||||
{
|
||||
uint16_t i;
|
||||
for ( i = 0; i < ANCC_LIST_ELEMENTS; i++ )
|
||||
{
|
||||
if ( (anccCb.anccList[i].notification_uid == pElement->notification_uid ) && (anccCb.anccList[i].noti_valid == true) )
|
||||
{
|
||||
// same notification uid received, update the element
|
||||
anccCb.anccList[i].notification_uid = pElement->notification_uid;
|
||||
anccCb.anccList[i].event_id = pElement->event_id;
|
||||
anccCb.anccList[i].event_flags = pElement->event_flags;
|
||||
anccCb.anccList[i].category_id = pElement->category_id;
|
||||
anccCb.anccList[i].category_count = pElement->category_count;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
for ( i = 0; i < ANCC_LIST_ELEMENTS; i++ )
|
||||
{
|
||||
if ( anccCb.anccList[i].noti_valid == false )
|
||||
{
|
||||
// found an empty slot
|
||||
anccCb.anccList[i].notification_uid = pElement->notification_uid;
|
||||
anccCb.anccList[i].event_id = pElement->event_id;
|
||||
anccCb.anccList[i].event_flags = pElement->event_flags;
|
||||
anccCb.anccList[i].category_id = pElement->category_id;
|
||||
anccCb.anccList[i].category_count = pElement->category_count;
|
||||
anccCb.anccList[i].noti_valid = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false; //no empty slot left
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn anccActionListPop
|
||||
*
|
||||
* \brief Return TRUE if element is popped out and removed from the list.
|
||||
*
|
||||
* \return TRUE if element is popped out and removed from the list.
|
||||
* FALSE if list is already empty.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
static bool_t anccActionListPop(void)
|
||||
{
|
||||
uint16_t i;
|
||||
for ( i = 0; i < ANCC_LIST_ELEMENTS; i++ )
|
||||
{
|
||||
if ( anccCb.anccList[ANCC_LIST_ELEMENTS - i - 1].noti_valid == true )
|
||||
{
|
||||
// found a valid element in the list
|
||||
anccCb.anccList[ANCC_LIST_ELEMENTS - i - 1].noti_valid = false;
|
||||
anccCb.active.handle = ANCC_LIST_ELEMENTS - i - 1;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false; //no element left in the list
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn AnccActionStart
|
||||
*
|
||||
* \brief Start periodic ancc operation. This function starts a timer to perform
|
||||
* periodic actions.
|
||||
*
|
||||
* \param timerEvt WSF event designated by the application for the timer.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void AnccActionStart(uint8_t timerEvt)
|
||||
{
|
||||
/* if this is first connection */
|
||||
if (anccNoConnActive() == FALSE)
|
||||
{
|
||||
/* initialize control block */
|
||||
anccCb.actionTimer.msg.event = timerEvt;
|
||||
|
||||
/* (re-)start timer */
|
||||
WsfTimerStop(&anccCb.actionTimer);
|
||||
WsfTimerStartMs(&anccCb.actionTimer, anccCb.cfg.period);
|
||||
}
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn AnccActionStop
|
||||
*
|
||||
* \brief Stop periodic ancc action.
|
||||
*
|
||||
* \param connId DM connection identifier.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void AnccActionStop(void)
|
||||
{
|
||||
/* stop timer */
|
||||
WsfTimerStop(&anccCb.actionTimer);
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn AnccGetNotificationAttribute
|
||||
*
|
||||
* \brief Send a command to the apple notification center service control point.
|
||||
*
|
||||
* \param pHdlList Characteristic handle list.
|
||||
* \param notiUid NotificationUid.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void AnccGetNotificationAttribute(uint16_t *pHdlList, uint32_t notiUid)
|
||||
{
|
||||
// An example to get notification attributes
|
||||
uint16_t max_len = 256;
|
||||
uint8_t buf[19]; // retrieve the complete attribute list
|
||||
if (pHdlList[ANCC_CONTROL_POINT_HDL_IDX] != ATT_HANDLE_NONE)
|
||||
{
|
||||
buf[0] = BLE_ANCS_COMMAND_ID_GET_NOTIF_ATTRIBUTES; // put command
|
||||
uint8_t * p = &buf[1];
|
||||
UINT32_TO_BSTREAM(p, notiUid); // encode notification uid
|
||||
|
||||
// encode attribute IDs
|
||||
buf[5] = BLE_ANCS_NOTIF_ATTR_ID_APP_IDENTIFIER;
|
||||
buf[6] = BLE_ANCS_NOTIF_ATTR_ID_TITLE;
|
||||
// 2 byte length
|
||||
buf[7] = max_len;
|
||||
buf[8] = max_len >> 8;
|
||||
buf[9] = BLE_ANCS_NOTIF_ATTR_ID_SUBTITLE;
|
||||
buf[10] = max_len;
|
||||
buf[11] = max_len >> 8;
|
||||
buf[12] = BLE_ANCS_NOTIF_ATTR_ID_MESSAGE;
|
||||
buf[13] = max_len;
|
||||
buf[14] = max_len >> 8;
|
||||
buf[15] = BLE_ANCS_NOTIF_ATTR_ID_MESSAGE_SIZE;
|
||||
buf[16] = BLE_ANCS_NOTIF_ATTR_ID_DATE;
|
||||
buf[17] = BLE_ANCS_NOTIF_ATTR_ID_POSITIVE_ACTION_LABEL;
|
||||
buf[18] = BLE_ANCS_NOTIF_ATTR_ID_NEGATIVE_ACTION_LABEL;
|
||||
AttcWriteReq(anccCb.connId, pHdlList[ANCC_CONTROL_POINT_HDL_IDX], sizeof(buf), buf);
|
||||
}
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn AncsGetAppAttribute
|
||||
*
|
||||
* \brief Send a command to the apple notification center service control point.
|
||||
*
|
||||
* \param pHdlList Connection identifier.
|
||||
* \param pAppId Attribute handle.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void AncsGetAppAttribute(uint16_t *pHdlList, uint8_t *pAppId)
|
||||
{
|
||||
// An example to get app attributes
|
||||
uint8_t buf[64]; // to hold the command, size of app identifier is unknown
|
||||
uint8_t count = 0;
|
||||
if (pHdlList[ANCC_CONTROL_POINT_HDL_IDX] != ATT_HANDLE_NONE)
|
||||
{
|
||||
buf[0] = BLE_ANCS_COMMAND_ID_GET_APP_ATTRIBUTES; // put command
|
||||
|
||||
while (pAppId[count++] != 0); // NULL terminated string
|
||||
if ( count > (64 - 2) )
|
||||
{
|
||||
// app identifier is too long
|
||||
}
|
||||
else
|
||||
{
|
||||
memcpy(&buf[1], pAppId, count);
|
||||
}
|
||||
|
||||
buf[count + 1] = 0; // app attribute id
|
||||
AttcWriteReq(anccCb.connId, pHdlList[ANCC_CONTROL_POINT_HDL_IDX], (count + 2), buf);
|
||||
}
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn AncsPerformNotiAction
|
||||
*
|
||||
* \brief Send a command to the apple notification center service control point.
|
||||
*
|
||||
* \param pHdlList Characteristic handle list.
|
||||
* \param actionId Notification action identifier.
|
||||
* \param notiUid NotificationUid.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void AncsPerformNotiAction(uint16_t *pHdlList, uint32_t notiUid, ancc_notif_action_id_values_t actionId)
|
||||
{
|
||||
// An example to performs notification action
|
||||
uint8_t buf[6]; //to hold the command, size of app identifier is unknown
|
||||
if (pHdlList[ANCC_CONTROL_POINT_HDL_IDX] != ATT_HANDLE_NONE)
|
||||
{
|
||||
buf[0] = BLE_ANCS_COMMAND_ID_GET_PERFORM_NOTIF_ACTION; // put command
|
||||
uint8_t * p = &buf[1];
|
||||
UINT32_TO_BSTREAM(p, notiUid); // encode notification uid
|
||||
buf[5] = actionId; //action id
|
||||
AttcWriteReq(anccCb.connId, pHdlList[ANCC_CONTROL_POINT_HDL_IDX], sizeof(buf), buf);
|
||||
}
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn AnccActionHandler
|
||||
*
|
||||
* \brief Routine to handle ancc related actions.
|
||||
*
|
||||
* \param actionTimerEvt WsfTimer event indication.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void AnccActionHandler(uint8_t actionTimerEvt)
|
||||
{
|
||||
// perform action
|
||||
if ( anccActionListPop() )
|
||||
{
|
||||
AnccGetNotificationAttribute(anccCb.hdlList, anccCb.anccList[anccCb.active.handle].notification_uid);
|
||||
AnccActionStart(actionTimerEvt);
|
||||
}
|
||||
else
|
||||
{
|
||||
//list empty
|
||||
AnccActionStop();
|
||||
}
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn anccAttrHandler
|
||||
*
|
||||
* \brief Static routine to handle attribute receiving and processing.
|
||||
*
|
||||
* \param pMsg pointer to ATT message.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
static bool anccAttrHandler(attEvt_t * pMsg)
|
||||
{
|
||||
uint8_t count = 0;
|
||||
uint16_t bytesRemaining = 0;
|
||||
|
||||
switch(anccCb.active.attrState)
|
||||
{
|
||||
case NOTI_ATTR_NEW_NOTIFICATION:
|
||||
// new notification
|
||||
anccCb.active.commandId = pMsg->pValue[0];
|
||||
if ( anccCb.active.commandId == BLE_ANCS_COMMAND_ID_GET_NOTIF_ATTRIBUTES )
|
||||
{
|
||||
BYTES_TO_UINT32(anccCb.active.notiUid, &(pMsg->pValue[1]));
|
||||
count = 4;
|
||||
}
|
||||
else if ( anccCb.active.commandId == BLE_ANCS_COMMAND_ID_GET_APP_ATTRIBUTES )
|
||||
{
|
||||
while(pMsg->pValue[count + 1] != 0) // NULL terminated string
|
||||
{
|
||||
anccCb.active.appId[count] = pMsg->pValue[count + 1];
|
||||
count++;
|
||||
}
|
||||
anccCb.active.appId[count + 1] = 0; // NULL terminated string
|
||||
}
|
||||
else
|
||||
{
|
||||
// BLE_ANCS_COMMAND_ID_GET_PERFORM_NOTIF_ACTION
|
||||
return false;
|
||||
}
|
||||
anccCb.active.parseIndex += count + 1;
|
||||
anccCb.active.attrState = NOTI_ATTR_NEW_ATTRIBUTE;
|
||||
anccCb.active.attrId = 0;
|
||||
anccCb.active.attrCount = 0;
|
||||
|
||||
if ( pMsg->valueLen > ANCC_ATTRI_BUFFER_SIZE_BYTES )
|
||||
{
|
||||
// notification size overflow
|
||||
}
|
||||
else
|
||||
{
|
||||
bytesRemaining = pMsg->valueLen;
|
||||
}
|
||||
// copy data
|
||||
memset(anccCb.active.attrDataBuf, 0, ANCC_ATTRI_BUFFER_SIZE_BYTES);
|
||||
anccCb.active.bufIndex = 0;
|
||||
for (uint16_t i = 0; i < bytesRemaining; i++)
|
||||
{
|
||||
anccCb.active.attrDataBuf[anccCb.active.bufIndex++] = pMsg->pValue[i];
|
||||
}
|
||||
// no break here by intention
|
||||
case NOTI_ATTR_NEW_ATTRIBUTE:
|
||||
// new attribute
|
||||
// check consistency of the attribute
|
||||
if ( anccCb.active.bufIndex - anccCb.active.parseIndex < 3 ) // 1 byte attribute id + 2 bytes attribute length
|
||||
{
|
||||
// attribute header not received completely
|
||||
anccCb.active.attrState = NOTI_ATTR_RECEIVING_ATTRIBUTE;
|
||||
return false;
|
||||
}
|
||||
|
||||
anccCb.active.attrId = anccCb.active.attrDataBuf[anccCb.active.parseIndex];
|
||||
BYTES_TO_UINT16(anccCb.active.attrLength, &(anccCb.active.attrDataBuf[anccCb.active.parseIndex + 1]));
|
||||
|
||||
if ( anccCb.active.attrLength > (anccCb.active.bufIndex - anccCb.active.parseIndex - 3) ) // 1 byte attribute id + 2 bytes attribute length
|
||||
{
|
||||
// attribute body not received completely
|
||||
anccCb.active.attrState = NOTI_ATTR_RECEIVING_ATTRIBUTE;
|
||||
return false;
|
||||
}
|
||||
|
||||
// parse attribute
|
||||
anccCb.active.attrCount++;
|
||||
anccCb.active.parseIndex += 3; // 1 byte attribute id + 2 bytes attribute length
|
||||
|
||||
if ( anccCb.active.attrId == BLE_ANCS_NOTIF_ATTR_ID_APP_IDENTIFIER )
|
||||
{
|
||||
uint8_t temp = 0;
|
||||
while(anccCb.active.attrDataBuf[anccCb.active.parseIndex + temp] != 0) // NULL terminated string
|
||||
{
|
||||
anccCb.active.appId[temp] = anccCb.active.attrDataBuf[anccCb.active.parseIndex + temp];
|
||||
temp++;
|
||||
}
|
||||
anccCb.active.appId[temp] = 0; // NULL terminated string
|
||||
}
|
||||
|
||||
//
|
||||
// attribute received
|
||||
// execute callback function
|
||||
//
|
||||
(*anccCb.attrCback)(&anccCb.active);
|
||||
|
||||
anccCb.active.parseIndex += anccCb.active.attrLength;
|
||||
|
||||
if ( anccCb.active.attrCount >= 8 ) //custom criteria
|
||||
{
|
||||
//notification reception done
|
||||
anccCb.active.attrState = NOTI_ATTR_NEW_NOTIFICATION;
|
||||
anccCb.active.parseIndex = 0;
|
||||
anccCb.active.bufIndex = 0;
|
||||
|
||||
//
|
||||
// notification received
|
||||
// execute callback function
|
||||
//
|
||||
(*anccCb.notiCback)(&anccCb.active, anccCb.active.notiUid);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true; // continue parsing
|
||||
// no need to break;
|
||||
case NOTI_ATTR_RECEIVING_ATTRIBUTE:
|
||||
// notification continuing
|
||||
bytesRemaining = 0;
|
||||
if ( anccCb.active.bufIndex + pMsg->valueLen > ANCC_ATTRI_BUFFER_SIZE_BYTES )
|
||||
{
|
||||
// notification size overflow
|
||||
anccCb.active.attrState = NOTI_ATTR_NEW_NOTIFICATION;
|
||||
anccCb.active.parseIndex = 0;
|
||||
anccCb.active.bufIndex = 0;
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
bytesRemaining = pMsg->valueLen;
|
||||
}
|
||||
// copy data
|
||||
for (uint16_t i = 0; i < bytesRemaining; i++)
|
||||
{
|
||||
anccCb.active.attrDataBuf[anccCb.active.bufIndex++] = pMsg->pValue[i];
|
||||
}
|
||||
anccCb.active.attrState = NOTI_ATTR_NEW_ATTRIBUTE;
|
||||
return true;
|
||||
// no need to break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn AnccNtfValueUpdate
|
||||
*
|
||||
* \brief Routine to handle any ancc related notify.
|
||||
*
|
||||
* \param pHdlList Characteristic handle list.
|
||||
* \param actionTimerEvt WsfTimer event indication.
|
||||
* \param pMsg pointer to ATT message.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void AnccNtfValueUpdate(uint16_t *pHdlList, attEvt_t * pMsg, uint8_t actionTimerEvt)
|
||||
{
|
||||
ancc_notif_t ancs_notif;
|
||||
uint8_t *p;
|
||||
|
||||
//notification received
|
||||
if (pMsg->handle == pHdlList[ANCC_NOTIFICATION_SOURCE_HDL_IDX])
|
||||
{
|
||||
// process notificiation source (brief)
|
||||
p = pMsg->pValue;
|
||||
ancs_notif.event_id = p[0];
|
||||
ancs_notif.event_flags = p[1];
|
||||
ancs_notif.category_id = p[2];
|
||||
ancs_notif.category_count = p[3];
|
||||
BYTES_TO_UINT32(ancs_notif.notification_uid, &p[4]);
|
||||
|
||||
if (BLE_ANCS_EVENT_ID_NOTIFICATION_REMOVED == ancs_notif.event_id)
|
||||
{
|
||||
if (NULL != anccCb.rmvCback)
|
||||
{
|
||||
anccCb.rmvCback(&ancs_notif);
|
||||
}
|
||||
}
|
||||
else if ( !anccActionListPush(&ancs_notif) )
|
||||
{
|
||||
// list full
|
||||
// APP_TRACE_INFO0("action list full...");
|
||||
}
|
||||
else
|
||||
{
|
||||
//
|
||||
// actions to be done with timer delays to avoid generating heavy traffic
|
||||
//
|
||||
AnccActionStart(actionTimerEvt);
|
||||
// APP_TRACE_INFO0("added to action list");
|
||||
}
|
||||
}
|
||||
else if ( pMsg->handle == pHdlList[ANCC_DATA_SOURCE_HDL_IDX] )
|
||||
{
|
||||
// process notificiation/app attributes
|
||||
while(anccAttrHandler(pMsg));
|
||||
}
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn AnccCbackRegister
|
||||
*
|
||||
* \brief Register the attribute received callback and notification completed callback.
|
||||
*
|
||||
* \param attrCback Pointer to attribute received callback function.
|
||||
* \param notiCback Pointer to notification completed callback function.
|
||||
*
|
||||
* \return None.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
void AnccCbackRegister(anccAttrRecvCback_t attrCback, anccNotiCmplCback_t notiCback, anccNotiRemoveCback_t rmvCback)
|
||||
{
|
||||
anccCb.attrCback = attrCback;
|
||||
anccCb.notiCback = notiCback;
|
||||
anccCb.rmvCback = rmvCback;
|
||||
}
|
||||
|
||||
|
||||
/*************************************************************************************************/
|
||||
/*!
|
||||
* \fn AnccStartServiceDiscovery
|
||||
*
|
||||
* \brief Do service discovery on connected connection.
|
||||
*
|
||||
* \return Return TRUE if service discovery is initiated successfully.
|
||||
*
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
bool_t AnccStartServiceDiscovery(void)
|
||||
{
|
||||
if (anccCb.connId != DM_CONN_ID_NONE)
|
||||
{
|
||||
appDiscStart(anccCb.connId);
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
Reference in New Issue
Block a user