Skip to content

Commit

Permalink
Merge pull request #75 from sagar-apple/efr32
Browse files Browse the repository at this point in the history
Import EFR32
  • Loading branch information
woody-apple authored Mar 18, 2020
2 parents 7211b47 + 5cacfa3 commit cf88e2c
Show file tree
Hide file tree
Showing 34 changed files with 5,806 additions and 0 deletions.
209 changes: 209 additions & 0 deletions src/include/platform/EFR32/BLEManagerImpl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
/*
*
* <COPYRIGHT>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @file
* Provides an implementation of the BLEManager singleton object
* for the Silicon Labs EFR32 platforms.
*/

#ifndef BLE_MANAGER_IMPL_H
#define BLE_MANAGER_IMPL_H

#if CHIP_DEVICE_CONFIG_ENABLE_WOBLE

#include "bg_types.h"
#include "rtos_gecko.h"
#include "gatt_db.h"

namespace chip {
namespace DeviceLayer {
namespace Internal {

/**
* Concrete implementation of the NetworkProvisioningServer singleton object for the EFR32 platforms.
*/
class BLEManagerImpl final : public BLEManager,
private ::chip::Ble::BleLayer,
private BlePlatformDelegate,
private BleApplicationDelegate
{
// Allow the BLEManager interface class to delegate method calls to
// the implementation methods provided by this class.
friend BLEManager;

// ===== Members that implement the BLEManager internal interface.

CHIP_ERROR _Init(void);
WoBLEServiceMode _GetWoBLEServiceMode(void);
CHIP_ERROR _SetWoBLEServiceMode(WoBLEServiceMode val);
bool _IsAdvertisingEnabled(void);
CHIP_ERROR _SetAdvertisingEnabled(bool val);
bool _IsFastAdvertisingEnabled(void);
CHIP_ERROR _SetFastAdvertisingEnabled(bool val);
bool _IsAdvertising(void);
CHIP_ERROR _GetDeviceName(char *buf, size_t bufSize);
CHIP_ERROR _SetDeviceName(const char *deviceName);
uint16_t _NumConnections(void);
void _OnPlatformEvent(const ChipDeviceEvent *event);
::chip::Ble::BleLayer *_GetBleLayer(void) const;

// ===== Members that implement virtual methods on BlePlatformDelegate.

bool SubscribeCharacteristic(BLE_CONNECTION_OBJECT conId,
const ChipBleUUID * svcId,
const ChipBleUUID * charId) override;
bool UnsubscribeCharacteristic(BLE_CONNECTION_OBJECT conId,
const ChipBleUUID * svcId,
const ChipBleUUID * charId) override;
bool CloseConnection(BLE_CONNECTION_OBJECT conId) override;
uint16_t GetMTU(BLE_CONNECTION_OBJECT conId) const override;
bool SendIndication(BLE_CONNECTION_OBJECT conId,
const ChipBleUUID * svcId,
const ChipBleUUID * charId,
PacketBuffer * pBuf) override;
bool SendWriteRequest(BLE_CONNECTION_OBJECT conId,
const ChipBleUUID * svcId,
const ChipBleUUID * charId,
PacketBuffer * pBuf) override;
bool SendReadRequest(BLE_CONNECTION_OBJECT conId,
const ChipBleUUID * svcId,
const ChipBleUUID * charId,
PacketBuffer * pBuf) override;
bool SendReadResponse(BLE_CONNECTION_OBJECT conId,
BLE_READ_REQUEST_CONTEXT requestContext,
const ChipBleUUID * svcId,
const ChipBleUUID * charId) override;

// ===== Members that implement virtual methods on BleApplicationDelegate.

void NotifyChipConnectionClosed(BLE_CONNECTION_OBJECT conId) override;

// ===== Members for internal use by the following friends.

friend BLEManager & BLEMgr(void);
friend BLEManagerImpl &BLEMgrImpl(void);

static BLEManagerImpl sInstance;

// ===== Private members reserved for use by this class only.

enum
{
kFlag_AdvertisingEnabled = 0x0001,
kFlag_FastAdvertisingEnabled = 0x0002,
kFlag_Advertising = 0x0004,
kFlag_RestartAdvertising = 0x0008,
kFlag_EFRBLEStackInitialized = 0x0010,
kFlag_DeviceNameSet = 0x0020,
};

enum
{
kMaxConnections = BLE_LAYER_NUM_BLE_ENDPOINTS,
kMaxDeviceNameLength = 16,
kUnusedIndex = 0xFF,
};

struct WoBLEConState
{
bd_addr address;
uint16_t mtu : 10;
uint16_t allocated : 1;
uint16_t subscribed : 1;
uint16_t unused : 4;
uint8_t connectionHandle;
uint8_t bondingHandle;
};

WoBLEConState mBleConnections[kMaxConnections];
uint8_t mIndConfId[kMaxConnections];
WoBLEServiceMode mServiceMode;
uint16_t mFlags;
char mDeviceName[kMaxDeviceNameLength + 1];

CHIP_ERROR MapBLEError(int bleErr);
void DriveBLEState(void);
CHIP_ERROR ConfigureAdvertisingData(void);
CHIP_ERROR StartAdvertising(void);
CHIP_ERROR StopAdvertising(void);
void UpdateMtu(volatile struct gecko_cmd_packet *evt);
void HandleBootEvent(void);
void HandleConnectEvent(volatile struct gecko_cmd_packet *evt);
void HandleConnectionCloseEvent(volatile struct gecko_cmd_packet *evt);
void HandleWriteEvent(volatile struct gecko_cmd_packet *evt);
void HandleTXCharCCCDWrite(volatile struct gecko_cmd_packet *evt);
void HandleRXCharWrite(volatile struct gecko_cmd_packet *evt);
void HandleTxConfirmationEvent(volatile struct gecko_cmd_packet *evt);
void HandleSoftTimerEvent(volatile struct gecko_cmd_packet *evt);
bool RemoveConnection(uint8_t connectionHandle);
void AddConnection(uint8_t connectionHandle, uint8_t bondingHandle);
WoBLEConState *GetConnectionState(uint8_t conId, bool allocate = false);
uint8_t GetTimerHandle(uint8_t connectionHandle, bool allocate = false);
static void DriveBLEState(intptr_t arg);
static void bluetoothStackEventHandler(void *p_arg);
};

/**
* Returns a reference to the public interface of the BLEManager singleton object.
*
* Internal components should use this to access features of the BLEManager object
* that are common to all platforms.
*/
inline BLEManager &BLEMgr(void)
{
return BLEManagerImpl::sInstance;
}

/**
* Returns the platform-specific implementation of the BLEManager singleton object.
*
* Internal components can use this to gain access to features of the BLEManager
* that are specific to the EFR32 platforms.
*/
inline BLEManagerImpl &BLEMgrImpl(void)
{
return BLEManagerImpl::sInstance;
}

inline ::chip::Ble::BleLayer *BLEManagerImpl::_GetBleLayer() const
{
return (BleLayer *)(this);
}

inline BLEManager::WoBLEServiceMode BLEManagerImpl::_GetWoBLEServiceMode(void)
{
return mServiceMode;
}

inline bool BLEManagerImpl::_IsAdvertisingEnabled(void)
{
return GetFlag(mFlags, kFlag_AdvertisingEnabled);
}

inline bool BLEManagerImpl::_IsFastAdvertisingEnabled(void)
{
return GetFlag(mFlags, kFlag_FastAdvertisingEnabled);
}

} // namespace Internal
} // namespace DeviceLayer
} // namespace chip

