Skip to content

Commit

Permalink
assorted fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pfoltyn committed Jul 31, 2015
1 parent 880ca2b commit c3aa2dc
Show file tree
Hide file tree
Showing 33 changed files with 10,899 additions and 193 deletions.
10 changes: 5 additions & 5 deletions Makefile.in
Expand Up @@ -3,7 +3,7 @@ PROJS = chan_dongles.so

chan_donglem_so_OBJS = app.o at_command.o at_parse.o at_queue.o at_read.o at_response.o \
chan_dongle.o channel.o char_conv.o cli.o helpers.o manager.o \
memmem.o ringbuffer.o cpvt.o dc_config.o pdu.o mixbuffer.o pdiscovery.o
memmem.o ringbuffer.o cpvt.o dc_config.o pdu.o mixbuffer.o pdiscovery.o at_log.o

chan_dongles_so_OBJS = single.o

Expand All @@ -13,15 +13,15 @@ discovery_OBJS = tools/discovery.o tools/tty.o

SOURCES = app.c at_command.c at_parse.c at_queue.c at_read.c at_response.c \
chan_dongle.c channel.c char_conv.c cli.c cpvt.c dc_config.c helpers.c \
manager.c memmem.c ringbuffer.c single.c pdu.c mixbuffer.c pdiscovery.c
manager.c memmem.c ringbuffer.c single.c pdu.c mixbuffer.c pdiscovery.c at_log.c

test_SOURCES = test/test1.c test/parse.c
tools_SOURCES = tools/discovery.c tools/tty.c

HEADERS = app.h at_command.h at_parse.h at_queue.h at_read.h at_response.h \
chan_dongle.h channel.h char_conv.h cli.h cpvt.h dc_config.h export.h \
helpers.h manager.h memmem.h ringbuffer.h pdu.h mixbuffer.h pdiscovery.h \
mutils.h
mutils.h at_log.h

tools_HEADERS = tools/tty.h

Expand All @@ -37,8 +37,8 @@ RM = @RM@ -fr
INSTALL = @INSTALL@
CHMOD = chmod

# -DAST_MODULE=\"$(PROJM)\" -D_THREAD_SAFE
CFLAGS = @CFLAGS@ -I$(srcdir) @CPPFLAGS@ @DEFS@ @AC_CFLAGS@
DEFS = -DASTERISK_VERSION_NUM=110000
CFLAGS = @CFLAGS@ -I$(srcdir) @CPPFLAGS@ $(DEFS) @DEFS@ @AC_CFLAGS@
LDFLAGS = @LDFLAGS@
SOLINK = @SOLINK@
LIBS = @LIBS@
Expand Down
2 changes: 1 addition & 1 deletion app.c
Expand Up @@ -18,7 +18,7 @@
#include <asterisk/app.h> /* AST_DECLARE_APP_ARGS() ... */
#include <asterisk/pbx.h> /* pbx_builtin_setvar_helper() */
#include <asterisk/module.h> /* ast_register_application2() ast_unregister_application() */
#include <asterisk/version.h> /* ASTERISK_VERSION_NUM */
#include <asterisk/ast_version.h> /* ASTERISK_VERSION_NUM */

#include "app.h" /* app_register() app_unregister() */
#include "chan_dongle.h" /* struct pvt */
Expand Down
35 changes: 29 additions & 6 deletions at_command.c
Expand Up @@ -30,6 +30,7 @@
#include "pdu.h" /* build_pdu() */

static const char cmd_at[] = "AT\r";
static const char cmd_cnma[] = "AT+CNMA\r";
static const char cmd_chld1x[] = "AT+CHLD=1%d\r";
static const char cmd_chld2[] = "AT+CHLD=2\r";
static const char cmd_clcc[] = "AT+CLCC\r";
Expand Down Expand Up @@ -113,6 +114,9 @@ EXPORT_DEF int at_enque_initialization(struct cpvt* cpvt, at_cmd_t from_command)
static const char cmd2[] = "ATZ\r";
static const char cmd3[] = "ATE0\r";

static const char cmd_fish[] = "AT+CSMS=1\r";
static const char cmd_soup[] = "AT+CSMP=49,\r";

static const char cmd5[] = "AT+CGMI\r";
static const char cmd6[] = "AT+CSCA?\r";
static const char cmd7[] = "AT+CGMM\r";
Expand All @@ -133,8 +137,8 @@ EXPORT_DEF int at_enque_initialization(struct cpvt* cpvt, at_cmd_t from_command)
static const char cmd19[] = "AT+CSSN=1,1\r";
static const char cmd21[] = "AT+CSCS=\"UCS2\"\r";

