doubango/trunk/tinySMS/src/tsms_address.c

153 lines
3.8 KiB
C

/*
* 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_address.c
* @brief SMS address encoder/decoder.
*
* @author Mamadou Diop <diopmamadou(at)doubango.org>
*
* @date Created: Sat Nov 8 16:54:58 2009 mdiop
*/
#include "tinySMS/tsms_address.h"
#include "tsk_string.h"
#include "tsk_memory.h"
#include <string.h>
/** swaps the address from 'abcd' to 'badc'
*/
char* tsms_address_swap(const char* in)
{
size_t i, in_len;
char* ret = tsk_null;
if(tsk_strnullORempty(in)){
goto bail;
}
in_len = strlen(in);
ret = tsk_calloc(in_len + 2/*\0 and trainling F*/, sizeof(uint8_t));
if(in_len>=2){
for(i=0; i<in_len; i+=2){
ret[i] = in[i+1];
ret[i+1] = in[i];
}
}
if(in_len & 0x01){ /* odd number? */
ret[i-2] = 'F';
ret[i-1] = in[i-2];
}
bail:
return ret;
}
/** Serialize the address as per 3GPP TS 23.040 v910 section 9.1.2.5. */
int tsms_address_serialize(const tsms_address_t* address, tsk_buffer_t* output)
{
char* number = tsk_null;
size_t len = 0;
uint8_t type_of_address;
if(!address ||!output){
return -1;
}
/* For more information see 3GPP TS 23.040 v910 section 9.1.2.5
1 - Address-Length
2 - Type-of-Address
+----+----+----+----+----+----+----+----+
| 1 | TON | NPI |
+----+----+----+----+----+----+----+----+
3 - Phone number in semi octets
*/
number = tsms_address_swap(address->digits);
if(number){
len = (address->type == tsms_addr_smsc) ?
((strlen(number)/2) + 1) /* Number of octets plus 1. */
: strlen(number); /* Number of BCD digits */
}
/* 1 - Address-Length */
tsk_buffer_append(output, &len, 1);
/* 2 - Type-of-Address */
type_of_address = (address->npi | (((address->ton << 4) | 0x80)));
tsk_buffer_append(output, &type_of_address, 1);
/* 3 - Phone number in semi octets */
tsk_buffer_append(output, number, len);
TSK_FREE(number);
return 0;
}
//=================================================================================================
// SMS Address object definition
//
static tsk_object_t* tsms_address_create(tsk_object_t * self, va_list * app)
{
tsms_address_t *address = self;
if(address){
const char* digits = va_arg(*app, const char*);
address->type = va_arg(*app, tsms_address_type_t);
if(!tsk_strnullORempty(digits)){
if(*digits == '+'){
address->npi = tsms_addr_npi_isdn;
address->ton = tsms_addr_ton_international;
address->digits = tsk_strdup((digits+1));
}
else{
address->npi = tsms_addr_npi_national;
address->ton = tsms_addr_ton_national;
address->digits = tsk_strdup((digits));
}
}
else{
address->npi = tsms_addr_npi_unknown;
address->ton = tsms_addr_ton_unknown;
}
}
return self;
}
static tsk_object_t* tsms_address_destroy(tsk_object_t * self)
{
tsms_address_t *address = self;
if(address){
TSK_FREE(address->digits);
}
return self;
}
static const tsk_object_def_t tsms_address_def_s =
{
sizeof(tsms_address_t),
tsms_address_create,
tsms_address_destroy,
tsk_null,
};
const tsk_object_def_t *tsms_address_def_t = &tsms_address_def_s;