#endif // CHIP_DEVICE_CONFIG_ENABLE_WOBLE

#endif // BLE_MANAGER_IMPL_H
46 changes: 46 additions & 0 deletions src/include/platform/EFR32/BlePlatformConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
*
* <COPYRIGHT>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @file
* Platform-specific configuration overrides for the OpenChip BLE
* Layer on EFR32 platforms using the Silicon Labs SDK.
*
*/

#ifndef BLE_PLATFORM_CONFIG_H
#define BLE_PLATFORM_CONFIG_H

#include "bg_errorcodes.h"

// ==================== Platform Adaptations ====================

#define BLE_CONNECTION_OBJECT uint8_t
#define BLE_CONNECTION_UNINITIALIZED ((uint8_t)-1)
#define BLE_MAX_RECEIVE_WINDOW_SIZE 5

#define BLE_CONFIG_ERROR_TYPE int32_t
#define BLE_CONFIG_NO_ERROR 0
#define BLE_CONFIG_ERROR_MIN 6000000
#define BLE_CONFIG_ERROR_MAX 6000999
#define _BLE_CONFIG_ERROR(e) (BLE_CONFIG_ERROR_MIN + (e))

// ========== Platform-specific Configuration Overrides =========

/* none so far */

#endif // BLE_PLATFORM_CONFIG_H
133 changes: 133 additions & 0 deletions src/include/platform/EFR32/CHIPDevicePlatformConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
*
* <COPYRIGHT>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @file
* Platform-specific configuration overrides for the Chip Device Layer
* on EFR32 platforms using the Silicon Labs SDK.
*/