static const char cmd22[] = "AT+CPMS=\"ME\",\"ME\",\"ME\"\r";
static const char cmd23[] = "AT+CNMI=2,1,0,0,0\r";
static const char cmd22[] = "AT+CPMS=\"SM\",\"SM\",\"SM\"\r";
static const char cmd23[] = "AT+CNMI=2,1,0,2,0\r";
static const char cmd24[] = "AT+CSQ\r";

static const at_queue_cmd_t st_cmds[] = {
Expand All @@ -158,6 +162,9 @@ EXPORT_DEF int at_enque_initialization(struct cpvt* cpvt, at_cmd_t from_command)
ATQ_CMD_DECLARE_ST(CMD_AT_CNUM, cmd16), /* Get Subscriber number */
ATQ_CMD_DECLARE_ST(CMD_AT_CVOICE, cmd17), /* read the current voice mode, and return sampling rate、data bit、frame period */

ATQ_CMD_DECLARE_ST(CMD_AT_CNMI, cmd_fish),
ATQ_CMD_DECLARE_ST(CMD_AT_CNMI, cmd_soup),

ATQ_CMD_DECLARE_ST(CMD_AT_CSCA, cmd6), /* Get SMS Service center address */
// ATQ_CMD_DECLARE_ST(CMD_AT_CLIP, cmd18), /* disable Calling line identification presentation in unsolicited response +CLIP: <number>,<type>[,<subaddr>,<satype>[,[<alpha>][,<CLI validitity>]] */
ATQ_CMD_DECLARE_ST(CMD_AT_CSSN, cmd19), /* activate Supplementary Service Notification with CSSI and CSSU */
Expand Down Expand Up @@ -261,17 +268,18 @@ EXPORT_DEF int at_enque_pdu(struct cpvt * cpvt, const char * pdu, attribute_unus
return -EINVAL;
}

at_cmd[1].data = ast_malloc(length + 2);
at_cmd[1].data = ast_malloc(length + 3);
if(!at_cmd[1].data)
{
return -ENOMEM;
}

at_cmd[1].length = length + 1;
at_cmd[1].length = length + 2;

memcpy(at_cmd[1].data, pdu, length);
at_cmd[1].data[length] = 0x1A;
at_cmd[1].data[length+1] = 0x0;
at_cmd[1].data[length] = 0x1A;
at_cmd[1].data[length+1] = 0x1A;
at_cmd[1].data[length+2] = 0x0;

at_cmd[0].length = snprintf(buf, sizeof(buf), "AT+CMGS=%d\r", (int)(pdulen / 2));
at_cmd[0].data = ast_strdup(buf);
Expand Down Expand Up @@ -707,6 +715,21 @@ EXPORT_DEF int at_enque_user_cmd(struct cpvt* cpvt, const char * input)
return at_enque_generic(cpvt, CMD_USER, 1, "%s\r", input);
}

/*!
* \brief Enque commands for acking SMS
* \param cpvt -- cpvt structure
* \return 0 on success
*/
EXPORT_DEF int at_enque_ack_sms (struct cpvt* cpvt)
{
int err;
at_queue_cmd_t cmds[] = {
ATQ_CMD_DECLARE_STIT(CMD_AT_CNMA, cmd_cnma, ATQ_CMD_TIMEOUT_40S, 0),
};

return at_queue_insert_const(cpvt, cmds, ITEMS_OF(cmds), 1);
}

/*!
* \brief Enque commands for reading SMS
* \param cpvt -- cpvt structure
Expand Down
3 changes: 3 additions & 0 deletions at_command.h
Expand Up @@ -35,6 +35,7 @@ typedef enum {
CMD_AT_CMGD,
CMD_AT_CMGF,
CMD_AT_CMGR,
CMD_AT_CNMA,

CMD_AT_CMGS,
CMD_AT_SMSTEXT,
Expand Down Expand Up @@ -104,6 +105,7 @@ INLINE_DECL const char* at_cmd2str (at_cmd_t cmd)
"AT+CMGD",
"AT+CMGF",
"AT+CMGR",
"AT+CNMA",

"AT+CMGS",
"SMSTEXT",
Expand Down Expand Up @@ -151,6 +153,7 @@ EXPORT_DECL int at_enque_initialization(struct cpvt * cpvt, at_cmd_t from_comman
EXPORT_DECL int at_enque_ping (struct cpvt * cpvt);
EXPORT_DECL int at_enque_cops (struct cpvt * cpvt);
EXPORT_DECL int at_enque_sms (struct cpvt * cpvt, const char * number, const char * msg, unsigned validity_min, int report_req, void ** id);
EXPORT_DECL int at_enque_ack_sms (struct cpvt * cpvt);
EXPORT_DECL int at_enque_pdu (struct cpvt * cpvt, const char * pdu, attribute_unused const char *, attribute_unused unsigned, attribute_unused int, void ** id);
EXPORT_DECL int at_enque_ussd (struct cpvt * cpvt, const char * code, attribute_unused const char *, attribute_unused unsigned, attribute_unused int, void ** id);
EXPORT_DECL int at_enque_dtmf (struct cpvt * cpvt, char digit);
Expand Down
169 changes: 169 additions & 0 deletions at_log.c
@@ -0,0 +1,169 @@
/*
Copyright (C) 2013
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */

