James Kuszmaul | 6439136 | 2021-01-17 11:32:00 -0800 | [diff] [blame^] | 1 | #include "transport.h" |
| 2 | #include <rawrtcdc/data_channel.h> |
| 3 | #include <rawrtcdc/sctp_capabilities.h> |
| 4 | #include <rawrtcdc/sctp_transport.h> |
| 5 | #include <re.h> |
| 6 | |
| 7 | /* |
| 8 | * Get the current state of the SCTP transport. |
| 9 | */ |
| 10 | enum rawrtc_code rawrtc_sctp_transport_get_state( |
| 11 | enum rawrtc_sctp_transport_state* const statep, // de-referenced |
| 12 | struct rawrtc_sctp_transport* const transport) { |
| 13 | // Check arguments |
| 14 | if (!statep || !transport) { |
| 15 | return RAWRTC_CODE_INVALID_ARGUMENT; |
| 16 | } |
| 17 | |
| 18 | // Set state & done |
| 19 | *statep = transport->state; |
| 20 | return RAWRTC_CODE_SUCCESS; |
| 21 | } |
| 22 | |
| 23 | /* |
| 24 | * Get the local port of the SCTP transport. |
| 25 | */ |
| 26 | enum rawrtc_code rawrtc_sctp_transport_get_port( |
| 27 | uint16_t* const portp, // de-referenced |
| 28 | struct rawrtc_sctp_transport* const transport) { |
| 29 | // Check arguments |
| 30 | if (!portp || !transport) { |
| 31 | return RAWRTC_CODE_INVALID_ARGUMENT; |
| 32 | } |
| 33 | |
| 34 | // Set port & done |
| 35 | *portp = transport->port; |
| 36 | return RAWRTC_CODE_SUCCESS; |
| 37 | } |
| 38 | |
| 39 | /* |
| 40 | * Get the number of streams allocated for the SCTP transport. |
| 41 | */ |
| 42 | enum rawrtc_code rawrtc_sctp_transport_get_n_streams( |
| 43 | uint16_t* const n_streamsp, // de-referenced |
| 44 | struct rawrtc_sctp_transport* const transport) { |
| 45 | // Check arguments |
| 46 | if (!n_streamsp || !transport) { |
| 47 | return RAWRTC_CODE_INVALID_ARGUMENT; |
| 48 | } |
| 49 | |
| 50 | // Set #streams & done |
| 51 | *n_streamsp = (uint16_t) transport->n_channels; |
| 52 | return RAWRTC_CODE_SUCCESS; |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | * Get the local SCTP transport capabilities (static). |
| 57 | * `*capabilitiesp` must be unreferenced. |
| 58 | */ |
| 59 | enum rawrtc_code rawrtc_sctp_transport_get_capabilities( |
| 60 | struct rawrtc_sctp_capabilities** const capabilitiesp // de-referenced |
| 61 | ) { |
| 62 | return rawrtc_sctp_capabilities_create(capabilitiesp, RAWRTC_SCTP_TRANSPORT_MAX_MESSAGE_SIZE); |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | * Set the SCTP transport's data channel handler. |
| 67 | */ |
| 68 | enum rawrtc_code rawrtc_sctp_transport_set_data_channel_handler( |
| 69 | struct rawrtc_sctp_transport* const transport, |
| 70 | rawrtc_data_channel_handler const data_channel_handler // nullable |
| 71 | ) { |
| 72 | // Check arguments |
| 73 | if (!transport) { |
| 74 | return RAWRTC_CODE_INVALID_ARGUMENT; |
| 75 | } |
| 76 | |
| 77 | // Set data channel handler & done |
| 78 | transport->data_channel_handler = data_channel_handler; |
| 79 | return RAWRTC_CODE_SUCCESS; |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * Get the SCTP transport's data channel handler. |
| 84 | * Returns `RAWRTC_CODE_NO_VALUE` in case no handler has been set. |
| 85 | */ |
| 86 | enum rawrtc_code rawrtc_sctp_transport_get_data_channel_handler( |
| 87 | rawrtc_data_channel_handler* const data_channel_handlerp, // de-referenced |
| 88 | struct rawrtc_sctp_transport* const transport) { |
| 89 | // Check arguments |
| 90 | if (!data_channel_handlerp || !transport) { |
| 91 | return RAWRTC_CODE_INVALID_ARGUMENT; |
| 92 | } |
| 93 | |
| 94 | // Get message handler (if any) |
| 95 | if (transport->data_channel_handler) { |
| 96 | *data_channel_handlerp = transport->data_channel_handler; |
| 97 | return RAWRTC_CODE_SUCCESS; |
| 98 | } else { |
| 99 | return RAWRTC_CODE_NO_VALUE; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * Set the SCTP transport's state change handler. |
| 105 | */ |
| 106 | enum rawrtc_code rawrtc_sctp_transport_set_state_change_handler( |
| 107 | struct rawrtc_sctp_transport* const transport, |
| 108 | rawrtc_sctp_transport_state_change_handler const state_change_handler // nullable |
| 109 | ) { |
| 110 | // Check arguments |
| 111 | if (!transport) { |
| 112 | return RAWRTC_CODE_INVALID_ARGUMENT; |
| 113 | } |
| 114 | |
| 115 | // Set data channel handler & done |
| 116 | transport->state_change_handler = state_change_handler; |
| 117 | return RAWRTC_CODE_SUCCESS; |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * Get the SCTP transport's state change handler. |
| 122 | * Returns `RAWRTC_CODE_NO_VALUE` in case no handler has been set. |
| 123 | */ |
| 124 | enum rawrtc_code rawrtc_sctp_transport_get_state_change_handler( |
| 125 | rawrtc_sctp_transport_state_change_handler* const state_change_handlerp, // de-referenced |
| 126 | struct rawrtc_sctp_transport* const transport) { |
| 127 | // Check arguments |
| 128 | if (!state_change_handlerp || !transport) { |
| 129 | return RAWRTC_CODE_INVALID_ARGUMENT; |
| 130 | } |
| 131 | |
| 132 | // Get message handler (if any) |
| 133 | if (transport->state_change_handler) { |
| 134 | *state_change_handlerp = transport->state_change_handler; |
| 135 | return RAWRTC_CODE_SUCCESS; |
| 136 | } else { |
| 137 | return RAWRTC_CODE_NO_VALUE; |
| 138 | } |
| 139 | } |