More accurate detection of USSD codes

This commit is contained in:
Pierre Pronchery 2014-02-20 00:55:47 +01:00
parent 6ccce8ff31
commit 86a1c498ec
6 changed files with 102 additions and 12 deletions

View File

@ -154,6 +154,7 @@ dist:
$(PACKAGE)-$(VERSION)/src/plugins/48x48/project.conf \
$(PACKAGE)-$(VERSION)/tests/modems.c \
$(PACKAGE)-$(VERSION)/tests/plugins.c \
$(PACKAGE)-$(VERSION)/tests/ussd.c \
$(PACKAGE)-$(VERSION)/tests/Makefile \
$(PACKAGE)-$(VERSION)/tests/tests.sh \
$(PACKAGE)-$(VERSION)/tests/project.conf \

View File

@ -1,5 +1,5 @@
/* $Id$ */
/* Copyright (c) 2011-2013 Pierre Pronchery <khorben@defora.org> */
/* Copyright (c) 2011-2014 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS Desktop Phone */
/* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -371,6 +371,7 @@ static void _on_code_ext_error(ModemPlugin * modem, char const * answer);
/* helpers */
static int _is_figure(int c);
static int _is_number(char const * number);
static int _is_ussd_code(char const * number);
/* variables */
@ -759,8 +760,6 @@ static char * _request_attention(ModemPlugin * modem, ModemRequest * request)
{
unsigned int type = request->type;
char buf[32];
char const * p;
size_t len;
switch(type)
{
@ -784,9 +783,7 @@ static char * _request_attention(ModemPlugin * modem, ModemRequest * request)
break;
case MODEM_REQUEST_CALL:
if(request->call.call_type == MODEM_CALL_TYPE_VOICE
&& (p = request->call.number) != NULL
&& (len = strlen(p)) > 2
&& p[0] == '*' && p[len - 1] == '#')
&& _is_ussd_code(request->call.number))
return _request_attention_call_ussd(modem,
request);
return _request_attention_call(modem, request);
@ -4086,3 +4083,19 @@ static int _is_number(char const * number)
return 0;
return 1;
}
/* is_ussd_code */
static int _is_ussd_code(char const * number)
{
if(number == NULL || number[0] != '*')
return 0;
for(number++; *number != '\0'; number++)
if((*number >= '0' && *number <= '9') || *number == '*')
continue;
else if(*number == '#' && *(number + 1) == '\0')
return 1;
else
return 0;
return 0;
}

View File

@ -1,4 +1,4 @@
TARGETS = modems plugins tests.log
TARGETS = modems plugins ussd tests.log
PREFIX = /usr/local
DESTDIR =
BINDIR = $(PREFIX)/bin
@ -31,7 +31,14 @@ plugins_LDFLAGS = $(LDFLAGSF) $(LDFLAGS)
plugins: $(plugins_OBJS)
$(CC) -o plugins $(plugins_OBJS) $(plugins_LDFLAGS)
tests.log: modems plugins tests.sh
ussd_OBJS = ussd.o
ussd_CFLAGS = $(CPPFLAGSF) $(CPPFLAGS) $(CFLAGSF) $(CFLAGS)
ussd_LDFLAGS = $(LDFLAGSF) $(LDFLAGS)
ussd: $(ussd_OBJS)
$(CC) -o ussd $(ussd_OBJS) $(ussd_LDFLAGS)
tests.log: modems plugins tests.sh ussd
./tests.sh -P "$(PREFIX)" -- "tests.log"
modems.o: modems.c
@ -40,8 +47,11 @@ modems.o: modems.c
plugins.o: plugins.c
$(CC) $(plugins_CFLAGS) -c plugins.c
ussd.o: ussd.c ../src/modems/hayes.c
$(CC) $(ussd_CFLAGS) -c ussd.c
clean:
$(RM) -- $(modems_OBJS) $(plugins_OBJS) $(tests.log_OBJS)
$(RM) -- $(modems_OBJS) $(plugins_OBJS) $(ussd_OBJS) $(tests.log_OBJS)
./tests.sh -c -P "$(PREFIX)" -- "tests.log"
distclean: clean

View File

@ -1,4 +1,4 @@
targets=modems,plugins,tests.log
targets=modems,plugins,ussd,tests.log
cppflags_force=-I ../include
cflags_force=-W `pkg-config --cflags libDesktop`
cflags=-Wall -g -O2
@ -16,4 +16,11 @@ sources=plugins.c
[tests.log]
type=script
script=./tests.sh
depends=modems,plugins,tests.sh
depends=modems,plugins,tests.sh,ussd
[ussd]
type=binary
sources=ussd.c
[ussd.c]
depends=../src/modems/hayes.c

View File

@ -98,7 +98,7 @@ target="$1"
$DATE > "$target"
FAILED=
echo "Performing tests:" 1>&2
#_test "plugins"
_test "ussd"
echo "Expected failures:" 1>&2
_fail "modems"
_fail "plugins"

59
tests/ussd.c Normal file
View File

@ -0,0 +1,59 @@
/* $Id$ */
/* Copyright (c) 2014 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS Desktop Phone */
/* This program 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, version 3 of the License.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */
#include "../src/modems/hayes.c"
/* private */
/* prototypes */
static int _ussd(void);
/* functions */
/* ussd */
static int _ussd(void)
{
int ret = 0;
const char * codes[] = { "*100#" };
const char * notcodes[] = { "*#06#" };
size_t i;
for(i = 0; i < sizeof(codes) / sizeof(*codes); i++)
if(!_is_ussd_code(codes[i]))
{
printf("%s: %s: %s\n", "ussd", codes[i],
"Is a valid USSD code");
ret = 2;
}
for(i = 0; i < sizeof(notcodes) / sizeof(*notcodes); i++)
if(_is_ussd_code(notcodes[i]))
{
printf("%s: %s: %s\n", "ussd", notcodes[i],
"Is not a valid USSD code");
ret = 2;
}
return ret;
}
/* public */
/* functions */
/* main */
int main(void)
{
return (_ussd() == 0) ? 0 : 2;
}