AZ_NODISCARD az_result az_iot_hub_client_get_user_name()

in sdk/src/azure/iot/az_iot_hub_client.c [55:140]


AZ_NODISCARD az_result az_iot_hub_client_get_user_name(
    az_iot_hub_client const* client,
    char* mqtt_user_name,
    size_t mqtt_user_name_size,
    size_t* out_mqtt_user_name_length)
{
  _az_PRECONDITION_NOT_NULL(client);
  _az_PRECONDITION_NOT_NULL(mqtt_user_name);
  _az_PRECONDITION(mqtt_user_name_size > 0);

  const az_span* const module_id = &(client->_internal.options.module_id);
  const az_span* const user_agent = &(client->_internal.options.user_agent);
  const az_span* const model_id = &(client->_internal.options.model_id);

  az_span mqtt_user_name_span
      = az_span_create((uint8_t*)mqtt_user_name, (int32_t)mqtt_user_name_size);

  int32_t required_length = az_span_size(client->_internal.iot_hub_hostname)
      + az_span_size(client->_internal.device_id) + (int32_t)sizeof(hub_client_forward_slash)
      + az_span_size(hub_service_api_version);
  if (az_span_size(*module_id) > 0)
  {
    required_length += az_span_size(*module_id) + (int32_t)sizeof(hub_client_forward_slash);
  }
  if (az_span_size(*user_agent) > 0)
  {
    required_length += az_span_size(hub_client_param_separator_span)
        + az_span_size(client_sdk_device_client_type_name)
        + az_span_size(hub_client_param_equals_span) + az_span_size(*user_agent);
  }
  // Note we skip the length of the model id since we have to url encode it. Bound checking is done
  // later.
  if (az_span_size(*model_id) > 0)
  {
    required_length += az_span_size(hub_client_param_separator_span)
        + az_span_size(hub_client_param_equals_span);
  }

  _az_RETURN_IF_NOT_ENOUGH_SIZE(
      mqtt_user_name_span, required_length + (int32_t)sizeof(null_terminator));

  az_span remainder = az_span_copy(mqtt_user_name_span, client->_internal.iot_hub_hostname);
  remainder = az_span_copy_u8(remainder, hub_client_forward_slash);
  remainder = az_span_copy(remainder, client->_internal.device_id);

  if (az_span_size(*module_id) > 0)
  {
    remainder = az_span_copy_u8(remainder, hub_client_forward_slash);
    remainder = az_span_copy(remainder, *module_id);
  }

  remainder = az_span_copy(remainder, hub_service_api_version);

  if (az_span_size(*user_agent) > 0)
  {
    remainder = az_span_copy_u8(remainder, *az_span_ptr(hub_client_param_separator_span));
    remainder = az_span_copy(remainder, client_sdk_device_client_type_name);
    remainder = az_span_copy_u8(remainder, *az_span_ptr(hub_client_param_equals_span));
    remainder = az_span_copy(remainder, *user_agent);
  }

  if (az_span_size(*model_id) > 0)
  {
    remainder = az_span_copy_u8(remainder, *az_span_ptr(hub_client_param_separator_span));
    remainder = az_span_copy(remainder, hub_digital_twin_model_id);
    remainder = az_span_copy_u8(remainder, *az_span_ptr(hub_client_param_equals_span));

    _az_RETURN_IF_FAILED(_az_span_copy_url_encode(remainder, *model_id, &remainder));
  }
  if (az_span_size(remainder) > 0)
  {
    remainder = az_span_copy_u8(remainder, null_terminator);
  }
  else
  {
    return AZ_ERROR_NOT_ENOUGH_SPACE;
  }

  if (out_mqtt_user_name_length)
  {
    *out_mqtt_user_name_length
        = mqtt_user_name_size - (size_t)az_span_size(remainder) - sizeof(null_terminator);
  }

  return AZ_OK;
}