parsing pair setup method

This commit is contained in:
hathach committed 2017-10-27 14:39:57 +07:00
1 parent 3ec80bc155
commit f606157039
5 files changed
+193 -36

No files matched your search

@@ -296,11 +296,11 @@ void HAPCharacteristic::_eventHandler(ble_evt_t* event)
if (hap_req->header.control.type != HAP_PDU_REQUEST)
{
hap_resp = createHapResponse(hap_req->header.tid, HAP_STATUS_UNSUPPORTED_PDU, NULL, 0);
hap_resp = createHapResponse(hap_req->header.tid, HAP_STATUS_UNSUPPORTED_PDU);
}
else if (hap_req->header.instance_id != _cid)
{
hap_resp = createHapResponse(hap_req->header.tid, HAP_STATUS_INVALID_INSTANCE_ID, NULL, 0);
hap_resp = createHapResponse(hap_req->header.tid, HAP_STATUS_INVALID_INSTANCE_ID);
}else
{
LOG_LV2("HAP", "Recv %s request", hap_opcode_str[hap_req->header.opcode]);
@@ -313,7 +313,7 @@ void HAPCharacteristic::_eventHandler(ble_evt_t* event)
case HAP_OPCODE_CHR_WRITE:
if (_hap_wr_cb)
{
hap_resp = _hap_wr_cb(*this, hap_req, gatt_req->op, gatt_req->offset);
hap_resp = _hap_wr_cb(*this, gatt_req, hap_req);
}
break;
@@ -331,7 +331,7 @@ void HAPCharacteristic::_eventHandler(ble_evt_t* event)
// case HAP_OPCODE_SVC_SIGNATURE_READ: break;
default:
hap_resp = createHapResponse(hap_req->header.tid, HAP_STATUS_UNSUPPORTED_PDU, NULL, 0);
hap_resp = createHapResponse(hap_req->header.tid, HAP_STATUS_UNSUPPORTED_PDU);
break;
}
}
+3 -2
View File
@@ -55,7 +55,7 @@ enum HAPChrProperties_t
class HAPCharacteristic : public BLECharacteristic
{
public:
typedef HAPResponse_t* (*hap_write_cb_t) (HAPCharacteristic& chr, HAPRequest_t* hap_req, uint8_t write_op, uint16_t offset);
typedef HAPResponse_t* (*hap_write_cb_t) (HAPCharacteristic& chr, ble_gatts_evt_write_t const* gatt_req, HAPRequest_t const* hap_req);
static BLEUuid _g_uuid_cid;
HAPCharacteristic(BLEUuid bleuuid, uint8_t format, uint16_t unit = UUID16_UNIT_UNITLESS);
@@ -71,6 +71,8 @@ class HAPCharacteristic : public BLECharacteristic
// Callbacks
void setHapWriteCallback(hap_write_cb_t fp);
HAPResponse_t* createHapResponse(uint8_t tid, uint8_t status, TLV8_t tlv_para[] = NULL, uint8_t count = 0);
/*------------- Internal Functions -------------*/
virtual void _eventHandler(ble_evt_t* event);
@@ -92,7 +94,6 @@ class HAPCharacteristic : public BLECharacteristic
HAPResponse_t* processChrSignatureRead(HAPRequest_t* hap_req);
HAPResponse_t* processChrRead(HAPRequest_t* hap_req);
HAPResponse_t* createHapResponse(uint8_t tid, uint8_t status, TLV8_t tlv_para[], uint8_t count);
};
#endif /* HAPCHARACTERISTIC_H_ */
+60
View File
@@ -0,0 +1,60 @@
/**************************************************************************/
/*!
@file HAPProcedure.cpp
@author hathach
@section LICENSE
Software License Agreement (BSD License)
Copyright (c) 2017, Adafruit Industries (adafruit.com)
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 holders nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''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 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.
*/
/**************************************************************************/
#include <Arduino.h>
#include "HAPUuid.h"
#include "HAPProcedure.h"
TLV8_t tlv8_decode_next(uint8_t const** pp_data, uint16_t* p_len)
{
TLV8_t tlv;
varclr(&tlv);
if ( (*p_len) )
{
uint8_t const* data = *pp_data;
tlv.type = data[0];
tlv.len = data[1];
tlv.value = data+2;
// next item
(*p_len) -= tlv.len+2;
(*pp_data) += tlv.len+2;
}
return tlv;
}
+4 -29
View File
@@ -85,35 +85,6 @@ enum HAPPduType_t
HAP_PDU_RESPONSE,
};
enum TLVError_t
{
TLV_ERROR_UNKNOWN = 1 ,
TLV_ERROR_AUTHENTICATION ,
TLV_ERROR_BACKOFF ,
TLV_ERROR_MAX_PEERS ,
TLV_ERROR_MAX_TRIES ,
TLV_ERROR_UNAVAILABLE ,
TLV_ERROR_BUSY
};
enum TLVType_t
{
TLV_TYPE_METHOD = 0 ,
TLV_TYPE_IDENTIFIER ,
TLV_TYPE_SALT ,
TLV_TYPE_PUBLIC_KEY ,
TLV_TYPE_PROOF ,
TLV_TYPE_ENCRYPTED_DATA ,
TLV_TYPE_STATE ,
TLV_TYPE_ERROR ,
TLV_TYPE_RETRY_DELAY ,
TLV_TYPE_CERTIFICATE ,
TLV_TYPE_SIGNATURE ,
TLV_TYPE_PERMISSIONS ,
TLV_TYPE_FRAGMENT_DATA ,
TLV_TYPE_FRAGMENT_LAST ,
TLV_TYPE_SEPARATOR
};
typedef struct ATTR_PACKED
{
@@ -122,6 +93,8 @@ typedef struct ATTR_PACKED
const void* value; // need serialization, not reflect physical layout
}TLV8_t;
typedef struct ATTR_PACKED
{
uint8_t lenext : 1; // not used
@@ -175,4 +148,6 @@ typedef struct ATTR_PACKED
TLV8_t tlv8_decode_next(uint8_t const** pp_data, uint16_t* p_len);
#endif /* HAPPROCEDURE_H_ */
+122 -1
View File
@@ -38,6 +38,66 @@
#include "HAPUuid.h"
#include "HAPPairing.h"
// kTLV type for pairing
enum {
PAIRING_TYPE_METHOD = 0,
PAIRING_TYPE_IDENTIFIER,
PAIRING_TYPE_SALT,
PAIRING_TYPE_PUBLIC_KEY,
PAIRING_TYPE_PROOF,
PAIRING_TYPE_ENCRYPTED_DATA,
PAIRING_TYPE_STATE,
PAIRING_TYPE_ERROR,
PAIRING_TYPE_RETRY_DELAY,
PAIRING_TYPE_CERTIFICATE,
PAIRING_TYPE_SIGNATURE,
PAIRING_TYPE_PERMISSIONS,
PAIRING_TYPE_FRAGMENT_DATA,
PAIRING_TYPE_FRAGMENT_LAST,
PAIRING_TYPE_SEPARATOR = 0xff
};
// kTLV value for pairing method
enum
{
PAIRING_METHOD_SETUP = 1,
PAIRING_METHOD_VERIFY,
PAIRING_METHOD_ADD,
PAIRING_METHOD_REMOVE,
PAIRING_METHOD_LIST
};
// kTLV value for pairing error
enum
{
PAIRING_ERROR_UNKNOWN = 1,
PAIRING_ERROR_AUTHENTICATION,
PAIRING_ERROR_BACKOFF,
PAIRING_ERROR_MAX_PEERS,
PAIRING_ERROR_MAX_TRIES,
PAIRING_ERROR_UNAVAILABLE,
PAIRING_ERROR_BUSY
};
// kTLV value for state
enum
{
PAIRING_STATE_M1 = 1,
PAIRING_STATE_M2,
PAIRING_STATE_M3,
PAIRING_STATE_M4,
};
#if CFG_DEBUG >= 2
static const char* pairing_method_str[] =
{
"", "Pair Setup", "Pair Verify", "Add Pairing", "Remove Pairing", "List Pairings"
};
#endif
static HAPResponse_t* pairing_setup_write_cb (HAPCharacteristic& chr, ble_gatts_evt_write_t const* gatt_req, HAPRequest_t const* hap_req);
HAPPairing::HAPPairing(void)
: HAPService(HAP_UUID_SVC_PAIRING),
_setup (HAP_UUID_CHR_PAIR_SETUP , BLE_GATT_CPF_FORMAT_STRUCT),
@@ -52,8 +112,8 @@ err_t HAPPairing::begin(void)
{
VERIFY_STATUS( HAPService::begin() ); // Invoke base class begin()
// TODO read, write using auth
_setup.setHapProperties(HAP_CHR_PROPS_READ | HAP_CHR_PROPS_WRITE);
_setup.setHapWriteCallback(pairing_setup_write_cb);
_setup.setMaxLen(BLE_GATTS_VAR_ATTR_LEN_MAX);
VERIFY_STATUS( _setup.begin() );
@@ -70,3 +130,64 @@ err_t HAPPairing::begin(void)
return ERROR_NONE;
}
static HAPResponse_t* pairing_setup_write_cb (HAPCharacteristic& chr, ble_gatts_evt_write_t const* gatt_req, HAPRequest_t const* hap_req)
{
VERIFY(gatt_req->len > 3, NULL);
HAPResponse_t* hap_resp = NULL;
uint16_t body_len = hap_req->body_len;
uint8_t const* body_data = hap_req->body_data;
uint8_t const* param_val = NULL;
uint16_t param_len = 0;
bool is_write_resp = false;
// TODO HAP Fragmentation
// Parse TLV data
while (body_len)
{
TLV8_t tlv = tlv8_decode_next(&body_data, &body_len);
switch (tlv.type)
{
case HAP_PARAM_VALUE:
param_val = (uint8_t const*) tlv.value;
param_len = (uint16_t) tlv.len;
break;
case HAP_PARAM_RETURN_RESP:
memcpy(&is_write_resp, tlv.value, 1);
break;
// ignore other type
default: break;
}
}
// Parse sub TLV (kTLV) data
while(param_len)
{
TLV8_t ktlv = tlv8_decode_next(&param_val, &param_len);
switch (ktlv.type)
{
case PAIRING_TYPE_METHOD:
LOG_LV2("HAP", "Method %s", pairing_method_str[ *((uint8_t const*)ktlv.value) ]);
break;
case PAIRING_TYPE_STATE:
LOG_LV2("HAP", "State = M%d", *((uint8_t const*)ktlv.value));
break;
// ignore other type
default: break;
}
}
return hap_resp;
}