#include <asterisk.h>
#include <asterisk/utils.h> /* ast_free() */

#include "channel.h"
#include "at_log.h"

#include <fcntl.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>


static int log_fd = -1;


static int
get_logfd (void)
{
if (log_fd < 0)
log_fd =
open ("/var/log/asterisk/dongle.log", O_WRONLY | O_APPEND | O_CREAT,
0666);
if (log_fd < 0)
return -1;
return 0;

}


static char *
now (void)
{
static char ret[1024];
char *ptr;

time_t t;
time (&t);


ret[0] = 0;
ptr = ctime (&t);

if (ptr)
strncpy (ret, ptr, sizeof (ret));

ptr = index ((char *) ret, '\n');
if (ptr)
*ptr = 0;

ptr = index ((char *) ret, '\r');
if (ptr)
*ptr = 0;

return ret;
}



static void
append_str (unsigned char **buf, size_t * len, const char *_ptr)
{
const unsigned char *ptr = (unsigned char *) _ptr;
while (*ptr && *len)
{
*((*buf)++) = *(ptr++);
(*len)--;
}
}

static void
append_escape (unsigned char **buf, size_t * len, unsigned char c)
{
char e[8];

switch (c)
{
case '\0':
append_str (buf, len, "\\0");
break;
case '\r':
append_str (buf, len, "\\r");
break;
case '\n':
append_str (buf, len, "\\n");
break;
case '\t':
append_str (buf, len, "\\t");
break;
default:
snprintf (e, sizeof (e), "\\x%02x", c);
append_str (buf, len, e);
}
}

static void
append_buf (unsigned char **buf, size_t * len, unsigned char *ptr,
size_t alen)
{
while (alen && *len)
{
if (((*ptr < 32) || (*ptr > 126)) && (*ptr != '\\'))
{
append_escape (buf, len, *(ptr++));
}
else
{
*((*buf)++) = *(ptr++);
(*len)--;
}
alen--;
}
}




EXPORT_DECL void
at_logv (struct pvt *pvt, const char *prefix, const struct iovec *iov,
size_t iov_count)
{
unsigned char buf[4096];
unsigned char *ptr = buf;
size_t blen = sizeof (buf);

if (get_logfd ())
return;

blen-=2;

append_str (&ptr, &blen, now ());

append_str (&ptr, &blen, " [");
if (pvt)
append_str (&ptr, &blen, PVT_ID (pvt));
else
append_str (&ptr, &blen, "unknown");
append_str (&ptr, &blen, "] ");
append_str (&ptr, &blen, prefix);
append_str (&ptr, &blen, " ");

while (iov_count--)
{
append_buf (&ptr, &blen, (unsigned char *) iov->iov_base, iov->iov_len);
iov++;
}
*(ptr++) = '\n';

write (log_fd, buf, (size_t) (ptr - buf));
}

EXPORT_DECL void
at_log (struct pvt *pvt, const char *prefix, const void *buf, size_t len)
{
struct iovec iov[1];

iov->iov_base = buf;
iov->iov_len = len;

at_logv (pvt, prefix, iov, 1);
}
18 changes: 18 additions & 0 deletions at_log.h
@@ -0,0 +1,18 @@
#ifndef CHAN_DONGLE_AT_LOG_H_INCLUDED
#define CHAN_DONGLE_AT_LOG_H_INCLUDED

#include <sys/time.h> /* struct timeval */

#include <asterisk.h>
#include <asterisk/linkedlists.h> /* AST_LIST_ENTRY */

#include "chan_dongle.h"
#include "at_log.h"
#include "export.h" /* EXPORT_DECL EXPORT_DEF */


EXPORT_DECL void at_logv(struct pvt *pvt, const char *prefix, const struct iovec *iov, size_t iov_count);
EXPORT_DECL void at_log(struct pvt *pvt, const char *prefix, const void *buf, size_t len);

#endif

0 comments on commit c3aa2dc

Please sign in to comment.