Form1.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. namespace test
  9. {
  10. public partial class Form1 : Form
  11. {
  12. /*const String REALM = "ims.inexbee.com";
  13. const String USER = "mamadou";
  14. const String PASSWORD = "mamadou";
  15. const String PROXY_CSCF_IP = "192.168.16.225";
  16. const uint PROXY_CSCF_PORT = 4060;*/
  17. const String REALM = "micromethod.com";
  18. const String USER = "mamadou";
  19. const String PASSWORD = "mamadou";
  20. const String PROXY_CSCF_IP = "192.168.16.104";
  21. const uint PROXY_CSCF_PORT = 5060;
  22. RegistrationSession regSession = null;
  23. PublicationSession pubSession = null;
  24. SubscriptionSession subSession = null;
  25. MyCallback callback = null;
  26. SipStack stack = null;
  27. public delegate void PrintDebug(String text);
  28. public PrintDebug mydel;
  29. const String PUBLISH_PAYLOAD = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
  30. "<presence xmlns:cp=\"urn:ietf:params:xml:ns:pidf:cipid\" xmlns:caps=\"urn:ietf:params:xml:ns:pidf:caps\" xmlns:rpid=\"urn:ietf:params:xml:ns:pidf:rpid\" xmlns:pdm=\"urn:ietf:params:xml:ns:pidf:data-model\" xmlns:p=\"urn:ietf:params:xml:ns:pidf-diff\" xmlns:op=\"urn:oma:xml:prs:pidf:oma-pres\" entity=\"sip:bob@ims.inexbee.com\" xmlns=\"urn:ietf:params:xml:ns:pidf\">" +
  31. "<pdm:person id=\"RPVRYNJH\">" +
  32. "<op:overriding-willingness>" +
  33. "<op:basic>open</op:basic>" +
  34. "</op:overriding-willingness>" +
  35. "<rpid:activities>" +
  36. "<rpid:busy />" +
  37. "</rpid:activities>" +
  38. "<rpid:mood>" +
  39. "<rpid:guilty />" +
  40. "</rpid:mood>" +
  41. "<cp:homepage>http://doubango.org</cp:homepage>" +
  42. "<pdm:note>Come share with me RCS Experience</pdm:note>" +
  43. "</pdm:person>" +
  44. "<pdm:device id=\"d0001\">" +
  45. "<status>" +
  46. "<basic>open</basic>" +
  47. "</status>" +
  48. "<caps:devcaps>" +
  49. "<caps:mobility>" +
  50. "<caps:supported>" +
  51. "<caps:fixed />" +
  52. "</caps:supported>" +
  53. "</caps:mobility>" +
  54. "</caps:devcaps>" +
  55. "<op:network-availability>" +
  56. "<op:network id=\"IMS\">" +
  57. "<op:active />" +
  58. "</op:network>" +
  59. "</op:network-availability>" +
  60. "<pdm:deviceID>urn:uuid:3ca50bcb-7a67-44f1-afd0-994a55f930f4</pdm:deviceID>" +
  61. "</pdm:device>" +
  62. "</presence>";
  63. public Form1()
  64. {
  65. InitializeComponent();
  66. bool success;
  67. mydel = new PrintDebug(PrintDebugMethod);
  68. /* Create call back */
  69. callback = new MyCallback(this);
  70. /* Create and configure the IMS/LTE stack */
  71. // stack = new SipStack(callback, String.Format("sip:{0}", REALM), String.Format("{0}@{1}", USER, REALM), String.Format("sip:{0}@{1}", USER, REALM));
  72. stack = new SipStack(callback, String.Format("sip:{0}", REALM), String.Format("{0}@{1}", USER, REALM), String.Format("sip:{0}@{1}", USER, REALM));
  73. if (!stack.isValid())
  74. {
  75. this.textBoxDebug.Text = "Invalid stack";
  76. return;
  77. }
  78. stack.addHeader("Allow", "INVITE, ACK, CANCEL, BYE, MESSAGE, OPTIONS, NOTIFY, PRACK, UPDATE, REFER");
  79. stack.addHeader("Privacy", "header; id");
  80. stack.addHeader("P-Access-Network-Info", "ADSL;utran-cell-id-3gpp=00000000");
  81. stack.addHeader("User-Agent", "IM-client/OMA1.0 doubango/v1.0.0");
  82. /* set password */
  83. success = stack.setPassword(PASSWORD);
  84. /* Sets Proxy-CSCF */
  85. success = stack.setProxyCSCF(PROXY_CSCF_IP, PROXY_CSCF_PORT, "udp", "ipv4");
  86. /* Starts the stack */
  87. success = stack.start();
  88. }
  89. private void buttonRegister_Click(object sender, EventArgs e)
  90. {
  91. if (this.regSession == null)
  92. {
  93. this.regSession = new RegistrationSession(stack);
  94. this.regSession.addCaps("+g.oma.sip-im");
  95. this.regSession.addCaps("+g.3gpp.smsip");
  96. this.regSession.addCaps("language", "\"en,fr\"");
  97. this.regSession.setExpires(30);
  98. }
  99. this.regSession.Register();
  100. }
  101. private void buttonUnRegister_Click(object sender, EventArgs e)
  102. {
  103. if (this.regSession != null)
  104. {
  105. this.regSession.UnRegister();
  106. }
  107. }
  108. private void buttonPublish_Click(object sender, EventArgs e)
  109. {
  110. if (this.pubSession == null)
  111. {
  112. this.pubSession = new PublicationSession(stack);
  113. this.pubSession.addHeader("Content-Type", "application/pidf+xml");
  114. this.pubSession.addHeader("Event", "presence");
  115. this.pubSession.setExpires(30);
  116. byte[] payload = Encoding.UTF8.GetBytes(PUBLISH_PAYLOAD);
  117. this.pubSession.Publish(payload, (uint)payload.Length);
  118. }
  119. }
  120. private void buttonUnPublish_Click(object sender, EventArgs e)
  121. {
  122. if (this.pubSession != null)
  123. {
  124. this.pubSession.UnPublish();
  125. }
  126. }
  127. void PrintDebugMethod(String text)
  128. {
  129. this.textBoxDebug.Text = text;
  130. }
  131. private void buttonSub_Click(object sender, EventArgs e)
  132. {
  133. if (this.subSession == null)
  134. {
  135. this.subSession = new SubscriptionSession(stack);
  136. this.subSession.addHeader("Accept", "application/reginfo+xml");
  137. this.subSession.addHeader("Event", "reg");
  138. this.subSession.setExpires(30);
  139. this.subSession.Subscribe();
  140. }
  141. }
  142. }
  143. public class MyCallback : SipCallback
  144. {
  145. Form1 form;
  146. public MyCallback(Form1 form)
  147. : base()
  148. {
  149. this.form = form;
  150. }
  151. public override int OnRegistrationEvent(RegistrationEvent e)
  152. {
  153. short code = e.getCode();
  154. tsip_register_event_type_t type = e.getType();
  155. String text;
  156. RegistrationSession session = e.getSession();
  157. switch (type)
  158. {
  159. case tsip_register_event_type_t.tsip_i_register:
  160. case tsip_register_event_type_t.tsip_ao_register:
  161. case tsip_register_event_type_t.tsip_i_unregister:
  162. case tsip_register_event_type_t.tsip_ao_unregister:
  163. break;
  164. }
  165. text = String.Format("OnRegistrationChanged() ==> {0}:{1}", code, e.getPhrase());
  166. if (this.form.InvokeRequired)
  167. {
  168. this.form.Invoke(this.form.mydel, new object[] { text });
  169. }
  170. return 0;
  171. }
  172. public override int OnSubscriptionEvent(SubscriptionEvent e)
  173. {
  174. switch (e.getType())
  175. {
  176. case tsip_subscribe_event_type_t.tsip_i_notify:
  177. String ev = e.getSipMessage().getSipHeaderValue("Event");
  178. Console.WriteLine("Event=%s", ev);
  179. break;
  180. default:
  181. break;
  182. }
  183. return base.OnSubscriptionEvent(e);
  184. }
  185. public override int OnPublicationEvent(PublicationEvent e)
  186. {
  187. short code = e.getCode();
  188. tsip_publish_event_type_t type = e.getType();
  189. String text;
  190. PublicationSession session = e.getSession();
  191. switch (type){
  192. default:
  193. break;
  194. }
  195. text = String.Format("OnPublicationChanged() ==> {0}:{1}", code, e.getPhrase());
  196. if (this.form.InvokeRequired)
  197. {
  198. this.form.Invoke(this.form.mydel, new object[] { text });
  199. }
  200. return 0;
  201. }
  202. }
  203. }