Line data Source code
1 : /* 2 : * 3 : * Copyright (c) 2020 Project CHIP Authors 4 : * 5 : * Licensed under the Apache License, Version 2.0 (the "License"); 6 : * you may not use this file except in compliance with the License. 7 : * You may obtain a copy of the License at 8 : * 9 : * http://www.apache.org/licenses/LICENSE-2.0 10 : * 11 : * Unless required by applicable law or agreed to in writing, software 12 : * distributed under the License is distributed on an "AS IS" BASIS, 13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 : * See the License for the specific language governing permissions and 15 : * limitations under the License. 16 : */ 17 : 18 : /** 19 : * @file 20 : * This file defines constant enumerations for all public (or 21 : * common) protocols. 22 : * 23 : */ 24 : 25 : #pragma once 26 : 27 : #include <lib/core/CHIPVendorIdentifiers.hpp> 28 : #include <lib/support/TypeTraits.h> 29 : 30 : namespace chip { 31 : namespace Protocols { 32 : 33 : // 34 : // CHIP Protocol Ids (32-bits max) 35 : // 36 : class Id 37 : { 38 : public: 39 9514 : constexpr Id(VendorId aVendorId, uint16_t aProtocolId) : mVendorId(aVendorId), mProtocolId(aProtocolId) {} 40 : 41 63419 : constexpr bool operator==(const Id & aOther) const 42 : { 43 63419 : return mVendorId == aOther.mVendorId && mProtocolId == aOther.mProtocolId; 44 : } 45 : 46 : constexpr bool operator!=(const Id & aOther) const { return !(*this == aOther); } 47 : 48 : // Convert the Protocols::Id to a TLV profile id. 49 : // NOTE: We may want to change the TLV reader/writer to take Protocols::Id 50 : // directly later on and get rid of this method. 51 : constexpr uint32_t ToTLVProfileId() const { return ToUint32(); } 52 : 53 : // Convert the protocol id to a 32-bit unsigned integer "fully qualified" 54 : // form as defined in the spec. This should only be used in the places 55 : // where the spec defines a 32-bit unsigned in as a wire representation of 56 : // protocol id. 57 70 : constexpr uint32_t ToFullyQualifiedSpecForm() const { return ToUint32(); } 58 : 59 61763 : constexpr VendorId GetVendorId() const { return mVendorId; } 60 66758 : constexpr uint16_t GetProtocolId() const { return mProtocolId; } 61 : 62 : static constexpr uint32_t kVendorIdShift = 16; 63 : 64 21 : static Id FromFullyQualifiedSpecForm(uint32_t aSpecForm) 65 : { 66 21 : return Id(static_cast<VendorId>(aSpecForm >> kVendorIdShift), 67 21 : static_cast<uint16_t>(aSpecForm & ((1 << kVendorIdShift) - 1))); 68 : } 69 : 70 : private: 71 70 : constexpr uint32_t ToUint32() const { return (static_cast<uint32_t>(mVendorId) << kVendorIdShift) | mProtocolId; } 72 : 73 : chip::VendorId mVendorId; 74 : uint16_t mProtocolId; 75 : }; 76 : 77 : // Common Protocols 78 : // 79 : // NOTE: Do not attempt to allocate these values yourself. 80 : #define CHIP_STANDARD_PROTOCOL(name, id) \ 81 : namespace name { \ 82 : static constexpr Protocols::Id Id(VendorId::Common, id); \ 83 : } // namespace name. 84 : 85 : CHIP_STANDARD_PROTOCOL(SecureChannel, 0x0000) // Secure Channel Protocol 86 : CHIP_STANDARD_PROTOCOL(InteractionModel, 0x0001) // Interaction Model Protocol 87 : CHIP_STANDARD_PROTOCOL(BDX, 0x0002) // Bulk Data Exchange Protocol 88 : CHIP_STANDARD_PROTOCOL(UserDirectedCommissioning, 0x0003) // User Directed Commissioning Protocol 89 : CHIP_STANDARD_PROTOCOL(Echo, 0x0004) // Echo Protocol. To be removed or standardized. 90 : 91 : #undef CHIP_STANDARD_PROTOCOL 92 : 93 : // Protocols reserved for internal protocol use 94 : static constexpr Id NotSpecified(VendorId::NotSpecified, 0xFFFF); // The profile ID is either not specified or a wildcard 95 : 96 : // 97 : // Pre-declaration of type traits that protocol headers are expected to define. 98 : // 99 : // A protocol header should first define an enumeration that lists the various message types as enum classes. 100 : // 101 : // It should then define MessageTypeTraits as a template specialization of that enumeration containing two methods: 102 : // 1. static constexpr const Protocols::Id & ProtocolId() that returns the Protocol ID 103 : // 2. static auto GetTypeToNameTable() that returns a pointer to std::array<MessageTypeNameLookup, N> where N = number of 104 : // messages in protocol. 105 : // 106 : template <typename T> 107 : struct MessageTypeTraits; 108 : 109 : // 110 : // Encapsulates a tuple of message type ID and its associated name. 111 : // 112 : struct MessageTypeNameLookup 113 : { 114 : // 115 : // Constructor that takes an enumeration value for a specific message ID and its associated name. 116 : // 117 : template <typename T> 118 : constexpr MessageTypeNameLookup(T id, const char * name) : mId(to_underlying(id)), mName(name) 119 : {} 120 : 121 : const uint8_t mId; 122 : const char * mName; 123 : }; 124 : 125 : // 126 : // Given a protocol ID and a message type ID, retrieve the logical name of that message. 127 : // 128 : // This will not return a nullptr. 129 : // 130 : const char * GetMessageTypeName(Id protocolId, uint8_t msgType); 131 : 132 : // 133 : // Given a protool ID, retrieve the logical name for that protocol. 134 : // 135 : // This will not return a nullptr. 136 : // 137 : const char * GetProtocolName(Id protocolId); 138 : 139 : } // namespace Protocols 140 : } // namespace chip