libsood

Zig library for Roon Core discovery message, with C-compatible API and WebAssembly.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
  101. 101
  102. 102
  103. 103
  104. 104
  105. 105
  106. 106
  107. 107
  108. 108
  109. 109
  110. 110
  111. 111
  112. 112
  113. 113
  114. 114
  115. 115
  116. 116
  117. 117
  118. 118
  119. 119
  120. 120
  121. 121
  122. 122
  123. 123
  124. 124
  125. 125
  126. 126
  127. 127
  128. 128
  129. 129
  130. 130
  131. 131
  132. 132
  133. 133
  134. 134
  135. 135
  136. 136
  137. 137
  138. 138
  139. 139
  140. 140
  141. 141
  142. 142
  143. 143
  144. 144
  145. 145
// Copyright 2025 Shota FUJI
//
// Licensed under the Zero-Clause BSD License or the Apache License, Version 2.0, at your option.
// You may not use, copy, modify, or distribute this file except according to those terms. You can
// find a copy of the Zero-Clause BSD License at LICENSES/0BSD.txt, and a copy of the Apache License,
// Version 2.0 at LICENSES/Apache-2.0.txt. You may also obtain a copy of the Apache License, Version
// 2.0 at <https://www.apache.org/licenses/LICENSE-2.0>
//
// SPDX-License-Identifier: 0BSD OR Apache-2.0
//
// ===
//
// WebAssembly API.
//
// This module does not export constants, because Zig has no support for WebAssembly
// Globals yet. Exporting const results in a pointer for the value in LLVM and compile
// error in self-hosted backend.
// <https://github.com/ziglang/zig/issues/4866>.
//
// For binary size and maintenance cost, WebAssembly API only expose high-level response
// parsing API. Low-level APIs such as Message.parse and Message.iterator are not included.

const std = @import("std");

const sood = @import("lib.zig");

// Shared result code constants.
const WasmResultCode = enum(u32) {
    OK = 0,
    ITERATOR_DONE = 1,
    ERR_PARSE_HEADER_SIZE_MISMATCH = 2,
    ERR_PARSE_INVALID_SIGNATURE = 3,
    ERR_PARSE_EMPTY_KEY = 4,
    ERR_PARSE_KEY_SIZE_MISMATCH = 5,
    ERR_PARSE_NON_UTF8_KEY = 6,
    ERR_PARSE_VALUE_SIZE_CORRUPTED = 7,
    ERR_PARSE_VALUE_SIZE_MISMATCH = 8,
    ERR_PARSE_NON_UTF8_VALUE = 9,
    ERR_VALIDATE_MISSING_REQUIRED_PROPERTY = 10,
    ERR_VALIDATE_UNEXPECTED_VALUE_TYPE = 11,
    ERR_VALIDATE_UNEXPECTED_MESSAGE_KIND = 12,
};

const LibError = sood.Message.HeaderParseError || sood.Message.PropertyParseError || sood.discovery.Response.SchemaError;

inline fn errorsToCode(err: LibError) WasmResultCode {
    return switch (err) {
        sood.Message.HeaderParseError.InvalidSignature => .ERR_PARSE_INVALID_SIGNATURE,
        sood.Message.HeaderParseError.InvalidHeaderSize => .ERR_PARSE_HEADER_SIZE_MISMATCH,
        sood.Message.PropertyParseError.EmptyKey => .ERR_PARSE_EMPTY_KEY,
        sood.Message.PropertyParseError.IncompleteKey => .ERR_PARSE_KEY_SIZE_MISMATCH,
        sood.Message.PropertyParseError.NonUTF8Key => .ERR_PARSE_NON_UTF8_KEY,
        sood.Message.PropertyParseError.InvalidValueSizeField => .ERR_PARSE_VALUE_SIZE_CORRUPTED,
        sood.Message.PropertyParseError.IncompleteValue => .ERR_PARSE_VALUE_SIZE_MISMATCH,
        sood.Message.PropertyParseError.NonUTF8Value => .ERR_PARSE_NON_UTF8_VALUE,
        sood.discovery.Response.SchemaError.MissingRequiredProperty => .ERR_VALIDATE_MISSING_REQUIRED_PROPERTY,
        sood.discovery.Response.SchemaError.UnexpectedPropertyValue => .ERR_VALIDATE_UNEXPECTED_VALUE_TYPE,
        sood.discovery.Response.SchemaError.NonResponseKindMessage => .ERR_VALIDATE_UNEXPECTED_MESSAGE_KIND,
    };
}

export fn result_code_string_ptr(code: WasmResultCode) [*]const u8 {
    return @tagName(code).ptr;
}

export fn result_code_string_len(code: WasmResultCode) usize {
    return @tagName(code).len;
}

export fn allocate_bytes(len: usize) [*]const u8 {
    return (std.heap.wasm_allocator.alloc(u8, len) catch @trap()).ptr;
}

export fn free_bytes(ptr: [*]const u8, len: usize) void {
    std.heap.wasm_allocator.free(ptr[0..len]);
}

export fn get_discovery_multicast_ipv4_address() [*]const u8 {
    return &sood.discovery.multicast_ipv4_address;
}

export fn get_discovery_server_udp_port() u16 {
    return sood.discovery.udp_port;
}

export fn prebuilt_discovery_query_ptr() [*]const u8 {
    return sood.discovery.Query.prebuilt.ptr;
}

export fn prebuilt_discovery_query_len() usize {
    return sood.discovery.Query.prebuilt.len;
}

export fn allocate_discovery_response() *sood.discovery.Response {
    return std.heap.wasm_allocator.create(sood.discovery.Response) catch @trap();
}

export fn free_discovery_response(ptr: *sood.discovery.Response) void {
    std.heap.wasm_allocator.destroy(ptr);
}

export fn parse_discovery_response_into(
    dst: *sood.discovery.Response,
    bytes_ptr: [*]const u8,
    bytes_len: usize,
) WasmResultCode {
    const resp = sood.discovery.Response.parse(bytes_ptr[0..bytes_len]) catch |err| {
        return errorsToCode(err);
    };

    dst.http_port = resp.http_port;
    dst.name = resp.name;
    dst.display_version = resp.display_version;
    dst.unique_id = resp.unique_id;

    return .OK;
}

export fn get_discovery_response_http_port(resp: *const sood.discovery.Response) u16 {
    return resp.http_port;
}

export fn get_discovery_response_name_ptr(resp: *const sood.discovery.Response) [*]const u8 {
    return resp.name.ptr;
}

export fn get_discovery_response_name_len(resp: *const sood.discovery.Response) usize {
    return resp.name.len;
}

export fn get_discovery_response_display_version_ptr(resp: *const sood.discovery.Response) [*]const u8 {
    return resp.display_version.ptr;
}

export fn get_discovery_response_display_version_len(resp: *const sood.discovery.Response) usize {
    return resp.display_version.len;
}

export fn get_discovery_response_unique_id_ptr(resp: *const sood.discovery.Response) [*]const u8 {
    return resp.unique_id.ptr;
}

export fn get_discovery_response_unique_id_len(resp: *const sood.discovery.Response) usize {
    return resp.unique_id.len;
}