Skip to content

Commit

Permalink
* BLEManager Implementation for QPG6100 updated for full Rendezvous f…
Browse files Browse the repository at this point in the history
…low (#4302)

* Stack usage optimizations Configuration manager port functionality
* Cleanup of smaller items
* Update qpg_sdk submodule for additional API

* comments integrated
* Merge conflict with #4304 fixed - Create -> Adopt
  • Loading branch information
tima-q authored Jan 13, 2021
1 parent ab9dda6 commit 872015b
Show file tree
Hide file tree
Showing 14 changed files with 236 additions and 96 deletions.
1 change: 1 addition & 0 deletions examples/lock-app/qpg6100/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export QPG6100_SDK_ROOT=${HOME}/qpg-connectedhomeip

```
cd ~/connectedhomeip/examples/lock-app/qpg6100
source third_party/connectedhomeip/scripts/activate.sh
gn gen out/debug
ninja -C out/debug
```
Expand Down
1 change: 0 additions & 1 deletion examples/lock-app/qpg6100/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

#include "Server.h"
#include "attribute-storage.h"
#include "gen/cluster-id.h"

#include <setup_payload/QRCodeSetupPayloadGenerator.h>
#include <setup_payload/SetupPayload.h>
Expand Down
2 changes: 0 additions & 2 deletions examples/lock-app/qpg6100/src/ZclCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
#include "AppTask.h"
#include "BoltLockManager.h"

#include "gen/attribute-id.h"
#include "gen/cluster-id.h"
#include <app/chip-zcl-zpro-codec.h>
#include <app/util/af-types.h>
#include <app/util/attribute-storage.h>
Expand Down
33 changes: 5 additions & 28 deletions examples/lock-app/qpg6100/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Copyright (c) 2020, Qorvo Inc
*
*
*
* This software is owned by Qorvo Inc
* and protected under applicable copyright laws.
* It is delivered under the terms of the license
* and is intended and supplied for use solely and
* exclusively with products manufactured by
* Qorvo Inc.
*
*
* THIS SOFTWARE IS PROVIDED IN AN "AS IS"
* CONDITION. NO WARRANTIES, WHETHER EXPRESS,
* IMPLIED OR STATUTORY, INCLUDING, BUT NOT
* LIMITED TO, IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
* QORVO INC. SHALL NOT, IN ANY
* CIRCUMSTANCES, BE LIABLE FOR SPECIAL,
* INCIDENTAL OR CONSEQUENTIAL DAMAGES,
* FOR ANY REASON WHATSOEVER.
*
* $Change: 154416 $
* $DateTime: 2020/08/21 09:47:42 $
*
*/

/** @file "main.cpp"
*
Expand All @@ -62,6 +34,8 @@

// CHIP includes
#include <platform/CHIPDeviceLayer.h>
#include <support/CHIPMem.h>
#include <support/CHIPPlatformMemory.h>
#include <support/logging/CHIPLogging.h>

// Application level logic
Expand Down Expand Up @@ -109,6 +83,9 @@ int CHIP_Init(void)
{
int ret = CHIP_ERROR_MAX;

// Init Chip memory management before the stack
chip::Platform::MemoryInit();

ChipLogProgress(NotSpecified, "Init CHIP Stack");
ret = PlatformMgr().InitChipStack();
if (ret != CHIP_NO_ERROR)
Expand Down
1 change: 0 additions & 1 deletion src/lwip/qpg6100/lwipopts.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
#define LWIP_TCPIP_CORE_LOCKING 1
#define TCP_QUEUE_OOSEQ 0
#define ARP_QUEUEING (0)
#define TCPIP_THREAD_NAME "LWIP"

#define LWIP_SOCKET 0

Expand Down
1 change: 1 addition & 0 deletions src/platform/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ if (chip_device_platform != "none" && chip_device_platform != "external") {
"qpg6100/ConfigurationManagerImpl.h",
"qpg6100/ConnectivityManagerImpl.cpp",
"qpg6100/ConnectivityManagerImpl.h",
"qpg6100/DeviceNetworkProvisioningDelegateImpl.cpp",
"qpg6100/DeviceNetworkProvisioningDelegateImpl.h",
"qpg6100/InetPlatformConfig.h",
"qpg6100/Logging.cpp",
Expand Down
172 changes: 161 additions & 11 deletions src/platform/qpg6100/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,34 @@

#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE

#include <ble/BleUUID.h>
#include <ble/CHIPBleServiceData.h>
#include <platform/internal/BLEManager.h>

#include <support/CodeUtils.h>
#include <support/logging/CHIPLogging.h>

using namespace chip::Ble;
using namespace ::chip;
using namespace ::chip::Ble;
using namespace ::chip::System;

namespace chip {
namespace DeviceLayer {
namespace Internal {

namespace {

const uint16_t UUID16_CHIPoBLEService = 0xFEAF;
// Advertising data content definitions
#define CHIP_ADV_DATA_TYPE_FLAGS 0x01
#define CHIP_ADV_DATA_TYPE_UUID 0x03
#define CHIP_ADV_DATA_FLAGS 0x06
#define CHIP_ADV_DATA_TYPE_NAME 0x09
#define CHIP_ADV_DATA_TYPE_SERVICE_DATA 0x16

#define CHIP_ADV_SHORT_UUID_LEN 2
#define CHIP_ADV_CHIP_OVER_BLE_SERVICE_UUID16 0xFEAF

// Full service UUID - CHIP_BLE_SVC_ID - taken from BleUUID.h header
const ChipBleUUID chipUUID_CHIPoBLEChar_RX = { { 0x18, 0xEE, 0x2E, 0xF5, 0x26, 0x3D, 0x45, 0x59, 0x95, 0x9F, 0x4F, 0x9C, 0x42, 0x9F,
0x9D, 0x11 } };

Expand Down Expand Up @@ -70,7 +82,7 @@ CHIP_ERROR BLEManagerImpl::_Init()
appCbacks.stackCback = ExternalCbHandler;
appCbacks.chrReadCback = HandleTXCharRead;
appCbacks.chrWriteCback = HandleRXCharWrite;
appCbacks.cccCback = HandleTXCharCCCDWrite;
appCbacks.cccCback = _handleTXCharCCCDWrite;
qvCHIP_BleInit(&appCbacks);

PlatformMgr().ScheduleWork(DriveBLEState, 0);
Expand Down Expand Up @@ -158,10 +170,37 @@ void BLEManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event)
switch (event->Type)
{
// Platform specific events
case DeviceEventType::kCHIPoBLESubscribe: {
ChipDeviceEvent connEstEvent;

ChipLogProgress(DeviceLayer, "_OnPlatformEvent kCHIPoBLESubscribe");
HandleSubscribeReceived(event->CHIPoBLESubscribe.ConId, &CHIP_BLE_SVC_ID, &chipUUID_CHIPoBLEChar_TX);
connEstEvent.Type = DeviceEventType::kCHIPoBLEConnectionEstablished;
PlatformMgr().PostEvent(&connEstEvent);
}
break;

case DeviceEventType::kCHIPoBLEUnsubscribe: {
ChipLogProgress(DeviceLayer, "_OnPlatformEvent kCHIPoBLEUnsubscribe");
HandleUnsubscribeReceived(event->CHIPoBLEUnsubscribe.ConId, &CHIP_BLE_SVC_ID, &chipUUID_CHIPoBLEChar_TX);
}
break;

case DeviceEventType::kCHIPoBLEWriteReceived: {
ChipLogProgress(DeviceLayer, "_OnPlatformEvent kCHIPoBLEWriteReceived");
HandleWriteReceived(event->CHIPoBLEWriteReceived.ConId, &CHIP_BLE_SVC_ID, &chipUUID_CHIPoBLEChar_RX,
PacketBufferHandle::Adopt(event->CHIPoBLEWriteReceived.Data));
}
break;
case DeviceEventType::kCHIPoBLETXCharWriteEvent:
HandleTXComplete(event);
break;

case DeviceEventType::kCHIPoBLEConnectionError: {
ChipLogProgress(DeviceLayer, "_OnPlatformEvent kCHIPoBLEConnectionError");
HandleConnectionError(event->CHIPoBLEConnectionError.ConId, event->CHIPoBLEConnectionError.Reason);
}
break;
// Generic CHIP events
case DeviceEventType::kFabricMembershipChange:
case DeviceEventType::kServiceProvisioningChange:
Expand Down Expand Up @@ -226,18 +265,18 @@ uint16_t BLEManagerImpl::GetMTU(BLE_CONNECTION_OBJECT conId) const
bool BLEManagerImpl::SendIndication(BLE_CONNECTION_OBJECT conId, const ChipBleUUID * svcId, const ChipBleUUID * charId,
PacketBufferHandle data)
{
CHIP_ERROR err = CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
CHIP_ERROR err = CHIP_NO_ERROR;
bool isRxHandle;
uint16_t cId;
uint16_t dataLen = data->DataLength();

VerifyOrExit(IsSubscribed(conId), err = CHIP_ERROR_INVALID_ARGUMENT);
ChipLogDetail(DeviceLayer, "Sending indication for CHIPoBLE TX characteristic (con %u, len %u)", conId, dataLen);
ChipLogDetail(DeviceLayer, "Sending indication for CHIPoBLE Client RX characteristic (con %u, len %u)", conId, dataLen);

isRxHandle = UUIDsMatch(&chipUUID_CHIPoBLEChar_RX, charId);
isRxHandle = UUIDsMatch(&chipUUID_CHIPoBLEChar_TX, charId);
cId = qvCHIP_BleGetHandle(isRxHandle);

qvCHIP_TxData(conId, cId, dataLen, data->Start());
qvCHIP_BleSendIndication(conId, cId, dataLen, data->Start());

exit:
if (err != CHIP_NO_ERROR)
Expand Down Expand Up @@ -327,11 +366,92 @@ void BLEManagerImpl::DriveBLEState(void)
}
}

CHIP_ERROR BLEManagerImpl::ConfigureAdvertisingData(void)
{
ChipBLEDeviceIdentificationInfo mDeviceIdInfo;
CHIP_ERROR err;
uint8_t index = 0;
uint8_t mDeviceNameLength = 0;
uint8_t mDeviceIdInfoLength = 0;

memset(mAdvDataBuf, 0, kMaxAdvertisementDataSetSize);
memset(mScanRespDataBuf, 0, kMaxAdvertisementDataSetSize);

err = ConfigurationMgr().GetBLEDeviceIdentificationInfo(mDeviceIdInfo);
SuccessOrExit(err);

if (!GetFlag(mFlags, kFlag_DeviceNameSet))
{
snprintf(mDeviceName, sizeof(mDeviceName), "%s%04" PRIX32, CHIP_DEVICE_CONFIG_BLE_DEVICE_NAME_PREFIX, (uint32_t) 0);

mDeviceName[kMaxDeviceNameLength] = 0;
qvCHIP_BleSetDeviceName(mDeviceName);
}

mDeviceNameLength = static_cast<uint8_t>(strlen(mDeviceName));
mDeviceIdInfoLength = sizeof(mDeviceIdInfo);

// Check sizes
static_assert(sizeof(mDeviceIdInfo) + CHIP_ADV_SHORT_UUID_LEN + 1 <= UINT8_MAX, "Our length won't fit in a uint8_t");
static_assert(2 + CHIP_ADV_SHORT_UUID_LEN + sizeof(mDeviceIdInfo) + 1 <= kMaxAdvertisementDataSetSize,
"Advertisement data buffer is not big enough");

// Fill in advertising data
index = 0;
mAdvDataBuf[index++] = 0x02; // length
mAdvDataBuf[index++] = CHIP_ADV_DATA_TYPE_FLAGS; // AD type : flags
mAdvDataBuf[index++] = CHIP_ADV_DATA_FLAGS; // AD value

mAdvDataBuf[index++] = static_cast<uint8_t>(mDeviceIdInfoLength + CHIP_ADV_SHORT_UUID_LEN + 1); // AD length
mAdvDataBuf[index++] = CHIP_ADV_DATA_TYPE_SERVICE_DATA; // AD type : Service Data
mAdvDataBuf[index++] = CHIP_ADV_CHIP_OVER_BLE_SERVICE_UUID16 & 0xFF; // AD value
mAdvDataBuf[index++] = (CHIP_ADV_CHIP_OVER_BLE_SERVICE_UUID16 >> 8) & 0xFF;
memcpy(&mAdvDataBuf[index], (void *) &mDeviceIdInfo, mDeviceIdInfoLength); // AD value
index = static_cast<uint8_t>(index + mDeviceIdInfoLength);

mAdvDataBuf[index++] = static_cast<uint8_t>(mDeviceNameLength + 1); // length
mAdvDataBuf[index++] = CHIP_ADV_DATA_TYPE_NAME; // AD type : name
memcpy(&mAdvDataBuf[index], mDeviceName, mDeviceNameLength); // AD value
index = static_cast<uint8_t>(index + mDeviceNameLength);

qvCHIP_BleSetAdvData(QV_ADV_DATA_LOC_ADV, index, mAdvDataBuf);

// Fill in scan response data
index = 0;
mScanRespDataBuf[index++] = CHIP_ADV_SHORT_UUID_LEN + 1; // AD length
mScanRespDataBuf[index++] = CHIP_ADV_DATA_TYPE_UUID; // AD type : uuid
mScanRespDataBuf[index++] = CHIP_ADV_CHIP_OVER_BLE_SERVICE_UUID16 & 0xFF; // AD value
mScanRespDataBuf[index++] = (CHIP_ADV_CHIP_OVER_BLE_SERVICE_UUID16 >> 8) & 0xFF;

qvCHIP_BleSetAdvData(QV_ADV_DATA_LOC_SCAN, index, mScanRespDataBuf);

exit:
return err;
}

CHIP_ERROR BLEManagerImpl::StartAdvertising(void)
{
CHIP_ERROR err;
uint16_t interval;
ChipLogProgress(DeviceLayer, "CHIPoBLE start advertising");

return qvCHIP_BleStartAdvertising();
err = ConfigureAdvertisingData();
SuccessOrExit(err);

SetFlag(mFlags, kFlag_Advertising, true);

interval = ((mNumGAPCons == 0 && !ConfigurationMgr().IsPairedToAccount()) || GetFlag(mFlags, kFlag_FastAdvertisingEnabled))
? CHIP_DEVICE_CONFIG_BLE_FAST_ADVERTISING_INTERVAL
: CHIP_DEVICE_CONFIG_BLE_SLOW_ADVERTISING_INTERVAL;

// Minimum and maximum interval are the same
qvCHIP_BleSetAdvInterval(interval, interval);

err = qvCHIP_BleStartAdvertising();
// TODO: translate from qvStatus_t to CHIP_ERROR

exit:
return err;
}

CHIP_ERROR BLEManagerImpl::StopAdvertising(void)
Expand Down Expand Up @@ -389,18 +509,18 @@ void BLEManagerImpl::HandleTXCharRead(uint16_t connId, uint16_t handle, uint8_t
{
uint8_t rsp = 0;

ChipLogProgress(DeviceLayer, "Read request received for CHIPoBLE TX characteristic (con %u)", connId);
ChipLogProgress(DeviceLayer, "Read request received for CHIPoBLE Client RX characteristic (con %u)", connId);

// Send a zero-length response.
qvCHIP_TxData(connId, handle, 0, &rsp);
qvCHIP_BleWriteAttr(connId, handle, 0, &rsp);
}

void BLEManagerImpl::HandleRXCharWrite(uint16_t connId, uint16_t handle, uint8_t operation, uint16_t offset, uint16_t len,
uint8_t * pValue, qvCHIP_Ble_Attr_t * pAttr)
{
CHIP_ERROR err = CHIP_NO_ERROR;

ChipLogProgress(DeviceLayer, "Write request received for CHIPoBLE RX characteristic (con %u, len %u)", connId, len);
ChipLogProgress(DeviceLayer, "Write request received for CHIPoBLE Client TX characteristic (con %u, len %u)", connId, len);

// Copy the data to a PacketBuffer.
PacketBufferHandle buf = PacketBuffer::New(0);
Expand Down Expand Up @@ -435,6 +555,26 @@ void BLEManagerImpl::HandleTXCharCCCDWrite(qvCHIP_Ble_AttsCccEvt_t * pEvt)
// Determine if the client is enabling or disabling indications.
indicationsEnabled = (pEvt->value != 0);

// If the client has requested to enabled indications
if (indicationsEnabled)
{
// Set subcription only the first time
if (!IsSubscribed(pEvt->hdr.param))
{
// Record that indications have been enabled for this connection.
err = SetSubscribed(pEvt->hdr.param);
VerifyOrExit(err != CHIP_ERROR_NO_MEMORY, err = CHIP_NO_ERROR);
SuccessOrExit(err);
}
}

else
{
// If indications had previously been enabled for this connection, record that they are no longer
// enabled.
UnsetSubscribed(pEvt->hdr.param);
}

// Post an event to the Chip queue to process either a CHIPoBLE Subscribe or Unsubscribe based on
// whether the client is enabling or disabling indications.
{
Expand All @@ -446,6 +586,7 @@ void BLEManagerImpl::HandleTXCharCCCDWrite(qvCHIP_Ble_AttsCccEvt_t * pEvt)

ChipLogProgress(DeviceLayer, "CHIPoBLE %s received", indicationsEnabled ? "subscribe" : "unsubscribe");

exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "HandleTXCharCCCDWrite() failed: %s", ErrorStr(err));
Expand Down Expand Up @@ -565,6 +706,10 @@ void BLEManagerImpl::HandleDmMsg(qvCHIP_Ble_DmEvt_t * pDmEvt)
PlatformMgr().PostEvent(&event);

// Force a refresh of the advertising state.
if (mNumGAPCons > 0)
{
mNumGAPCons--;
}
SetFlag(mFlags, kFlag_AdvertisingRefreshNeeded);
PlatformMgr().ScheduleWork(DriveBLEState, 0);
break;
Expand Down Expand Up @@ -693,6 +838,11 @@ bool BLEManagerImpl::IsSubscribed(uint16_t conId)
return false;
}

void BLEManagerImpl::_handleTXCharCCCDWrite(qvCHIP_Ble_AttsCccEvt_t * event)
{
sInstance.HandleTXCharCCCDWrite(event);
}

} // namespace Internal
} // namespace DeviceLayer
} // namespace chip
Expand Down
Loading

0 comments on commit 872015b

Please sign in to comment.