freeswitch-types 1.4.0

FreeSWITCH ESL protocol types: channel state, events, headers, commands, and variables
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
use std::fmt;
use std::str::FromStr;

/// Generates `EslEventType` enum with `Display`, `FromStr`, `as_str`, `parse_event_type`,
/// and the six predefined event-group constants.
///
/// Each variant row carries an optional `[group, ...]` tag list. The macro
/// generates `CHANNEL_EVENTS`, `IN_CALL_EVENTS`, `MEDIA_EVENTS`,
/// `PRESENCE_EVENTS`, `SYSTEM_EVENTS`, and `CONFERENCE_EVENTS` by filtering the
/// variant table — no separate sync step required.
macro_rules! esl_event_types {
    // ------------------------------------------------------------------
    // Filter arms — TT-munch through variant tuples `(Variant [tags...])`,
    // accumulating those whose tag list contains the target group.
    // Accumulator is `[$($acc:ident),*]`; items are space-separated tuples.
    // ------------------------------------------------------------------

    // channel
    (@filter_channel [$($acc:ident),*]) => { &[$(EslEventType::$acc,)*] };
    (@filter_channel [$($acc:ident),*] ($v:ident [channel $(,$_g:ident)*]) $($tail:tt)*) => {
        esl_event_types!(@filter_channel [$($acc,)* $v] $($tail)*)
    };
    (@filter_channel [$($acc:ident),*] ($v:ident [$_h:ident $(,$tg:ident)*]) $($tail:tt)*) => {
        esl_event_types!(@filter_channel [$($acc),*] ($v [$($tg),*]) $($tail)*)
    };
    (@filter_channel [$($acc:ident),*] ($v:ident []) $($tail:tt)*) => {
        esl_event_types!(@filter_channel [$($acc),*] $($tail)*)
    };

    // in_call
    (@filter_in_call [$($acc:ident),*]) => { &[$(EslEventType::$acc,)*] };
    (@filter_in_call [$($acc:ident),*] ($v:ident [in_call $(,$_g:ident)*]) $($tail:tt)*) => {
        esl_event_types!(@filter_in_call [$($acc,)* $v] $($tail)*)
    };
    (@filter_in_call [$($acc:ident),*] ($v:ident [$_h:ident $(,$tg:ident)*]) $($tail:tt)*) => {
        esl_event_types!(@filter_in_call [$($acc),*] ($v [$($tg),*]) $($tail)*)
    };
    (@filter_in_call [$($acc:ident),*] ($v:ident []) $($tail:tt)*) => {
        esl_event_types!(@filter_in_call [$($acc),*] $($tail)*)
    };

    // media
    (@filter_media [$($acc:ident),*]) => { &[$(EslEventType::$acc,)*] };
    (@filter_media [$($acc:ident),*] ($v:ident [media $(,$_g:ident)*]) $($tail:tt)*) => {
        esl_event_types!(@filter_media [$($acc,)* $v] $($tail)*)
    };
    (@filter_media [$($acc:ident),*] ($v:ident [$_h:ident $(,$tg:ident)*]) $($tail:tt)*) => {
        esl_event_types!(@filter_media [$($acc),*] ($v [$($tg),*]) $($tail)*)
    };
    (@filter_media [$($acc:ident),*] ($v:ident []) $($tail:tt)*) => {
        esl_event_types!(@filter_media [$($acc),*] $($tail)*)
    };

    // presence
    (@filter_presence [$($acc:ident),*]) => { &[$(EslEventType::$acc,)*] };
    (@filter_presence [$($acc:ident),*] ($v:ident [presence $(,$_g:ident)*]) $($tail:tt)*) => {
        esl_event_types!(@filter_presence [$($acc,)* $v] $($tail)*)
    };
    (@filter_presence [$($acc:ident),*] ($v:ident [$_h:ident $(,$tg:ident)*]) $($tail:tt)*) => {
        esl_event_types!(@filter_presence [$($acc),*] ($v [$($tg),*]) $($tail)*)
    };
    (@filter_presence [$($acc:ident),*] ($v:ident []) $($tail:tt)*) => {
        esl_event_types!(@filter_presence [$($acc),*] $($tail)*)
    };

    // system
    (@filter_system [$($acc:ident),*]) => { &[$(EslEventType::$acc,)*] };
    (@filter_system [$($acc:ident),*] ($v:ident [system $(,$_g:ident)*]) $($tail:tt)*) => {
        esl_event_types!(@filter_system [$($acc,)* $v] $($tail)*)
    };
    (@filter_system [$($acc:ident),*] ($v:ident [$_h:ident $(,$tg:ident)*]) $($tail:tt)*) => {
        esl_event_types!(@filter_system [$($acc),*] ($v [$($tg),*]) $($tail)*)
    };
    (@filter_system [$($acc:ident),*] ($v:ident []) $($tail:tt)*) => {
        esl_event_types!(@filter_system [$($acc),*] $($tail)*)
    };

    // conference
    (@filter_conference [$($acc:ident),*]) => { &[$(EslEventType::$acc,)*] };
    (@filter_conference [$($acc:ident),*] ($v:ident [conference $(,$_g:ident)*]) $($tail:tt)*) => {
        esl_event_types!(@filter_conference [$($acc,)* $v] $($tail)*)
    };
    (@filter_conference [$($acc:ident),*] ($v:ident [$_h:ident $(,$tg:ident)*]) $($tail:tt)*) => {
        esl_event_types!(@filter_conference [$($acc),*] ($v [$($tg),*]) $($tail)*)
    };
    (@filter_conference [$($acc:ident),*] ($v:ident []) $($tail:tt)*) => {
        esl_event_types!(@filter_conference [$($acc),*] $($tail)*)
    };

    // ------------------------------------------------------------------
    // Main arm — generate enum, Display, FromStr, and group constants.
    // Each variant row: `Variant => "WIRE_NAME" [group, ...]`
    // Empty tag list `[]` = no group membership.
    // ------------------------------------------------------------------
    (
        $(
            $(#[$attr:meta])*
            $variant:ident => $wire:literal [$($vgroup:ident),*]
        ),+ $(,)?
        ;
        // Extra variants not in the main match (after All)
        $(
            $(#[$extra_attr:meta])*
            $extra_variant:ident => $extra_wire:literal [$($egroup:ident),*]
        ),* $(,)?
    ) => {
        /// FreeSWITCH event types matching the canonical order from `esl_event.h`
        /// and `switch_event.c` EVENT_NAMES[].
        ///
        /// Variant names are the canonical wire names (e.g. `ChannelCreate` = `CHANNEL_CREATE`).
        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
        #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
        #[non_exhaustive]
        #[allow(missing_docs)]
        pub enum EslEventType {
            $(
                $(#[$attr])*
                $variant,
            )+
            $(
                $(#[$extra_attr])*
                $extra_variant,
            )*
        }

        impl fmt::Display for EslEventType {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f.write_str(self.as_str())
            }
        }

        impl EslEventType {
            /// Returns the canonical wire name as a static string slice.
            pub const fn as_str(&self) -> &'static str {
                match self {
                    $( EslEventType::$variant => $wire, )+
                    $( EslEventType::$extra_variant => $extra_wire, )*
                }
            }

            /// Parse event type from wire name (canonical case).
            pub fn parse_event_type(s: &str) -> Option<Self> {
                match s {
                    $( $wire => Some(EslEventType::$variant), )+
                    $( $extra_wire => Some(EslEventType::$extra_variant), )*
                    _ => None,
                }
            }

            // -- Event group constants ----------------------------------------
            //
            // Predefined slices for common subscription patterns. Pass directly
            // to `EslClient::subscribe_events()`.
            //
            // Group membership is declared inline via the `[group, ...]` tag on
            // each variant row above. Adding a new variant and tagging it
            // automatically includes it in the right constant — no separate
            // maintenance step required.

            #[doc = "Every `CHANNEL_*` event type."]
            #[doc = ""]
            #[doc = "Covers the full channel lifecycle: creation, state changes, execution,"]
            #[doc = "bridging, hold, park, progress, originate, and destruction."]
            #[doc = ""]
            #[doc = "```rust"]
            #[doc = "use freeswitch_types::EslEventType;"]
            #[doc = "assert!(EslEventType::CHANNEL_EVENTS.contains(&EslEventType::ChannelCreate));"]
            #[doc = "assert!(EslEventType::CHANNEL_EVENTS.contains(&EslEventType::ChannelHangupComplete));"]
            #[doc = "```"]
            pub const CHANNEL_EVENTS: &[EslEventType] = esl_event_types!(
                @filter_channel []
                $(($variant [$($vgroup),*]))+
                $(($extra_variant [$($egroup),*]))*
            );

            #[doc = "In-call events: DTMF, VAD speech detection, media security, and call updates."]
            #[doc = ""]
            #[doc = "Events that fire during an established call, tied to RTP/media activity"]
            #[doc = "rather than signaling state transitions."]
            #[doc = ""]
            #[doc = "```rust"]
            #[doc = "use freeswitch_types::EslEventType;"]
            #[doc = "assert!(EslEventType::IN_CALL_EVENTS.contains(&EslEventType::Dtmf));"]
            #[doc = "assert!(EslEventType::IN_CALL_EVENTS.contains(&EslEventType::Talk));"]
            #[doc = "```"]
            pub const IN_CALL_EVENTS: &[EslEventType] = esl_event_types!(
                @filter_in_call []
                $(($variant [$($vgroup),*]))+
                $(($extra_variant [$($egroup),*]))*
            );

            #[doc = "Media-related events: playback, recording, media bugs, and detection."]
            #[doc = ""]
            #[doc = "Useful for IVR applications that need to track media operations without"]
            #[doc = "subscribing to the full channel lifecycle."]
            #[doc = ""]
            #[doc = "```rust"]
            #[doc = "use freeswitch_types::EslEventType;"]
            #[doc = "assert!(EslEventType::MEDIA_EVENTS.contains(&EslEventType::PlaybackStart));"]
            #[doc = "assert!(EslEventType::MEDIA_EVENTS.contains(&EslEventType::DetectedSpeech));"]
            #[doc = "```"]
            pub const MEDIA_EVENTS: &[EslEventType] = esl_event_types!(
                @filter_media []
                $(($variant [$($vgroup),*]))+
                $(($extra_variant [$($egroup),*]))*
            );

            #[doc = "Presence and messaging events."]
            #[doc = ""]
            #[doc = "For applications that track user presence (BLF, buddy lists) or"]
            #[doc = "message-waiting indicators (voicemail MWI)."]
            #[doc = ""]
            #[doc = "```rust"]
            #[doc = "use freeswitch_types::EslEventType;"]
            #[doc = "assert!(EslEventType::PRESENCE_EVENTS.contains(&EslEventType::PresenceIn));"]
            #[doc = "assert!(EslEventType::PRESENCE_EVENTS.contains(&EslEventType::MessageWaiting));"]
            #[doc = "```"]
            pub const PRESENCE_EVENTS: &[EslEventType] = esl_event_types!(
                @filter_presence []
                $(($variant [$($vgroup),*]))+
                $(($extra_variant [$($egroup),*]))*
            );

            #[doc = "System lifecycle events."]
            #[doc = ""]
            #[doc = "Server startup/shutdown, heartbeats, module loading, and XML reloads."]
            #[doc = "Useful for monitoring dashboards and operational tooling."]
            #[doc = ""]
            #[doc = "```rust"]
            #[doc = "use freeswitch_types::EslEventType;"]
            #[doc = "assert!(EslEventType::SYSTEM_EVENTS.contains(&EslEventType::Heartbeat));"]
            #[doc = "assert!(EslEventType::SYSTEM_EVENTS.contains(&EslEventType::Shutdown));"]
            #[doc = "```"]
            pub const SYSTEM_EVENTS: &[EslEventType] = esl_event_types!(
                @filter_system []
                $(($variant [$($vgroup),*]))+
                $(($extra_variant [$($egroup),*]))*
            );

            #[doc = "Conference-related events."]
            #[doc = ""]
            #[doc = "```rust"]
            #[doc = "use freeswitch_types::EslEventType;"]
            #[doc = "assert!(EslEventType::CONFERENCE_EVENTS.contains(&EslEventType::ConferenceData));"]
            #[doc = "```"]
            pub const CONFERENCE_EVENTS: &[EslEventType] = esl_event_types!(
                @filter_conference []
                $(($variant [$($vgroup),*]))+
                $(($extra_variant [$($egroup),*]))*
            );
        }

        impl FromStr for EslEventType {
            type Err = ParseEventTypeError;

            fn from_str(s: &str) -> Result<Self, Self::Err> {
                Self::parse_event_type(s).ok_or_else(|| ParseEventTypeError(s.to_string()))
            }
        }
    };
}

esl_event_types! {
    Custom               => "CUSTOM"                   [],
    Clone                => "CLONE"                    [],
    ChannelCreate        => "CHANNEL_CREATE"           [channel],
    ChannelDestroy       => "CHANNEL_DESTROY"          [channel],
    ChannelState         => "CHANNEL_STATE"            [channel],
    ChannelCallstate     => "CHANNEL_CALLSTATE"        [channel],
    ChannelAnswer        => "CHANNEL_ANSWER"           [channel],
    ChannelHangup        => "CHANNEL_HANGUP"           [channel],
    ChannelHangupComplete => "CHANNEL_HANGUP_COMPLETE" [channel],
    ChannelExecute       => "CHANNEL_EXECUTE"          [channel],
    ChannelExecuteComplete => "CHANNEL_EXECUTE_COMPLETE" [channel],
    ChannelHold          => "CHANNEL_HOLD"             [channel],
    ChannelUnhold        => "CHANNEL_UNHOLD"           [channel],
    ChannelBridge        => "CHANNEL_BRIDGE"           [channel],
    ChannelUnbridge      => "CHANNEL_UNBRIDGE"         [channel],
    ChannelProgress      => "CHANNEL_PROGRESS"         [channel],
    ChannelProgressMedia => "CHANNEL_PROGRESS_MEDIA"   [channel],
    ChannelOutgoing      => "CHANNEL_OUTGOING"         [channel],
    ChannelPark          => "CHANNEL_PARK"             [channel],
    ChannelUnpark        => "CHANNEL_UNPARK"           [channel],
    ChannelApplication   => "CHANNEL_APPLICATION"      [channel],
    ChannelOriginate     => "CHANNEL_ORIGINATE"        [channel],
    ChannelUuid          => "CHANNEL_UUID"             [channel],
    Api                  => "API"                      [],
    Log                  => "LOG"                      [],
    InboundChan          => "INBOUND_CHAN"             [],
    OutboundChan         => "OUTBOUND_CHAN"            [],
    Startup              => "STARTUP"                  [system],
    Shutdown             => "SHUTDOWN"                 [system],
    Publish              => "PUBLISH"                  [],
    Unpublish            => "UNPUBLISH"                [],
    Talk                 => "TALK"                     [in_call],
    Notalk               => "NOTALK"                   [in_call],
    SessionCrash         => "SESSION_CRASH"            [system],
    ModuleLoad           => "MODULE_LOAD"              [system],
    ModuleUnload         => "MODULE_UNLOAD"            [system],
    Dtmf                 => "DTMF"                     [in_call],
    Message              => "MESSAGE"                  [],
    PresenceIn           => "PRESENCE_IN"              [presence],
    NotifyIn             => "NOTIFY_IN"                [],
    PresenceOut          => "PRESENCE_OUT"             [presence],
    PresenceProbe        => "PRESENCE_PROBE"           [presence],
    MessageWaiting       => "MESSAGE_WAITING"          [presence],
    MessageQuery         => "MESSAGE_QUERY"            [presence],
    Roster               => "ROSTER"                   [presence],
    Codec                => "CODEC"                    [],
    BackgroundJob        => "BACKGROUND_JOB"           [],
    DetectedSpeech       => "DETECTED_SPEECH"          [media],
    DetectedTone         => "DETECTED_TONE"            [media],
    PrivateCommand       => "PRIVATE_COMMAND"          [],
    Heartbeat            => "HEARTBEAT"                [system],
    Trap                 => "TRAP"                     [],
    AddSchedule          => "ADD_SCHEDULE"             [],
    DelSchedule          => "DEL_SCHEDULE"             [],
    ExeSchedule          => "EXE_SCHEDULE"             [],
    ReSchedule           => "RE_SCHEDULE"              [],
    ReloadXml            => "RELOADXML"                [system],
    Notify               => "NOTIFY"                   [],
    PhoneFeature         => "PHONE_FEATURE"            [],
    PhoneFeatureSubscribe => "PHONE_FEATURE_SUBSCRIBE" [],
    SendMessage          => "SEND_MESSAGE"             [],
    RecvMessage          => "RECV_MESSAGE"             [],
    RequestParams        => "REQUEST_PARAMS"           [],
    ChannelData          => "CHANNEL_DATA"             [channel],
    General              => "GENERAL"                  [],
    Command              => "COMMAND"                  [],
    SessionHeartbeat     => "SESSION_HEARTBEAT"        [system],
    ClientDisconnected   => "CLIENT_DISCONNECTED"      [],
    ServerDisconnected   => "SERVER_DISCONNECTED"      [],
    SendInfo             => "SEND_INFO"                [],
    RecvInfo             => "RECV_INFO"                [],
    RecvRtcpMessage      => "RECV_RTCP_MESSAGE"        [in_call],
    SendRtcpMessage      => "SEND_RTCP_MESSAGE"        [in_call],
    CallSecure           => "CALL_SECURE"              [in_call],
    Nat                  => "NAT"                      [],
    RecordStart          => "RECORD_START"             [media],
    RecordStop           => "RECORD_STOP"              [media],
    PlaybackStart        => "PLAYBACK_START"           [media],
    PlaybackStop         => "PLAYBACK_STOP"            [media],
    CallUpdate           => "CALL_UPDATE"              [in_call],
    Failure              => "FAILURE"                  [],
    SocketData           => "SOCKET_DATA"              [],
    MediaBugStart        => "MEDIA_BUG_START"          [media],
    MediaBugStop         => "MEDIA_BUG_STOP"           [media],
    ConferenceDataQuery  => "CONFERENCE_DATA_QUERY"    [conference],
    ConferenceData       => "CONFERENCE_DATA"          [conference],
    CallSetupReq         => "CALL_SETUP_REQ"           [],
    CallSetupResult      => "CALL_SETUP_RESULT"        [],
    CallDetail           => "CALL_DETAIL"              [],
    DeviceState          => "DEVICE_STATE"             [],
    Text                 => "TEXT"                     [],
    ShutdownRequested    => "SHUTDOWN_REQUESTED"       [system],
    /// Subscribe to all events
    All                  => "ALL"                      [];
    // --- Not in libs/esl/ EVENT_NAMES[], only in switch_event.c ---
    // check-event-types.sh stops scanning at the All variant above.
    /// Present in `switch_event.c` but not in `libs/esl/` EVENT_NAMES[].
    StartRecording => "START_RECORDING" [media],
}

parse_error! { ParseEventTypeError("event type"); }

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_notify_in_parse() {
        assert_eq!(
            EslEventType::parse_event_type("NOTIFY_IN"),
            Some(EslEventType::NotifyIn)
        );
        assert_eq!(EslEventType::parse_event_type("notify_in"), None);
    }

    #[test]
    fn test_notify_in_display() {
        assert_eq!(EslEventType::NotifyIn.to_string(), "NOTIFY_IN");
    }

    #[test]
    fn test_notify_in_distinct_from_notify() {
        assert_ne!(EslEventType::Notify, EslEventType::NotifyIn);
        assert_ne!(
            EslEventType::Notify.to_string(),
            EslEventType::NotifyIn.to_string()
        );
    }

    #[test]
    fn test_wire_names_match_c_esl() {
        assert_eq!(
            EslEventType::ChannelOutgoing.to_string(),
            "CHANNEL_OUTGOING"
        );
        assert_eq!(EslEventType::Api.to_string(), "API");
        assert_eq!(EslEventType::ReloadXml.to_string(), "RELOADXML");
        assert_eq!(EslEventType::PresenceIn.to_string(), "PRESENCE_IN");
        assert_eq!(EslEventType::Roster.to_string(), "ROSTER");
        assert_eq!(EslEventType::Text.to_string(), "TEXT");
        assert_eq!(EslEventType::ReSchedule.to_string(), "RE_SCHEDULE");

        assert_eq!(
            EslEventType::parse_event_type("CHANNEL_OUTGOING"),
            Some(EslEventType::ChannelOutgoing)
        );
        assert_eq!(
            EslEventType::parse_event_type("API"),
            Some(EslEventType::Api)
        );
        assert_eq!(
            EslEventType::parse_event_type("RELOADXML"),
            Some(EslEventType::ReloadXml)
        );
        assert_eq!(
            EslEventType::parse_event_type("PRESENCE_IN"),
            Some(EslEventType::PresenceIn)
        );
    }

    #[test]
    fn test_event_type_from_str() {
        assert_eq!(
            "CHANNEL_ANSWER".parse::<EslEventType>(),
            Ok(EslEventType::ChannelAnswer)
        );
        assert!("channel_answer"
            .parse::<EslEventType>()
            .is_err());
        assert!("UNKNOWN_EVENT"
            .parse::<EslEventType>()
            .is_err());
    }
}