test_rpdu.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * Copyright (C) 2009 Mamadou Diop.
  3. *
  4. * Contact: Mamadou Diop <diopmamadou(at)doubango.org>
  5. *
  6. * This file is part of Open Source Doubango Framework.
  7. *
  8. * DOUBANGO is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * DOUBANGO is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with DOUBANGO.
  20. *
  21. */
  22. #ifndef _TEST_SMSRPDU_H
  23. #define _TEST_SMSRPDU_H
  24. void test_i_rpdata(const void* data, tsk_size_t size, tsk_bool_t MobOrig)
  25. {
  26. tsms_rpdu_message_t* rp_message = tsk_null;
  27. tsms_tpdu_message_t* tpdu = tsk_null;
  28. if(!(rp_message = tsms_rpdu_message_deserialize(data, size))) {
  29. TSK_DEBUG_ERROR("Failed to deserialize the RP-MESSAGE");
  30. goto bail;
  31. }
  32. switch(rp_message->mti) {
  33. case tsms_rpdu_type_data_mo:
  34. case tsms_rpdu_type_data_mt: {
  35. char* ascii = tsk_null;
  36. tsms_rpdu_data_t* rp_data = TSMS_RPDU_DATA(rp_message);
  37. if((tpdu = tsms_tpdu_message_deserialize(rp_data->udata->data, rp_data->udata->size, MobOrig))) {
  38. if(tpdu->mti == tsms_tpdu_mti_deliver_mt || tpdu->mti == tsms_tpdu_mti_submit_mo) { /* SMS-SUBMIT or SMS-DELIVER? */
  39. if((ascii = tsms_tpdu_message_get_payload(tpdu))) {
  40. TSK_DEBUG_INFO("ASCII message=%s", ascii);
  41. TSK_FREE(ascii);
  42. }
  43. }
  44. }
  45. break;
  46. }
  47. case tsms_rpdu_type_ack_mo:
  48. case tsms_rpdu_type_ack_mt: {
  49. tsms_rpdu_ack_t* rp_ack = TSMS_RPDU_ACK(rp_message);
  50. // ...do whatever you want
  51. if(rp_ack->udata && (tpdu = tsms_tpdu_message_deserialize(rp_ack->udata->data, rp_ack->udata->size, MobOrig))) {
  52. // ...do whatever you want
  53. }
  54. TSK_DEBUG_INFO("RP-ACK");
  55. break;
  56. }
  57. case tsms_rpdu_type_error_mo:
  58. case tsms_rpdu_type_error_mt: {
  59. tsms_rpdu_error_t* rp_error = TSMS_RPDU_ERROR(rp_message);
  60. // ...do whatever you want
  61. if(rp_error->udata && (tpdu = tsms_tpdu_message_deserialize(rp_error->udata->data, rp_error->udata->size, MobOrig))) {
  62. // ...do whatever you want
  63. }
  64. TSK_DEBUG_INFO("RP-ERROR");
  65. break;
  66. }
  67. case tsms_rpdu_type_smma_mo: {
  68. tsms_rpdu_smma_t* rp_smma = TSMS_RPDU_SMMA(rp_message);
  69. // ...do whatever you want
  70. TSK_DEBUG_INFO("RP-SMMA");
  71. break;
  72. }
  73. default: {
  74. TSK_DEBUG_INFO("Unknown RP-Message type (%u).", rp_message->mti);
  75. break;
  76. }
  77. }
  78. bail:
  79. TSK_OBJECT_SAFE_FREE(rp_message);
  80. TSK_OBJECT_SAFE_FREE(tpdu);
  81. }
  82. void test_o_rpdata_submit()
  83. {
  84. //== Sending RP-DATA(SMS-SUBMIT) ==
  85. int ret;
  86. tsk_buffer_t* buffer = tsk_null;
  87. tsms_tpdu_submit_t* sms_submit = tsk_null;
  88. tsms_rpdu_data_t* rp_data = tsk_null;
  89. const char* smsc = "+331000009";
  90. const char* destination = "+333361234567";
  91. const char* short_message = "hello world";
  92. uint8_t mr = 0xF5;
  93. uint8_t message_number = 0xF8;
  94. char* hex;
  95. // create SMS-SUBMIT message
  96. sms_submit = tsms_tpdu_submit_create(mr, smsc, destination);
  97. // Set content for SMS-SUBMIT
  98. if((buffer = tsms_pack_to_7bit(short_message))) {
  99. ret = tsms_tpdu_submit_set_userdata(sms_submit, buffer, tsms_alpha_7bit);
  100. TSK_OBJECT_SAFE_FREE(buffer);
  101. }
  102. // create RP-DATA message and print its content (for test only)
  103. rp_data = tsms_rpdu_data_create_mo(mr, smsc, TSMS_TPDU_MESSAGE(sms_submit));
  104. if((hex = tsms_rpdu_message_tohexastring(TSMS_RPDU_MESSAGE(rp_data)))) {
  105. TSK_DEBUG_INFO("RP-DATA=%s", hex);
  106. TSK_FREE(hex);
  107. }
  108. // serialize
  109. buffer = tsk_buffer_create_null();
  110. ret = tsms_rpdu_data_serialize(rp_data, buffer);
  111. // send(socket, buffer->data, buffer->size);
  112. // print result (hex) to the console
  113. printhex("==RP-DATA(SMS-SUBMIT):", buffer->data, buffer->size);
  114. // receiving
  115. test_i_rpdata(buffer->data, buffer->size, tsk_true);
  116. TSK_OBJECT_SAFE_FREE(buffer);
  117. TSK_OBJECT_SAFE_FREE(sms_submit);
  118. TSK_OBJECT_SAFE_FREE(rp_data);
  119. }
  120. void test_o_rpdata_deliver()
  121. {
  122. //== Sending RP-DATA(SMS-DELIVER) ==
  123. int ret;
  124. tsk_buffer_t* buffer = tsk_null;
  125. tsms_tpdu_deliver_t* sms_deliver = tsk_null;
  126. tsms_rpdu_data_t* rp_data = tsk_null;
  127. const char* smsc = "+331000000";
  128. const char* originator = "+3361234567";
  129. const char* content = "hello world!";
  130. // create SMS-DELIVER message
  131. sms_deliver = tsms_tpdu_deliver_create(smsc, originator);
  132. // Set content for SMS-DELIVER
  133. if((buffer = tsms_pack_to_7bit(content))) {
  134. ret = tsms_tpdu_deliver_set_userdata(sms_deliver, buffer, tsms_alpha_7bit);
  135. TSK_OBJECT_SAFE_FREE(buffer);
  136. }
  137. // create RP-DATA message
  138. rp_data = tsms_rpdu_data_create_mt(0x01, smsc, TSMS_TPDU_MESSAGE(sms_deliver));
  139. // serialize
  140. buffer = tsk_buffer_create_null();
  141. ret = tsms_rpdu_data_serialize(rp_data, buffer);
  142. // send(socket, buffer->data, buffer->size);
  143. // print result (hex) to the console
  144. printhex("==RP-DATA(SMS-DELIVER):", buffer->data, buffer->size);
  145. // receiving
  146. test_i_rpdata(buffer->data, buffer->size, tsk_false);
  147. TSK_OBJECT_SAFE_FREE(buffer);
  148. TSK_OBJECT_SAFE_FREE(sms_deliver);
  149. TSK_OBJECT_SAFE_FREE(rp_data);
  150. }
  151. void test_o_rpdata_smma()
  152. {
  153. //== Sending RP-SMMA ==
  154. int ret;
  155. tsk_buffer_t* buffer = tsk_null;
  156. tsms_rpdu_smma_t* rp_smma = tsk_null;
  157. uint8_t mr = 0xF5;
  158. // create RP-SMMA message
  159. rp_smma = tsms_rpdu_smma_create(mr);
  160. // serialize
  161. buffer = tsk_buffer_create_null();
  162. ret = tsms_rpdu_data_serialize(rp_smma, buffer);
  163. // send(socket, buffer->data, buffer->size);
  164. // print result (hex) to the console
  165. printhex("==RP-SMMA:", buffer->data, buffer->size);
  166. // receiving
  167. test_i_rpdata(buffer->data, buffer->size, tsk_true);
  168. TSK_OBJECT_SAFE_FREE(buffer);
  169. TSK_OBJECT_SAFE_FREE(rp_smma);
  170. }
  171. void test_o_rpdata_ack()
  172. {
  173. //== Sending RP-ACK(SMS-DELIVER-REPORT) ==
  174. int ret;
  175. tsk_buffer_t* buffer = tsk_null;
  176. tsms_tpdu_report_t* sms_report = tsk_null;
  177. tsms_rpdu_ack_t* rp_ack= tsk_null;
  178. const char* smsc = "+331000000";
  179. tsk_bool_t isSUBMIT = tsk_false; /* isDELIVER */
  180. tsk_bool_t isERROR = tsk_false;
  181. uint8_t mr = 0xF5;
  182. // create SMS-DELIVER-REPORT message
  183. sms_report = tsms_tpdu_report_create(smsc, isSUBMIT, isERROR);
  184. // create RP-ACK message (From MS to SC)
  185. rp_ack = tsms_rpdu_ack_create_mo(mr, TSMS_TPDU_MESSAGE(sms_report));
  186. // serialize
  187. buffer = tsk_buffer_create_null();
  188. if(!(ret = tsms_rpdu_data_serialize(rp_ack, buffer))) {
  189. // send(socket, buffer->data, buffer->size);
  190. // print result (hex) to the console
  191. printhex("==RP-ACK(SMS-DELIVER-REPORT):", buffer->data, buffer->size);
  192. }
  193. // receiving
  194. test_i_rpdata(buffer->data, buffer->size, tsk_true);
  195. TSK_OBJECT_SAFE_FREE(buffer);
  196. TSK_OBJECT_SAFE_FREE(sms_report);
  197. TSK_OBJECT_SAFE_FREE(rp_ack);
  198. }
  199. void test_o_rpdata_error()
  200. {
  201. //== Sending RP-ERROR(SMS-DELIVER-REPORT) ==
  202. int ret;
  203. tsk_buffer_t* buffer = tsk_null;
  204. tsms_tpdu_report_t* sms_report = tsk_null;
  205. tsms_rpdu_error_t* rp_error= tsk_null;
  206. tsk_bool_t isSUBMIT = tsk_false; /* isDELIVER */
  207. tsk_bool_t isERROR = tsk_true;
  208. const char* smsc = "+331000000";
  209. uint8_t mr = 0xF5;
  210. // create SMS-DELIVER-REPORT message
  211. sms_report = tsms_tpdu_report_create(smsc, isSUBMIT, isERROR);
  212. // create RP-ERROR message
  213. rp_error = tsms_rpdu_error_create_mo(mr, TSMS_TPDU_MESSAGE(sms_report), 0x0A/*call barred*/);
  214. // serialize
  215. buffer = tsk_buffer_create_null();
  216. if(!(ret = tsms_rpdu_data_serialize(rp_error, buffer))) {
  217. // send(socket, buffer->data, buffer->size);
  218. // print result (hex) to the console
  219. printhex("==RP-ERROR(SMS-DELIVER-REPORT):", buffer->data, buffer->size);
  220. }
  221. // receiving
  222. test_i_rpdata(buffer->data, buffer->size, tsk_true);
  223. TSK_OBJECT_SAFE_FREE(buffer);
  224. TSK_OBJECT_SAFE_FREE(sms_report);
  225. TSK_OBJECT_SAFE_FREE(rp_error);
  226. }
  227. void test_rpdu()
  228. {
  229. test_o_rpdata_submit();
  230. //test_o_rpdata_deliver();
  231. //test_o_rpdata_smma();
  232. //test_o_rpdata_ack();
  233. //test_o_rpdata_error();
  234. //const char* data = "\x03\x01\x41\x09\x01\x00\x01\x80\x01\x32\x42\x00\x69";
  235. //test_i_rpdata(data, 13, tsk_false);
  236. }
  237. #endif /* _TEST_SMSRPDU_H */