@@ -73,7 +73,7 @@ static int get_mqtt_client_config_for_iot_hub(azure_iot_t* azure_iot, mqtt_clien
7373static az_span generate_dps_register_custom_property (az_span model_id, az_span data_buffer, az_span* remainder);
7474
7575#define is_device_provisioned (azure_iot ) \
76- (!is_az_span_empty (azure_iot->config->iot_hub_fqdn) && !is_az_span_empty (azure_iot->config->device_id))
76+ (!az_span_is_content_equal (azure_iot->config->iot_hub_fqdn, AZ_SPAN_EMPTY ) && !az_span_is_content_equal (azure_iot->config->device_id, AZ_SPAN_EMPTY ))
7777
7878/* --- Public API --- */
7979void azure_iot_init (azure_iot_t * azure_iot, azure_iot_config_t * azure_iot_config)
@@ -82,19 +82,27 @@ void azure_iot_init(azure_iot_t* azure_iot, azure_iot_config_t* azure_iot_config
8282 _az_PRECONDITION_NOT_NULL (azure_iot_config);
8383 if (azure_iot_config->use_device_provisioning )
8484 {
85- _az_PRECONDITION (is_az_span_empty (azure_iot_config->iot_hub_fqdn ));
86- _az_PRECONDITION (is_az_span_empty (azure_iot_config->device_id ));
85+ _az_PRECONDITION (az_span_is_content_equal (azure_iot_config->iot_hub_fqdn , AZ_SPAN_EMPTY ));
86+ _az_PRECONDITION (az_span_is_content_equal (azure_iot_config->device_id , AZ_SPAN_EMPTY ));
8787 _az_PRECONDITION_VALID_SPAN (azure_iot_config->dps_id_scope , 1 , false );
8888 _az_PRECONDITION_VALID_SPAN (azure_iot_config->dps_registration_id , 1 , false );
8989 }
9090 else
9191 {
9292 _az_PRECONDITION_VALID_SPAN (azure_iot_config->iot_hub_fqdn , 1 , false );
9393 _az_PRECONDITION_VALID_SPAN (azure_iot_config->device_id , 1 , false );
94- _az_PRECONDITION (is_az_span_empty (azure_iot_config->dps_id_scope ));
95- _az_PRECONDITION (is_az_span_empty (azure_iot_config->dps_registration_id ));
94+ _az_PRECONDITION (az_span_is_content_equal (azure_iot_config->dps_id_scope , AZ_SPAN_EMPTY ));
95+ _az_PRECONDITION (az_span_is_content_equal (azure_iot_config->dps_registration_id , AZ_SPAN_EMPTY ));
9696 }
97- _az_PRECONDITION_VALID_SPAN (azure_iot_config->device_key , 1 , false );
97+
98+ // Either device key or device certificate and certificate key should be defined.
99+ if (az_span_is_content_equal (azure_iot_config->device_key , AZ_SPAN_EMPTY) &&
100+ (az_span_is_content_equal (azure_iot_config->device_certificate , AZ_SPAN_EMPTY) || az_span_is_content_equal (azure_iot_config->device_certificate_private_key , AZ_SPAN_EMPTY) ))
101+ {
102+ LogError (" Please define either a device key or a device certificate and certificate private key. See iot_configs.h" );
103+ return ;
104+ }
105+
98106 _az_PRECONDITION_VALID_SPAN (azure_iot_config->data_buffer , 1 , false );
99107 _az_PRECONDITION_NOT_NULL (azure_iot_config->data_manipulation_functions .base64_decode );
100108 _az_PRECONDITION_NOT_NULL (azure_iot_config->data_manipulation_functions .base64_encode );
@@ -318,7 +326,7 @@ void azure_iot_do_work(azure_iot_t* azure_iot)
318326
319327 mqtt_message.topic = split_az_span (data_buffer, length + 1 , &data_buffer);
320328
321- if (is_az_span_empty (mqtt_message.topic ) || is_az_span_empty (data_buffer))
329+ if (az_span_is_content_equal (mqtt_message.topic , AZ_SPAN_EMPTY ) || az_span_is_content_equal (data_buffer, AZ_SPAN_EMPTY ))
322330 {
323331 azure_iot->state = azure_iot_state_error;
324332 LogError (" Failed reserving memory for DPS register payload." );
@@ -328,7 +336,7 @@ void azure_iot_do_work(azure_iot_t* azure_iot)
328336 dps_register_custom_property = generate_dps_register_custom_property (
329337 azure_iot->config ->model_id , data_buffer, &mqtt_message.payload );
330338
331- if (is_az_span_empty (dps_register_custom_property))
339+ if (az_span_is_content_equal (dps_register_custom_property, AZ_SPAN_EMPTY ))
332340 {
333341 azure_iot->state = azure_iot_state_error;
334342 LogError (" Failed generating DPS register custom property payload." );
@@ -804,11 +812,11 @@ int azure_iot_mqtt_client_message_received(azure_iot_t* azure_iot, mqtt_message_
804812 {
805813 result = RESULT_OK;
806814
807- if (is_az_span_empty (azure_iot->dps_operation_id ))
815+ if (az_span_is_content_equal (azure_iot->dps_operation_id , AZ_SPAN_EMPTY ))
808816 {
809817 azure_iot->dps_operation_id = slice_and_copy_az_span (azure_iot->data_buffer , register_response.operation_id , &azure_iot->data_buffer );
810818
811- if (is_az_span_empty (azure_iot->dps_operation_id ))
819+ if (az_span_is_content_equal (azure_iot->dps_operation_id , AZ_SPAN_EMPTY ))
812820 {
813821 azure_iot->state = azure_iot_state_error;
814822 LogError (" Failed reserving memory for DPS operation id." );
@@ -829,7 +837,7 @@ int azure_iot_mqtt_client_message_received(azure_iot_t* azure_iot, mqtt_message_
829837
830838 azure_iot->config ->iot_hub_fqdn = slice_and_copy_az_span (data_buffer, register_response.registration_state .assigned_hub_hostname , &data_buffer);
831839
832- if (is_az_span_empty (azure_iot->config ->iot_hub_fqdn ))
840+ if (az_span_is_content_equal (azure_iot->config ->iot_hub_fqdn , AZ_SPAN_EMPTY ))
833841 {
834842 azure_iot->state = azure_iot_state_error;
835843 LogError (" Failed saving IoT Hub fqdn from provisioning." );
@@ -839,7 +847,7 @@ int azure_iot_mqtt_client_message_received(azure_iot_t* azure_iot, mqtt_message_
839847 {
840848 azure_iot->config ->device_id = slice_and_copy_az_span (data_buffer, register_response.registration_state .device_id , &data_buffer);
841849
842- if (is_az_span_empty (azure_iot->config ->device_id ))
850+ if (az_span_is_content_equal (azure_iot->config ->device_id , AZ_SPAN_EMPTY ))
843851 {
844852 azure_iot->state = azure_iot_state_error;
845853 LogError (" Failed saving device id from provisioning." );
@@ -947,20 +955,29 @@ static int get_mqtt_client_config_for_dps(azure_iot_t* azure_iot, mqtt_client_co
947955 data_buffer_span = azure_iot->data_buffer ;
948956
949957 password_span = split_az_span (data_buffer_span, MQTT_PASSWORD_BUFFER_SIZE, &data_buffer_span);
950- EXIT_IF_TRUE (is_az_span_empty (password_span), RESULT_ERROR, " Failed reserving buffer for password_span." );
958+ EXIT_IF_TRUE (az_span_is_content_equal (password_span, AZ_SPAN_EMPTY ), RESULT_ERROR, " Failed reserving buffer for password_span." );
951959
952- password_length = generate_sas_token_for_dps (
953- &azure_iot->dps_client ,
954- azure_iot->config ->device_key ,
955- azure_iot->config ->sas_token_lifetime_in_minutes ,
956- data_buffer_span,
957- azure_iot->config ->data_manipulation_functions ,
958- password_span,
959- &azure_iot->sas_token_expiration_time );
960- EXIT_IF_TRUE (password_length == 0 , RESULT_ERROR, " Failed creating mqtt password for DPS connection." );
960+ if (!az_span_is_content_equal (azure_iot->config ->device_key , AZ_SPAN_EMPTY))
961+ {
962+ password_length = generate_sas_token_for_dps (
963+ &azure_iot->dps_client ,
964+ azure_iot->config ->device_key ,
965+ azure_iot->config ->sas_token_lifetime_in_minutes ,
966+ data_buffer_span,
967+ azure_iot->config ->data_manipulation_functions ,
968+ password_span,
969+ &azure_iot->sas_token_expiration_time );
970+ EXIT_IF_TRUE (password_length == 0 , RESULT_ERROR, " Failed creating mqtt password for DPS connection." );
971+
972+ mqtt_client_config->password = password_span;
973+ }
974+ else
975+ {
976+ mqtt_client_config->password = AZ_SPAN_EMPTY;
977+ }
961978
962979 client_id_span = split_az_span (data_buffer_span, MQTT_CLIENT_ID_BUFFER_SIZE, &data_buffer_span);
963- EXIT_IF_TRUE (is_az_span_empty (client_id_span), RESULT_ERROR, " Failed reserving buffer for client_id_span." );
980+ EXIT_IF_TRUE (az_span_is_content_equal (client_id_span, AZ_SPAN_EMPTY ), RESULT_ERROR, " Failed reserving buffer for client_id_span." );
964981
965982 azrc = az_iot_provisioning_client_get_client_id (
966983 &azure_iot->dps_client , (char *)az_span_ptr (client_id_span), az_span_size (client_id_span), &client_id_length);
@@ -977,7 +994,6 @@ static int get_mqtt_client_config_for_dps(azure_iot_t* azure_iot, mqtt_client_co
977994
978995 mqtt_client_config->client_id = client_id_span;
979996 mqtt_client_config->username = username_span;
980- mqtt_client_config->password = password_span;
981997
982998 return RESULT_OK;
983999}
@@ -1015,7 +1031,7 @@ static int get_mqtt_client_config_for_iot_hub(azure_iot_t* azure_iot, mqtt_clien
10151031 data_buffer_span = azure_iot->data_buffer ;
10161032
10171033 password_span = split_az_span (data_buffer_span, MQTT_PASSWORD_BUFFER_SIZE, &data_buffer_span);
1018- EXIT_IF_TRUE (is_az_span_empty (password_span), RESULT_ERROR, " Failed reserving buffer for password_span." );
1034+ EXIT_IF_TRUE (az_span_is_content_equal (password_span, AZ_SPAN_EMPTY ), RESULT_ERROR, " Failed reserving buffer for password_span." );
10191035
10201036 password_length = generate_sas_token_for_iot_hub (
10211037 &azure_iot->iot_hub_client ,
@@ -1028,7 +1044,7 @@ static int get_mqtt_client_config_for_iot_hub(azure_iot_t* azure_iot, mqtt_clien
10281044 EXIT_IF_TRUE (password_length == 0 , RESULT_ERROR, " Failed creating mqtt password for IoT Hub connection." );
10291045
10301046 client_id_span = split_az_span (data_buffer_span, MQTT_CLIENT_ID_BUFFER_SIZE, &data_buffer_span);
1031- EXIT_IF_TRUE (is_az_span_empty (client_id_span), RESULT_ERROR, " Failed reserving buffer for client_id_span." );
1047+ EXIT_IF_TRUE (az_span_is_content_equal (client_id_span, AZ_SPAN_EMPTY ), RESULT_ERROR, " Failed reserving buffer for client_id_span." );
10321048
10331049 azrc = az_iot_hub_client_get_client_id (
10341050 &azure_iot->iot_hub_client , (char *)az_span_ptr (client_id_span), az_span_size (client_id_span), &client_id_length);
@@ -1093,26 +1109,26 @@ static int generate_sas_token_for_dps(
10931109
10941110 // Step 2.a.
10951111 plain_sas_signature = split_az_span (data_buffer_span, PLAIN_SAS_SIGNATURE_BUFFER_SIZE, &data_buffer_span);
1096- EXIT_IF_TRUE (is_az_span_empty (plain_sas_signature), 0 , " Failed reserving buffer for plain sas token." );
1112+ EXIT_IF_TRUE (az_span_is_content_equal (plain_sas_signature, AZ_SPAN_EMPTY ), 0 , " Failed reserving buffer for plain sas token." );
10971113
10981114 rc = az_iot_provisioning_client_sas_get_signature (
10991115 provisioning_client, *expiration_time, plain_sas_signature, &plain_sas_signature);
11001116 EXIT_IF_AZ_FAILED (rc, 0 , " Could not get the signature for SAS key." );
11011117
11021118 // Step 2.b.
11031119 sas_signature = split_az_span (data_buffer_span, SAS_SIGNATURE_BUFFER_SIZE, &data_buffer_span);
1104- EXIT_IF_TRUE (is_az_span_empty (sas_signature), 0 , " Failed reserving buffer for sas_signature." );
1120+ EXIT_IF_TRUE (az_span_is_content_equal (sas_signature, AZ_SPAN_EMPTY ), 0 , " Failed reserving buffer for sas_signature." );
11051121
11061122 decoded_sas_key = split_az_span (data_buffer_span, DECODED_SAS_KEY_BUFFER_SIZE, &data_buffer_span);
1107- EXIT_IF_TRUE (is_az_span_empty (decoded_sas_key), 0 , " Failed reserving buffer for decoded_sas_key." );
1123+ EXIT_IF_TRUE (az_span_is_content_equal (decoded_sas_key, AZ_SPAN_EMPTY ), 0 , " Failed reserving buffer for decoded_sas_key." );
11081124
11091125 result = data_manipulation_functions.base64_decode (
11101126 az_span_ptr (device_key), az_span_size (device_key), az_span_ptr (decoded_sas_key), az_span_size (decoded_sas_key), &decoded_sas_key_length);
11111127 EXIT_IF_TRUE (result != 0 , 0 , " Failed decoding SAS key." );
11121128
11131129 // Step 2.c.
11141130 sas_hmac256_signed_signature = split_az_span (data_buffer_span, SAS_HMAC256_ENCRYPTED_SIGNATURE_BUFFER_SIZE, &data_buffer_span);
1115- EXIT_IF_TRUE (is_az_span_empty (sas_hmac256_signed_signature), 0 , " Failed reserving buffer for sas_hmac256_signed_signature." );
1131+ EXIT_IF_TRUE (az_span_is_content_equal (sas_hmac256_signed_signature, AZ_SPAN_EMPTY ), 0 , " Failed reserving buffer for sas_hmac256_signed_signature." );
11161132
11171133 result = data_manipulation_functions.hmac_sha256_encrypt (
11181134 az_span_ptr (decoded_sas_key), decoded_sas_key_length,
@@ -1184,26 +1200,26 @@ static int generate_sas_token_for_iot_hub(
11841200
11851201 // Step 2.a.
11861202 plain_sas_signature = split_az_span (data_buffer_span, PLAIN_SAS_SIGNATURE_BUFFER_SIZE, &data_buffer_span);
1187- EXIT_IF_TRUE (is_az_span_empty (plain_sas_signature), 0 , " Failed reserving buffer for plain sas token." );
1203+ EXIT_IF_TRUE (az_span_is_content_equal (plain_sas_signature, AZ_SPAN_EMPTY ), 0 , " Failed reserving buffer for plain sas token." );
11881204
11891205 rc = az_iot_hub_client_sas_get_signature (
11901206 iot_hub_client, *expiration_time, plain_sas_signature, &plain_sas_signature);
11911207 EXIT_IF_AZ_FAILED (rc, 0 , " Could not get the signature for SAS key." );
11921208
11931209 // Step 2.b.
11941210 sas_signature = split_az_span (data_buffer_span, SAS_SIGNATURE_BUFFER_SIZE, &data_buffer_span);
1195- EXIT_IF_TRUE (is_az_span_empty (sas_signature), 0 , " Failed reserving buffer for sas_signature." );
1211+ EXIT_IF_TRUE (az_span_is_content_equal (sas_signature, AZ_SPAN_EMPTY ), 0 , " Failed reserving buffer for sas_signature." );
11961212
11971213 decoded_sas_key = split_az_span (data_buffer_span, DECODED_SAS_KEY_BUFFER_SIZE, &data_buffer_span);
1198- EXIT_IF_TRUE (is_az_span_empty (decoded_sas_key), 0 , " Failed reserving buffer for decoded_sas_key." );
1214+ EXIT_IF_TRUE (az_span_is_content_equal (decoded_sas_key, AZ_SPAN_EMPTY ), 0 , " Failed reserving buffer for decoded_sas_key." );
11991215
12001216 result = data_manipulation_functions.base64_decode (
12011217 az_span_ptr (device_key), az_span_size (device_key), az_span_ptr (decoded_sas_key), az_span_size (decoded_sas_key), &decoded_sas_key_length);
12021218 EXIT_IF_TRUE (result != 0 , 0 , " Failed decoding SAS key." );
12031219
12041220 // Step 2.c.
12051221 sas_hmac256_signed_signature = split_az_span (data_buffer_span, SAS_HMAC256_ENCRYPTED_SIGNATURE_BUFFER_SIZE, &data_buffer_span);
1206- EXIT_IF_TRUE (is_az_span_empty (sas_hmac256_signed_signature), 0 , " Failed reserving buffer for sas_hmac256_signed_signature." );
1222+ EXIT_IF_TRUE (az_span_is_content_equal (sas_hmac256_signed_signature, AZ_SPAN_EMPTY ), 0 , " Failed reserving buffer for sas_hmac256_signed_signature." );
12071223
12081224 result = data_manipulation_functions.hmac_sha256_encrypt (
12091225 az_span_ptr (decoded_sas_key), decoded_sas_key_length,
@@ -1251,16 +1267,16 @@ static az_span generate_dps_register_custom_property(az_span model_id, az_span d
12511267 size_t length = lengthof (DPS_REGISTER_CUSTOM_PAYLOAD_BEGIN) + az_span_size (model_id) + lengthof (DPS_REGISTER_CUSTOM_PAYLOAD_END);
12521268
12531269 custom_property = split_az_span (data_buffer, length, remainder);
1254- EXIT_IF_TRUE (is_az_span_empty (custom_property), AZ_SPAN_EMPTY, " Failed generating DPS register custom property (not enough space)." );
1270+ EXIT_IF_TRUE (az_span_is_content_equal (custom_property, AZ_SPAN_EMPTY ), AZ_SPAN_EMPTY, " Failed generating DPS register custom property (not enough space)." );
12551271
12561272 data_buffer = az_span_copy (data_buffer, AZ_SPAN_FROM_STR (DPS_REGISTER_CUSTOM_PAYLOAD_BEGIN));
1257- EXIT_IF_TRUE (is_az_span_empty (data_buffer), AZ_SPAN_EMPTY, " Failed generating DPS register custom property (prefix)." );
1273+ EXIT_IF_TRUE (az_span_is_content_equal (data_buffer, AZ_SPAN_EMPTY ), AZ_SPAN_EMPTY, " Failed generating DPS register custom property (prefix)." );
12581274
12591275 data_buffer = az_span_copy (data_buffer, model_id);
1260- EXIT_IF_TRUE (is_az_span_empty (data_buffer), AZ_SPAN_EMPTY, " Failed generating DPS register custom property (model id)." );
1276+ EXIT_IF_TRUE (az_span_is_content_equal (data_buffer, AZ_SPAN_EMPTY ), AZ_SPAN_EMPTY, " Failed generating DPS register custom property (model id)." );
12611277
12621278 data_buffer = az_span_copy (data_buffer, AZ_SPAN_FROM_STR (DPS_REGISTER_CUSTOM_PAYLOAD_END));
1263- EXIT_IF_TRUE (is_az_span_empty (data_buffer), AZ_SPAN_EMPTY, " Failed generating DPS register custom property (suffix)." );
1279+ EXIT_IF_TRUE (az_span_is_content_equal (data_buffer, AZ_SPAN_EMPTY ), AZ_SPAN_EMPTY, " Failed generating DPS register custom property (suffix)." );
12641280
12651281 return custom_property;
12661282}
@@ -1270,7 +1286,7 @@ az_span split_az_span(az_span span, int32_t size, az_span* remainder)
12701286{
12711287 az_span result = az_span_slice (span, 0 , size);
12721288
1273- if (remainder != NULL && !is_az_span_empty (result))
1289+ if (remainder != NULL && !az_span_is_content_equal (result, AZ_SPAN_EMPTY ))
12741290 {
12751291 *remainder = az_span_slice (span, size, az_span_size (span));
12761292 }
@@ -1282,12 +1298,12 @@ az_span slice_and_copy_az_span(az_span destination, az_span source, az_span* rem
12821298{
12831299 az_span result = split_az_span (destination, az_span_size (source), remainder);
12841300
1285- if (is_az_span_empty (*remainder))
1301+ if (az_span_is_content_equal (*remainder, AZ_SPAN_EMPTY ))
12861302 {
12871303 result = AZ_SPAN_EMPTY;
12881304 }
12891305
1290- if (!is_az_span_empty (result))
1306+ if (!az_span_is_content_equal (result, AZ_SPAN_EMPTY ))
12911307 {
12921308 (void )az_span_copy (result, source);
12931309 }
0 commit comments