cal_core/device/
sip_gateway.rs

1use std::fmt::{Debug, Formatter};
2use serde::{Serialize, Deserialize};
3use cal_jambonz::listen::Listen;
4use crate::{NumberFormat, VoiceServer, Region, FlowState, ConnectState};
5use std::sync::Arc;
6use crate::device::device::{Connector, EndpointDevice};
7use crate::device::shared::{AiOptions, CallControl, RecordOptions, TranscribeOptions};
8
9#[derive(Serialize, Deserialize, Clone)]
10#[serde(rename_all = "camelCase")]
11pub struct SipGateway {
12    #[serde(default = "crate::device::shared::build_format")]
13    pub format: NumberFormat,
14    #[serde(default)]
15    pub trunks: Vec<String>,
16    #[serde(default)]
17    pub proxies: Vec<String>,
18    #[serde(default = "crate::device::shared::build_present")]
19    pub present: String,
20    #[serde(default = "crate::device::shared::build_country_code")]
21    pub country_code: String,
22    #[serde(default = "crate::device::shared::build_connect_to")]
23    pub on_complete: String,
24    #[serde(default = "crate::device::shared::build_connect_to")]
25    pub on_busy: String,
26    #[serde(default = "crate::device::shared::build_connect_to")]
27    pub on_fail: String,
28    #[serde(default = "crate::device::shared::build_connect_to")]
29    pub on_no_answer: String,
30    #[serde(default = "crate::device::shared::build_ring_time")]
31    pub ring_time: u8,
32    #[serde(default = "crate::device::shared::build_call_time")]
33    pub max_call_time: u16,
34    #[serde(default)]
35    pub transcribe_options: TranscribeOptions,
36    #[serde(default)]
37    pub record_options: RecordOptions,
38    #[serde(default)]
39    pub ai_options: AiOptions,
40    #[serde(default)]
41    pub call_control: CallControl,
42}
43
44impl Debug for SipGateway {
45    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
46        f.debug_struct("SipGateway")
47            .field("format", &self.format)
48            .field("trunks", &self.trunks)
49            .field("proxies", &self.proxies)
50            .field("present", &self.present)
51            .field("country_code", &self.country_code)
52            .field("on_complete", &self.on_complete)
53            .field("on_busy", &self.on_busy)
54            .field("on_fail", &self.on_fail)
55            .field("on_no_answer", &self.on_no_answer)
56            .field("ring_time", &self.ring_time)
57            .field("max_call_time", &self.max_call_time)
58            .field("transcribe_options", &self.transcribe_options)
59            .field("record_options", &self.record_options)
60            .field("ai_options", &self.ai_options)
61            .field("call_control", &self.call_control)
62            .finish()
63    }
64}
65
66impl Connector for SipGateway {
67    fn get_connect_to(&mut self, state: &mut FlowState) -> ConnectState {
68        match &state.get_dial_status() {
69            Some(status) => match status {
70                cal_jambonz::shared::shared::SIPStatus::Ok => {
71                    ConnectState::complete(self.on_complete.as_str())
72                }
73                cal_jambonz::shared::shared::SIPStatus::RequestTerminated => {
74                    ConnectState::no_answer(self.on_no_answer.as_str())
75                }
76                cal_jambonz::shared::shared::SIPStatus::BusyHere | cal_jambonz::shared::shared::SIPStatus::BusyEverywhere => {
77                    ConnectState::busy(self.on_busy.as_str())
78                }
79                _ => ConnectState::fail(self.on_fail.as_str()),
80            },
81            None => ConnectState::dialling(),
82        }
83    }
84}
85
86impl EndpointDevice for SipGateway {
87    fn device_type_name(&self) -> &'static str {
88        "SipGateway"
89    }
90
91    fn supports_routing(&self) -> bool {
92        true
93    }
94}
95
96impl crate::device::device::Endpoint for SipGateway {
97    fn get_listen(&self, _state: &FlowState) -> Option<Listen> {
98        None
99    }
100
101    fn get_proxy(&self, _state: &FlowState, _regions: Vec<Arc<Region>>) -> Option<VoiceServer> {
102        None
103    }
104}