diff options
Diffstat (limited to 'tinySMS/src/tpdu')
-rw-r--r-- | tinySMS/src/tpdu/tsms_tpdu_command.c | 275 | ||||
-rw-r--r-- | tinySMS/src/tpdu/tsms_tpdu_deliver.c | 263 | ||||
-rw-r--r-- | tinySMS/src/tpdu/tsms_tpdu_report.c | 284 | ||||
-rw-r--r-- | tinySMS/src/tpdu/tsms_tpdu_status_report.c | 327 | ||||
-rw-r--r-- | tinySMS/src/tpdu/tsms_tpdu_submit.c | 317 |
5 files changed, 1466 insertions, 0 deletions
diff --git a/tinySMS/src/tpdu/tsms_tpdu_command.c b/tinySMS/src/tpdu/tsms_tpdu_command.c new file mode 100644 index 0000000..4733edc --- /dev/null +++ b/tinySMS/src/tpdu/tsms_tpdu_command.c @@ -0,0 +1,275 @@ +/* +* 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. +* +*/ + +/**@file tsms_tpdu_command.c + * @brief SMS TPDU SMS-COMMAND message as per 3GPP TS 23.040 section 9.2.2.4. + * + * @author Mamadou Diop <diopmamadou(at)doubango.org> + * + + */ +#include "tinysms/tpdu/tsms_tpdu_command.h" + +#include "tsk_debug.h" + +#define TSMS_ERROR_TOO_SHORT()\ + TSK_DEBUG_ERROR("SMS-COMMAND == Data too short.");\ + failed = tsk_true;\ + goto bail; + +/** internal function used to deserialse a SMS-COMMAND message from binary content. */ +tsms_tpdu_message_t* _tsms_tpdu_command_deserialize(const void* data, tsk_size_t size) +{ + /* You don't need to test data and test, this is an internal function called by tsms_tpdu_message_deserialize() */ + tsms_tpdu_command_t* self = tsms_tpdu_command_create(0, tsk_null, tsk_null, 0, tsms_tpdu_cmd_rel); + tsk_bool_t failed = tsk_false; + tsk_size_t any_len; + const uint8_t* pdata = data; + const uint8_t* pend = pdata + size; + + /* SMSC address */ +#if TSMS_TPDU_APPEND_SMSC + if(!(self->smsc = tsms_address_deserialize(data, size, tsms_addr_smsc, &any_len)) || !any_len){ + TSK_DEBUG_ERROR("SMS-COMMAND == Failed to parse SMSC address"); + failed = tsk_true; + goto bail; + } + else{ + if((pdata += any_len) >= pend){ + TSMS_ERROR_TOO_SHORT(); + } + } +#endif + + /* SMS-COMMAND first Octect: + - TP-Message-Type-Indicator(2b) + - TP-User-Data-Header-Indicator(1b) + - TP-Status-Report-Request(1b) + + +----+----+----+----+----+----+----+----+ + | |UDHI|SRR | | | | MTI | + +----+----+----+----+----+----+----+----+ + */ + TSMS_TPDU_MESSAGE(self)->mti = (*pdata & 0x03); + self->srr = (*pdata & 0x20)>>5, + self->udhi = (*pdata & 0x40)>>6; + if((++pdata) >= pend){ + TSMS_ERROR_TOO_SHORT(); + } + + /* 3GPP TS 23.040 ==> 9.2.3.6 TP-Message-Reference (TP-MR) + * 1o */ + self->mr = *pdata; + if((++pdata) >= pend){ + TSMS_ERROR_TOO_SHORT(); + } + + /* 3GPP TS 23.040 ==> 9.2.3.9 TP-Protocol-Identifier (TP-PID) + * 1o */ + TSMS_TPDU_MESSAGE(self)->pid = *pdata; + if((++pdata) >= pend){ + TSMS_ERROR_TOO_SHORT(); + } + + /* 3GPP TS 23.040 ==> 9.2.3.19 TP-Command-Type (TP-CT) + * 1o */ + self->ct = *pdata; + if((++pdata) >= pend){ + TSMS_ERROR_TOO_SHORT(); + } + + /* 3GPP TS 23.040 ==> 9.2.3.18 TP-Message-Number (TP-MN) + * 1o */ + self->mn = *pdata; + if((++pdata) >= pend){ + TSMS_ERROR_TOO_SHORT(); + } + + /* 3GPP TS 23.040 ==> 9.2.3.8 TP-Destination-Address (TP-DA) */ + if(!(self->da = tsms_address_deserialize(pdata, (pend-pdata), tsms_addr_da, &any_len)) || !any_len){ + TSK_DEBUG_ERROR("SMS-DELIVER == Failed to parse DA address"); + failed = tsk_true; + goto bail; + } + else{ + if((pdata += any_len) >= pend){ + TSMS_ERROR_TOO_SHORT(); + } + } + + /* 3GPP TS 23.040 ==> 9.2.3.20 TP Command Data Length (TP CDL) + * 1o */ + TSMS_TPDU_MESSAGE(self)->udl = *pdata; + pdata++; + + /* 3GPP TS 23.040 ==> 9.2.3.21 TP Command Data (TP CD) */ + if((pend-pdata) > 0){ + TSMS_TPDU_MESSAGE(self)->ud = tsk_buffer_create(pdata, (pend-pdata)); + } + + bail: + if(failed){ + TSK_OBJECT_SAFE_FREE(self); + } + return TSMS_TPDU_MESSAGE(self); +} + +/** internal function used to serialize a SMS-COMMAND message as binary content. */ +int _tsms_tpdu_command_serialize(const tsms_tpdu_command_t* self, tsk_buffer_t* output) +{ + uint8_t _1byte; + + if(!self){ + return -1; + } + + /* SMSC address */ +#if TSMS_TPDU_APPEND_SMSC + tsms_address_serialize(self->smsc, output); +#endif + + /* SMS-COMMAND first Octect: + - TP-Message-Type-Indicator(2b) + - TP-User-Data-Header-Indicator(1b) + - TP-Status-Report-Request(1b) + + +----+----+----+----+----+----+----+----+ + | |UDHI|SRR | | | | MTI | + +----+----+----+----+----+----+----+----+ + */ + _1byte = (TSMS_TPDU_MESSAGE(self)->mti & 0xF3); /*2b*/ + _1byte |= + /*1b*/ + /*2b*/ + ((uint8_t)self->srr) << 5 /*1b*/ + | ((uint8_t)self->udhi) << 6; /*1b*/ + tsk_buffer_append(output, &_1byte, 1); + + /* 3GPP TS 23.040 ==> 9.2.3.6 TP-Message-Reference (TP-MR) */ + tsk_buffer_append(output, &self->mr, 1); /*0-255 ==> 1o*/ + + /* 3GPP TS 23.040 ==> 9.2.3.9 TP-Protocol-Identifier (TP-PID) */ + tsk_buffer_append(output, &TSMS_TPDU_MESSAGE(self)->pid, 1); /*1o*/ + + /* 3GPP TS 23.040 ==> 9.2.3.19 TP-Command-Type (TP-CT) */ + tsk_buffer_append(output, &self->ct, 1); /*1o*/ + + /* 3GPP TS 23.040 ==> 9.2.3.18 TP-Message-Number (TP-MN) */ + tsk_buffer_append(output, &self->mn, 1); /*1o*/ + + /* 3GPP TS 23.040 ==> 9.2.3.8 TP-Destination-Address (TP-DA) */ + tsms_address_serialize(self->da, output); + + /* 3GPP TS 23.040 ==> 9.2.3.20 TP Command Data Length (TP CDL) */ + tsk_buffer_append(output, &TSMS_TPDU_MESSAGE(self)->udl, 1); /*1o*/ + + /* 3GPP TS 23.040 ==> 9.2.3.21 TP Command Data (TP CD) */ + tsk_buffer_append(output, TSK_BUFFER_DATA(TSMS_TPDU_MESSAGE(self)->ud), TSK_BUFFER_SIZE(TSMS_TPDU_MESSAGE(self)->ud)); + + return 0; +} + +/**@ingroup tsms_tpdu_group +* Creates new @a SMS-COMMAND message. +* @a SMS-COMMAND messages are used to convey commands from the MS (Mobile Station) to the SC (Service Center).<br> +* For more information, please refer to 3GPP TS 23.040 section 9.2.2.4. +* @param mr TP-Message-Reference (TP-MR) as per 3GPP TS 23.040 section 9.2.3.6. +* @param smsc The address of the SMSC. e.g. "+331253688". +* @param dest The address of the destination. e.g. "+331253688". +* @param msg_num The message number. For more information, please refer to 3GPP TS 23.040 section 9.2.3.18 (TP-MN). +* @param cmd The command type as per 3GPP TS 23.040 v910 section 9.2.3.19 (TP-CT). +* @retval SMS-COMMAND message. +* +* See For more information, see @ref tsms_tpdu_group_COMMAND "SMS-COMMAND". +*/ +tsms_tpdu_command_t* tsms_tpdu_command_create(uint8_t mr, const tsms_address_string_t smsc, const tsms_address_string_t dest, uint8_t msg_num, tsms_tpdu_cmd_t cmd) +{ + return tsk_object_new(tsms_tpdu_command_def_t, mr, smsc, dest, msg_num, cmd); +} + + + +//================================================================================================= +// SMS TPDU SMS-COMMAND object definition +// +static tsk_object_t* tsms_tpdu_command_ctor(tsk_object_t * self, va_list * app) +{ + tsms_tpdu_command_t *command = self; + if(command){ + const char* smsc, *dest; + uint8_t mr, msg_num; + tsms_tpdu_cmd_t cmd; + +#if defined(__GNUC__) + mr = (uint8_t)va_arg(*app, unsigned); +#else + mr = va_arg(*app, uint8_t); +#endif + + smsc = va_arg(*app, const char*); + dest = va_arg(*app, const char*); + +#if defined(__GNUC__) + msg_num = (uint8_t)va_arg(*app, unsigned); +#else + msg_num = va_arg(*app, uint8_t); +#endif + cmd = va_arg(*app, tsms_tpdu_cmd_t); + + /* init base*/ + tsms_tpdu_message_init(TSMS_TPDU_MESSAGE(command), tsms_tpdu_mti_command_mo); + /* init self */ + command->mr = mr; + if(smsc){ + command->smsc = tsms_address_smsc_create((const uint8_t*)smsc); + } + if(dest){ + command->da = tsms_address_da_create((const uint8_t*)dest); + } + command->mn = msg_num; + command->ct = cmd; + } + return self; +} + +static tsk_object_t* tsms_tpdu_command_dtor(tsk_object_t * self) +{ + tsms_tpdu_command_t *command = self; + if(command){ + /*deinit base*/ + tsms_tpdu_message_deinit(TSMS_TPDU_MESSAGE(command)); + /*deinit self*/ + TSK_OBJECT_SAFE_FREE(command->smsc); + TSK_OBJECT_SAFE_FREE(command->da); + } + + return self; +} + +static const tsk_object_def_t tsms_tpdu_command_def_s = +{ + sizeof(tsms_tpdu_command_t), + tsms_tpdu_command_ctor, + tsms_tpdu_command_dtor, + tsk_null, +}; +const tsk_object_def_t *tsms_tpdu_command_def_t = &tsms_tpdu_command_def_s; diff --git a/tinySMS/src/tpdu/tsms_tpdu_deliver.c b/tinySMS/src/tpdu/tsms_tpdu_deliver.c new file mode 100644 index 0000000..afc4086 --- /dev/null +++ b/tinySMS/src/tpdu/tsms_tpdu_deliver.c @@ -0,0 +1,263 @@ +/* +* 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. +* +*/ + +/**@file tsms_tpdu_deliver.c + * @brief SMS TPDU SMS-DELIVER message as per 3GPP TS 23.040 section 9.2.2.1. + * + * @author Mamadou Diop <diopmamadou(at)doubango.org> + * + + */ +#include "tinysms/tpdu/tsms_tpdu_deliver.h" + +#include "tsk_string.h" +#include "tsk_memory.h" +#include "tsk_debug.h" + +#include <string.h> /* memcpy */ + +#define TSMS_ERROR_TOO_SHORT()\ + TSK_DEBUG_ERROR("SMS-DELIVER == Data too short.");\ + failed = tsk_true;\ + goto bail; + +/** internal function used to deserialize a buffer containing a SMS-DELIVER message. */ +tsms_tpdu_message_t* _tsms_tpdu_deliver_deserialize(const void* data, tsk_size_t size) +{ + /* You don't need to test data and test, this is an internal function called by tsms_tpdu_message_deserialize() */ + tsms_tpdu_deliver_t* self = tsms_tpdu_deliver_create(tsk_null, tsk_null); + tsk_bool_t failed = tsk_false; + tsk_size_t any_len; + const uint8_t* pdata = data; + const uint8_t* pend = pdata + size; + + /* SMSC address */ +#if TSMS_TPDU_APPEND_SMSC + if(!(self->smsc = tsms_address_deserialize(data, size, tsms_addr_smsc, &any_len)) || !any_len){ + TSK_DEBUG_ERROR("SMS-DELIVER == Failed to parse SMSC address"); + failed = tsk_true; + goto bail; + } + else{ + if((pdata += any_len) >= pend){ + TSMS_ERROR_TOO_SHORT(); + } + } +#endif + + /* SMS-DELIVER first Octect: + - TP-Message-Type-Indicator(2b) + - TP-More-Messages-to-Send(1b) + - TP-Loop-Prevention(1b) + - TP-Reply-Path(1b) + - TP-User-Data-Header-Indicator(1b) + - TP-Status-Report-Indication(1b) + +----+----+----+----+----+----+----+----+ + | RP|UDHI|SRI | |LP |MMS | MTI | + +----+----+----+----+----+----+----+----+ + */ + TSMS_TPDU_MESSAGE(self)->mti = (*pdata & 0x03); + self->mms = (*pdata & 0x04)>>2, + self->lp = (*pdata & 0x08)>>3, + self->sri = (*pdata & 0x20)>>5, + self->udhi = (*pdata & 0x40)>>6, + self->rp = (*pdata & 0x80)>>7; + if((++pdata) >= pend){ + TSMS_ERROR_TOO_SHORT(); + } + + /* 3GPP TS 23.040 ==> TP-Originating-Address (TP-OA)*/ + if(!(self->oa = tsms_address_deserialize(pdata, (pend-pdata), tsms_addr_oa, &any_len)) || !any_len){ + TSK_DEBUG_ERROR("SMS-DELIVER == Failed to parse OA address"); + failed = tsk_true; + goto bail; + } + else{ + if((pdata += any_len) >= pend){ + TSMS_ERROR_TOO_SHORT(); + } + } + + /* 3GPP TS 23.040 ==> 9.2.3.9 TP-Protocol-Identifier (TP-PID) + * 1o */ + TSMS_TPDU_MESSAGE(self)->pid = *pdata; + if((++pdata) >= pend){ + TSMS_ERROR_TOO_SHORT(); + } + + /* 3GPP TS 23.040 ==> 9.2.3.10 TP-Data-Coding-Scheme (TP-DCS) + * 1o */ + TSMS_TPDU_MESSAGE(self)->dcs = *pdata; + if((++pdata) >= pend){ + TSMS_ERROR_TOO_SHORT(); + } + + /* 3GPP TS 23.040 ==> TP-Service-Centre-Time-Stamp (TP-SCTS) + * 7o */ + if((pend - pdata)<=7){ + TSMS_ERROR_TOO_SHORT(); + } + memcpy(self->scts, pdata, 7); + pdata += 7; + + + /* 3GPP TS 23.040 ==> 9.2.3.16 TP-User-Data-Length (TP-UDL) + * 1o */ + TSMS_TPDU_MESSAGE(self)->udl = *pdata; + pdata++; + + /* 3GPP TS 23.040 ==> 9.2.3.24 TP-User Data (TP-UD) */ + if((pend-pdata) > 0){ + TSMS_TPDU_MESSAGE(self)->ud = tsk_buffer_create(pdata, (pend-pdata)); + } + + bail: + if(failed){ + TSK_OBJECT_SAFE_FREE(self); + } + return TSMS_TPDU_MESSAGE(self); +} + +/** internal function used to serialize a SMS-DELIVER message. */ +int _tsms_tpdu_deliver_serialize(const tsms_tpdu_deliver_t* self, tsk_buffer_t* output) +{ + uint8_t _1byte; + + if(!self){ + return -1; + } + +/* SMSC address */ +#if TSMS_TPDU_APPEND_SMSC + tsms_address_serialize(self->smsc, output); +#endif + + /* SMS-DELIVER first Octect: + - TP-Message-Type-Indicator(2b) + - TP-More-Messages-to-Send(1b) + - TP-Loop-Prevention(1b) + - TP-Reply-Path(1b) + - TP-User-Data-Header-Indicator(1b) + - TP-Status-Report-Indication(1b) + +----+----+----+----+----+----+----+----+ + | RP|UDHI|SRI | |LP |MMS | MTI | + +----+----+----+----+----+----+----+----+ + */ + _1byte = (TSMS_TPDU_MESSAGE(self)->mti & 0xF3); /*2b*/ + _1byte |= ((uint8_t)self->mms) << 2 /*1b*/ + | ((uint8_t)self->lp) << 3 /*1b*/ + /*1b (unused)*/ + | ((uint8_t)self->sri) << 5 /*1b*/ + | ((uint8_t)self->udhi) << 6 /*1b*/ + | ((uint8_t)self->rp) << 7; /*1b*/ + tsk_buffer_append(output, &_1byte, 1); + + /* 3GPP TS 23.040 ==> TP-Originating-Address (TP-OA)*/ + tsms_address_serialize(self->oa, output); + + /* 3GPP TS 23.040 ==> 9.2.3.9 TP-Protocol-Identifier (TP-PID)*/ + tsk_buffer_append(output, &TSMS_TPDU_MESSAGE(self)->pid, 1); /*1o*/ + + /* 3GPP TS 23.040 ==> 9.2.3.10 TP-Data-Coding-Scheme (TP-DCS)*/ + tsk_buffer_append(output, &TSMS_TPDU_MESSAGE(self)->dcs, 1); /*1o*/ + + /* 3GPP TS 23.040 ==> TP-Service-Centre-Time-Stamp (TP-SCTS)*/ + tsk_buffer_append(output, self->scts, sizeof(self->scts)); /*7o*/ + + /* 3GPP TS 23.040 ==> 9.2.3.16 TP-User-Data-Length (TP-UDL) */ + tsk_buffer_append(output, &TSMS_TPDU_MESSAGE(self)->udl, 1); /*1o*/ + + /* 3GPP TS 23.040 ==> 9.2.3.24 TP-User Data (TP-UD) */ + tsk_buffer_append(output, TSK_BUFFER_DATA(TSMS_TPDU_MESSAGE(self)->ud), TSK_BUFFER_SIZE(TSMS_TPDU_MESSAGE(self)->ud)); + + return 0; +} + +/**@ingroup tsms_tpdu_group +* Creates new @a SMS-DELIVER message. +* @a SMS-DELIVER messages are used to convey short messages from the SC (Service Center) to the MS (Mobile Station).<br> +* For more information, please refer to 3GPP TS 23.040 section 9.2.2.1. +* @param smsc SMSC address. e.g. "+331253688". +* @param orig The Originator address. e.g. "+331253688". +* @retval SMS-DELIVER message. +* +* See For more information, see @ref tsms_tpdu_group_DELIVER "SMS-DELIVER". +*/ +tsms_tpdu_deliver_t* tsms_tpdu_deliver_create(const tsms_address_string_t smsc, const tsms_address_string_t orig) +{ + tsms_tpdu_deliver_t* ret = tsk_null; + + if(!(ret = tsk_object_new(tsms_tpdu_deliver_def_t, smsc, orig))){ + goto bail; + } + +bail: + return ret; +} + +//================================================================================================= +// SMS TPDU SMS-DELIVER object definition +// +static tsk_object_t* tsms_tpdu_deliver_ctor(tsk_object_t * self, va_list * app) +{ + tsms_tpdu_deliver_t *deliver = self; + if(deliver){ + const char* smsc, *orig; + + smsc = va_arg(*app, const char*); + orig = va_arg(*app, const char*); + + /* init base*/ + tsms_tpdu_message_init(TSMS_TPDU_MESSAGE(deliver), tsms_tpdu_mti_deliver_mt); + /* init self */ + deliver->mms = TSMS_TPDU_DEFAULT_MMS; + if(smsc){ + deliver->smsc = tsms_address_smsc_create((const uint8_t*)smsc); + } + if(orig){ + deliver->oa = tsms_address_oa_create((const uint8_t*)orig); + } + } + return self; +} + +static tsk_object_t* tsms_tpdu_deliver_dtor(tsk_object_t * self) +{ + tsms_tpdu_deliver_t *deliver = self; + if(deliver){ + /*deinit base*/ + tsms_tpdu_message_deinit(TSMS_TPDU_MESSAGE(deliver)); + /*deinit self*/ + TSK_OBJECT_SAFE_FREE(deliver->oa); + TSK_OBJECT_SAFE_FREE(deliver->smsc); + } + return self; +} + +static const tsk_object_def_t tsms_tpdu_deliver_def_s = +{ + sizeof(tsms_tpdu_deliver_t), + tsms_tpdu_deliver_ctor, + tsms_tpdu_deliver_dtor, + tsk_null, +}; +const tsk_object_def_t *tsms_tpdu_deliver_def_t = &tsms_tpdu_deliver_def_s; diff --git a/tinySMS/src/tpdu/tsms_tpdu_report.c b/tinySMS/src/tpdu/tsms_tpdu_report.c new file mode 100644 index 0000000..73f6132 --- /dev/null +++ b/tinySMS/src/tpdu/tsms_tpdu_report.c @@ -0,0 +1,284 @@ +/* +* 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. +* +*/ + +/**@file tsms_tpdu_report.c + * @brief SMS TPDU SMS-DELIVER-REPORT and SMS-SUBMIT-REPORT message as per 3GPP TS 23.040 section 9.2.2.1a and 9.2.2.2a. + * + * @author Mamadou Diop <diopmamadou(at)doubango.org> + * + + */ +#include "tinysms/tpdu/tsms_tpdu_report.h" + +#include "tsk_string.h" +#include "tsk_memory.h" +#include "tsk_debug.h" + +#include <string.h> /* memcpy */ + +#define TSMS_ERROR_TOO_SHORT()\ + TSK_DEBUG_ERROR("SMS-DELIVER-REPORT/MS-SUBMIT-REPORT == Data too short.");\ + failed = tsk_true;\ + goto bail; + +tsms_tpdu_message_t* _tsms_tpdu_report_deserialize_2(const void* data, tsk_size_t size, tsk_bool_t error) +{ + /* You don't need to test data and test, this is an internal function called by tsms_tpdu_message_deserialize() */ + tsms_tpdu_report_t* self = tsms_tpdu_report_create(tsk_null, tsk_false, error); + tsk_bool_t failed = tsk_false; +// tsk_size_t any_len; + const uint8_t* pdata = data; + const uint8_t* pend = pdata + size; + + /* SMSC address */ +#if TSMS_TPDU_APPEND_SMSC && 0 + if(!(self->smsc = tsms_address_deserialize(data, size, tsms_addr_smsc, &any_len)) || !any_len){ + TSK_DEBUG_ERROR("SMS-DELIVER == Failed to parse SMSC address"); + failed = tsk_true; + goto bail; + } + else{ + if((pdata += any_len) >= pend){ + TSMS_ERROR_TOO_SHORT(); + } + } +#endif + + /* SMS-DELIVER-REPORT/MS-SUBMIT-REPORT (both ACK and ERROR) first Octect: + - TP-Message-Type-Indicator(2b) + - TP-User-Data-Header-Indication(1b) + + +----+----+----+----+----+----+----+----+ + | |UDHI| | | | | MTI | + +----+----+----+----+----+----+----+----+ + */ + TSMS_TPDU_MESSAGE(self)->mti = *pdata & 0x03; + self->udhi = (*pdata & 0x40)>>6; + if((++pdata) >= pend){ + TSMS_ERROR_TOO_SHORT(); + } + + if(self->error){ /* FIXME ==> should comes from RP layer */ + /* 3GPP TS 23.040 ==> 9.2.3.22 TP-Failure-Cause(TP-FCS) */ + //tsk_buffer_append(output, &self->fcs, 1); /*0x00-0xFF ==> 1o*/ + } + + /* 3GPP TS 23.040 ==> 9.2.3.27 TP-Parameter-Indicator (TP-PI) + * 1o + bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 + Extension bit Reserved Reserved Reserved Reserved TP UDL TP DCS TP PID + */ + self->pi = *pdata; + if((++pdata) >= pend){ + TSMS_ERROR_TOO_SHORT(); + } + + if(TSMS_TPDU_MESSAGE(self)->mti == tsms_tpdu_mti_submit_report_mt){ + /* 3GPP TS 23.040 ==> TP-Service-Centre-Time-Stamp (TP-SCTS) + * 7o */ + if((pend - pdata)<7){ + TSMS_ERROR_TOO_SHORT(); + } + memcpy(self->scts, pdata, 7); + pdata += 7; + } + + if(self->pi & 0x01){ /* bit 0 */ + /* 3GPP TS 23.040 ==> 9.2.3.9 TP-Protocol-Identifier (TP-PID) + * 1o */ + TSMS_TPDU_MESSAGE(self)->pid = *pdata; + if((++pdata) >= pend){ + TSMS_ERROR_TOO_SHORT(); + } + } + + if(self->pi & 0x02){ /* bit 1 */ + /* 3GPP TS 23.040 ==> 9.2.3.10 TP-Data-Coding-Scheme (TP-DCS) + * 1o */ + TSMS_TPDU_MESSAGE(self)->dcs = *pdata; + if((++pdata) >= pend){ + TSMS_ERROR_TOO_SHORT(); + } + } + + if(self->pi & 0x04){ /* bit 2 */ + /* 3GPP TS 23.040 ==> 9.2.3.16 TP-User-Data-Length (TP-UDL) + * 1o */ + TSMS_TPDU_MESSAGE(self)->udl = *pdata; + pdata++; + + /* 3GPP TS 23.040 ==> 9.2.3.24 TP-User Data (TP-UD) */ + if((pend-pdata) > 0){ + TSMS_TPDU_MESSAGE(self)->ud = tsk_buffer_create(pdata, (pend-pdata)); + } + } + +bail: + if(failed){ + TSK_OBJECT_SAFE_FREE(self); + } + return TSMS_TPDU_MESSAGE(self); +} + +tsms_tpdu_message_t* _tsms_tpdu_report_deserialize(const void* data, tsk_size_t size) +{ + return _tsms_tpdu_report_deserialize_2(data, size, tsk_false); +} + +int _tsms_tpdu_report_serialize(const tsms_tpdu_report_t* self, tsk_buffer_t* output) +{ + uint8_t _1byte; + + if(!self){ + return -1; + } + + /* SMSC address */ +#if TSMS_TPDU_APPEND_SMSC + tsms_address_serialize(self->smsc, output); +#endif + + /* SMS-DELIVER-REPORT/MS-SUBMIT-REPORT (both ACK and ERROR) first Octect: + - TP-Message-Type-Indicator(2b) + - TP-User-Data-Header-Indication(1b) + + +----+----+----+----+----+----+----+----+ + | |UDHI| | | | | MTI | + +----+----+----+----+----+----+----+----+ + */ + _1byte = (TSMS_TPDU_MESSAGE(self)->mti & 0xF3); /*2b*/ + _1byte |= ((uint8_t)self->udhi) << 6; /*1b*/ + tsk_buffer_append(output, &_1byte, 1); + + if(self->error){ + /* 3GPP TS 23.040 ==> 9.2.3.22 TP-Failure-Cause(TP-FCS) */ + tsk_buffer_append(output, &self->fcs, 1); /*0x00-0xFF ==> 1o*/ + } + + /* 3GPP TS 23.040 ==> 9.2.3.27 TP-Parameter-Indicator (TP-PI) + bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 + Extension bit Reserved Reserved Reserved Reserved TP UDL TP DCS TP PID + As we are the serializer, we know which field should be added or not ==> append our own TP-PI field + */ + _1byte = self->pi | 0x07 /* Bits 2,1 and 0 to '1' */; + tsk_buffer_append(output, &_1byte, 1); /* 1o*/ + + if(TSMS_TPDU_MESSAGE(self)->mti == tsms_tpdu_mti_submit_report_mt){ + /* 3GPP TS 23.040 ==> TP-Service-Centre-Time-Stamp (TP-SCTS) */ + tsk_buffer_append(output, self->scts, sizeof(self->scts)); /*7o*/ + } + + /* 3GPP TS 23.040 ==> 9.2.3.9 TP-Protocol-Identifier (TP-PID) */ + tsk_buffer_append(output, &TSMS_TPDU_MESSAGE(self)->pid, 1); /*1o*/ + + /* 3GPP TS 23.040 ==> 9.2.3.10 TP-Data-Coding-Scheme (TP-DCS) */ + tsk_buffer_append(output, &TSMS_TPDU_MESSAGE(self)->dcs, 1); /*1o*/ + + /* 3GPP TS 23.040 ==> 9.2.3.16 TP-User-Data-Length (TP-UDL) */ + tsk_buffer_append(output, &TSMS_TPDU_MESSAGE(self)->udl, 1); /*1o*/ + + /* 3GPP TS 23.040 ==> 9.2.3.24 TP-User Data (TP-UD) */ + tsk_buffer_append(output, TSK_BUFFER_DATA(TSMS_TPDU_MESSAGE(self)->ud), TSK_BUFFER_SIZE(TSMS_TPDU_MESSAGE(self)->ud)); + + return 0; +} + +tsms_tpdu_report_t* tsms_tpdu_report_create(const tsms_address_string_t smsc, tsk_bool_t submit, tsk_bool_t error) +{ + tsms_tpdu_report_t* ret = tsk_null; + + if(!(ret = tsk_object_new(tsms_tpdu_report_def_t, smsc, submit, error))){ + goto bail; + } + +bail: + return ret; +} + + +int tsms_tpdu_report_set_fcs(tsms_tpdu_report_t* self, uint8_t code) +{ + if(self){ + tsms_tpdu_report_t* report = self; + report->fcs = code; + return 0; + } + return -1; +} + +//================================================================================================= +// SMS TPDU SMS-DELIVER-REPORT/SMS-SUBMIT-REPORT object definition +// +static tsk_object_t* tsms_tpdu_report_ctor(tsk_object_t * self, va_list * app) +{ + tsms_tpdu_report_t *report = self; + if(report){ + const char* smsc; + tsk_bool_t error, submit; + + smsc = va_arg(*app, const char*); + submit = va_arg(*app, tsk_bool_t); + error = va_arg(*app, tsk_bool_t); + + /* init base*/ + tsms_tpdu_message_init(TSMS_TPDU_MESSAGE(report), submit?tsms_tpdu_mti_submit_report_mt + :tsms_tpdu_mti_deliver_report_mo); + /* init self */ + if(smsc){ + report->smsc = tsms_address_smsc_create((const uint8_t*)smsc); + } + report->error = error; + + /*init self*/ + if(report->error){ + report->fcs = TSMS_TPDU_DEFAULT_FCS; + } + } + else{ + TSK_DEBUG_ERROR("Null"); + } + return self; +} + +static tsk_object_t* tsms_tpdu_report_dtor(tsk_object_t * self) +{ + tsms_tpdu_report_t *report = self; + if(report){ + /*deinit base*/ + tsms_tpdu_message_deinit(TSMS_TPDU_MESSAGE(report)); + /*deinit self*/ + TSK_OBJECT_SAFE_FREE(report->smsc); + } + else{ + TSK_DEBUG_ERROR("Null"); + } + return self; +} + +static const tsk_object_def_t tsms_tpdu_report_def_s = +{ + sizeof(tsms_tpdu_report_t), + tsms_tpdu_report_ctor, + tsms_tpdu_report_dtor, + tsk_null, +}; +const tsk_object_def_t *tsms_tpdu_report_def_t = &tsms_tpdu_report_def_s; diff --git a/tinySMS/src/tpdu/tsms_tpdu_status_report.c b/tinySMS/src/tpdu/tsms_tpdu_status_report.c new file mode 100644 index 0000000..5c08b1b --- /dev/null +++ b/tinySMS/src/tpdu/tsms_tpdu_status_report.c @@ -0,0 +1,327 @@ +/* +* 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. +* +*/ + +/**@file tsms_tpdu_status_report.c + * @brief SMS TPDU SMS-STATUS-REPORT message as per 3GPP TS 23.040 section 9.2.2.3. + * + * @author Mamadou Diop <diopmamadou(at)doubango.org> + * + + */ +#include "tinysms/tpdu/tsms_tpdu_status_report.h" + +#include "tsk_string.h" +#include "tsk_memory.h" +#include "tsk_debug.h" + +#include <string.h> /* memcpy */ + +#define TSMS_ERROR_TOO_SHORT()\ + TSK_DEBUG_ERROR("SMS-STATUS-REPORT == Data too short.");\ + failed = tsk_true;\ + goto bail; + +/** internal function used to deserialize a SMS-STATUS-REPORT message from a binary buffer. */ +tsms_tpdu_message_t* _tsms_tpdu_status_report_deserialize(const void* data, tsk_size_t size) +{ + /* You don't need to test data and test, this is an internal function called by tsms_tpdu_message_deserialize() */ + tsms_tpdu_status_report_t* self = tsms_tpdu_status_report_create(0, tsk_null, tsk_null, tsms_tpdu_status_received, tsk_false); + tsk_bool_t failed = tsk_false; + tsk_size_t any_len; + const uint8_t* pdata = data; + const uint8_t* pend = pdata + size; + + /* SMSC address */ +#if TSMS_TPDU_APPEND_SMSC + if(!(self->smsc = tsms_address_deserialize(data, size, tsms_addr_smsc, &any_len)) || !any_len){ + TSK_DEBUG_ERROR("SMS-STATUS-REPORT == Failed to parse SMSC address"); + failed = tsk_true; + goto bail; + } + else{ + if((pdata += any_len) >= pend){ + TSMS_ERROR_TOO_SHORT(); + } + } +#endif + + /* SMS-STATUS-REPORT first Octect: + - TP-Message-Type-Indicator(2b) + - TP-More-Messages-to-Send(1b) + - TP-Loop-Prevention(1b) + - TP-User-Data-Header-Indicator(1b) + - TP-Status-Report-Qualifier(1b) + +----+----+----+----+----+----+----+----+ + | |UDHI| SRQ| |LP |MMS | MTI | + +----+----+----+----+----+----+----+----+ + */ + TSMS_TPDU_MESSAGE(self)->mti = *pdata & 0x03; + self->mms = (*pdata & 0x04)>>2, + self->lp = (*pdata & 0x08)>>3, + self->srq = (*pdata & 0x20)>>5, + self->udhi = (*pdata & 0x40)>>6; + if((++pdata) >= pend){ + TSMS_ERROR_TOO_SHORT(); + } + + /* 3GPP TS 23.040 ==> 9.2.3.6 TP-Message-Reference (TP-MR) + * 1o */ + self->mr = *pdata; + if((++pdata) >= pend){ + TSMS_ERROR_TOO_SHORT(); + } + + /* 3GPP TS 23.040 ==> xxxx TP-Recipient-Address (TP-RA) */ + if(!(self->ra = tsms_address_deserialize(pdata, (pend-pdata), tsms_addr_da, &any_len)) || !any_len){ + TSK_DEBUG_ERROR("SMS-STATUS-REPORT == Failed to parse RA address"); + failed = tsk_true; + goto bail; + } + else{ + if((pdata += any_len) >= pend){ + TSMS_ERROR_TOO_SHORT(); + } + } + + /* 3GPP TS 23.040 ==> TP-Service-Centre-Time-Stamp (TP-SCTS) + * 7o */ + if((pend - pdata)<=7){ + TSMS_ERROR_TOO_SHORT(); + } + memcpy(self->scts, pdata, 7); + pdata += 7; + + /* 3GPP TS 23.040 ==> xxxx TP Discharge Time(TP-DT) + * 7o */ + if((pend - pdata)<=7){ + TSMS_ERROR_TOO_SHORT(); + } + memcpy(self->dt, pdata, 7); + pdata += 7; + + /* 3GPP TS 23.040 ==> 9.2.3.15 TP Status(TP-ST) + * 1o */ + self->st = *pdata; + if((++pdata) >= pend){ + TSMS_ERROR_TOO_SHORT(); + } + + /* 3GPP TS 23.040 ==> xxxx TP-Parameter-Indicator (TP-PI) + * 1o */ + self->pi = *pdata; + if((++pdata) >= pend){ + TSMS_ERROR_TOO_SHORT(); + } + + /* 3GPP TS 23.040 ==> 9.2.3.9 TP-Protocol-Identifier (TP-PID) + * 1o */ + TSMS_TPDU_MESSAGE(self)->pid = *pdata; + if((++pdata) >= pend){ + TSMS_ERROR_TOO_SHORT(); + } + + /* 3GPP TS 23.040 ==> 9.2.3.10 TP-Data-Coding-Scheme (TP-DCS) + * 1o */ + TSMS_TPDU_MESSAGE(self)->dcs = *pdata; + if((++pdata) >= pend){ + TSMS_ERROR_TOO_SHORT(); + } + + /* 3GPP TS 23.040 ==> 9.2.3.16 TP-User-Data-Length (TP-UDL) + * 1o */ + TSMS_TPDU_MESSAGE(self)->udl = *pdata; + pdata++; + + /* 3GPP TS 23.040 ==> 9.2.3.24 TP-User Data (TP-UD) */ + if((pend-pdata) > 0){ + TSMS_TPDU_MESSAGE(self)->ud = tsk_buffer_create(pdata, (pend-pdata)); + } + + bail: + if(failed){ + TSK_OBJECT_SAFE_FREE(self); + } + return TSMS_TPDU_MESSAGE(self); +} + +/* internal function used to serialize a SMS-STATUS-REPORT to binary content. */ +int _tsms_tpdu_status_report_serialize(const tsms_tpdu_status_report_t* self, tsk_buffer_t* output) +{ + uint8_t _1byte; + + if(!self){ + return -1; + } + + /* SMSC address */ +#if TSMS_TPDU_APPEND_SMSC + tsms_address_serialize(self->smsc, output); +#endif + + /* SMS-STATUS-REPORT first Octect: + - TP-Message-Type-Indicator(2b) + - TP-More-Messages-to-Send(1b) + - TP-Loop-Prevention(1b) + - TP-User-Data-Header-Indicator(1b) + - TP-Status-Report-Qualifier(1b) + +----+----+----+----+----+----+----+----+ + | |UDHI| SRQ| |LP |MMS | MTI | + +----+----+----+----+----+----+----+----+ + */ + _1byte = (TSMS_TPDU_MESSAGE(self)->mti & 0xF3); /*2b*/ + _1byte |= ((uint8_t)self->mms) << 2 /*1b*/ + | ((uint8_t)self->lp) << 3 /*1b*/ + /*1b (unused)*/ + | ((uint8_t)self->srq) << 5 /*1b*/ + | ((uint8_t)self->udhi) << 6; /*1b*/ + tsk_buffer_append(output, &_1byte, 1); + + /* 3GPP TS 23.040 ==> 9.2.3.6 TP-Message-Reference (TP-MR) */ + tsk_buffer_append(output, &self->mr, 1); /*0-255 ==> 1o*/ + + /* 3GPP TS 23.040 ==> xxxx TP-Recipient-Address (TP-RA) */ + tsms_address_serialize(self->ra, output); + + /* 3GPP TS 23.040 ==> TP-Service-Centre-Time-Stamp (TP-SCTS) */ + tsk_buffer_append(output, self->scts, sizeof(self->scts)); /*7o*/ + + /* 3GPP TS 23.040 ==> xxxx TP Discharge Time(TP-DT) */ + tsk_buffer_append(output, self->dt, sizeof(self->dt)); /*7o*/ + + /* 3GPP TS 23.040 ==> 9.2.3.15 TP Status(TP-ST) */ + tsk_buffer_append(output, &self->st, 1); /*0-255 ==> 1o*/ + + /* 3GPP TS 23.040 ==> 9.2.3.27 TP-Parameter-Indicator (TP-PI) + bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 + Extension bit Reserved Reserved Reserved Reserved TP UDL TP DCS TP PID + As we are the serializer, we know which field should be added or not ==> append our own TP-PI field + */ + _1byte = self->pi | 0x07 /* Bits 2,1 and 0 to '1' */; + tsk_buffer_append(output, &_1byte, 1); /* 1o*/ + + /* 3GPP TS 23.040 ==> 9.2.3.9 TP-Protocol-Identifier (TP-PID) */ + tsk_buffer_append(output, &TSMS_TPDU_MESSAGE(self)->pid, 1); /*1o*/ + + /* 3GPP TS 23.040 ==> 9.2.3.10 TP-Data-Coding-Scheme (TP-DCS) */ + tsk_buffer_append(output, &TSMS_TPDU_MESSAGE(self)->dcs, 1); /*1o*/ + + /* 3GPP TS 23.040 ==> 9.2.3.16 TP-User-Data-Length (TP-UDL) */ + tsk_buffer_append(output, &TSMS_TPDU_MESSAGE(self)->udl, 1); /*1o*/ + + /* 3GPP TS 23.040 ==> 9.2.3.24 TP-User Data (TP-UD) */ + tsk_buffer_append(output, TSK_BUFFER_DATA(TSMS_TPDU_MESSAGE(self)->ud), TSK_BUFFER_SIZE(TSMS_TPDU_MESSAGE(self)->ud)); + + return 0; +} + +/**@ingroup tsms_tpdu_group +* Creates new @a SMS-STATUS-REPORT message. +* @a SMS-STATUS-REPORT messages are used to convey status reports from the SC (Service Center) to the MS (Mobile Station).<br> +* For more information, please refer to 3GPP TS 23.040 section 9.2.2.3. +* @param mr TP-Message-Reference (TP-MR) as per 3GPP TS 23.040 section 9.2.3.6. +* @param smsc The address of the SMSC. e.g. "+331253688". +* @param recipient The address of the recipient. e.g. "+331253688". +* @param status the status code as per 3GPP TS 23.040 v910 section 9.2.3.15 (TP-ST). +* @param submit Indicates whether the report is for a @a SMS-SUBMIT message or not (@a SMS-COMMAND).<br> +* See 3GPP TS 23.040 v910 section 9.2.3.26 (TP-SRQ). +* @retval SMS-STATUS-REPORT message. +* +* See For more information, see @ref tsms_tpdu_group_STATUS_REPORT "SMS-STATUS-REPORT". +*/ +tsms_tpdu_status_report_t* tsms_tpdu_status_report_create(uint8_t mr, const tsms_address_string_t smsc, const tsms_address_string_t recipient, tsms_tpdu_status_type_t status, tsk_bool_t submit) +{ + tsms_tpdu_status_report_t* ret = tsk_null; + + if(!(ret = tsk_object_new(tsms_tpdu_status_report_def_t, mr, smsc, recipient, status))){ + goto bail; + } + +bail: + return ret; +} + +//================================================================================================= +// SMS TPDU SMS-SUMBIT object definition +// +static tsk_object_t* tsms_tpdu_status_report_ctor(tsk_object_t * self, va_list * app) +{ + tsms_tpdu_status_report_t *status_report = self; + if(status_report){ + const char* smsc, *recipient; + uint8_t mr; + tsms_tpdu_status_type_t status; + tsk_bool_t submit; + +#if defined(__GNUC__) + mr = (uint8_t)va_arg(*app, unsigned); +#else + mr = va_arg(*app, uint8_t); +#endif + smsc = va_arg(*app, const char*); + recipient = va_arg(*app, const char*); + status = va_arg(*app, tsms_tpdu_status_type_t); + submit = va_arg(*app, tsk_bool_t); + + /* init base*/ + tsms_tpdu_message_init(TSMS_TPDU_MESSAGE(status_report), tsms_tpdu_mti_status_report_mt); + /* init self */ + status_report->mr = mr; + if(smsc){ + status_report->smsc = tsms_address_smsc_create((const uint8_t*)smsc); + } + if(recipient){ + status_report->ra = tsms_address_da_create((const uint8_t*)recipient); + } + status_report->st = status; + status_report->mms = TSMS_TPDU_DEFAULT_MMS; + status_report->srq = submit?0:1/*SMS-COMAND*/; + } + else{ + TSK_DEBUG_ERROR("Null"); + } + return self; +} + +static tsk_object_t* tsms_tpdu_status_report_dtor(tsk_object_t * self) +{ + tsms_tpdu_status_report_t *status_report = self; + if(status_report){ + /*deinit base*/ + tsms_tpdu_message_deinit(TSMS_TPDU_MESSAGE(status_report)); + /*deinit self*/ + TSK_OBJECT_SAFE_FREE(status_report->smsc); + TSK_OBJECT_SAFE_FREE(status_report->ra); + } + else{ + TSK_DEBUG_ERROR("Null"); + } + return self; +} + +static const tsk_object_def_t tsms_tpdu_status_report_def_s = +{ + sizeof(tsms_tpdu_status_report_t), + tsms_tpdu_status_report_ctor, + tsms_tpdu_status_report_dtor, + tsk_null, +}; +const tsk_object_def_t *tsms_tpdu_status_report_def_t = &tsms_tpdu_status_report_def_s; diff --git a/tinySMS/src/tpdu/tsms_tpdu_submit.c b/tinySMS/src/tpdu/tsms_tpdu_submit.c new file mode 100644 index 0000000..22095c0 --- /dev/null +++ b/tinySMS/src/tpdu/tsms_tpdu_submit.c @@ -0,0 +1,317 @@ +/* +* 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. +* +*/ + +/**@file tsms_tpdu_submit.c + * @brief SMS TPDU SMS-SUBMIT (MO) message as per 3GPP TS 23.040 section 9.2.2.2. + * + * @author Mamadou Diop <diopmamadou(at)doubango.org> + * + + */ +#include "tinysms/tpdu/tsms_tpdu_submit.h" + +#include "tsk_string.h" +#include "tsk_memory.h" +#include "tsk_debug.h" + +#include <string.h> /* memcpy */ + +#define TSMS_ERROR_TOO_SHORT()\ + TSK_DEBUG_ERROR("SMS-SUBMIT == Data too short.");\ + failed = tsk_true;\ + goto bail; + +/** internal function used to deserialize SMS-SUBMIT function. +*/ +tsms_tpdu_message_t* _tsms_tpdu_submit_deserialize(const void* data, tsk_size_t size) +{ + /* You don't need to test data and test, this is an internal function called by tsms_tpdu_message_deserialize() */ + tsms_tpdu_submit_t* self = tsms_tpdu_submit_create(0, tsk_null, tsk_null); + tsk_bool_t failed = tsk_false; + tsk_size_t any_len; + const uint8_t* pdata = data; + const uint8_t* pend = pdata + size; + + /* SMSC address */ +#if TSMS_TPDU_APPEND_SMSC + if(!(self->smsc = tsms_address_deserialize(data, size, tsms_addr_smsc, &any_len)) || !any_len){ + TSK_DEBUG_ERROR("SMS-SUBMIT == Failed to parse SMSC address"); + failed = tsk_true; + goto bail; + } + else{ + if((pdata += any_len) >= pend){ + TSMS_ERROR_TOO_SHORT(); + } + } +#endif + + /* SMS-SUBMIT first Octect: + - TP-Message-Type-Indicator(2b) + - TP-Reject-Duplicates(1b) + - TP-Validity-Period-Format(1b) + - TP-Reply-Path(1b) + - TP-User-Data-Header-Indicator(1b) + - TP-Status-Report-Request(1b) + + +----+----+----+----+----+----+----+----+ + |RP |UDHI|SRR |VPF | RD | MTI | + +----+----+----+----+----+----+----+----+ + */ + TSMS_TPDU_MESSAGE(self)->mti = *pdata & 0x03; + self->rd = (*pdata & 0x04)>>2, + self->vpf = (*pdata & 0x18)>>3, + self->srr = (*pdata & 0x20)>>5, + self->udhi = (*pdata & 0x40)>>6, + self->rp = (*pdata & 0x80)>>7; + if((++pdata) >= pend){ + TSMS_ERROR_TOO_SHORT(); + } + + /* 3GPP TS 23.040 ==> 9.2.3.6 TP-Message-Reference (TP-MR) + * 1o */ + self->mr = *pdata; + if((++pdata) >= pend){ + TSMS_ERROR_TOO_SHORT(); + } + + /* 3GPP TS 23.040 ==> 9.2.3.8 TP-Destination-Address (TP-DA) */ + if(!(self->da = tsms_address_deserialize(pdata, (pend-pdata), tsms_addr_da, &any_len)) || !any_len){ + TSK_DEBUG_ERROR("SMS-SUBMIT == Failed to parse DA address"); + failed = tsk_true; + goto bail; + } + else{ + if((pdata += any_len) >= pend){ + TSMS_ERROR_TOO_SHORT(); + } + } + + /* 3GPP TS 23.040 ==> 9.2.3.9 TP-Protocol-Identifier (TP-PID) + * 1o */ + TSMS_TPDU_MESSAGE(self)->pid = *pdata; + if((++pdata) >= pend){ + TSMS_ERROR_TOO_SHORT(); + } + + /* 3GPP TS 23.040 ==> 9.2.3.10 TP-Data-Coding-Scheme (TP-DCS) + * 1o */ + TSMS_TPDU_MESSAGE(self)->dcs = *pdata; + if((++pdata) >= pend){ + TSMS_ERROR_TOO_SHORT(); + } + + /* 3GPP TS 23.040 ==> 9.2.3.12 TP-Validity-Period + * 1o for Relative format (9.2.3.12.1) + * 7o for Absolute format (9.2.3.12.2) + * 7o for Enhanced format (9.2.3.12.3)*/ + switch(self->vpf){ + case tsms_tpdu_vpf_relative: + any_len = 1; break; + case tsms_tpdu_vpf_enhanced: + case tsms_tpdu_vpf_absolute: + any_len = 7; break; + default: + case tsms_tpdu_vpf_not_present: + any_len = 0; break; + } + if((tsk_size_t)(pend-pdata) <= any_len){ + TSMS_ERROR_TOO_SHORT(); + } + memcpy(self->vp, pdata, any_len); + pdata += any_len; + + /* 3GPP TS 23.040 ==> 9.2.3.16 TP-User-Data-Length (TP-UDL) + * 1o */ + TSMS_TPDU_MESSAGE(self)->udl = *pdata; + pdata++; + + /* 3GPP TS 23.040 ==> 9.2.3.24 TP-User Data (TP-UD) */ + if((pend-pdata) > 0){ + TSMS_TPDU_MESSAGE(self)->ud = tsk_buffer_create(pdata, (pend-pdata)); + } + +bail: + if(failed){ + TSK_OBJECT_SAFE_FREE(self); + } + return TSMS_TPDU_MESSAGE(self); +} + + +/** internal function used to serialize a SMS-SUBMIT message +*/ +int _tsms_tpdu_submit_serialize(const tsms_tpdu_submit_t* self, tsk_buffer_t* output) +{ + uint8_t _1byte; + + if(!self){ + return -1; + } + + /* SMSC address */ +#if TSMS_TPDU_APPEND_SMSC + tsms_address_serialize(self->smsc, output); +#endif + + /* SMS-SUBMIT first Octect: + - TP-Message-Type-Indicator(2b) + - TP-Reject-Duplicates(1b) + - TP-Validity-Period-Format(1b) + - TP-Reply-Path(1b) + - TP-User-Data-Header-Indicator(1b) + - TP-Status-Report-Request(1b) + + +----+----+----+----+----+----+----+----+ + |RP |UDHI|SRR |VPF | RD | MTI | + +----+----+----+----+----+----+----+----+ + */ + _1byte = (TSMS_TPDU_MESSAGE(self)->mti & 0xF3); /*2b*/ + _1byte |= ((uint8_t)self->rd) << 2 /*1b*/ + | ((uint8_t)self->vpf) << 3 /*2b*/ + | ((uint8_t)self->srr) << 5 /*1b*/ + | ((uint8_t)self->udhi) << 6; /*1b*/ + tsk_buffer_append(output, &_1byte, 1); + + /* 3GPP TS 23.040 ==> 9.2.3.6 TP-Message-Reference (TP-MR) */ + tsk_buffer_append(output, &self->mr, 1); /*0-255 ==> 1o*/ + + /* 3GPP TS 23.040 ==> 9.2.3.8 TP-Destination-Address (TP-DA) */ + tsms_address_serialize(self->da, output); + + /* 3GPP TS 23.040 ==> 9.2.3.9 TP-Protocol-Identifier (TP-PID) */ + tsk_buffer_append(output, &TSMS_TPDU_MESSAGE(self)->pid, 1); /*1o*/ + + /* 3GPP TS 23.040 ==> 9.2.3.10 TP-Data-Coding-Scheme (TP-DCS) */ + tsk_buffer_append(output, &TSMS_TPDU_MESSAGE(self)->dcs, 1); /*1o*/ + + /* 3GPP TS 23.040 ==> 9.2.3.12 TP-Validity-Period + * 1o for Relative format (9.2.3.12.1) + * 7o for Absolute format (9.2.3.12.2) + * 7o for Enhanced format (9.2.3.12.3)*/ + switch(self->vpf){ + case tsms_tpdu_vpf_relative: + tsk_buffer_append(output, &self->vp, 1); + break; + case tsms_tpdu_vpf_enhanced: + case tsms_tpdu_vpf_absolute: + tsk_buffer_append(output, &self->vp, 7); + break; + default: + case tsms_tpdu_vpf_not_present: + break; + } + + /* 3GPP TS 23.040 ==> 9.2.3.16 TP-User-Data-Length (TP-UDL) */ + tsk_buffer_append(output, &TSMS_TPDU_MESSAGE(self)->udl, 1); /*1o*/ + + /* 3GPP TS 23.040 ==> 9.2.3.24 TP-User Data (TP-UD) */ + tsk_buffer_append(output, TSK_BUFFER_DATA(TSMS_TPDU_MESSAGE(self)->ud), TSK_BUFFER_SIZE(TSMS_TPDU_MESSAGE(self)->ud)); + + return 0; +} + +/**@ingroup tsms_tpdu_group +* Creates new @a SMS-SUBMIT message. +* @a SMS-SUBMIT messages are used to convey short messages from the MS (Mobile Station) to the SC (Service Center).<br> +* For more information, please refer to 3GPP TS 23.040 section 9.2.2.2. +* @param mr TP-Message-Reference (TP-MR) as per 3GPP TS 23.040 section 9.2.3.6. +* @param smsc The address of the SMS. e.g. "+331253688". +* @param dest The destination address. e.g. "+331253688". +* @retval SMS-SUBMIT message. +* +* See For more information, see @ref tsms_tpdu_group_SUBMIT "SMS-SUBMIT". +*/ +tsms_tpdu_submit_t* tsms_tpdu_submit_create(uint8_t mr, const tsms_address_string_t smsc, const tsms_address_string_t dest) +{ + tsms_tpdu_submit_t* ret = tsk_null; + + if(!(ret = tsk_object_new(tsms_tpdu_submit_def_t, mr, smsc, dest))){ + goto bail; + } + +bail: + return ret; +} + +//================================================================================================= +// SMS TPDU SMS-SUMBIT object definition +// +static tsk_object_t* tsms_tpdu_submit_ctor(tsk_object_t * self, va_list * app) +{ + tsms_tpdu_submit_t *submit = self; + if(submit){ + const char* smsc, *dest; + uint8_t mr; + +#if defined(__GNUC__) + mr = (uint8_t)va_arg(*app, unsigned); +#else + mr = va_arg(*app, uint8_t); +#endif + smsc = va_arg(*app, const char*); + dest = va_arg(*app, const char*); + + /* init base*/ + tsms_tpdu_message_init(TSMS_TPDU_MESSAGE(submit), tsms_tpdu_mti_submit_mo); + /* init self */ + submit->mr = mr; + if(smsc){ + submit->smsc = tsms_address_smsc_create((const uint8_t*)smsc); + } + if(dest){ + submit->da = tsms_address_da_create((const uint8_t*)dest); + } + + submit->vp[0] = TSMS_TPDU_DEFAULT_VP; + submit->vpf = TSMS_TPDU_DEFAULT_VPF; + } + else{ + TSK_DEBUG_ERROR("Null"); + } + return self; +} + +static tsk_object_t* tsms_tpdu_submit_dtor(tsk_object_t * self) +{ + tsms_tpdu_submit_t *submit = self; + if(submit){ + /*deinit base*/ + tsms_tpdu_message_deinit(TSMS_TPDU_MESSAGE(submit)); + /*deinit self*/ + TSK_OBJECT_SAFE_FREE(submit->smsc); + TSK_OBJECT_SAFE_FREE(submit->da); + } + else{ + TSK_DEBUG_ERROR("Null"); + } + return self; +} + +static const tsk_object_def_t tsms_tpdu_submit_def_s = +{ + sizeof(tsms_tpdu_submit_t), + tsms_tpdu_submit_ctor, + tsms_tpdu_submit_dtor, + tsk_null, +}; +const tsk_object_def_t *tsms_tpdu_submit_def_t = &tsms_tpdu_submit_def_s; |