tsms_common.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright (C) 2009 Mamadou Diop.
  3. *
  4. * Contact: Mamadou Diop <diopmamadou [at) doubango (DOT) 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. /**@file tsms_common.h
  23. * @brief SMS Commons (3GPP TS 23.038 and al.).
  24. *
  25. * @author Mamadou Diop <diopmamadou [at) doubango (DOT) org>
  26. *
  27. */
  28. #ifndef TINYSMS_TSMS_COMMON_H
  29. #define TINYSMS_TSMS_COMMON_H
  30. #include "tinysms_config.h"
  31. #include "tinysms/tsms_address.h"
  32. #include "tsk_buffer.h"
  33. TSMS_BEGIN_DECLS
  34. /** SMS alphabet values as per 3GPP TS 23.038 v911 section 4.
  35. * Part of TP-DCS (SMS Data Coding Scheme).
  36. */
  37. typedef enum tsms_alphabet_e {
  38. /*11*/ tsms_alpha_reserved = 0x03,
  39. /*00*/ tsms_alpha_7bit = 0x00,
  40. /*01*/ tsms_alpha_8bit = 0x01,
  41. /*10*/ tsms_alpha_ucs2 = 0x02
  42. }
  43. tsms_alphabet_t;
  44. /* 3GPP TS 23.038 v911 section 4 */
  45. #define TSMS_ALPHA_FROM_DCS(dcs) (((dcs) & 0x0C) >> 2) /* Bit3 and Bit2 */
  46. /* ======================== TPDU ========================
  47. =========================================================*/
  48. /** 3GPP TS 23.040 - 9.2.3.1 TP Message Type Indicator (TP MTI) */
  49. typedef enum tsms_tpdu_mti_e {
  50. /*0 0*/ tsms_tpdu_mti_deliver_mt = 0x00, /**< SMS-DELIVER (in the direction SC to MS)*/
  51. /*0 0*/ tsms_tpdu_mti_deliver_report_mo = 0x00, /**< SMS-DELIVER-REPORT (in the direction MS to SC)*/
  52. /*1 0*/ tsms_tpdu_mti_status_report_mt = 0x02, /**< SMS-STATUS-REPORT (in the direction SC to MS)*/
  53. /*1 0*/ tsms_tpdu_mti_command_mo = 0x02, /**< SMS-COMMAND (in the direction MS to SC)*/
  54. /*0 1*/ tsms_tpdu_mti_submit_mo = 0x01, /**< SMS-SUBMIT (in the direction MS to SC)*/
  55. /*0 1*/ tsms_tpdu_mti_submit_report_mt = 0x01, /**< SMS-SUBMIT-REPORT (in the direction SC to MS)*/
  56. /*1 1*/ tsms_tpdu_mti_reserved = 0x03 /**< Reserved*/
  57. }
  58. tsms_tpdu_mti_t;
  59. #define TSMS_TPDU_MTI_IS_RESERVED(mti) ((mti) == tsms_tpdu_mti_reserved)
  60. #define TSMS_TPDU_MTI_IS_MO(mti) (((mti) == tsms_tpdu_mti_deliver_report_mo) || ((mti) == tsms_tpdu_mti_command_mo) || ((mti) == tsms_tpdu_mti_submit_mo))
  61. /** 3GPP TS 23.040 - 9.2.3.1 TP Message Type Indicator (TP MTI) */
  62. typedef enum tsms_tpdu_vpf_e {
  63. // Bit4 and Bit3
  64. /*0 0*/ tsms_tpdu_vpf_not_present = 0x00, /**< TP VP field not present */
  65. /*1 0*/ tsms_tpdu_vpf_relative = 0x02, /**< TP VP field present - relative format*/
  66. /*0 1*/ tsms_tpdu_vpf_enhanced = 0x01, /**< TP-VP field present - enhanced format */
  67. /*1 1*/ tsms_tpdu_vpf_absolute = 0x03, /**< TP VP field present - absolute format */
  68. }
  69. tsms_tpdu_vpf_t;
  70. /** SM-TL base type as per 3GPP TS 23.040 section 9.2.
  71. */
  72. typedef struct tsms_tpdu_message_s {
  73. TSK_DECLARE_OBJECT;
  74. tsk_bool_t MobOrig;
  75. /** TP Message Type Indicator (TP MTI) as per TS 23.040 section 9.2.3.1. 2-bit field. */
  76. tsms_tpdu_mti_t mti;
  77. /** TP Protocol Identifier (M - o)
  78. * Parameter identifying the above layer protocol, if any. */
  79. uint8_t pid;
  80. /** TP Data Coding Scheme (M - o)
  81. * Parameter identifying the coding scheme within the TP-User-Data. */
  82. uint8_t dcs;
  83. /** TP User Data Length (M - I)
  84. * Parameter indicating the length of the TP User Data field to follow. */
  85. uint8_t udl;
  86. /** TP User Data (O - v)
  87. * User data. */
  88. tsk_buffer_t* ud;
  89. }
  90. tsms_tpdu_message_t;
  91. #define TSMS_DECLARE_TPDU_MESSAGE tsms_tpdu_message_t tpdu
  92. #define TSMS_TPDU_MESSAGE(self) ((tsms_tpdu_message_t*)(self))
  93. /**@ingroup tsms_tpdu_group
  94. * @def tsms_tpdu_message_serialize_mo
  95. * Serialize an outgoing (MS to SC) @a TP-Message as binary content.
  96. * @param self The @a TP-Message (any SMS-*) to serialize.
  97. * @param output A pointer to the @a output buffer.
  98. * @retval Zero if succeed and non-zero error code otherwise.
  99. */
  100. /**@ingroup tsms_tpdu_group
  101. * @def tsms_tpdu_message_serialize_mt
  102. * Serialize an incoming (SC to MS) @a TP-Message as binary content.
  103. * @param self The @a TP-Message (SMS-SUBMIT, SMS-DELIVER, SMS-COMMAND ...) to serialize.
  104. * @param output A pointer to the @a output buffer.
  105. * @retval Zero if succeed and non-zero error code otherwise.
  106. */
  107. /**@ingroup tsms_tpdu_group
  108. * @def tsms_tpdu_message_deserialize_mo
  109. * Deserialize the outgoing (MO to SC) binary content as a @a TP-Message.
  110. * @param data A pointer to the binary data.
  111. * @param size The size of the buffer holding the binary data.
  112. * @retval @ref tsms_tpdu_message_t if succeed and Null otherwise.
  113. */
  114. /**@ingroup tsms_tpdu_group
  115. * @def tsms_tpdu_message_deserialize_mt
  116. * Deserialize the incoming (SC to MS) binary content as a @a TP-Message.
  117. * @param data A pointer to the binary data.
  118. * @param size The size of the buffer holding the binary data.
  119. * @retval @ref tsms_tpdu_message_t if succeed and Null otherwise.
  120. */
  121. int tsms_tpdu_message_init(tsms_tpdu_message_t* self, tsms_tpdu_mti_t mti);
  122. TINYSMS_API int tsms_tpdu_message_serialize(const tsms_tpdu_message_t* self, tsk_buffer_t* output, tsk_bool_t MobOrig);
  123. #define tsms_tpdu_message_serialize_mo(self, output) tsms_tpdu_message_serialize(self, output, tsk_true)
  124. #define tsms_tpdu_message_serialize_mt(self, output) tsms_tpdu_message_serialize(self, output, tsk_false)
  125. TINYSMS_API tsms_tpdu_message_t* tsms_tpdu_message_deserialize(const void* data, tsk_size_t size, tsk_bool_t MobOrig);
  126. #define tsms_tpdu_message_deserialize_mo(data, size) tsms_tpdu_message_deserialize(data, size, tsk_true)
  127. #define tsms_tpdu_message_deserialize_mt(data, size) tsms_tpdu_message_deserialize(data, size, tsk_false)
  128. TINYSMS_API char* tsms_tpdu_message_tostring(const tsms_tpdu_message_t* self, tsk_bool_t MobOrig);
  129. TINYSMS_API char* tsms_tpdu_message_tohexastring(const tsms_tpdu_message_t* self, tsk_bool_t MobOrig);
  130. TINYSMS_API char* tsms_tpdu_message_get_payload(const tsms_tpdu_message_t* self);
  131. TINYSMS_API int tsms_tpdu_message_set_userdata(tsms_tpdu_message_t* self, const tsk_buffer_t* udata, tsms_alphabet_t alpha);
  132. int tsms_tpdu_message_deinit(tsms_tpdu_message_t* self);
  133. #define TSMS_TPDU_DEFAULT_PID 0x00 /**< 3GPP TS 23.040 section 9.2.3.9 - TP-Protocol-Identifier (TP-PID) */
  134. #define TSMS_TPDU_DEFAULT_DCS 0x00 /**< 3GPP TS 23.040 section 9.2.3.10 - TP-Data-Coding-Scheme (TP-DCS) (default class, 7 bit message) + GSM 03.38*/
  135. #define TSMS_TPDU_DEFAULT_VP 0xAA /**< 3GPP TS 23.040 section 9.2.3.12 - TP-Validity-Period */
  136. #define TSMS_TPDU_DEFAULT_VPF tsms_tpdu_vpf_relative /**< 3GPP TS 23.040 section 9.2.3.3 - TP Validity Period Format (TP VPF) */
  137. #define TSMS_TPDU_DEFAULT_FCS 0xFF /**< 3GPP TS 23.040 section 9.2.3.22 - TP-Failure-Cause (TP-FCS) */
  138. #define TSMS_TPDU_DEFAULT_PI 0x00 /**< 3GPP TS 23.040 section 9.2.3.27 - TP-Parameter-Indicator (TP-PI) */
  139. #define TSMS_TPDU_DEFAULT_SCTS "00000000000000" /**< 3GPP TS 23.040 section 9.2.3.11 - TP-Service-Centre-Time-Stamp (TP-SCTS) */
  140. #define TSMS_TPDU_DEFAULT_MMS 0x01 /**< 3GPP TS 23.040 section 9.2.3.2 - TP More Messages to Send (TP-MMS) */
  141. /**< Indicates whether to append SMSC address at the begining of the TPDU content.
  142. */
  143. #define TSMS_TPDU_APPEND_SMSC 0
  144. /* ======================== RPDU ========================
  145. =========================================================*/
  146. /** RP-MTI types as per 3GPP TS 24.011 section 8.2.2
  147. * 3bit field located in the first octet of all RP-Messages. */
  148. typedef enum tsms_rpdu_type_e {
  149. /*000*/ tsms_rpdu_type_data_mo = 0x00, /**< RP-DATA message ms->n */
  150. /*001*/ tsms_rpdu_type_data_mt = 0x01, /**< RP-DATA message n->ms */
  151. /*010*/ tsms_rpdu_type_ack_mo = 0x02, /**< RP-ACK message ms->n */
  152. /*011*/ tsms_rpdu_type_ack_mt = 0x03, /**< RP-ACK message n->ms */
  153. /*100*/ tsms_rpdu_type_error_mo = 0x04, /**< RP-ERROR message ms->n */
  154. /*101*/ tsms_rpdu_type_error_mt = 0x05, /**< RP-ERROR message n->ms */
  155. /*110*/ tsms_rpdu_type_smma_mo = 0x06, /**< RP-SMMA message ms->n */
  156. }
  157. tsms_rpdu_type_t;
  158. #define TSMS_RPDU_TYPE_IS_MO(type) (((type) == tsms_rpdu_type_data_mo) \
  159. || ((type) == tsms_rpdu_type_ack_mo) \
  160. || ((type) == tsms_rpdu_type_error_mo) \
  161. || ((type) == tsms_rpdu_type_smma_mo))
  162. typedef struct tsms_rpdu_message_s {
  163. TSK_DECLARE_OBJECT;
  164. tsms_rpdu_type_t mti;
  165. uint8_t mr; /**< Message Reference. */
  166. }
  167. tsms_rpdu_message_t;
  168. #define TSMS_DECLARE_RPDU_MESSAGE tsms_rpdu_message_t rpdu
  169. #define TSMS_RPDU_MESSAGE(self) ((tsms_rpdu_message_t*)(self))
  170. TINYSMS_API int tsms_rpdu_message_serialize(const tsms_rpdu_message_t* self, tsk_buffer_t* output);
  171. TINYSMS_API tsms_rpdu_message_t* tsms_rpdu_message_deserialize(const void* data, tsk_size_t size);
  172. TINYSMS_API char* tsms_rpdu_message_tohexastring(const tsms_rpdu_message_t* self);
  173. TSMS_END_DECLS
  174. #endif /* TINYSMS_TSMS_COMMON_H */