summaryrefslogtreecommitdiffstats
path: root/tinySMS/src/tsms_common.c
diff options
context:
space:
mode:
Diffstat (limited to 'tinySMS/src/tsms_common.c')
-rwxr-xr-xtinySMS/src/tsms_common.c498
1 files changed, 259 insertions, 239 deletions
diff --git a/tinySMS/src/tsms_common.c b/tinySMS/src/tsms_common.c
index 92cda4c..f1e926a 100755
--- a/tinySMS/src/tsms_common.c
+++ b/tinySMS/src/tsms_common.c
@@ -2,19 +2,19 @@
* Copyright (C) 2009 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango.org>
-*
+*
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
-*
+*
* DOUBANGO is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
-*
+*
* You should have received a copy of the GNU General Public License
* along with DOUBANGO.
*
@@ -37,7 +37,7 @@
#include <string.h> /* strlen() */
-/* ======================== TPDU ========================
+/* ======================== TPDU ========================
=========================================================*/
extern tsms_tpdu_message_t* _tsms_tpdu_submit_deserialize(const void* data, tsk_size_t size);
@@ -54,14 +54,14 @@ extern int _tsms_tpdu_status_report_serialize(const tsms_tpdu_message_t* self, t
int tsms_tpdu_message_init(tsms_tpdu_message_t* self, tsms_tpdu_mti_t mti)
{
- if(self){
- self->MobOrig = tsk_true;
- self->mti = mti;
- self->pid = TSMS_TPDU_DEFAULT_PID;
- self->dcs = TSMS_TPDU_DEFAULT_DCS;
- return 0;
- }
- return -1;
+ if(self) {
+ self->MobOrig = tsk_true;
+ self->mti = mti;
+ self->pid = TSMS_TPDU_DEFAULT_PID;
+ self->dcs = TSMS_TPDU_DEFAULT_DCS;
+ return 0;
+ }
+ return -1;
}
/**@ingroup tsms_tpdu_group
@@ -75,36 +75,44 @@ int tsms_tpdu_message_init(tsms_tpdu_message_t* self, tsms_tpdu_mti_t mti)
*/
int tsms_tpdu_message_serialize(const tsms_tpdu_message_t* self, tsk_buffer_t* output, tsk_bool_t MobOrig)
{
- if(!self){
- return -1;
- }
-
- /* 3GPP TS 23.040 - 9.2.3.1 TP Message Type Indicator (TP MTI)
- bit1 bit0 Message type
- 0 0 SMS-DELIVER (in the direction SC to MS)
- 0 0 SMS-DELIVER-REPORT (in the direction MS to SC)
- 1 0 SMS-STATUS-REPORT (in the direction SC to MS)
- 1 0 SMS-COMMAND (in the direction MS to SC)
- 0 1 SMS-SUBMIT (in the direction MS to SC)
- 0 1 SMS-SUBMIT-REPORT (in the direction SC to MS)
- 1 1 Reserved
- */
- if(MobOrig){ /* MO */
- switch(self->mti){
- case tsms_tpdu_mti_deliver_report_mo: return _tsms_tpdu_report_serialize(self, output); /* SMS-DELIVER-REPORT */
- case tsms_tpdu_mti_command_mo: return _tsms_tpdu_command_serialize(self, output); /* SMS-COMMAND */
- case tsms_tpdu_mti_submit_mo: return _tsms_tpdu_submit_serialize(self, output); /* SMS-SUBMIT */
- default: return -2;
- }
- }
- else{ /* MT */
- switch(self->mti){
- case tsms_tpdu_mti_deliver_mt: return _tsms_tpdu_deliver_serialize(self, output); /* SMS-DELIVER */
- case tsms_tpdu_mti_status_report_mt: return _tsms_tpdu_status_report_serialize(self, output); /* SMS-STATUS-REPORT */
- case tsms_tpdu_mti_submit_report_mt: return _tsms_tpdu_report_serialize(self, output); /* SMS-SUBMIT-REPORT */
- default: return -2;
- }
- }
+ if(!self) {
+ return -1;
+ }
+
+ /* 3GPP TS 23.040 - 9.2.3.1 TP Message Type Indicator (TP MTI)
+ bit1 bit0 Message type
+ 0 0 SMS-DELIVER (in the direction SC to MS)
+ 0 0 SMS-DELIVER-REPORT (in the direction MS to SC)
+ 1 0 SMS-STATUS-REPORT (in the direction SC to MS)
+ 1 0 SMS-COMMAND (in the direction MS to SC)
+ 0 1 SMS-SUBMIT (in the direction MS to SC)
+ 0 1 SMS-SUBMIT-REPORT (in the direction SC to MS)
+ 1 1 Reserved
+ */
+ if(MobOrig) { /* MO */
+ switch(self->mti) {
+ case tsms_tpdu_mti_deliver_report_mo:
+ return _tsms_tpdu_report_serialize(self, output); /* SMS-DELIVER-REPORT */
+ case tsms_tpdu_mti_command_mo:
+ return _tsms_tpdu_command_serialize(self, output); /* SMS-COMMAND */
+ case tsms_tpdu_mti_submit_mo:
+ return _tsms_tpdu_submit_serialize(self, output); /* SMS-SUBMIT */
+ default:
+ return -2;
+ }
+ }
+ else { /* MT */
+ switch(self->mti) {
+ case tsms_tpdu_mti_deliver_mt:
+ return _tsms_tpdu_deliver_serialize(self, output); /* SMS-DELIVER */
+ case tsms_tpdu_mti_status_report_mt:
+ return _tsms_tpdu_status_report_serialize(self, output); /* SMS-STATUS-REPORT */
+ case tsms_tpdu_mti_submit_report_mt:
+ return _tsms_tpdu_report_serialize(self, output); /* SMS-SUBMIT-REPORT */
+ default:
+ return -2;
+ }
+ }
}
/**@ingroup tsms_tpdu_group
@@ -116,56 +124,70 @@ int tsms_tpdu_message_serialize(const tsms_tpdu_message_t* self, tsk_buffer_t* o
*/
tsms_tpdu_message_t* tsms_tpdu_message_deserialize(const void* data, tsk_size_t size, tsk_bool_t MobOrig)
{
- tsms_tpdu_mti_t mti;
- uint8_t smsc_len = 0;
- tsms_tpdu_message_t* ret = tsk_null;
-
- if(!data || size<=1){
- TSK_DEBUG_ERROR("Invalid parameter.");
- return tsk_null;
- }
+ tsms_tpdu_mti_t mti;
+ uint8_t smsc_len = 0;
+ tsms_tpdu_message_t* ret = tsk_null;
+
+ if(!data || size<=1) {
+ TSK_DEBUG_ERROR("Invalid parameter.");
+ return tsk_null;
+ }
#if TSMS_TPDU_APPEND_SMSC
- smsc_len = *((uint8_t*)data) + 1/* len itself*/;
- if(smsc_len>=size){
- TSK_DEBUG_ERROR("Too short.");
- return tsk_null;
- }
+ smsc_len = *((uint8_t*)data) + 1/* len itself*/;
+ if(smsc_len>=size) {
+ TSK_DEBUG_ERROR("Too short.");
+ return tsk_null;
+ }
#endif
-
- mti = (*(((uint8_t*)data)+smsc_len) & 0x03);
-
- /* 3GPP TS 23.040 - 9.2.3.1 TP Message Type Indicator (TP MTI)
- bit1 bit0 Message type
- 0 0 SMS-DELIVER (in the direction SC to MS)
- 0 0 SMS-DELIVER-REPORT (in the direction MS to SC)
- 1 0 SMS-STATUS-REPORT (in the direction SC to MS)
- 1 0 SMS-COMMAND (in the direction MS to SC)
- 0 1 SMS-SUBMIT (in the direction MS to SC)
- 0 1 SMS-SUBMIT-REPORT (in the direction SC to MS)
- 1 1 Reserved
- */
- if(MobOrig){ /* MO */
- switch(mti){
- case tsms_tpdu_mti_deliver_report_mo: ret = _tsms_tpdu_report_deserialize(data, size); break; /* SMS-DELIVER-REPORT */
- case tsms_tpdu_mti_command_mo: ret = _tsms_tpdu_command_deserialize(data, size); break; /* SMS-COMMAND */
- case tsms_tpdu_mti_submit_mo: ret = _tsms_tpdu_submit_deserialize(data, size); break; /* SMS-SUBMIT */
- default: return tsk_null;
- }
- }
- else{ /* MT */
- switch(mti){
- case tsms_tpdu_mti_deliver_mt: ret = _tsms_tpdu_deliver_deserialize(data, size); break;/* SMS-DELIVER */
- case tsms_tpdu_mti_status_report_mt: ret = _tsms_tpdu_status_report_deserialize(data, size); break;/* SMS-STATUS-REPORT */
- case tsms_tpdu_mti_submit_report_mt: ret = _tsms_tpdu_report_deserialize(data, size); break;/* SMS-SUBMIT-REPORT */
- default: return tsk_null;
- }
- }
-
- if(ret){
- ret->MobOrig = MobOrig;
- }
- return ret;
+
+ mti = (*(((uint8_t*)data)+smsc_len) & 0x03);
+
+ /* 3GPP TS 23.040 - 9.2.3.1 TP Message Type Indicator (TP MTI)
+ bit1 bit0 Message type
+ 0 0 SMS-DELIVER (in the direction SC to MS)
+ 0 0 SMS-DELIVER-REPORT (in the direction MS to SC)
+ 1 0 SMS-STATUS-REPORT (in the direction SC to MS)
+ 1 0 SMS-COMMAND (in the direction MS to SC)
+ 0 1 SMS-SUBMIT (in the direction MS to SC)
+ 0 1 SMS-SUBMIT-REPORT (in the direction SC to MS)
+ 1 1 Reserved
+ */
+ if(MobOrig) { /* MO */
+ switch(mti) {
+ case tsms_tpdu_mti_deliver_report_mo:
+ ret = _tsms_tpdu_report_deserialize(data, size);
+ break; /* SMS-DELIVER-REPORT */
+ case tsms_tpdu_mti_command_mo:
+ ret = _tsms_tpdu_command_deserialize(data, size);
+ break; /* SMS-COMMAND */
+ case tsms_tpdu_mti_submit_mo:
+ ret = _tsms_tpdu_submit_deserialize(data, size);
+ break; /* SMS-SUBMIT */
+ default:
+ return tsk_null;
+ }
+ }
+ else { /* MT */
+ switch(mti) {
+ case tsms_tpdu_mti_deliver_mt:
+ ret = _tsms_tpdu_deliver_deserialize(data, size);
+ break;/* SMS-DELIVER */
+ case tsms_tpdu_mti_status_report_mt:
+ ret = _tsms_tpdu_status_report_deserialize(data, size);
+ break;/* SMS-STATUS-REPORT */
+ case tsms_tpdu_mti_submit_report_mt:
+ ret = _tsms_tpdu_report_deserialize(data, size);
+ break;/* SMS-SUBMIT-REPORT */
+ default:
+ return tsk_null;
+ }
+ }
+
+ if(ret) {
+ ret->MobOrig = MobOrig;
+ }
+ return ret;
}
/**@ingroup tsms_tpdu_group
@@ -173,21 +195,21 @@ tsms_tpdu_message_t* tsms_tpdu_message_deserialize(const void* data, tsk_size_t
*/
char* tsms_tpdu_message_tostring(const tsms_tpdu_message_t* self, tsk_bool_t MobOrig)
{
- char* ret = tsk_null;
- tsk_buffer_t* output = tsk_null;
- if(!self){
- goto bail;
- }
-
- if((output = tsk_buffer_create_null())){
- if(!tsms_tpdu_message_serialize(self, output, MobOrig)){
- ret = tsk_strndup(output->data, output->size);
- }
- TSK_OBJECT_SAFE_FREE(output);
- }
+ char* ret = tsk_null;
+ tsk_buffer_t* output = tsk_null;
+ if(!self) {
+ goto bail;
+ }
+
+ if((output = tsk_buffer_create_null())) {
+ if(!tsms_tpdu_message_serialize(self, output, MobOrig)) {
+ ret = tsk_strndup(output->data, output->size);
+ }
+ TSK_OBJECT_SAFE_FREE(output);
+ }
bail:
- return ret;
+ return ret;
}
/**@ingroup tsms_tpdu_group
@@ -200,24 +222,24 @@ bail:
*/
char* tsms_tpdu_message_tohexastring(const tsms_tpdu_message_t* self, tsk_bool_t MobOrig)
{
- char* ret = tsk_null;
- tsk_buffer_t* output = tsk_null;
- if(!self){
- goto bail;
- }
-
- if((output = tsk_buffer_create_null())){
- if(!tsms_tpdu_message_serialize(self, output, MobOrig)){
- tsk_size_t i;
- for(i=0;i<output->size;i++){
- tsk_strcat_2(&ret, "%.2X", *(TSK_BUFFER_TO_U8(output)+i));
- }
- }
- TSK_OBJECT_SAFE_FREE(output);
- }
+ char* ret = tsk_null;
+ tsk_buffer_t* output = tsk_null;
+ if(!self) {
+ goto bail;
+ }
+
+ if((output = tsk_buffer_create_null())) {
+ if(!tsms_tpdu_message_serialize(self, output, MobOrig)) {
+ tsk_size_t i;
+ for(i=0; i<output->size; i++) {
+ tsk_strcat_2(&ret, "%.2X", *(TSK_BUFFER_TO_U8(output)+i));
+ }
+ }
+ TSK_OBJECT_SAFE_FREE(output);
+ }
bail:
- return ret;
+ return ret;
}
/**@ingroup tsms_tpdu_group
@@ -227,25 +249,24 @@ bail:
*/
char* tsms_tpdu_message_get_payload(const tsms_tpdu_message_t* self)
{
- if(!self || !self->ud || !self->ud->data || !self->ud->size){
- TSK_DEBUG_WARN("No content.");
- return tsk_null;
- }
-
- switch(TSMS_ALPHA_FROM_DCS(self->dcs)){
- case tsms_alpha_7bit:
- return tsms_pack_from_7bit(self->ud->data, self->ud->size);
- case tsms_alpha_8bit:
- return tsms_pack_from_8bit(self->ud->data, self->ud->size);
- case tsms_alpha_ucs2:
- return tsms_pack_from_ucs2(self->ud->data, self->ud->size);
- case tsms_alpha_reserved:
- default:
- {
- TSK_DEBUG_ERROR("%d alpha not suported", TSMS_ALPHA_FROM_DCS(self->dcs));
- return tsk_null;
- }
- }
+ if(!self || !self->ud || !self->ud->data || !self->ud->size) {
+ TSK_DEBUG_WARN("No content.");
+ return tsk_null;
+ }
+
+ switch(TSMS_ALPHA_FROM_DCS(self->dcs)) {
+ case tsms_alpha_7bit:
+ return tsms_pack_from_7bit(self->ud->data, self->ud->size);
+ case tsms_alpha_8bit:
+ return tsms_pack_from_8bit(self->ud->data, self->ud->size);
+ case tsms_alpha_ucs2:
+ return tsms_pack_from_ucs2(self->ud->data, self->ud->size);
+ case tsms_alpha_reserved:
+ default: {
+ TSK_DEBUG_ERROR("%d alpha not suported", TSMS_ALPHA_FROM_DCS(self->dcs));
+ return tsk_null;
+ }
+ }
}
/**@ingroup tsms_tpdu_group
@@ -258,57 +279,56 @@ char* tsms_tpdu_message_get_payload(const tsms_tpdu_message_t* self)
*/
int tsms_tpdu_message_set_userdata(tsms_tpdu_message_t* self, const tsk_buffer_t* udata, tsms_alphabet_t alpha)
{
- if(!self || !udata){
- return -1;
- }
- /* SMS alphabet values as per 3GPP TS 23.038 v911 section 4.
- * Part of TP-DCS (SMS Data Coding Scheme).
- */
- self->dcs = ((self->dcs & 0xF3) | (alpha << 2)); /* Bit3 and Bit2 */
-
- TSK_OBJECT_SAFE_FREE(self->ud);
-
- /* 3GPP TS 23.040 ==> 9.2.3.16 TP-User-Data-Length (TP-UDL)
- * (alpha = SMS_ALPHA_7bit) ==> number of septets.
- * ((alpha == SMS_ALPHA_8bit) || (alpha == SMS_ALPHA_UCS2)) ==> number of octes.
- */
-
- /* 3GPP TS 23.040 ==> 9.2.3.24 TP-User Data (TP-UD) */
- switch(alpha){
- case tsms_alpha_7bit: {
- self->udl = (udata->size) + (udata->size/7);
- self->ud = tsk_buffer_create(udata->data, udata->size);
- }
- break;
- case tsms_alpha_8bit:
- case tsms_alpha_ucs2: {
- self->udl = udata->size;
- self->ud = tsk_buffer_create(udata->data, udata->size);
- }
- break;
-
- default:
- {
- TSK_DEBUG_ERROR("Invalid Alphabet.");
- return -2;
- }
- }
- return 0;
+ if(!self || !udata) {
+ return -1;
+ }
+ /* SMS alphabet values as per 3GPP TS 23.038 v911 section 4.
+ * Part of TP-DCS (SMS Data Coding Scheme).
+ */
+ self->dcs = ((self->dcs & 0xF3) | (alpha << 2)); /* Bit3 and Bit2 */
+
+ TSK_OBJECT_SAFE_FREE(self->ud);
+
+ /* 3GPP TS 23.040 ==> 9.2.3.16 TP-User-Data-Length (TP-UDL)
+ * (alpha = SMS_ALPHA_7bit) ==> number of septets.
+ * ((alpha == SMS_ALPHA_8bit) || (alpha == SMS_ALPHA_UCS2)) ==> number of octes.
+ */
+
+ /* 3GPP TS 23.040 ==> 9.2.3.24 TP-User Data (TP-UD) */
+ switch(alpha) {
+ case tsms_alpha_7bit: {
+ self->udl = (udata->size) + (udata->size/7);
+ self->ud = tsk_buffer_create(udata->data, udata->size);
+ }
+ break;
+ case tsms_alpha_8bit:
+ case tsms_alpha_ucs2: {
+ self->udl = udata->size;
+ self->ud = tsk_buffer_create(udata->data, udata->size);
+ }
+ break;
+
+ default: {
+ TSK_DEBUG_ERROR("Invalid Alphabet.");
+ return -2;
+ }
+ }
+ return 0;
}
/** internal function used to deinit a TP-Message.*/
int tsms_tpdu_message_deinit(tsms_tpdu_message_t* self)
{
- if(self){
- TSK_OBJECT_SAFE_FREE(self->ud);
- return 0;
- }
- return -1;
+ if(self) {
+ TSK_OBJECT_SAFE_FREE(self->ud);
+ return 0;
+ }
+ return -1;
}
-/* ======================== RPDU ========================
+/* ======================== RPDU ========================
=========================================================*/
extern int _tsms_rpdu_rpdata_serialize(const tsms_rpdu_message_t* self, tsk_buffer_t* output);
@@ -333,26 +353,26 @@ extern tsms_rpdu_message_t* _tsms_rpdu_rperror_deserialize(const void* data, tsk
*/
int tsms_rpdu_message_serialize(const tsms_rpdu_message_t* self, tsk_buffer_t* output)
{
- if(!self || !output){
- TSK_DEBUG_ERROR("Invalid Parameter");
- return -1;
- }
-
- switch(self->mti){
- case tsms_rpdu_type_data_mo:
- case tsms_rpdu_type_data_mt:
- return _tsms_rpdu_rpdata_serialize(self, output);
- case tsms_rpdu_type_ack_mo:
- case tsms_rpdu_type_ack_mt:
- return _tsms_rpdu_rpack_serialize(self, output);
- case tsms_rpdu_type_error_mo:
- case tsms_rpdu_type_error_mt:
- return _tsms_rpdu_rperror_serialize(self, output);
- case tsms_rpdu_type_smma_mo:
- return _tsms_rpdu_rpsmma_serialize(self, output);
- }
-
- return -2;
+ if(!self || !output) {
+ TSK_DEBUG_ERROR("Invalid Parameter");
+ return -1;
+ }
+
+ switch(self->mti) {
+ case tsms_rpdu_type_data_mo:
+ case tsms_rpdu_type_data_mt:
+ return _tsms_rpdu_rpdata_serialize(self, output);
+ case tsms_rpdu_type_ack_mo:
+ case tsms_rpdu_type_ack_mt:
+ return _tsms_rpdu_rpack_serialize(self, output);
+ case tsms_rpdu_type_error_mo:
+ case tsms_rpdu_type_error_mt:
+ return _tsms_rpdu_rperror_serialize(self, output);
+ case tsms_rpdu_type_smma_mo:
+ return _tsms_rpdu_rpsmma_serialize(self, output);
+ }
+
+ return -2;
}
/**@ingroup tsms_rpdu_group
@@ -363,30 +383,30 @@ int tsms_rpdu_message_serialize(const tsms_rpdu_message_t* self, tsk_buffer_t* o
*/
tsms_rpdu_message_t* tsms_rpdu_message_deserialize(const void* data, tsk_size_t size)
{
- tsms_rpdu_type_t mti;
-
- if(!data || size<2 /* MTI and MR*/){
- TSK_DEBUG_ERROR("Invalid parameter.");
- return tsk_null;
- }
-
- mti = (*((uint8_t*)data) & 0x07);
-
- switch(mti){
- case tsms_rpdu_type_data_mo:
- case tsms_rpdu_type_data_mt:
- return _tsms_rpdu_rpdata_deserialize(data, size);
- case tsms_rpdu_type_ack_mo:
- case tsms_rpdu_type_ack_mt:
- return _tsms_rpdu_rpack_deserialize(data, size);
- case tsms_rpdu_type_error_mo:
- case tsms_rpdu_type_error_mt:
- return _tsms_rpdu_rperror_deserialize(data, size);
- case tsms_rpdu_type_smma_mo:
- return _tsms_rpdu_rpsmma_deserialize(data, size);
- }
-
- return tsk_null;
+ tsms_rpdu_type_t mti;
+
+ if(!data || size<2 /* MTI and MR*/) {
+ TSK_DEBUG_ERROR("Invalid parameter.");
+ return tsk_null;
+ }
+
+ mti = (*((uint8_t*)data) & 0x07);
+
+ switch(mti) {
+ case tsms_rpdu_type_data_mo:
+ case tsms_rpdu_type_data_mt:
+ return _tsms_rpdu_rpdata_deserialize(data, size);
+ case tsms_rpdu_type_ack_mo:
+ case tsms_rpdu_type_ack_mt:
+ return _tsms_rpdu_rpack_deserialize(data, size);
+ case tsms_rpdu_type_error_mo:
+ case tsms_rpdu_type_error_mt:
+ return _tsms_rpdu_rperror_deserialize(data, size);
+ case tsms_rpdu_type_smma_mo:
+ return _tsms_rpdu_rpsmma_deserialize(data, size);
+ }
+
+ return tsk_null;
}
/**@ingroup tsms_rpdu_group
@@ -396,22 +416,22 @@ tsms_rpdu_message_t* tsms_rpdu_message_deserialize(const void* data, tsk_size_t
*/
char* tsms_rpdu_message_tohexastring(const tsms_rpdu_message_t* self)
{
- char* ret = tsk_null;
- tsk_buffer_t* output = tsk_null;
- if(!self){
- goto bail;
- }
-
- if((output = tsk_buffer_create_null())){
- if(!tsms_rpdu_message_serialize(self, output)){
- tsk_size_t i;
- for(i=0;i<output->size;i++){
- tsk_strcat_2(&ret, "%.2X", *(TSK_BUFFER_TO_U8(output)+i));
- }
- }
- TSK_OBJECT_SAFE_FREE(output);
- }
+ char* ret = tsk_null;
+ tsk_buffer_t* output = tsk_null;
+ if(!self) {
+ goto bail;
+ }
+
+ if((output = tsk_buffer_create_null())) {
+ if(!tsms_rpdu_message_serialize(self, output)) {
+ tsk_size_t i;
+ for(i=0; i<output->size; i++) {
+ tsk_strcat_2(&ret, "%.2X", *(TSK_BUFFER_TO_U8(output)+i));
+ }
+ }
+ TSK_OBJECT_SAFE_FREE(output);
+ }
bail:
- return ret;
+ return ret;
}
OpenPOWER on IntegriCloud