--- /dev/null
+/* \r
+ * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application - mod_managed\r
+ * Copyright (C) 2008, Michael Giagnocavo <mgg@giagnocavo.net>\r
+ *\r
+ * Version: MPL 1.1\r
+ *\r
+ * The contents of this file are subject to the Mozilla Public License Version\r
+ * 1.1 (the "License"); you may not use this file except in compliance with\r
+ * the License. You may obtain a copy of the License at\r
+ * http://www.mozilla.org/MPL/\r
+ *\r
+ * Software distributed under the License is distributed on an "AS IS" basis,\r
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License\r
+ * for the specific language governing rights and limitations under the\r
+ * License.\r
+ *\r
+ * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application - mod_managed\r
+ *\r
+ * The Initial Developer of the Original Code is\r
+ * Michael Giagnocavo <mgg@giagnocavo.net>\r
+ * Portions created by the Initial Developer are Copyright (C)\r
+ * the Initial Developer. All Rights Reserved.\r
+ *\r
+ * Contributor(s):\r
+ * \r
+ * Michael Giagnocavo <mgg@giagnocavo.net>\r
+ * \r
+ * ChannelVariables.cs -- Strongly typed channel variables for ManagedSession\r
+ *\r
+ */\r
+using System;\r
+using System.Collections.Generic;\r
+using System.Linq;\r
+using System.Text;\r
+using System.Runtime.InteropServices;\r
+\r
+namespace FreeSWITCH.Native {\r
+ public partial class ManagedSession {\r
+\r
+ // Need to find a better place to put these - then make them public\r
+ static readonly DateTime epoch = new DateTime(1970, 1, 1);\r
+ static DateTime epochUsToDateTime(long us){\r
+ return us == 0 ? \r
+ DateTime.MinValue :\r
+ epoch.AddMilliseconds((double)((decimal)us / 1000m));\r
+ }\r
+ static bool strToBool(string s) {\r
+ if (string.IsNullOrEmpty(s)) return false;\r
+ switch (s.ToLowerInvariant()) {\r
+ case "true":\r
+ case "yes":\r
+ case "on":\r
+ case "enable":\r
+ case "enabled":\r
+ case "active":\r
+ case "allow":\r
+ return true;\r
+ default:\r
+ // Numbers are true\r
+ long tmp;\r
+ return long.TryParse(s, out tmp);\r
+ }\r
+ }\r
+ static string boolToStr(bool b) {\r
+ return b ? "true" : "false";\r
+ }\r
+\r
+ ChannelVariables _variables; // Set on ManagedSession init\r
+ public ChannelVariables Variables {\r
+ get {\r
+ if (_variables == null) {\r
+ _variables = new ChannelVariables(this);\r
+ }\r
+ return _variables;\r
+ }\r
+ }\r
+\r
+ /// <summary>Strongly typed access to common variables</summary>\r
+ public class ChannelVariables {\r
+ readonly ManagedSession sess;\r
+ internal ChannelVariables(ManagedSession session) {\r
+ this.sess = session;\r
+ }\r
+\r
+ public IDictionary<string, string> GetAllVariables() {\r
+ var dic = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);\r
+ var evt = Native.freeswitch.switch_channel_variable_first(sess.channel);\r
+ while(evt != null) {\r
+ dic.Add(evt.name, evt.value);\r
+ evt = evt.next;\r
+ }\r
+ Native.freeswitch.switch_channel_variable_last(sess.channel);\r
+ return dic;\r
+ }\r
+\r
+ /*** Settings ***/\r
+ const string bypass_media = "bypass_media";\r
+ public bool BypassMedia {\r
+ get { return strToBool(sess.GetVariable(bypass_media)); }\r
+ set { sess.SetVariable(bypass_media, boolToStr(value)); }\r
+ }\r
+\r
+ /*** Times ***/\r
+ const string created_time = "created_time";\r
+ const string answered_time = "answered_time";\r
+ const string hangup_time = "hangup_time";\r
+ const string progress_time = "progress_time";\r
+ const string transfer_time = "transfer_time";\r
+\r
+ public DateTime CreatedTime {\r
+ get { return epochUsToDateTime(long.Parse(sess.GetVariable(created_time))); }\r
+ }\r
+\r
+ DateTime? readUsecsDateTime(string varName) {\r
+ var v = sess.GetVariable(varName);\r
+ if (string.IsNullOrEmpty(v)) return null;\r
+ else {\r
+ var d = epochUsToDateTime(long.Parse(v));\r
+ if (d == DateTime.MinValue) return null;\r
+ return d;\r
+ }\r
+ }\r
+\r
+ public DateTime? AnsweredTime {\r
+ get { return readUsecsDateTime(answered_time); }\r
+ }\r
+ public DateTime? HangupTime {\r
+ get { return readUsecsDateTime(hangup_time); }\r
+ }\r
+ public DateTime? ProgressTime {\r
+ get { return readUsecsDateTime(progress_time); }\r
+ }\r
+ public DateTime? TransferTime {\r
+ get { return readUsecsDateTime(transfer_time); }\r
+ }\r
+\r
+ \r
+ /*** SIP Variables ***/\r
+ const string sofia_profile_name = "sofia_profile_name";\r
+ const string sip_received_ip = "sip_received_ip";\r
+ const string sip_received_port = "sip_received_port";\r
+ const string sip_via_protocol = "sip_via_protocol";\r
+ const string sip_from_user = "sip_from_user";\r
+ const string sip_from_uri = "sip_from_uri";\r
+ const string sip_from_host = "sip_from_host";\r
+ const string sip_req_user = "sip_req_user";\r
+ const string sip_req_uri = "sip_req_uri";\r
+ const string sip_req_host = "sip_req_host";\r
+ const string sip_to_user = "sip_to_user";\r
+ const string sip_to_uri = "sip_to_uri";\r
+ const string sip_to_host = "sip_to_host";\r
+ const string sip_contact_user = "sip_contact_user";\r
+ const string sip_contact_port = "sip_contact_port";\r
+ const string sip_contact_uri = "sip_contact_uri";\r
+ const string sip_contact_host = "sip_contact_host";\r
+ const string sip_call_id = "sip_call_id";\r
+ const string sip_destination_url = "sip_destination_url";\r
+ const string sip_term_status = "sip_term_status";\r
+ const string sip_term_cause = "sip_term_status";\r
+ const string switch_r_sdp = "switch_r_sdp";\r
+ const string switch_m_sdp = "switch_m_sdp";\r
+ const string sip_hangup_phrase = "sip_hangup_phrase";\r
+\r
+\r
+ short? readShort(string varName) {\r
+ var s = sess.GetVariable(varName);\r
+ if (string.IsNullOrEmpty(s)) return null;\r
+ short res;\r
+ if (short.TryParse(s, out res)) return res;\r
+ else return null;\r
+ }\r
+ int? readInt(string varName) {\r
+ var s = sess.GetVariable(varName);\r
+ if (string.IsNullOrEmpty(s)) return null;\r
+ int res;\r
+ if (int.TryParse(s, out res)) return res;\r
+ else return null;\r
+ }\r
+\r
+ // String suffix is added when the var is better represented as \r
+ // a different data type, but for now we return string\r
+\r
+ public string SofiaProfileName { get { return sess.GetVariable(sofia_profile_name); } }\r
+ public string SipCallID { get { return sess.GetVariable(sip_call_id); } }\r
+ public string SipReceivedIP { get { return sess.GetVariable(sip_received_ip); } }\r
+ public short? SipReceivedPort { get { return readShort(sip_received_port); } }\r
+ public string SipViaProtocolString { get { return sess.GetVariable(sip_via_protocol); } }\r
+ public string SipFromUser { get { return sess.GetVariable(sip_from_user); } }\r
+ public string SipFromUriString { get { return sess.GetVariable(sip_from_uri); } }\r
+ public string SipFromHost { get { return sess.GetVariable(sip_from_host); } }\r
+ public string SipReqUser { get { return sess.GetVariable(sip_req_user); } }\r
+ public string SipReqUriString { get { return sess.GetVariable(sip_req_uri); } }\r
+ public string SipReqHost { get { return sess.GetVariable(sip_req_host); } }\r
+ public string SipToUser { get { return sess.GetVariable(sip_to_user); } }\r
+ public string SipToUriString { get { return sess.GetVariable(sip_to_uri); } }\r
+ public string SipToHost { get { return sess.GetVariable(sip_to_host); } }\r
+ public string SipContactUser { get { return sess.GetVariable(sip_contact_user); } }\r
+ public string SipContactUriString { get { return sess.GetVariable(sip_contact_uri); } }\r
+ public string SipContactHost { get { return sess.GetVariable(sip_contact_host); } }\r
+ public short? SipContactPort { get { return readShort(sip_contact_port); } }\r
+ public string SipDestinationUrlString { get { return sess.GetVariable(sip_destination_url); } }\r
+ public int? SipTermStatus { get { return readInt(sip_term_status); } }\r
+ public int? SipTermCause { get { return readInt(sip_term_cause); } }\r
+ public string SwitchRSdp { get { return sess.GetVariable(switch_r_sdp); } }\r
+ public string SwitchMSdp { get { return sess.GetVariable(switch_m_sdp); } }\r
+ public string SipHangupPhrase { get { return sess.GetVariable(sip_hangup_phrase); } }\r
+\r
+ /*** Other ***/\r
+\r
+ const string proto_specific_hangup_cause = "proto_specific_hangup_cause";\r
+ const string hangup_cause = "hangup_cause";\r
+ const string hangup_cause_q850 = "hangup_cause_q850";\r
+ const string originate_disposition = "originate_disposition";\r
+ const string direction = "direction";\r
+\r
+ public string ProtoSpecificHangupCause { get { return sess.GetVariable(proto_specific_hangup_cause); } }\r
+ public string HangupCauseString { get { return sess.GetVariable(hangup_cause); } }\r
+ public int? HangupCauseQ850 { get { return readInt(hangup_cause_q850); } }\r
+ public string OriginateDispositionString { get { return sess.GetVariable(originate_disposition); } }\r
+\r
+ public Native.switch_call_direction_t CallDirection {\r
+ get {\r
+ var s = sess.GetVariable(direction);\r
+ if (string.IsNullOrEmpty(s)) return switch_call_direction_t.SWITCH_CALL_DIRECTION_INBOUND; // I guess\r
+ return s.ToLowerInvariant() == "inbound" ? switch_call_direction_t.SWITCH_CALL_DIRECTION_INBOUND : switch_call_direction_t.SWITCH_CALL_DIRECTION_OUTBOUND;\r
+ }\r
+\r
+ }\r
+ }\r
+ }\r
+}
\ No newline at end of file