123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794 |
- #include "mesh_services.h"
- CONST uint8 mesh_prov_UUID[ATT_BT_UUID_SIZE] =
- {
- LO_UINT16(MESH_PROV_SERVICE_UUID), HI_UINT16(MESH_PROV_SERVICE_UUID)
- };
- CONST uint8 mesh_prov_data_in_UUID[ATT_BT_UUID_SIZE] =
- {
- LO_UINT16(MESH_PROV_DATA_IN_UUID), HI_UINT16(MESH_PROV_DATA_IN_UUID)
- };
- CONST uint8 mesh_prov_data_out_UUID[ATT_BT_UUID_SIZE] =
- {
- LO_UINT16(MESH_PROV_DATA_OUT_UUID), HI_UINT16(MESH_PROV_DATA_OUT_UUID)
- };
- CONST uint8 mesh_proxy_UUID[ATT_BT_UUID_SIZE] =
- {
- LO_UINT16(MESH_PROXY_SERVICE_UUID), HI_UINT16(MESH_PROXY_SERVICE_UUID)
- };
- CONST uint8 mesh_proxy_data_in_UUID[ATT_BT_UUID_SIZE] =
- {
- LO_UINT16(MESH_PROXY_DATA_IN_UUID), HI_UINT16(MESH_PROXY_DATA_IN_UUID)
- };
- CONST uint8 mesh_proxy_data_out_UUID[ATT_BT_UUID_SIZE] =
- {
- LO_UINT16(MESH_PROXY_DATA_OUT_UUID), HI_UINT16(MESH_PROXY_DATA_OUT_UUID)
- };
- static mesh_prov_cb * prov_cb;
- static mesh_proxy_cb * proxy_cb;
- static CONST gattAttrType_t mesh_prov_service =
- {ATT_BT_UUID_SIZE, mesh_prov_UUID};
- static uint8 mesh_prov_data_in_props = GATT_PROP_WRITE_NO_RSP;
- static uint8 mesh_prov_data_in_val[20];
- static uint8 mesh_prov_data_out_props = GATT_PROP_NOTIFY;
- static uint8 mesh_prov_data_out_val[20];
- static gattCharCfg_t mesh_prov_data_out_cccd[GATT_MAX_NUM_CONN];
- static CONST gattAttrType_t mesh_proxy_service =
- {ATT_BT_UUID_SIZE, mesh_proxy_UUID};
- static uint8 mesh_proxy_data_in_props = GATT_PROP_WRITE_NO_RSP;
- static uint8 mesh_proxy_data_in_val[20];
- static uint8 mesh_proxy_data_out_props = GATT_PROP_NOTIFY;
- static uint8 mesh_proxy_data_out_val[20];
- static gattCharCfg_t mesh_proxy_data_out_cccd[GATT_MAX_NUM_CONN];
- static gattAttribute_t mesh_prov_attr_tbl[MESH_PROV_IDX_NB] =
- {
-
- {
- { ATT_BT_UUID_SIZE, primaryServiceUUID },
- GATT_PERMIT_READ,
- 0,
- (uint8 *)&mesh_prov_service
- },
-
- {
- { ATT_BT_UUID_SIZE, characterUUID },
- GATT_PERMIT_READ,
- 0,
- &mesh_prov_data_in_props
- },
-
- {
- { ATT_BT_UUID_SIZE, mesh_prov_data_in_UUID },
- GATT_PERMIT_WRITE,
- 0,
- &mesh_prov_data_in_val[0]
- },
-
- {
- { ATT_BT_UUID_SIZE, characterUUID },
- GATT_PERMIT_READ,
- 0,
- &mesh_prov_data_out_props
- },
-
- {
- { ATT_BT_UUID_SIZE, mesh_prov_data_out_UUID },
- 0,
- 0,
- &mesh_prov_data_out_val[0]
- },
-
- {
- { ATT_BT_UUID_SIZE, clientCharCfgUUID },
- GATT_PERMIT_READ | GATT_PERMIT_WRITE,
- 0,
- (uint8 *) &mesh_prov_data_out_cccd
- },
- };
- static gattAttribute_t mesh_proxy_attr_tbl[MESH_PROXY_IDX_NB] =
- {
-
- {
- { ATT_BT_UUID_SIZE, primaryServiceUUID },
- GATT_PERMIT_READ,
- 0,
- (uint8 *)&mesh_proxy_service
- },
-
- {
- { ATT_BT_UUID_SIZE, characterUUID },
- GATT_PERMIT_READ,
- 0,
- &mesh_proxy_data_in_props
- },
-
- {
- { ATT_BT_UUID_SIZE, mesh_proxy_data_in_UUID },
- GATT_PERMIT_WRITE,
- 0,
- &mesh_proxy_data_in_val[0]
- },
-
- {
- { ATT_BT_UUID_SIZE, characterUUID },
- GATT_PERMIT_READ,
- 0,
- &mesh_proxy_data_out_props
- },
-
- {
- { ATT_BT_UUID_SIZE, mesh_proxy_data_out_UUID },
- 0,
- 0,
- &mesh_proxy_data_out_val[0]
- },
-
- {
- { ATT_BT_UUID_SIZE, clientCharCfgUUID },
- GATT_PERMIT_READ | GATT_PERMIT_WRITE,
- 0,
- (uint8 *) &mesh_proxy_data_out_cccd
- },
- };
- static bStatus_t mesh_prov_write_cb
- (
- uint16 connHandle,
- gattAttribute_t *pAttr,
- uint8 *pValue,
- uint8 len,
- uint16 offset
- );
- static bStatus_t mesh_proxy_write_cb
- (
- uint16 connHandle,
- gattAttribute_t *pAttr,
- uint8 *pValue,
- uint8 len,
- uint16 offset
- );
- static void mesh_prov_handle_conn
- (
- uint16 connHandle,
- uint8 changeType
- );
- static void mesh_proxy_handle_conn
- (
- uint16 connHandle,
- uint8 changeType
- );
- CONST gattServiceCBs_t mesh_prov_internal_cbs =
- {
- NULL,
- mesh_prov_write_cb,
- NULL
- };
- CONST gattServiceCBs_t mesh_proxy_internal_cbs =
- {
- NULL,
- mesh_proxy_write_cb,
- NULL
- };
- bStatus_t mesh_prov_init(mesh_prov_cb *cb)
- {
- uint8 status = FAILURE;
-
- GATTServApp_InitCharCfg
- (
- INVALID_CONNHANDLE,
- mesh_prov_data_out_cccd
- );
-
- VOID linkDB_Register(mesh_prov_handle_conn);
-
- status = GATTServApp_RegisterService
- (
- mesh_prov_attr_tbl,
- GATT_NUM_ATTRS(mesh_prov_attr_tbl),
- &mesh_prov_internal_cbs
- );
-
- prov_cb = cb;
- return (status);
- }
- bStatus_t mesh_proxy_init(mesh_proxy_cb *cb)
- {
- uint8 status = FAILURE;
-
- GATTServApp_InitCharCfg
- (
- INVALID_CONNHANDLE,
- mesh_proxy_data_out_cccd
- );
-
- VOID linkDB_Register(mesh_proxy_handle_conn);
-
- status = GATTServApp_RegisterService
- (
- mesh_proxy_attr_tbl,
- GATT_NUM_ATTRS(mesh_proxy_attr_tbl),
- &mesh_proxy_internal_cbs
- );
-
- proxy_cb = cb;
- return (status);
- }
- bStatus_t mesh_prov_deinit(void)
- {
- uint8 status = SUCCESS;
-
- prov_cb = NULL;
-
- status = GATTServApp_DeregisterService
- (
- GATT_SERVICE_HANDLE(mesh_prov_attr_tbl),
- NULL
- );
- return ( status );
- }
- bStatus_t mesh_proxy_deinit(void)
- {
- uint8 status = SUCCESS;
-
- proxy_cb = NULL;
-
- status = GATTServApp_DeregisterService
- (
- GATT_SERVICE_HANDLE(mesh_proxy_attr_tbl),
- NULL
- );
- return ( status );
- }
- bStatus_t mesh_prov_notify_data_out
- (
- uint16 conn_hndl,
- uint8 attidx,
- uint8 * val,
- uint8 val_len
- )
- {
- attHandleValueNoti_t mesh_prov_notif;
- uint16 value = GATTServApp_ReadCharCfg
- (
- conn_hndl,
- mesh_prov_data_out_cccd
- );
-
- (void)attidx;
-
- mesh_prov_notif.handle =\
- mesh_prov_attr_tbl[MESH_PROV_DATA_OUT_VALUE_VAL].handle;
-
- mesh_prov_notif.len = val_len;
- osal_memcpy(mesh_prov_notif.value, val, val_len);
-
- if ( value & GATT_CLIENT_CFG_NOTIFY )
- {
-
- return GATT_Notification( conn_hndl, &mesh_prov_notif, FALSE );
- }
- return bleIncorrectMode;
- }
- bStatus_t mesh_proxy_notify_data_out
- (
- uint16 conn_hndl,
- uint8 attidx,
- uint8 * val,
- uint8 val_len
- )
- {
- attHandleValueNoti_t mesh_proxy_notif;
- uint16 value = GATTServApp_ReadCharCfg
- (
- conn_hndl,
- mesh_proxy_data_out_cccd
- );
-
- (void)attidx;
-
- mesh_proxy_notif.handle =\
- mesh_proxy_attr_tbl[MESH_PROV_DATA_OUT_VALUE_VAL].handle;
-
- mesh_proxy_notif.len = val_len;
- osal_memcpy(mesh_proxy_notif.value, val, val_len);
-
- if ( value & GATT_CLIENT_CFG_NOTIFY )
- {
-
- return GATT_Notification( conn_hndl, &mesh_proxy_notif, FALSE );
- }
- return bleIncorrectMode;
- }
- static bStatus_t mesh_prov_write_cb
- (
- uint16 connHandle,
- gattAttribute_t * pAttr,
- uint8 * pValue,
- uint8 len,
- uint16 offset
- )
- {
- bStatus_t status = SUCCESS;
-
- if ( gattPermitAuthorWrite( pAttr->permissions ) )
- {
-
- return ( ATT_ERR_INSUFFICIENT_AUTHOR );
- }
- if ( pAttr->type.len == ATT_BT_UUID_SIZE )
- {
-
- uint16 uuid = BUILD_UINT16( pAttr->type.uuid[0], pAttr->type.uuid[1]);
- switch (uuid)
- {
- case MESH_PROV_DATA_IN_UUID:
-
-
-
- if ( offset != 0 )
- {
- status = ATT_ERR_ATTR_NOT_LONG;
- }
-
- if (SUCCESS == status)
- {
-
-
-
-
- if (NULL != prov_cb)
- {
- prov_cb->prov_data_in_cb
- (
- connHandle,
- offset,
- len,
- pValue
- );
- }
- }
- break;
- case GATT_CLIENT_CHAR_CFG_UUID:
-
- status = GATTServApp_ProcessCCCWriteReq
- (
- connHandle,
- pAttr,
- pValue,
- len,
- offset,
- GATT_CLIENT_CFG_NOTIFY
- );
- if ( status == SUCCESS )
- {
- uint16 t_cccd_val = BUILD_UINT16( pValue[0], pValue[1] );
- t_cccd_val = (GATT_CLIENT_CFG_NOTIFY == t_cccd_val) ?\
- TRUE : FALSE;
-
- if (NULL != prov_cb)
- {
- prov_cb->prov_data_out_ccd_cb
- (
- connHandle,
- t_cccd_val
- );
- }
- }
- break;
- default:
- status = ATT_ERR_ATTR_NOT_FOUND;
- break;
- }
- }
- else
- {
-
- status = ATT_ERR_INVALID_HANDLE;
- }
- return ( status );
- }
- static bStatus_t mesh_proxy_write_cb
- (
- uint16 connHandle,
- gattAttribute_t * pAttr,
- uint8 * pValue,
- uint8 len,
- uint16 offset
- )
- {
- bStatus_t status = SUCCESS;
-
- if ( gattPermitAuthorWrite( pAttr->permissions ) )
- {
-
- return ( ATT_ERR_INSUFFICIENT_AUTHOR );
- }
- if ( pAttr->type.len == ATT_BT_UUID_SIZE )
- {
-
- uint16 uuid = BUILD_UINT16( pAttr->type.uuid[0], pAttr->type.uuid[1]);
- switch (uuid)
- {
- case MESH_PROXY_DATA_IN_UUID:
-
-
-
- if ( offset != 0 )
- {
- status = ATT_ERR_ATTR_NOT_LONG;
- }
-
- if (SUCCESS == status)
- {
-
-
-
-
- if (NULL != proxy_cb)
- {
- proxy_cb->proxy_data_in_cb
- (
- connHandle,
- offset,
- len,
- pValue
- );
- }
- }
- break;
- case GATT_CLIENT_CHAR_CFG_UUID:
-
- status = GATTServApp_ProcessCCCWriteReq
- (
- connHandle,
- pAttr,
- pValue,
- len,
- offset,
- GATT_CLIENT_CFG_NOTIFY
- );
- if ( status == SUCCESS )
- {
- uint16 t_cccd_val = BUILD_UINT16( pValue[0], pValue[1] );
- t_cccd_val = (GATT_CLIENT_CFG_NOTIFY == t_cccd_val) ?\
- TRUE : FALSE;
-
- if (NULL != proxy_cb)
- {
- proxy_cb->proxy_data_out_ccd_cb
- (
- connHandle,
- t_cccd_val
- );
- }
- }
- break;
- default:
- status = ATT_ERR_ATTR_NOT_FOUND;
- break;
- }
- }
- else
- {
-
- status = ATT_ERR_INVALID_HANDLE;
- }
- return ( status );
- }
- static void mesh_prov_handle_conn( uint16 connHandle, uint8 changeType )
- {
-
- if ( connHandle != LOOPBACK_CONNHANDLE )
- {
-
- if ( ( changeType == LINKDB_STATUS_UPDATE_REMOVED ) ||
- ( ( changeType == LINKDB_STATUS_UPDATE_STATEFLAGS ) &&
- ( !linkDB_Up( connHandle ) ) ) )
- {
- GATTServApp_InitCharCfg( connHandle, mesh_prov_data_out_cccd );
- }
- }
- }
- static void mesh_proxy_handle_conn( uint16 connHandle, uint8 changeType )
- {
-
- if ( connHandle != LOOPBACK_CONNHANDLE )
- {
-
- if ( ( changeType == LINKDB_STATUS_UPDATE_REMOVED ) ||
- ( ( changeType == LINKDB_STATUS_UPDATE_STATEFLAGS ) &&
- ( !linkDB_Up( connHandle ) ) ) )
- {
- GATTServApp_InitCharCfg( connHandle, mesh_proxy_data_out_cccd );
- }
- }
- }
|