| // Get the size of the VMO |
| uint64_t content_size_prop = 0; |
| zx_status_t status = config_vmo.get_prop_content_size(&content_size_prop); |
| ZX_ASSERT_MSG(status == ZX_OK, "Could not get content size of config VMO"); |
| size_t vmo_content_size = static_cast<size_t>(content_size_prop); |
| |
| // Checksum length must be correct |
| uint16_t checksum_length = 0; |
| status = config_vmo.read(&checksum_length, 0, 2); |
| ZX_ASSERT_MSG(status == ZX_OK, "Could not read checksum length from config VMO"); |
| |
| // Verify Checksum |
| std::vector<uint8_t> checksum(checksum_length); |
| status = config_vmo.read(checksum.data(), 2, checksum_length); |
| ZX_ASSERT_MSG(status == ZX_OK, "Could not read checksum from config VMO"); |
| std::vector<uint8_t> expected_checksum { |
| {{#each expected_checksum}} |
| {{(hex_byte this)}}{{#unless @last}},{{/unless}} |
| {{/each}} |
| }; |
| ZX_ASSERT_MSG(checksum == expected_checksum, "Invalid checksum for config VMO"); |
| |
| // Read the FIDL struct into memory, skipping the checksum length and the checksum itself |
| size_t header = 2 + checksum_length; |
| size_t fidl_struct_size = vmo_content_size - header; |
| |
| // Align the struct pointer to 8 bytes by using a generic allocation (as required by FIDL) |
| std::unique_ptr<uint8_t[]> fidl_struct(new uint8_t[fidl_struct_size]); |
| status = config_vmo.read(fidl_struct.get(), header, fidl_struct_size); |
| ZX_ASSERT_MSG(status == ZX_OK, "Could not read FIDL struct from config VMO"); |
| |
| // Decode the FIDL struct |
| fit::result result = fidl::InplaceUnpersist<{{fidl_cpp_namespace}}::wire::Config>( |
| cpp20::span(fidl_struct.get(), fidl_struct_size)); |
| ZX_ASSERT_MSG(result.is_ok(), "Could not decode Config FIDL structure"); |
| {{fidl_cpp_namespace}}::wire::Config& fidl_config = *result.value(); |
| |
| // Convert the configuration into a new struct |
| Config c\{{ |
| {{#each fields}} |
| {{#if (is_string_vector type_)}} |
| .{{ident}} = from_vector_string_view(fidl_config.{{ident}}) |
| {{else}} |
| {{#if (is_vector type_)}} |
| .{{ident}} = from_vector_view(fidl_config.{{ident}}) |
| {{else}} |
| {{#if (is_string type_)}} |
| .{{ident}} = std::string(fidl_config.{{ident}}.get()) |
| {{else}} |
| .{{ident}} = fidl_config.{{ident}} |
| {{/if}} |
| {{/if}} |
| {{/if}} |
| {{~#unless @last}},{{/unless}} |
| {{~/each~}} |
| }}; |
| |
| return c; |