#ifndef CHIP_DEVICE_PLATFORM_CONFIG_H
#define CHIP_DEVICE_PLATFORM_CONFIG_H

// ==================== Platform Adaptations ====================

#define CHIP_DEVICE_CONFIG_EFR32_NVM3_ERROR_MIN 12000000
#define CHIP_DEVICE_CONFIG_EFR32_BLE_ERROR_MIN 13000000

#define CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION 0
#define CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP 0

#define CHIP_DEVICE_CONFIG_ENABLE_THREAD 1
#define CHIP_DEVICE_CONFIG_ENABLE_WOBLE 1

#define CHIP_DEVICE_CONFIG_ENABLE_CHIP_TIME_SERVICE_TIME_SYNC 1
#define CHIP_DEVICE_CONFIG_ENABLE_SERVICE_DIRECTORY_TIME_SYNC 0

#define CHIP_DEVICE_CONFIG_PERSISTED_STORAGE_CRIT_EIDC_KEY 2
#define CHIP_DEVICE_CONFIG_PERSISTED_STORAGE_PROD_EIDC_KEY 3
#define CHIP_DEVICE_CONFIG_PERSISTED_STORAGE_INFO_EIDC_KEY 4
#define CHIP_DEVICE_CONFIG_PERSISTED_STORAGE_DEBUG_EIDC_KEY 5

// ========== Platform-specific Configuration =========

// These are configuration options that are unique to the EFR32 platform.
// These can be overridden by the application as needed.
/**
* @def CHIP_DEVICE_CONFIG_LOG_MESSAGE_MAX_SIZE
*
* The maximum size of any log message.
*/
#ifndef CHIP_DEVICE_CONFIG_LOG_MESSAGE_MAX_SIZE
#define CHIP_DEVICE_CONFIG_LOG_MESSAGE_MAX_SIZE 150
#endif // CHIP_DEVICE_CONFIG_LOG_MESSAGE_MAX_SIZE

// -------------- EFR32 NVM3 Storage Configuration -------------

/**
* @def CHIP_DEVICE_CONFIG_NVM3_MAX_NUM_OBJECTS
*
* @brief
* Configures the size of the nvm3 cache and should be set >= the
* maximum number of Chip Config objects, e.g...
* Factory configs[5], System configs[23], Counter configs[32] + margin[4] = 64.
*
*/
#ifndef CHIP_DEVICE_CONFIG_NVM3_MAX_NUM_OBJECTS
#define CHIP_DEVICE_CONFIG_NVM3_MAX_NUM_OBJECTS 64
#endif // CHIP_DEVICE_CONFIG_NVM3_MAX_NUM_OBJECTS

