blob: ae51caa5098760805d673631952ce65c3a7bb993 [file] [log] [blame]
James Kuszmaul4a42b182021-01-17 11:32:46 -08001#include "options.h"
2#include "../ice_server/server.h"
3#include <rawrtc/ice_gather_options.h>
4#include <rawrtc/ice_server.h>
5#include <rawrtcc/code.h>
6#include <re.h>
7
8static enum rawrtc_ice_gather_policy const map_enum_ice_gather_policy[] = {
9 RAWRTC_ICE_GATHER_POLICY_ALL,
10 RAWRTC_ICE_GATHER_POLICY_NOHOST,
11 RAWRTC_ICE_GATHER_POLICY_RELAY,
12};
13
14static char const* const map_str_ice_gather_policy[] = {
15 "all",
16 "nohost",
17 "relay",
18};
19
20static size_t const map_ice_gather_policy_length = ARRAY_SIZE(map_enum_ice_gather_policy);
21
22/*
23 * Translate an ICE gather policy to str.
24 */
25char const* rawrtc_ice_gather_policy_to_str(enum rawrtc_ice_gather_policy const policy) {
26 size_t i;
27
28 for (i = 0; i < map_ice_gather_policy_length; ++i) {
29 if (map_enum_ice_gather_policy[i] == policy) {
30 return map_str_ice_gather_policy[i];
31 }
32 }
33
34 return "???";
35}
36
37/*
38 * Translate a str to an ICE gather policy (case-insensitive).
39 */
40enum rawrtc_code rawrtc_str_to_ice_gather_policy(
41 enum rawrtc_ice_gather_policy* const policyp, // de-referenced
42 char const* const str) {
43 size_t i;
44
45 // Check arguments
46 if (!policyp || !str) {
47 return RAWRTC_CODE_INVALID_ARGUMENT;
48 }
49
50 for (i = 0; i < map_ice_gather_policy_length; ++i) {
51 if (str_casecmp(map_str_ice_gather_policy[i], str) == 0) {
52 *policyp = map_enum_ice_gather_policy[i];
53 return RAWRTC_CODE_SUCCESS;
54 }
55 }
56
57 return RAWRTC_CODE_NO_VALUE;
58}
59
60/*
61 * Print debug information for the ICE gather options.
62 */
63int rawrtc_ice_gather_options_debug(
64 struct re_printf* const pf, struct rawrtc_ice_gather_options const* const options) {
65 int err = 0;
66 struct le* le;
67
68 // Check arguments
69 if (!options) {
70 return 0;
71 }
72
73 err |= re_hprintf(pf, "----- ICE Gather Options <%p> -----\n", options);
74
75 // Gather policy
76 err |= re_hprintf(
77 pf, " gather_policy=%s\n", rawrtc_ice_gather_policy_to_str(options->gather_policy));
78
79 // ICE servers
80 for (le = list_head(&options->ice_servers); le != NULL; le = le->next) {
81 struct rawrtc_ice_server* const server = le->data;
82 err |= re_hprintf(pf, "%H", rawrtc_ice_server_debug, server);
83 }
84
85 // Done
86 return err;
87}