Exemplo n.º 1
0
/*********************************************************************
 * @fn      ResetWriteAttrCB
 *
 * @brief   Validate attribute data prior to a write operation
 *
 * @param   connHandle - connection message was received on
 * @param   pAttr - pointer to attribute
 * @param   pValue - pointer to data to be written
 * @param   len - length of data
 * @param   offset - offset of the first octet to be written
 * @param   method - type of write message
 *
 * @return  SUCCESS, blePending or Failure
 */
static bStatus_t ResetWriteAttrCB(uint16_t connHandle,
                                  gattAttribute_t *pAttr,
                                  uint8_t *pValue, uint16_t len,
                                  uint16_t offset, uint8_t method)
{
    bStatus_t status = SUCCESS;

    if ( pAttr->type.len == ATT_UUID_SIZE )
    {
        // 128-bit UUID
        if (!memcmp(pAttr->type.uuid, resetCharUUID, ATT_UUID_SIZE))
        {
            uint16_t crc[2] = {0x0000, 0x0000};

            // Invalidate the image.
            OADTarget_writeFlash(OAD_IMG_R_PAGE, OAD_IMG_R_OSET + OAD_IMG_CRC_OSET,
                                 (uint8_t *)crc, 4);

            // Reset.
            HAL_SYSTEM_RESET();
        }
        else
        {
            status = ATT_ERR_INVALID_HANDLE;
        }
    }

    return status;
}
/*********************************************************************
 * @fn      OADTarget_setCrc
 *
 * @brief   Set the CRC shadow of the downloaded image.
 *
 * @param   pCrc - pointer to the new image header
 *
 * @return  None.
 */
void OADTarget_setCrc(uint16_t *pCrc)
{
  // Write CRC shadow to downloaded image.
  OADTarget_writeFlash(OAD_IMG_D_PAGE, OAD_IMG_CRC_OSET, (uint8_t *)pCrc,
                       sizeof(uint16_t) * 2);
    
  // Read the CRC and CRC shadow back from downloaded image.
  OADTarget_readFlash(OAD_IMG_D_PAGE, OAD_IMG_CRC_OSET, (uint8_t *)pCrc, 
                      sizeof(uint16_t) * 2);  
}