/**
* @def CHIP_DEVICE_CONFIG_NVM3_MAX_OBJECT_SIZE
*
* @brief
* This determines the max size for any Chip nvm3 object
* (e.g. for Config 'string' or 'binary' types).
*/
#ifndef CHIP_DEVICE_CONFIG_NVM3_MAX_OBJECT_SIZE
#define CHIP_DEVICE_CONFIG_NVM3_MAX_OBJECT_SIZE 1000
#endif // CHIP_DEVICE_CONFIG_NVM3_MAX_OBJECT_SIZE

/**
* @def CHIP_DEVICE_CONFIG_NVM3_NUM_FLASH_PAGES_FOR_STORAGE
*
* @brief
* This determines the Flash size used for nvm3 data storage:-
* (assuming 2k Flash page size) => Total Flash size for nvm3: 8 * 2k = 16k
* The total size should allow sufficient margin for wear-levelling and
* repacking.
*/
#ifndef CHIP_DEVICE_CONFIG_NVM3_NUM_FLASH_PAGES_FOR_STORAGE
#define CHIP_DEVICE_CONFIG_NVM3_NUM_FLASH_PAGES_FOR_STORAGE 8
#endif // CHIP_DEVICE_CONFIG_NVM3_NUM_FLASH_PAGES_FOR_STORAGE

// ========== Platform-specific Configuration Overrides =========

#ifndef CHIP_DEVICE_CONFIG_BLE_LL_TASK_PRIORITY
#define CHIP_DEVICE_CONFIG_BLE_LL_TASK_PRIORITY (configTIMER_TASK_PRIORITY - 1)
#endif // CHIP_DEVICE_CONFIG_BLE_LL_TASK_PRIORITY

#ifndef CHIP_DEVICE_CONFIG_BLE_STACK_TASK_PRIORITY
#define CHIP_DEVICE_CONFIG_BLE_STACK_TASK_PRIORITY (CHIP_DEVICE_CONFIG_BLE_LL_TASK_PRIORITY - 1)
#endif // CHIP_DEVICE_CONFIG_BLE_STACK_TASK_PRIORITY

#ifndef CHIP_DEVICE_CONFIG_BLE_APP_TASK_PRIORITY
#define CHIP_DEVICE_CONFIG_BLE_APP_TASK_PRIORITY (CHIP_DEVICE_CONFIG_BLE_STACK_TASK_PRIORITY - 1)
#endif // CHIP_DEVICE_CONFIG_BLE_STACK_TASK_PRIORITY

#ifndef CHIP_DEVICE_CONFIG_BLE_APP_TASK_STACK_SIZE
#define CHIP_DEVICE_CONFIG_BLE_APP_TASK_STACK_SIZE 1024
#endif // CHIP_DEVICE_CONFIG_BLE_APP_TASK_STACK_SIZE

#ifndef CHIP_DEVICE_CONFIG_CHIP_TASK_STACK_SIZE
#define CHIP_DEVICE_CONFIG_CHIP_TASK_STACK_SIZE 8192
#endif // CHIP_DEVICE_CONFIG_CHIP_TASK_STACK_SIZE

#ifndef CHIP_DEVICE_CONFIG_THREAD_TASK_STACK_SIZE
#define CHIP_DEVICE_CONFIG_THREAD_TASK_STACK_SIZE 8192
#endif // CHIP_DEVICE_CONFIG_THREAD_TASK_STACK_SIZE

#define CHIP_DEVICE_CONFIG_ENABLE_WIFI_TELEMETRY 0
#define CHIP_DEVICE_CONFIG_ENABLE_THREAD_TELEMETRY 1
#define CHIP_DEVICE_CONFIG_ENABLE_THREAD_TELEMETRY_FULL 0
#define CHIP_DEVICE_CONFIG_ENABLE_TUNNEL_TELEMETRY 0

#ifndef CHIP_DEVICE_CONFIG_BLE_APP_TASK_NAME
#define CHIP_DEVICE_CONFIG_BLE_APP_TASK_NAME "Bluetooth App Task"
#endif // CHIP_DEVICE_CONFIG_BLE_APP_TASK_NAME

#endif // CHIP_DEVICE_PLATFORM_CONFIG_H
Loading

0 comments on commit cf88e2c

Please sign in to comment.