parsing pair setup method
This commit is contained in:
1 parent
3ec80bc155
commit
f606157039
5 files changed
+193
-36
No files matched your search
@@ -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(¶m_val, ¶m_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;
|
||||
}
|
||||
Reference in new issue
Block a user