Yufeng Wang | 01d375f | 2020-11-02 10:23:16 -0800 | [diff] [blame] | 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 | |
Zang MingJie | 53dd583 | 2021-09-03 03:05:16 +0800 | [diff] [blame] | 27 | #include <lib/core/CHIPVendorIdentifiers.hpp> |
Jerry Johns | 9441289 | 2022-09-13 16:33:36 -0400 | [diff] [blame] | 28 | #include <lib/support/TypeTraits.h> |
Yufeng Wang | 01d375f | 2020-11-02 10:23:16 -0800 | [diff] [blame] | 29 | |
| 30 | namespace chip { |
| 31 | namespace Protocols { |
| 32 | |
| 33 | // |
| 34 | // CHIP Protocol Ids (32-bits max) |
| 35 | // |
Boris Zbarsky | f0b7f2b | 2021-03-22 12:32:13 -0400 | [diff] [blame] | 36 | class Id |
Yufeng Wang | 01d375f | 2020-11-02 10:23:16 -0800 | [diff] [blame] | 37 | { |
Boris Zbarsky | f0b7f2b | 2021-03-22 12:32:13 -0400 | [diff] [blame] | 38 | public: |
| 39 | constexpr Id(VendorId aVendorId, uint16_t aProtocolId) : mVendorId(aVendorId), mProtocolId(aProtocolId) {} |
Yufeng Wang | 01d375f | 2020-11-02 10:23:16 -0800 | [diff] [blame] | 40 | |
Boris Zbarsky | 3108006 | 2021-03-25 14:24:55 -0400 | [diff] [blame] | 41 | constexpr bool operator==(const Id & aOther) const |
| 42 | { |
| 43 | return mVendorId == aOther.mVendorId && mProtocolId == aOther.mProtocolId; |
| 44 | } |
Yufeng Wang | 01d375f | 2020-11-02 10:23:16 -0800 | [diff] [blame] | 45 | |
Boris Zbarsky | f5d69c2 | 2021-07-14 16:07:55 -0400 | [diff] [blame] | 46 | constexpr bool operator!=(const Id & aOther) const { return !(*this == aOther); } |
| 47 | |
Boris Zbarsky | f0b7f2b | 2021-03-22 12:32:13 -0400 | [diff] [blame] | 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(); } |
Yufeng Wang | 01d375f | 2020-11-02 10:23:16 -0800 | [diff] [blame] | 52 | |
Boris Zbarsky | f0b7f2b | 2021-03-22 12:32:13 -0400 | [diff] [blame] | 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 | constexpr uint32_t ToFullyQualifiedSpecForm() const { return ToUint32(); } |
| 58 | |
| 59 | constexpr VendorId GetVendorId() const { return mVendorId; } |
| 60 | constexpr uint16_t GetProtocolId() const { return mProtocolId; } |
| 61 | |
Boris Zbarsky | aae56d2 | 2022-06-15 20:35:05 -0400 | [diff] [blame] | 62 | static constexpr uint32_t kVendorIdShift = 16; |
| 63 | |
| 64 | static Id FromFullyQualifiedSpecForm(uint32_t aSpecForm) |
| 65 | { |
| 66 | return Id(static_cast<VendorId>(aSpecForm >> kVendorIdShift), |
| 67 | static_cast<uint16_t>(aSpecForm & ((1 << kVendorIdShift) - 1))); |
| 68 | } |
| 69 | |
Boris Zbarsky | f0b7f2b | 2021-03-22 12:32:13 -0400 | [diff] [blame] | 70 | private: |
Boris Zbarsky | aae56d2 | 2022-06-15 20:35:05 -0400 | [diff] [blame] | 71 | constexpr uint32_t ToUint32() const { return (static_cast<uint32_t>(mVendorId) << kVendorIdShift) | mProtocolId; } |
Boris Zbarsky | f0b7f2b | 2021-03-22 12:32:13 -0400 | [diff] [blame] | 72 | |
| 73 | chip::VendorId mVendorId; |
| 74 | uint16_t mProtocolId; |
Yufeng Wang | 01d375f | 2020-11-02 10:23:16 -0800 | [diff] [blame] | 75 | }; |
| 76 | |
Boris Zbarsky | f0b7f2b | 2021-03-22 12:32:13 -0400 | [diff] [blame] | 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 | |
Yufeng Wang | 6e46b7a | 2021-05-10 11:44:10 -0700 | [diff] [blame] | 85 | CHIP_STANDARD_PROTOCOL(SecureChannel, 0x0000) // Secure Channel Protocol |
Boris Zbarsky | e22c2bb | 2021-09-21 20:22:22 -0400 | [diff] [blame] | 86 | CHIP_STANDARD_PROTOCOL(InteractionModel, 0x0001) // Interaction Model Protocol |
Yufeng Wang | 6e46b7a | 2021-05-10 11:44:10 -0700 | [diff] [blame] | 87 | CHIP_STANDARD_PROTOCOL(BDX, 0x0002) // Bulk Data Exchange Protocol |
| 88 | CHIP_STANDARD_PROTOCOL(UserDirectedCommissioning, 0x0003) // User Directed Commissioning Protocol |
Boris Zbarsky | e22c2bb | 2021-09-21 20:22:22 -0400 | [diff] [blame] | 89 | CHIP_STANDARD_PROTOCOL(Echo, 0x0004) // Echo Protocol. To be removed or standardized. |
Boris Zbarsky | f0b7f2b | 2021-03-22 12:32:13 -0400 | [diff] [blame] | 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 | |
Jerry Johns | 9441289 | 2022-09-13 16:33:36 -0400 | [diff] [blame] | 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 | // |
Boris Zbarsky | e55d117 | 2021-01-26 15:02:38 -0500 | [diff] [blame] | 106 | template <typename T> |
| 107 | struct MessageTypeTraits; |
| 108 | |
Jerry Johns | 9441289 | 2022-09-13 16:33:36 -0400 | [diff] [blame] | 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 | |
Yufeng Wang | 01d375f | 2020-11-02 10:23:16 -0800 | [diff] [blame] | 139 | } // namespace Protocols |
| 140 | } // namespace chip |