void prov_transport_http_dowork()

in provisioning_client/src/prov_transport_http_client.c [872:984]


void prov_transport_http_dowork(PROV_DEVICE_TRANSPORT_HANDLE handle)
{
    if (handle != NULL)
    {
        PROV_TRANSPORT_HTTP_INFO* http_info = (PROV_TRANSPORT_HTTP_INFO*)handle;
        uhttp_client_dowork(http_info->http_client);

        if (http_info->http_connected || http_info->transport_state == TRANSPORT_CLIENT_STATE_ERROR)
        {
            switch (http_info->transport_state)
            {
            case TRANSPORT_CLIENT_STATE_REG_SEND:
                if (send_registration_info(http_info) != 0)
                {
                    LogError("NULL sas_token provided in challenge callback.");
                    http_info->transport_state = TRANSPORT_CLIENT_STATE_ERROR;
                }
                else
                {
                    http_info->transport_state = TRANSPORT_CLIENT_STATE_REG_SENT;
                }
                break;
            case TRANSPORT_CLIENT_STATE_STATUS_RECV:
            case TRANSPORT_CLIENT_STATE_REG_RECV:
            {
                PROV_JSON_INFO* parse_info = http_info->json_parse_cb(http_info->payload_data, http_info->json_ctx);
                if (parse_info == NULL)
                {
                    LogError("Unable to process registration reply.");
                    http_info->transport_state = TRANSPORT_CLIENT_STATE_ERROR;
                }
                else
                {
                    switch (parse_info->prov_status)
                    {
                        case PROV_DEVICE_TRANSPORT_STATUS_ASSIGNED:
                            http_info->register_data_cb(PROV_DEVICE_TRANSPORT_RESULT_OK, parse_info->authorization_key, parse_info->iothub_uri, parse_info->device_id, http_info->user_ctx);
                            http_info->transport_state = TRANSPORT_CLIENT_STATE_IDLE;
                            break;

                        case PROV_DEVICE_TRANSPORT_STATUS_UNASSIGNED:
                        {
                            const unsigned char* nonce = BUFFER_u_char(parse_info->authorization_key);
                            size_t nonce_len = BUFFER_length(parse_info->authorization_key);

                            http_info->sas_token = http_info->challenge_cb(nonce, nonce_len, parse_info->key_name, http_info->user_ctx);
                            if (http_info->sas_token == NULL)
                            {
                                LogError("NULL sas_token provided in challenge callback.");
                                http_info->transport_state = TRANSPORT_CLIENT_STATE_ERROR;
                            }
                            else
                            {
                                send_challenge_response(http_info);
                                http_info->transport_state = TRANSPORT_CLIENT_STATE_REG_SENT;
                            }
                            break;
                        }

                        case PROV_DEVICE_TRANSPORT_STATUS_ASSIGNING:
                            if (parse_info->operation_id == NULL)
                            {
                                LogError("Failure operation Id invalid");
                                http_info->transport_state = TRANSPORT_CLIENT_STATE_ERROR;
                            }
                            else if (http_info->operation_id == NULL && mallocAndStrcpy_s(&http_info->operation_id, parse_info->operation_id) != 0)
                            {
                                LogError("Failure copying operation Id");
                                http_info->transport_state = TRANSPORT_CLIENT_STATE_ERROR;
                            }
                            else
                            {
                                if (http_info->status_cb != NULL)
                                {
                                    http_info->status_cb(parse_info->prov_status, http_info->retry_after_value, http_info->status_ctx);
                                }
                                http_info->transport_state = TRANSPORT_CLIENT_STATE_IDLE;
                            }
                            break;

                        default:
                        case PROV_DEVICE_TRANSPORT_STATUS_ERROR:
                            LogError("Unable to process status reply");
                            http_info->transport_state = TRANSPORT_CLIENT_STATE_ERROR;
                            break;
                    }
                    free_json_parse_info(parse_info);
                }
                break;
            }

            case TRANSPORT_CLIENT_STATE_TRANSIENT:
                if (http_info->status_cb != NULL)
                {
                    http_info->status_cb(PROV_DEVICE_TRANSPORT_STATUS_TRANSIENT, http_info->retry_after_value, http_info->status_ctx);
                }
                http_info->transport_state = TRANSPORT_CLIENT_STATE_IDLE;
                break;

            case TRANSPORT_CLIENT_STATE_ERROR:
                http_info->register_data_cb(PROV_DEVICE_TRANSPORT_RESULT_ERROR, NULL, NULL, NULL, http_info->user_ctx);
                http_info->transport_state = TRANSPORT_CLIENT_STATE_IDLE;
                break;

            case TRANSPORT_CLIENT_STATE_REG_SENT:
            case TRANSPORT_CLIENT_STATE_STATUS_SENT:
            case TRANSPORT_CLIENT_STATE_IDLE:
            default:
                break;
            }
        }
    }
}