ThingsBoard Client SDK 0.16.0
Client SDK to connect with ThingsBoard IoT Platform from IoT devices (Arduino, Espressif, etc.)
Loading...
Searching...
No Matches
Provision.h
Go to the documentation of this file.
1#ifndef Provision_h
2#define Provision_h
3
4// Local includes.
7
8
9// Provision topics.
10char constexpr PROV_RESPONSE_TOPIC[] = "/provision/response";
11char constexpr PROV_REQUEST_TOPIC[] = "/provision/request";
12// Provision data keys.
13char constexpr DEVICE_NAME_KEY[] = "deviceName";
14char constexpr PROV_DEVICE_KEY[] = "provisionDeviceKey";
15char constexpr PROV_DEVICE_SECRET_KEY[] = "provisionDeviceSecret";
16char constexpr PROV_CRED_TYPE_KEY[] = "credentialsType";
17char constexpr PROV_TOKEN[] = "token";
18char constexpr PROV_CRED_USERNAME[] = "username";
19char constexpr PROV_CRED_PASSWORD[] = "password";
20char constexpr PROV_CRED_CLIENT_ID[] = "clientId";
21char constexpr PROV_CRED_HASH[] = "hash";
22
23
27template <typename Logger = DefaultLogger>
29 public:
31 Provision() = default;
32
33 ~Provision() override = default;
34
47 bool Provision_Request(Provision_Callback const & callback) {
48 char const * provision_device_key = callback.Get_Device_Key();
49 char const * provision_device_secret = callback.Get_Device_Secret();
50
51 if (Helper::String_IsNull_Or_Empty(provision_device_key) || Helper::String_IsNull_Or_Empty(provision_device_secret)) {
52 return false;
53 }
54 else if (!Provision_Subscribe(callback)) {
55 return false;
56 }
57
58 StaticJsonDocument<JSON_OBJECT_SIZE(9)> request_buffer;
59 char const * device_name = callback.Get_Device_Name();
60 char const * access_token = callback.Get_Device_Access_Token();
61 char const * cred_username = callback.Get_Credentials_Username();
62 char const * cred_password = callback.Get_Credentials_Password();
63 char const * cred_client_id = callback.Get_Credentials_Client_ID();
64 char const * hash = callback.Get_Certificate_Hash();
65 char const * credentials_type = callback.Get_Credentials_Type();
66
67 // Deciding which underlying provisioning method is restricted, by the Provision_Callback class.
68 // Meaning only the key-value pairs that are needed for the given provisioning method are set,
69 // resulting in the rest not being sent and therefore the provisioning request having the correct formatting
70 if (!Helper::String_IsNull_Or_Empty(device_name)) {
71 request_buffer[DEVICE_NAME_KEY] = device_name;
72 }
73 if (!Helper::String_IsNull_Or_Empty(access_token)) {
74 request_buffer[PROV_TOKEN] = access_token;
75 }
76 if (!Helper::String_IsNull_Or_Empty(cred_username)) {
77 request_buffer[PROV_CRED_USERNAME] = cred_username;
78 }
79 if (!Helper::String_IsNull_Or_Empty(cred_password)) {
80 request_buffer[PROV_CRED_PASSWORD] = cred_password;
81 }
82 if (!Helper::String_IsNull_Or_Empty(cred_client_id)) {
83 request_buffer[PROV_CRED_CLIENT_ID] = cred_client_id;
84 }
86 request_buffer[PROV_CRED_HASH] = hash;
87 }
88 if (!Helper::String_IsNull_Or_Empty(credentials_type)) {
89 request_buffer[PROV_CRED_TYPE_KEY] = credentials_type;
90 }
91 request_buffer[PROV_DEVICE_KEY] = provision_device_key;
92 request_buffer[PROV_DEVICE_SECRET_KEY] = provision_device_secret;
93 auto & request_callback = m_provision_callback.Get_Request_Timeout();
94 request_callback.Start_Timeout_Timer();
95 return m_send_json_callback.Call_Callback(PROV_REQUEST_TOPIC, request_buffer);
96 }
97
99 return API_Process_Type::JSON;
100 }
101
102 void Process_Response(char const * topic, uint8_t * payload, uint32_t length) override {
103 // Nothing to do
104 }
105
106 void Process_Json_Response(char const * topic, JsonDocument const & data) override {
107 auto & request_callback = m_provision_callback.Get_Request_Timeout();
108 request_callback.Stop_Timeout_Timer();
109 m_provision_callback.Call_Callback(data);
110 // Unsubscribe from the provision response topic.
111 // Will be resubscribed if another request is sent anyway
112 (void)Provision_Unsubscribe();
113 }
114
115 bool Is_Response_Topic_Matching(char const * topic) const override {
116 return strncmp(PROV_RESPONSE_TOPIC, topic, strlen(PROV_RESPONSE_TOPIC) + 1) == 0;
117 }
118
119 bool Unsubscribe() override {
120 return Provision_Unsubscribe();
121 }
122
124 m_provision_callback = Provision_Callback();
125 return true;
126 }
127
128#if !THINGSBOARD_USE_ESP_TIMER
129 void loop() override {
130 auto & request_callback = m_provision_callback.Get_Request_Timeout();
131 request_callback.Update_Timeout_Timer();
132 }
133#endif // !THINGSBOARD_USE_ESP_TIMER
134
135 void Initialize() override {
136 // Nothing to do
137 }
138
140 m_send_json_callback.Set_Callback(send_json_callback);
141 }
142
143private:
147 bool Provision_Subscribe(Provision_Callback const & callback) {
148 m_provision_callback = callback;
149 return true;
150 }
151
154 bool Provision_Unsubscribe() {
156 }
157
158 Callback<bool, char const * const, JsonDocument const &> m_send_json_callback = {}; // Send json document callback
159
160 Provision_Callback m_provision_callback = {}; // Provision response callback
161};
162
163#endif // Provision_h
API_Process_Type
Possible processing types an API Implementation uses to handle responses from the server.
Definition: API_Process_Type.h:19
char constexpr PROV_DEVICE_KEY[]
Definition: Provision.h:14
char constexpr PROV_TOKEN[]
Definition: Provision.h:17
char constexpr PROV_CRED_TYPE_KEY[]
Definition: Provision.h:16
char constexpr PROV_DEVICE_SECRET_KEY[]
Definition: Provision.h:15
char constexpr PROV_CRED_HASH[]
Definition: Provision.h:21
char constexpr PROV_CRED_CLIENT_ID[]
Definition: Provision.h:20
char constexpr PROV_CRED_PASSWORD[]
Definition: Provision.h:19
char constexpr PROV_RESPONSE_TOPIC[]
Definition: Provision.h:10
char constexpr PROV_REQUEST_TOPIC[]
Definition: Provision.h:11
char constexpr PROV_CRED_USERNAME[]
Definition: Provision.h:18
char constexpr DEVICE_NAME_KEY[]
Definition: Provision.h:13
General purpose safe callback wrapper. Expects either c-style or c++ style function pointer,...
Definition: Callback.h:30
std::function< return_type(argument_types... arguments)> function
Callback signature.
Definition: Callback.h:34
void Set_Callback(function callback)
Sets the callback method that will be called upon data arrival with the given data that was received....
Definition: Callback.h:72
return_type Call_Callback(argument_types const &... arguments) const
Calls the callback that was subscribed, when this class instance was initally created.
Definition: Callback.h:62
static bool String_IsNull_Or_Empty(char const *str)
Returns wheter the given string is either a nullptr or is an empty string, meaning it only contains a...
Definition: Helper.cpp:27
Base functionality required by all API implementation.
Definition: IAPI_Implementation.h:37
Provisioning callback wrapper.
Definition: Provision_Callback.h:28
char const * Get_Credentials_Type() const
Gets the string containing the used credentials type that decides which provisioning method is actual...
Definition: Provision_Callback.cpp:136
char const * Get_Credentials_Client_ID() const
Gets the basic MQTT credentials client ID, that will be used by the provisioned device.
Definition: Provision_Callback.cpp:120
Timeoutable_Request & Get_Request_Timeout()
Gets the request timeout callback.
Definition: Provision_Callback.cpp:140
char const * Get_Credentials_Username() const
Gets the basic MQTT credentials username, that will be used by the provisioned device.
Definition: Provision_Callback.cpp:104
char const * Get_Certificate_Hash() const
Gets the public X.509 certificate hash, that will be used by the provisioned device.
Definition: Provision_Callback.cpp:128
char const * Get_Device_Name() const
Gets the name the created device should have on the cloud.
Definition: Provision_Callback.cpp:88
char const * Get_Credentials_Password() const
Gets the basic MQTT credentials password, that will be used by the provisioned device.
Definition: Provision_Callback.cpp:112
char const * Get_Device_Access_Token() const
Gets the access token generated by the device, that will be used by the provisioned device.
Definition: Provision_Callback.cpp:96
char const * Get_Device_Secret() const
Gets the provisioning secret of the Device Profile that should be used to create the device under.
Definition: Provision_Callback.cpp:80
char const * Get_Device_Key() const
Gets the provisioning key of the Device Profile, that should be used to create the device under.
Definition: Provision_Callback.cpp:72
Handles the internal implementation of the ThingsBoard provision API. See https://thingsboard....
Definition: Provision.h:28
void Initialize() override
Method that allows to construct internal objects, after the required callback member methods have bee...
Definition: Provision.h:135
void Set_Client_Callbacks(Callback< void, IAPI_Implementation & >::function subscribe_api_callback, Callback< bool, char const *const, JsonDocument const & >::function send_json_callback, Callback< bool, char const *const, char const *const >::function send_json_string_callback, Callback< bool, char const *const >::function subscribe_topic_callback, Callback< bool, char const *const >::function unsubscribe_topic_callback, Callback< uint16_t >::function get_receive_size_callback, Callback< uint16_t >::function get_send_size_callback, Callback< bool, uint16_t, uint16_t >::function set_buffer_size_callback, Callback< size_t * >::function get_request_id_callback) override
Sets the underlying callbacks that are required for the different API Implementation to communicate w...
Definition: Provision.h:139
void Process_Response(char const *topic, uint8_t *payload, uint32_t length) override
Process callback that will be called upon response arrival.
Definition: Provision.h:102
bool Is_Response_Topic_Matching(char const *topic) const override
Compares received response topic and the topic this api implementation handles responses on,...
Definition: Provision.h:115
bool Unsubscribe() override
Unsubcribes all callbacks, to clear up any ongoing subscriptions and stop receiving information over ...
Definition: Provision.h:119
bool Provision_Request(Provision_Callback const &callback)
Requests othe provisioning of a new device, which will call the passed callback. If the credentials f...
Definition: Provision.h:47
void loop() override
Internal loop method to update inernal timers for API calls that can timeout.
Definition: Provision.h:129
~Provision() override=default
Provision()=default
Constructor.
API_Process_Type Get_Process_Type() const override
Returns the way the server response should be processed.
Definition: Provision.h:98
void Process_Json_Response(char const *topic, JsonDocument const &data) override
Process callback that will be called upon response arrival.
Definition: Provision.h:106
bool Resubscribe_Permanent_Subscriptions() override
Forwards the call to let the API clear up any ongoing single-event subscriptions (Provision,...
Definition: Provision.h:123
void Update_Timeout_Timer()
Updates the internal timeout timer.
Definition: Timeoutable_Request.cpp:20
void Start_Timeout_Timer()
Starts the internal timeout timer if we actually received a configured valid timeout time and a valid...
Definition: Timeoutable_Request.cpp:25
void Stop_Timeout_Timer()
Stops the internal timeout timer, is called as soon as an answer is received from the cloud....
Definition: Timeoutable_Request.cpp:32