blob: feb075e05ae4225fb03a352cb3c6f1473ed131a5 [file] [log] [blame]
Yufeng Wang01d375f2020-11-02 10:23:16 -08001/*
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 MingJie53dd5832021-09-03 03:05:16 +080027#include <lib/core/CHIPVendorIdentifiers.hpp>
Jerry Johns94412892022-09-13 16:33:36 -040028#include <lib/support/TypeTraits.h>
Yufeng Wang01d375f2020-11-02 10:23:16 -080029
30namespace chip {
31namespace Protocols {
32
33//
34// CHIP Protocol Ids (32-bits max)
35//
Boris Zbarskyf0b7f2b2021-03-22 12:32:13 -040036class Id
Yufeng Wang01d375f2020-11-02 10:23:16 -080037{
Boris Zbarskyf0b7f2b2021-03-22 12:32:13 -040038public:
39 constexpr Id(VendorId aVendorId, uint16_t aProtocolId) : mVendorId(aVendorId), mProtocolId(aProtocolId) {}
Yufeng Wang01d375f2020-11-02 10:23:16 -080040
Boris Zbarsky31080062021-03-25 14:24:55 -040041 constexpr bool operator==(const Id & aOther) const
42 {
43 return mVendorId == aOther.mVendorId && mProtocolId == aOther.mProtocolId;
44 }
Yufeng Wang01d375f2020-11-02 10:23:16 -080045
Boris Zbarskyf5d69c22021-07-14 16:07:55 -040046 constexpr bool operator!=(const Id & aOther) const { return !(*this == aOther); }
47
Boris Zbarskyf0b7f2b2021-03-22 12:32:13 -040048 // 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 Wang01d375f2020-11-02 10:23:16 -080052
Boris Zbarskyf0b7f2b2021-03-22 12:32:13 -040053 // 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 Zbarskyaae56d22022-06-15 20:35:05 -040062 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 Zbarskyf0b7f2b2021-03-22 12:32:13 -040070private:
Boris Zbarskyaae56d22022-06-15 20:35:05 -040071 constexpr uint32_t ToUint32() const { return (static_cast<uint32_t>(mVendorId) << kVendorIdShift) | mProtocolId; }
Boris Zbarskyf0b7f2b2021-03-22 12:32:13 -040072
73 chip::VendorId mVendorId;
74 uint16_t mProtocolId;
Yufeng Wang01d375f2020-11-02 10:23:16 -080075};
76
Boris Zbarskyf0b7f2b2021-03-22 12:32:13 -040077// 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 Wang6e46b7a2021-05-10 11:44:10 -070085CHIP_STANDARD_PROTOCOL(SecureChannel, 0x0000) // Secure Channel Protocol
Boris Zbarskye22c2bb2021-09-21 20:22:22 -040086CHIP_STANDARD_PROTOCOL(InteractionModel, 0x0001) // Interaction Model Protocol
Yufeng Wang6e46b7a2021-05-10 11:44:10 -070087CHIP_STANDARD_PROTOCOL(BDX, 0x0002) // Bulk Data Exchange Protocol
88CHIP_STANDARD_PROTOCOL(UserDirectedCommissioning, 0x0003) // User Directed Commissioning Protocol
Boris Zbarskye22c2bb2021-09-21 20:22:22 -040089CHIP_STANDARD_PROTOCOL(Echo, 0x0004) // Echo Protocol. To be removed or standardized.
Boris Zbarskyf0b7f2b2021-03-22 12:32:13 -040090
91#undef CHIP_STANDARD_PROTOCOL
92
93// Protocols reserved for internal protocol use
94static constexpr Id NotSpecified(VendorId::NotSpecified, 0xFFFF); // The profile ID is either not specified or a wildcard
95
Jerry Johns94412892022-09-13 16:33:36 -040096//
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 Zbarskye55d1172021-01-26 15:02:38 -0500106template <typename T>
107struct MessageTypeTraits;
108
Jerry Johns94412892022-09-13 16:33:36 -0400109//
110// Encapsulates a tuple of message type ID and its associated name.
111//
112struct 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//
130const 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//
137const char * GetProtocolName(Id protocolId);
138
Yufeng Wang01d375f2020-11-02 10:23:16 -0800139} // namespace Protocols
140} // namespace chip