|
| 1 | +#include "AzureDpsClient.h" |
| 2 | +#include <az_result.h> |
| 3 | +#include <az_span.h> |
| 4 | + |
| 5 | +static constexpr size_t SignatureMaxSize = 256; |
| 6 | +static constexpr size_t MqttClientIdMaxSize = 128; |
| 7 | +static constexpr size_t MqttUsernameMaxSize = 128; |
| 8 | +static constexpr size_t MqttPasswordMaxSize = 300; |
| 9 | +static constexpr size_t RegisterPublishTopicMaxSize = 128; |
| 10 | +static constexpr size_t QueryStatusPublishTopicMaxSize = 256; |
| 11 | + |
| 12 | +AzureDpsClient::AzureDpsClient() : |
| 13 | + ResponseValid{ false } |
| 14 | +{ |
| 15 | +} |
| 16 | + |
| 17 | +int AzureDpsClient::Init(const std::string& endpoint, const std::string& idScope, const std::string& registrationId) |
| 18 | +{ |
| 19 | + ResponseValid = false; |
| 20 | + |
| 21 | + Endpoint = endpoint; |
| 22 | + IdScope = idScope; |
| 23 | + RegistrationId = registrationId; |
| 24 | + |
| 25 | + const az_span endpointSpan{ az_span_create((uint8_t*)&Endpoint[0], Endpoint.size()) }; |
| 26 | + const az_span idScopeSpan{ az_span_create((uint8_t*)&IdScope[0], IdScope.size()) }; |
| 27 | + const az_span registrationIdSpan{ az_span_create((uint8_t*)&RegistrationId[0], RegistrationId.size()) }; |
| 28 | + if (az_result_failed(az_iot_provisioning_client_init(&ProvClient, endpointSpan, idScopeSpan, registrationIdSpan, NULL))) return -1; |
| 29 | + |
| 30 | + return 0; |
| 31 | +} |
| 32 | + |
| 33 | +std::vector<uint8_t> AzureDpsClient::GetSignature(const uint64_t& expirationEpochTime) |
| 34 | +{ |
| 35 | + uint8_t signature[SignatureMaxSize]; |
| 36 | + az_span signatureSpan = az_span_create(signature, sizeof(signature)); |
| 37 | + az_span signatureValidSpan; |
| 38 | + if (az_result_failed(az_iot_provisioning_client_sas_get_signature(&ProvClient, expirationEpochTime, signatureSpan, &signatureValidSpan))) return std::vector<uint8_t>(); |
| 39 | + |
| 40 | + return std::vector<uint8_t>(az_span_ptr(signatureValidSpan), az_span_ptr(signatureValidSpan) + az_span_size(signatureValidSpan)); |
| 41 | +} |
| 42 | + |
| 43 | +std::string AzureDpsClient::GetMqttClientId() |
| 44 | +{ |
| 45 | + char mqttClientId[MqttClientIdMaxSize]; |
| 46 | + if (az_result_failed(az_iot_provisioning_client_get_client_id(&ProvClient, mqttClientId, sizeof(mqttClientId), NULL))) return std::string(); |
| 47 | + |
| 48 | + return mqttClientId; |
| 49 | +} |
| 50 | + |
| 51 | +std::string AzureDpsClient::GetMqttUsername() |
| 52 | +{ |
| 53 | + char mqttUsername[MqttUsernameMaxSize]; |
| 54 | + if (az_result_failed(az_iot_provisioning_client_get_user_name(&ProvClient, mqttUsername, sizeof(mqttUsername), NULL))) return std::string(); |
| 55 | + |
| 56 | + return mqttUsername; |
| 57 | +} |
| 58 | + |
| 59 | +std::string AzureDpsClient::GetMqttPassword(const std::string& encryptedSignature, const uint64_t& expirationEpochTime) |
| 60 | +{ |
| 61 | + char mqttPassword[MqttPasswordMaxSize]; |
| 62 | + az_span encryptedSignatureSpan = az_span_create((uint8_t*)&encryptedSignature[0], encryptedSignature.size()); |
| 63 | + if (az_result_failed(az_iot_provisioning_client_sas_get_password(&ProvClient, encryptedSignatureSpan, expirationEpochTime, AZ_SPAN_EMPTY, mqttPassword, sizeof(mqttPassword), NULL))) return std::string(); |
| 64 | + |
| 65 | + return mqttPassword; |
| 66 | +} |
| 67 | + |
| 68 | +std::string AzureDpsClient::GetRegisterPublishTopic() |
| 69 | +{ |
| 70 | + char registerPublishTopic[RegisterPublishTopicMaxSize]; |
| 71 | + if (az_result_failed(az_iot_provisioning_client_register_get_publish_topic(&ProvClient, registerPublishTopic, sizeof(registerPublishTopic), NULL))) return std::string(); |
| 72 | + |
| 73 | + return registerPublishTopic; |
| 74 | +} |
| 75 | + |
| 76 | +std::string AzureDpsClient::GetRegisterSubscribeTopic() const |
| 77 | +{ |
| 78 | + return AZ_IOT_PROVISIONING_CLIENT_REGISTER_SUBSCRIBE_TOPIC; |
| 79 | +} |
| 80 | + |
| 81 | +int AzureDpsClient::RegisterSubscribeWork(const std::string& topic, const std::vector<uint8_t>& payload) |
| 82 | +{ |
| 83 | + ResponseValid = false; |
| 84 | + |
| 85 | + ResponseTopic = topic; |
| 86 | + ResponsePayload = payload; |
| 87 | + |
| 88 | + if (az_result_failed(az_iot_provisioning_client_parse_received_topic_and_payload(&ProvClient, az_span_create((uint8_t*)&ResponseTopic[0], ResponseTopic.size()), az_span_create((uint8_t*)&ResponsePayload[0], ResponsePayload.size()), &Response))) return -1; |
| 89 | + |
| 90 | + ResponseValid = true; |
| 91 | + |
| 92 | + return 0; |
| 93 | +} |
| 94 | + |
| 95 | +bool AzureDpsClient::IsRegisterOperationCompleted() |
| 96 | +{ |
| 97 | + if (!ResponseValid) return false; |
| 98 | + |
| 99 | + return az_iot_provisioning_client_operation_complete(GetOperationStatus(Response)); |
| 100 | +} |
| 101 | + |
| 102 | +int AzureDpsClient::GetWaitBeforeQueryStatusSeconds() const |
| 103 | +{ |
| 104 | + if (!ResponseValid) return 0; |
| 105 | + |
| 106 | + return Response.retry_after_seconds; |
| 107 | +} |
| 108 | + |
| 109 | +std::string AzureDpsClient::GetQueryStatusPublishTopic() |
| 110 | +{ |
| 111 | + if (!ResponseValid) return std::string(); |
| 112 | + |
| 113 | + char queryStatusPublishTopic[QueryStatusPublishTopicMaxSize]; |
| 114 | + if (az_result_failed(az_iot_provisioning_client_query_status_get_publish_topic(&ProvClient, Response.operation_id, queryStatusPublishTopic, sizeof(queryStatusPublishTopic), NULL))) return std::string(); |
| 115 | + |
| 116 | + return queryStatusPublishTopic; |
| 117 | +} |
| 118 | + |
| 119 | +bool AzureDpsClient::IsAssigned() |
| 120 | +{ |
| 121 | + if (!ResponseValid) return false; |
| 122 | + |
| 123 | + return GetOperationStatus(Response) == AZ_IOT_PROVISIONING_STATUS_ASSIGNED; |
| 124 | +} |
| 125 | + |
| 126 | +std::string AzureDpsClient::GetHubHost() |
| 127 | +{ |
| 128 | + if (!IsAssigned()) return std::string(); |
| 129 | + |
| 130 | + const az_span& span{ Response.registration_state.assigned_hub_hostname }; |
| 131 | + return std::string(az_span_ptr(span), az_span_ptr(span) + az_span_size(span)); |
| 132 | +} |
| 133 | + |
| 134 | +std::string AzureDpsClient::GetDeviceId() |
| 135 | +{ |
| 136 | + if (!IsAssigned()) return std::string(); |
| 137 | + |
| 138 | + const az_span& span{ Response.registration_state.device_id }; |
| 139 | + return std::string(az_span_ptr(span), az_span_ptr(span) + az_span_size(span)); |
| 140 | +} |
| 141 | + |
| 142 | +az_iot_provisioning_client_operation_status AzureDpsClient::GetOperationStatus(az_iot_provisioning_client_register_response& response) |
| 143 | +{ |
| 144 | + return response.operation_status; |
| 145 | +} |
0 commit comments