bluetooth control motor
Dependents: BLE_LED_IDB0XA1_demo MOTOR_BLE_V2 Motor_Ble_v1 Motor_Ble_v10223 ... more
Fork of X_NUCLEO_IDB0XA1 by
Revision 267:cd7870e466b3, committed 2016-09-15
- Comitter:
- Vincent Coubard
- Date:
- Thu Sep 15 10:51:30 2016 +0100
- Branch:
- bde03b1e2c8e385819a23a5b6b31c97b4811d0a3
- Parent:
- 266:b49e28134d83
- Child:
- 268:c0a1e03c5736
- Commit message:
- Sync with bde03b1e2c8e385819a23a5b6b31c97b4811d0a3
2016-07-12 11:31:21+01:00: Vincent Coubard
* Handle write authorization requests.
* Fix onDataWritten call (regression from master).
Changed in this revision
--- a/source/BlueNRGGattServer.cpp Thu Sep 15 10:51:29 2016 +0100 +++ b/source/BlueNRGGattServer.cpp Thu Sep 15 10:51:30 2016 +0100 @@ -176,7 +176,7 @@ (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE| GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE))) { PRINTF("Setting up Gatt GATT_NOTIFY_ATTRIBUTE_WRITE Mask\n\r"); - Gatt_Evt_Mask = Gatt_Evt_Mask | GATT_NOTIFY_ATTRIBUTE_WRITE /* | GATT_NOTIFY_WRITE_REQ_AND_WAIT_FOR_APPL_RESP */; + Gatt_Evt_Mask = Gatt_Evt_Mask | GATT_NOTIFY_ATTRIBUTE_WRITE | GATT_NOTIFY_WRITE_REQ_AND_WAIT_FOR_APPL_RESP; } if((p_char->getProperties() & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ| @@ -567,6 +567,42 @@ return BLE_ERROR_NONE; } +// ask if the write request should be accepted of rejected +// return 0 in case of success or an ATT error response in +// case of faillure +uint8_t BlueNRGGattServer::Write_Request_CB( + uint16_t connection_handle, uint16_t attr_handle, uint8_t data_length, + const uint8_t* data) { + + GattCharacteristic* characteristic = getCharacteristicFromHandle(attr_handle); + if(!characteristic) { + return AUTH_CALLBACK_REPLY_ATTERR_INVALID_HANDLE & 0xFF; + } + + // check if the data length is in range + if (characteristic->getValueAttribute().getMaxLength() < data_length) { + return AUTH_CALLBACK_REPLY_ATTERR_INVALID_ATT_VAL_LENGTH & 0xFF; + } + + // if the length of the characteristic value is fixed + // then the data in input should be of that length + if (characteristic->getValueAttribute().hasVariableLength() == false && + characteristic->getValueAttribute().getMaxLength() != data_length) { + return AUTH_CALLBACK_REPLY_ATTERR_INVALID_ATT_VAL_LENGTH & 0xFF; + } + + GattWriteAuthCallbackParams params = { + connection_handle, + attr_handle, + /* offset */ 0, + data_length, + data, + /* authorizationReply */ AUTH_CALLBACK_REPLY_ATTERR_WRITE_NOT_PERMITTED + }; + + return characteristic->authorizeWrite(¶ms) & 0xFF; +} + /**************************************************************************/ /*! @brief Returns the GattCharacteristic according to the handle provided
--- a/source/platform/btle.cpp Thu Sep 15 10:51:29 2016 +0100 +++ b/source/platform/btle.cpp Thu Sep 15 10:51:30 2016 +0100 @@ -369,13 +369,15 @@ //BlueNRGGattServer::getInstance().handleEvent(GattServerEvents::GATT_EVENT_DATA_WRITTEN, attr_handle); //Write the actual Data to the Attr Handle? (uint8_1[])att_data contains the data if ((p_char->getValueAttribute().getValuePtr() != NULL) && (p_char->getValueAttribute().getLength() > 0)) { - BlueNRGGattServer::getInstance().write(p_char->getValueAttribute().getHandle(), - (uint8_t*)att_data, - data_length, - false); + BlueNRGGattServer::getInstance().write( + p_char->getValueAttribute().getHandle(), + (uint8_t*)att_data, + data_length, + false + ); + } BlueNRGGattServer::getInstance().HCIDataWrittenEvent(&writeParams); - } } else { PRINTF("*****WRITE DESCRIPTOR CASE\n\r"); @@ -522,11 +524,31 @@ switch(blue_evt->ecode){ - // case EVT_BLUE_GATT_WRITE_PERMIT_REQ: - // { - // printf("write request !!!!\r\"); - // } - // break; + case EVT_BLUE_GATT_WRITE_PERMIT_REQ: + { + PRINTF("EVT_BLUE_GATT_WRITE_PERMIT_REQ\r\n"); + evt_gatt_write_permit_req* write_req = (evt_gatt_write_permit_req*)blue_evt->data; + + // ask the local server if the write operation is authorized + uint8_t err_code = BlueNRGGattServer::getInstance().Write_Request_CB( + write_req->conn_handle, + write_req->attr_handle, + write_req->data_length, + write_req->data + ); + uint8_t write_status = err_code == 0 ? 0 : 1; + + // reply to the shield + tBleStatus err = aci_gatt_write_response( + write_req->conn_handle, + write_req->attr_handle, + write_status, + err_code, + write_req->data_length, + write_req->data + ); + } + break; case EVT_BLUE_GATT_READ_PERMIT_REQ: {
--- a/x-nucleo-idb0xa1/BlueNRGGattServer.h Thu Sep 15 10:51:29 2016 +0100 +++ b/x-nucleo-idb0xa1/BlueNRGGattServer.h Thu Sep 15 10:51:30 2016 +0100 @@ -78,6 +78,10 @@ void eventCallback(void); //void hwCallback(void *pckt); ble_error_t Read_Request_CB(uint16_t attributeHandle); + uint8_t Write_Request_CB( + uint16_t connection_handle, uint16_t attr_handle, + uint8_t data_length, const uint8_t* data + ); GattCharacteristic* getCharacteristicFromHandle(uint16_t charHandle); void HCIDataWrittenEvent(const GattWriteCallbackParams *params); void HCIDataReadEvent(const GattReadCallbackParams *params);