|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2020 Google LLC. |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +/** |
| 20 | + * @file |
| 21 | + * Defines generic trait catalogs that are used to house trait data sources/sinks and can be used with the |
| 22 | + * various WDM engines to correctly map to/from resources specified in a WDM path to actual trait |
| 23 | + * data instances. |
| 24 | + * |
| 25 | + */ |
| 26 | + |
| 27 | +#ifndef _WEAVE_DATA_MANAGEMENT_GENERIC_TRAIT_CATALOG_IMPL_CURRENT_H |
| 28 | +#define _WEAVE_DATA_MANAGEMENT_GENERIC_TRAIT_CATALOG_IMPL_CURRENT_H |
| 29 | + |
| 30 | +#include <map> |
| 31 | +#include <queue> |
| 32 | +#include <limits> |
| 33 | +#include <Weave/Profiles/data-management/Current/WdmManagedNamespace.h> |
| 34 | +#include <Weave/Profiles/data-management/TraitCatalog.h> |
| 35 | + |
| 36 | +namespace nl { |
| 37 | +namespace Weave { |
| 38 | +namespace Profiles { |
| 39 | +namespace WeaveMakeManagedNamespaceIdentifier(DataManagement, kWeaveManagedNamespaceDesignation_Current) { |
| 40 | +/** |
| 41 | + * @class GenericTraitCatalogImpl |
| 42 | + * |
| 43 | + * @brief A Weave provided implementation of the TraitCatalogBase interface for a collection of trait data instances |
| 44 | + * that all refer to the same resource. It provides a c++ map-backed storage for these instances. |
| 45 | + */ |
| 46 | +template <typename T> |
| 47 | +class GenericTraitCatalogImpl : public TraitCatalogBase<T> |
| 48 | +{ |
| 49 | +public: |
| 50 | + GenericTraitCatalogImpl(void); |
| 51 | + virtual ~GenericTraitCatalogImpl(void); |
| 52 | + |
| 53 | + void SetNodeId(uint64_t aNodeId); |
| 54 | + |
| 55 | + WEAVE_ERROR Add(const ResourceIdentifier & aResourceId, const uint64_t & aInstanceId, PropertyPathHandle basePathHandle, |
| 56 | + T * traitInstance, TraitDataHandle & aHandle); |
| 57 | + WEAVE_ERROR Remove(T * traitInstance); |
| 58 | + WEAVE_ERROR Remove(TraitDataHandle aHandle); |
| 59 | + WEAVE_ERROR PrepareSubscriptionSpecificPathList(TraitPath * pathList, uint16_t pathListSize, TraitDataHandle aHandle); |
| 60 | + WEAVE_ERROR PrepareSubscriptionPathList(TraitPath * pathList, uint16_t pathListSize, uint16_t & pathListLen); |
| 61 | + WEAVE_ERROR Clear(void); |
| 62 | + |
| 63 | + virtual WEAVE_ERROR AddressToHandle(TLV::TLVReader & aReader, TraitDataHandle & aHandle, |
| 64 | + SchemaVersionRange & aSchemaVersionRange) const; |
| 65 | + virtual WEAVE_ERROR HandleToAddress(TraitDataHandle aHandle, TLV::TLVWriter & aWriter, |
| 66 | + SchemaVersionRange & aSchemaVersionRange) const; |
| 67 | + virtual WEAVE_ERROR Locate(TraitDataHandle aHandle, T ** aTraitInstance) const; |
| 68 | + virtual WEAVE_ERROR Locate(T * aTraitInstance, TraitDataHandle & aHandle) const; |
| 69 | + |
| 70 | + WEAVE_ERROR Locate(uint32_t aProfileId, uint64_t aInstanceId, ResourceIdentifier aResourceId, TraitDataHandle & aHandle) const; |
| 71 | + |
| 72 | + WEAVE_ERROR Locate(uint32_t aProfileId, uint64_t aInstanceId, ResourceIdentifier aResourceId, T ** aTraitInstance) const; |
| 73 | + |
| 74 | + virtual WEAVE_ERROR DispatchEvent(uint16_t aEvent, void * aContext) const; |
| 75 | + virtual void Iterate(IteratorCallback aCallback, void * aContext); |
| 76 | +#if WEAVE_CONFIG_ENABLE_WDM_UPDATE |
| 77 | + virtual WEAVE_ERROR GetInstanceId(TraitDataHandle aHandle, uint64_t & aInstanceId) const; |
| 78 | + virtual WEAVE_ERROR GetResourceId(TraitDataHandle aHandle, ResourceIdentifier & aResourceId) const; |
| 79 | +#endif // WEAVE_CONFIG_ENABLE_WDM_UPDATE |
| 80 | + |
| 81 | + /** |
| 82 | + * Return the number of trait instances in the catalog. |
| 83 | + */ |
| 84 | + uint32_t Size(void) const; |
| 85 | + |
| 86 | +private: |
| 87 | + struct CatalogItem |
| 88 | + { |
| 89 | + uint32_t mProfileId; |
| 90 | + uint64_t mInstanceId; |
| 91 | + ResourceIdentifier mResourceId; |
| 92 | + T * mItem; |
| 93 | + PropertyPathHandle mBasePathHandle; |
| 94 | + }; |
| 95 | + |
| 96 | + TraitDataHandle GetNextHandle(); |
| 97 | + |
| 98 | + uint64_t mNodeId; |
| 99 | + std::map<TraitDataHandle, CatalogItem *> mItemStore; |
| 100 | + std::queue<TraitDataHandle> mRecycledHandles; |
| 101 | +}; |
| 102 | + |
| 103 | +typedef GenericTraitCatalogImpl<TraitDataSink> GenericTraitSinkCatalog; |
| 104 | +typedef GenericTraitCatalogImpl<TraitDataSource> GenericTraitSourceCatalog; |
| 105 | + |
| 106 | +}; // namespace WeaveMakeManagedNamespaceIdentifier(DataManagement, kWeaveManagedNamespaceDesignation_Current) |
| 107 | +}; // namespace Profiles |
| 108 | +}; // namespace Weave |
| 109 | +}; // namespace nl |
| 110 | + |
| 111 | +#endif // _WEAVE_DATA_MANAGEMENT_GENERIC_TRAIT_CATALOG_IMPL_CURRENT_H |
0 commit comments