SMSEncoder.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * Copyright (C) 2009 Mamadou Diop.
  3. *
  4. * Contact: Mamadou Diop <diopmamadou@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. #include "SMSEncoder.h"
  23. // Short description: http://betelco.blogspot.com/2009/10/sms-over-3gpp-ims-network.html
  24. RPMessage::RPMessage(twrap_rpmessage_type_t _type, tsms_rpdu_message_t* _rp_message)
  25. {
  26. this->rp_message = (tsms_rpdu_message_t*)tsk_object_ref(_rp_message);
  27. this->type = _type;
  28. this->tmpBuffer = tsk_null;
  29. }
  30. RPMessage::RPMessage() :
  31. rp_message(tsk_null),
  32. type(twrap_rpmessage_type_sms_none),
  33. tmpBuffer(tsk_null)
  34. {
  35. }
  36. twrap_rpmessage_type_t RPMessage::getType()
  37. {
  38. return this->type;
  39. }
  40. unsigned RPMessage::getPayloadLength()
  41. {
  42. if(!this->tmpBuffer) {
  43. if((this->tmpBuffer = tsk_buffer_create_null())) {
  44. tsms_rpdu_data_serialize(this->rp_message, this->tmpBuffer);
  45. }
  46. }
  47. return this->tmpBuffer ? this->tmpBuffer->size : 0;
  48. }
  49. unsigned RPMessage::getPayload(void* output, unsigned maxsize)
  50. {
  51. unsigned retsize = 0;
  52. if(!this->tmpBuffer) {
  53. if((this->tmpBuffer = tsk_buffer_create_null())) {
  54. tsms_rpdu_message_serialize(this->rp_message, this->tmpBuffer);
  55. }
  56. }
  57. if(output && maxsize && this->tmpBuffer && this->tmpBuffer->data) {
  58. retsize = (this->tmpBuffer->size > maxsize) ? maxsize : this->tmpBuffer->size;
  59. memcpy(output, this->tmpBuffer->data, retsize);
  60. }
  61. return retsize;
  62. }
  63. RPMessage::~RPMessage()
  64. {
  65. TSK_OBJECT_SAFE_FREE(this->rp_message);
  66. TSK_OBJECT_SAFE_FREE(this->tmpBuffer);
  67. }
  68. SMSData::SMSData(twrap_sms_type_t _type, int _mr, const void* _ascii, tsk_size_t _size): oa(tsk_null), da(tsk_null)
  69. {
  70. this->type = _type;
  71. this->mr = _mr;
  72. if((this->size = _size)) {
  73. if((this->ascii = tsk_calloc(size+1, 1))) {
  74. memcpy(this->ascii, _ascii, _size);
  75. }
  76. }
  77. else {
  78. this->ascii = tsk_null;
  79. }
  80. }
  81. SMSData::SMSData(): oa(tsk_null), da(tsk_null)
  82. {
  83. this->type = twrap_sms_type_none;
  84. this->mr = 0;
  85. this->size = 0;
  86. this->ascii = tsk_null;
  87. }
  88. SMSData::~SMSData()
  89. {
  90. TSK_FREE(this->ascii);
  91. TSK_FREE(this->oa);
  92. TSK_FREE(this->da);
  93. }
  94. twrap_sms_type_t SMSData::getType()
  95. {
  96. return this->type;
  97. }
  98. int SMSData::getMR()
  99. {
  100. return this->mr;
  101. }
  102. unsigned SMSData::getPayloadLength()
  103. {
  104. return this->size;
  105. }
  106. unsigned SMSData::getPayload(void* output, unsigned maxsize)
  107. {
  108. unsigned retsize = 0;
  109. if(output && maxsize && this->ascii) {
  110. retsize = (this->size > maxsize) ? maxsize : this->size;
  111. memcpy(output, this->ascii, retsize);
  112. }
  113. return retsize;
  114. }
  115. const char* SMSData::getOA()
  116. {
  117. return this->oa;
  118. }
  119. const char* SMSData::getDA()
  120. {
  121. return this->da;
  122. }
  123. void SMSData::setOA(const char* _oa)
  124. {
  125. TSK_FREE(this->oa);
  126. this->oa = tsk_strdup(_oa);
  127. }
  128. void SMSData::setDA(const char* _da)
  129. {
  130. TSK_FREE(this->da);
  131. this->da = tsk_strdup(_da);
  132. }
  133. // More information about RP-DATA: http://www.doubango.org/API/tinySMS/group__tsms__rpdu__group.html#tsms_rpdu_group_DATA
  134. RPMessage* SMSEncoder::encodeSubmit(int mr, const char *smsc, const char *destination, const char *ascii)
  135. {
  136. int ret;
  137. tsk_buffer_t* buffer = tsk_null;
  138. tsms_tpdu_submit_t* sms_submit = tsk_null;
  139. tsms_rpdu_data_t* rp_data = tsk_null;
  140. RPMessage* encodedData = tsk_null;
  141. if(!smsc || ! destination || !ascii) {
  142. TSK_DEBUG_ERROR("Invalid parameter");
  143. return tsk_null;
  144. }
  145. if(mr<0 || mr>0xFF) {
  146. TSK_DEBUG_WARN("Invalid Message Reference");
  147. mr &= 0xFF;
  148. }
  149. // create SMS-SUBMIT message
  150. if(!(sms_submit = tsms_tpdu_submit_create(mr, (const uint8_t*)smsc, (const uint8_t*)destination))) {
  151. TSK_DEBUG_ERROR("Failed to create the TPDU SMS-SUBMIT message");
  152. goto bail;
  153. }
  154. // Set content for SMS-SUBMIT
  155. if((buffer = tsms_pack_to_7bit(ascii))) {
  156. ret = tsms_tpdu_submit_set_userdata(sms_submit, buffer, tsms_alpha_7bit);
  157. TSK_OBJECT_SAFE_FREE(buffer);
  158. }
  159. else {
  160. TSK_DEBUG_ERROR("Failed to encode the TPDU SMS-SUBMIT message");
  161. goto bail;
  162. }
  163. // create RP-DATA(SMS-SUBMIT)
  164. if((rp_data = tsms_rpdu_data_create_mo(mr, (const uint8_t*)smsc, TSMS_TPDU_MESSAGE(sms_submit)))) {
  165. encodedData = new RPMessage(twrap_rpmessage_type_sms_submit, TSMS_RPDU_MESSAGE(rp_data));
  166. }
  167. else {
  168. TSK_DEBUG_ERROR("Failed to create the RP-DATA(SMS-SUBMIT) message");
  169. goto bail;
  170. }
  171. bail:
  172. TSK_OBJECT_SAFE_FREE(buffer);
  173. TSK_OBJECT_SAFE_FREE(sms_submit);
  174. TSK_OBJECT_SAFE_FREE(rp_data);
  175. return encodedData;
  176. }
  177. // More information about RP-DATA: http://www.doubango.org/API/tinySMS/group__tsms__rpdu__group.html#tsms_rpdu_group_DATA
  178. RPMessage* SMSEncoder::encodeDeliver(int mr, const char* smsc, const char* originator, const char* ascii)
  179. {
  180. int ret;
  181. tsk_buffer_t* buffer = tsk_null;
  182. tsms_tpdu_deliver_t* sms_deliver = tsk_null;
  183. tsms_rpdu_data_t* rp_data = tsk_null;
  184. RPMessage* encodedData = tsk_null;
  185. if(!smsc || ! originator || !ascii) {
  186. TSK_DEBUG_ERROR("Invalid parameter");
  187. return tsk_null;
  188. }
  189. if(mr<0 || mr>0xFF) {
  190. TSK_DEBUG_WARN("Invalid Message Reference");
  191. mr &= 0xFF;
  192. }
  193. // create SMS-DELIVER message
  194. sms_deliver = tsms_tpdu_deliver_create((const uint8_t*)smsc, (const uint8_t*)originator);
  195. // Set content for SMS-DELIVER
  196. if((buffer = tsms_pack_to_7bit(ascii))) {
  197. ret = tsms_tpdu_deliver_set_userdata(sms_deliver, buffer, tsms_alpha_7bit);
  198. TSK_OBJECT_SAFE_FREE(buffer);
  199. }
  200. // create RP-DATA message
  201. if((rp_data = tsms_rpdu_data_create_mt(mr, (const uint8_t*)smsc, TSMS_TPDU_MESSAGE(sms_deliver)))) {
  202. encodedData = new RPMessage(twrap_rpmessage_type_sms_deliver, TSMS_RPDU_MESSAGE(rp_data));
  203. }
  204. else {
  205. TSK_DEBUG_ERROR("Failed to create the RP-DATA(SMS-DELIVER) message");
  206. goto bail;
  207. }
  208. bail:
  209. TSK_OBJECT_SAFE_FREE(buffer);
  210. TSK_OBJECT_SAFE_FREE(sms_deliver);
  211. TSK_OBJECT_SAFE_FREE(rp_data);
  212. return encodedData;
  213. }
  214. RPMessage* SMSEncoder::encodeACK(int mr, const char* smsc, const char* destination, bool forSUBMIT)
  215. {
  216. tsms_tpdu_report_t* sms_report = tsk_null;
  217. tsms_rpdu_ack_t* rp_ack = tsk_null;
  218. tsk_bool_t isSUBMIT = forSUBMIT ? tsk_true : tsk_false;
  219. tsk_bool_t isERROR = tsk_false;
  220. RPMessage* encodedData = tsk_null;
  221. // create SMS-DELIVER(or SUBMIT)-REPORT message
  222. sms_report = tsms_tpdu_report_create((const uint8_t*)smsc, isSUBMIT, isERROR);
  223. // create RP-ACK message (From MS to SC)
  224. if((rp_ack = tsms_rpdu_ack_create_mo(mr, TSMS_TPDU_MESSAGE(sms_report)))) {
  225. encodedData = new RPMessage(twrap_rpmessage_type_sms_ack, TSMS_RPDU_MESSAGE(rp_ack));
  226. }
  227. TSK_OBJECT_SAFE_FREE(sms_report);
  228. TSK_OBJECT_SAFE_FREE(rp_ack);
  229. return encodedData;
  230. }
  231. RPMessage* SMSEncoder::encodeError(int mr, const char* smsc, const char* destination, bool forSUBMIT)
  232. {
  233. tsms_tpdu_report_t* sms_report = tsk_null;
  234. tsms_rpdu_error_t* rp_error= tsk_null;
  235. tsk_bool_t isSUBMIT = forSUBMIT ? tsk_true : tsk_false;
  236. tsk_bool_t isERROR = tsk_true;
  237. RPMessage* encodedData = tsk_null;
  238. // create SMS-DELIVER-REPORT message
  239. sms_report = tsms_tpdu_report_create((const uint8_t*)smsc, isSUBMIT, isERROR);
  240. // create RP-ERROR message
  241. if((rp_error = tsms_rpdu_error_create_mo(mr, TSMS_TPDU_MESSAGE(sms_report), 0x0A/*call barred*/))) {
  242. encodedData = new RPMessage(twrap_rpmessage_type_sms_error, TSMS_RPDU_MESSAGE(rp_error));
  243. }
  244. TSK_OBJECT_SAFE_FREE(sms_report);
  245. TSK_OBJECT_SAFE_FREE(rp_error);
  246. return encodedData;
  247. }
  248. SMSData* SMSEncoder::decode(const void* data, unsigned size, bool MobOrig)
  249. {
  250. tsms_rpdu_message_t* rp_message = tsk_null;
  251. tsms_tpdu_message_t* tpdu = tsk_null;
  252. SMSData* decodedData = tsk_null;
  253. if(!(rp_message = tsms_rpdu_message_deserialize(data, size))) {
  254. TSK_DEBUG_ERROR("Failed to deserialize the RP-MESSAGE");
  255. goto bail;
  256. }
  257. switch(rp_message->mti) {
  258. case tsms_rpdu_type_data_mo:
  259. case tsms_rpdu_type_data_mt: {
  260. char* ascii = tsk_null;
  261. tsms_rpdu_data_t* rp_data = TSMS_RPDU_DATA(rp_message);
  262. if((tpdu = tsms_tpdu_message_deserialize(rp_data->udata->data, rp_data->udata->size, MobOrig))) {
  263. if(tpdu->mti == tsms_tpdu_mti_deliver_mt || tpdu->mti == tsms_tpdu_mti_submit_mo) { /* SMS-SUBMIT or SMS-DELIVER? */
  264. ascii = tsms_tpdu_message_get_payload(tpdu);
  265. decodedData = new SMSData(twrap_sms_type_rpdata, rp_message->mr, ascii, (tsk_size_t)tsk_strlen(ascii));
  266. if(tpdu->mti == tsms_tpdu_mti_deliver_mt) {
  267. tsms_tpdu_deliver_t* tpdu_deliver = (tsms_tpdu_deliver_t*)tpdu;
  268. decodedData->setOA(tpdu_deliver->oa ? tpdu_deliver->oa->digits : tsk_null);
  269. }
  270. /* IMPORTANT: to not uncomment
  271. else if(TSK_OBJECT_DEF(tpdu) == tsms_tpdu_submit_def_t){
  272. tsms_tpdu_submit_t* tpdu_submit = (tsms_tpdu_submit_t*)tpdu;
  273. decodedData->setDA(tpdu_submit->da ? tpdu_submit->da->digits : tsk_null);
  274. }*/
  275. TSK_FREE(ascii);
  276. }
  277. }
  278. break;
  279. }
  280. case tsms_rpdu_type_ack_mo:
  281. case tsms_rpdu_type_ack_mt: {
  282. tsms_rpdu_ack_t* rp_ack = TSMS_RPDU_ACK(rp_message);
  283. // ...do whatever you want
  284. if(rp_ack->udata && (tpdu = tsms_tpdu_message_deserialize(rp_ack->udata->data, rp_ack->udata->size, MobOrig))) {
  285. // ...do whatever you want
  286. }
  287. decodedData = new SMSData(twrap_sms_type_ack, rp_message->mr, tsk_null, 0);
  288. break;
  289. }
  290. case tsms_rpdu_type_error_mo:
  291. case tsms_rpdu_type_error_mt: {
  292. tsms_rpdu_error_t* rp_error = TSMS_RPDU_ERROR(rp_message);
  293. // ...do whatever you want
  294. if(rp_error->udata && (tpdu = tsms_tpdu_message_deserialize(rp_error->udata->data, rp_error->udata->size, MobOrig))) {
  295. // ...do whatever you want
  296. }
  297. decodedData = new SMSData(twrap_sms_type_error, rp_message->mr, tsk_null, 0);
  298. break;
  299. }
  300. case tsms_rpdu_type_smma_mo: {
  301. // tsms_rpdu_smma_t* rp_smma = TSMS_RPDU_SMMA(rp_message);
  302. // ...do whatever you want
  303. decodedData = new SMSData(twrap_sms_type_smma, rp_message->mr, tsk_null, 0);
  304. break;
  305. }
  306. default: {
  307. TSK_DEBUG_INFO("Unknown RP-Message type (%u).", rp_message->mti);
  308. break;
  309. }
  310. }
  311. bail:
  312. TSK_OBJECT_SAFE_FREE(rp_message);
  313. TSK_OBJECT_SAFE_FREE(tpdu);
  314. return decodedData;
  315. }