--- app_sms.c.147592 2008-10-09 09:21:32.000000000 +1300 +++ app_sms.c 2008-10-13 22:56:00.000000000 +1300 @@ -257,8 +257,9 @@ /*! \brief copy number, skipping non digits apart from leading + */ static void numcpy(char *d, char *s) { - if (*s == '+') + if (*s == '+'){ *d++ = *s++; + } while (*s) { if (isdigit(*s)) { *d++ = *s; @@ -284,40 +285,47 @@ static long utf8decode(unsigned char **pp) { unsigned char *p = *pp; - if (!*p) + if (!*p) { return 0; /* null termination of string */ + } (*pp)++; - if (*p < 0xC0) + if (*p < 0xC0) { return *p; /* ascii or continuation character */ + } if (*p < 0xE0) { - if (*p < 0xC2 || (p[1] & 0xC0) != 0x80) + if (*p < 0xC2 || (p[1] & 0xC0) != 0x80) { return *p; /* not valid UTF-8 */ + } (*pp)++; return ((*p & 0x1F) << 6) + (p[1] & 0x3F); } if (*p < 0xF0) { - if ((*p == 0xE0 && p[1] < 0xA0) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80) + if ((*p == 0xE0 && p[1] < 0xA0) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80) { return *p; /* not valid UTF-8 */ + } (*pp) += 2; return ((*p & 0x0F) << 12) + ((p[1] & 0x3F) << 6) + (p[2] & 0x3F); } if (*p < 0xF8) { - if ((*p == 0xF0 && p[1] < 0x90) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80 || (p[3] & 0xC0) != 0x80) + if ((*p == 0xF0 && p[1] < 0x90) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80 || (p[3] & 0xC0) != 0x80) { return *p; /* not valid UTF-8 */ + } (*pp) += 3; return ((*p & 0x07) << 18) + ((p[1] & 0x3F) << 12) + ((p[2] & 0x3F) << 6) + (p[3] & 0x3F); } if (*p < 0xFC) { if ((*p == 0xF8 && p[1] < 0x88) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80 || (p[3] & 0xC0) != 0x80 - || (p[4] & 0xC0) != 0x80) + || (p[4] & 0xC0) != 0x80) { return *p; /* not valid UTF-8 */ + } (*pp) += 4; return ((*p & 0x03) << 24) + ((p[1] & 0x3F) << 18) + ((p[2] & 0x3F) << 12) + ((p[3] & 0x3F) << 6) + (p[4] & 0x3F); } if (*p < 0xFE) { if ((*p == 0xFC && p[1] < 0x84) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80 || (p[3] & 0xC0) != 0x80 - || (p[4] & 0xC0) != 0x80 || (p[5] & 0xC0) != 0x80) + || (p[4] & 0xC0) != 0x80 || (p[5] & 0xC0) != 0x80) { return *p; /* not valid UTF-8 */ + } (*pp) += 5; return ((*p & 0x01) << 30) + ((p[1] & 0x3F) << 24) + ((p[2] & 0x3F) << 18) + ((p[3] & 0x3F) << 12) + ((p[4] & 0x3F) << 6) + (p[5] & 0x3F); } @@ -335,8 +343,9 @@ unsigned char n = 0; /* output character count */ unsigned char dummy[SMSLEN]; - if (o == NULL) /* output to a dummy buffer if o not set */ + if (o == NULL) { /* output to a dummy buffer if o not set */ o = dummy; + } if (udhl) { /* header */ o[p++] = udhl; @@ -349,14 +358,16 @@ b -= 7; n++; } - if (n >= SMSLEN) + if (n >= SMSLEN) { return n; + } } if (b) { b = 7 - b; - if (++n >= SMSLEN) + if (++n >= SMSLEN) { return n; - }; /* filling to septet boundary */ + } + } /* filling to septet boundary */ } o[p] = 0; /* message */ @@ -382,8 +393,9 @@ n++; } } - if (v == 128) + if (v == 128) { return -1; /* invalid character */ + } /* store, same as above */ o[p] |= (v << b); b += 7; @@ -392,8 +404,9 @@ p++; o[p] = (v >> (7 - b)); } - if (++n >= SMSLEN) + if (++n >= SMSLEN) { return n; + } } return n; } @@ -416,18 +429,21 @@ o[p++] = udhl; while (udhl--) { o[p++] = *udh++; - if (p >= SMSLEN_8) + if (p >= SMSLEN_8) { return p; + } } } while (udl--) { long u; u = *ud++; - if (u < 0 || u > 0xFF) + if (u < 0 || u > 0xFF) { return -1; /* not valid */ + } o[p++] = u; - if (p >= SMSLEN_8) + if (p >= SMSLEN_8) { return p; + } } return p; } @@ -452,19 +468,22 @@ o[p++] = udhl; while (udhl--) { o[p++] = *udh++; - if (p >= SMSLEN_8) + if (p >= SMSLEN_8) { return p; + } } } while (udl--) { long u; u = *ud++; o[p++] = (u >> 8); - if (p >= SMSLEN_8) + if (p >= SMSLEN_8) { return p - 1; /* could not fit last character */ + } o[p++] = u; - if (p >= SMSLEN_8) + if (p >= SMSLEN_8) { return p; + } } return p; } @@ -474,27 +493,30 @@ static int packsms(unsigned char dcs, unsigned char *base, unsigned int udhl, unsigned char *udh, int udl, unsigned short *ud) { unsigned char *p = base; - if (udl == 0) + if (udl == 0) { *p++ = 0; /* no user data */ - else { + } else { int l = 0; if (is7bit(dcs)) { /* 7 bit */ l = packsms7(p + 1, udhl, udh, udl, ud); - if (l < 0) + if (l < 0) { l = 0; + } *p++ = l; p += (l * 7 + 7) / 8; } else if (is8bit(dcs)) { /* 8 bit */ l = packsms8(p + 1, udhl, udh, udl, ud); - if (l < 0) + if (l < 0) { l = 0; + } *p++ = l; p += l; } else { /* UCS-2 */ l = packsms16(p + 1, udhl, udh, udl, ud); - if (l < 0) + if (l < 0) { l = 0; + } *p++ = l; p += l; } @@ -522,10 +544,11 @@ *o++ = ((t.tm_hour % 10) << 4) + t.tm_hour / 10; *o++ = ((t.tm_min % 10) << 4) + t.tm_min / 10; *o++ = ((t.tm_sec % 10) << 4) + t.tm_sec / 10; - if (z < 0) + if (z < 0) { *o++ = (((-z) % 10) << 4) + (-z) / 10 + 0x08; - else + } else { *o++ = ((z % 10) << 4) + z / 10; + } } /*! \brief unpack a date and return */ @@ -540,10 +563,11 @@ t.tm_min = (i[4] & 0xF) * 10 + (i[4] >> 4); t.tm_sec = (i[5] & 0xF) * 10 + (i[5] >> 4); t.tm_isdst = 0; - if (i[6] & 0x08) + if (i[6] & 0x08) { t.tm_min += 15 * ((i[6] & 0x7) * 10 + (i[6] >> 4)); - else + } else { t.tm_min -= 15 * ((i[6] & 0x7) * 10 + (i[6] >> 4)); + } return ast_mktime(&t, NULL); } @@ -569,8 +593,9 @@ while (b >= 7) { b -= 7; l--; - if (!l) + if (!l) { break; + } } } /* adjust for fill, septets */ @@ -582,20 +607,22 @@ } while (l--) { unsigned char v; - if (b < 2) + if (b < 2) { v = ((i[p] >> b) & 0x7F); /* everything in one byte */ - else + } else { v = ((((i[p] >> b) + (i[p + 1] << (8 - b)))) & 0x7F); + } b += 7; if (b >= 8) { b -= 8; p++; } /* 0x00A0 is the encoding of ESC (27) in defaultalphabet */ - if (o > ud && o[-1] == 0x00A0 && escapes[v]) + if (o > ud && o[-1] == 0x00A0 && escapes[v]) { o[-1] = escapes[v]; - else + } else { *o++ = defaultalphabet[v]; + } } *udl = (o - ud); } @@ -621,8 +648,9 @@ } } } - while (l--) + while (l--) { *o++ = *i++; /* not to UTF-8 as explicitly 8 bit coding in DCS */ + } *udl = (o - ud); } @@ -648,8 +676,9 @@ } while (l--) { int v = *i++; - if (l--) + if (l--) { v = (v << 8) + *i++; + } *o++ = v; } *udl = (o - ud); @@ -662,10 +691,11 @@ if (is7bit(dcs)) { unpacksms7(i, l, udh, udhl, ud, udl, udhi); l = (l * 7 + 7) / 8; /* adjust length to return */ - } else if (is8bit(dcs)) + } else if (is8bit(dcs)) { unpacksms8(i, l, udh, udhl, ud, udl, udhi); - else + } else { unpacksms16(i, l, udh, udhl, ud, udl, udhi); + } return l + 1; } @@ -674,13 +704,15 @@ { unsigned char l = i[0], p; - if (i[1] == 0x91) + if (i[1] == 0x91) { *o++ = '+'; + } for (p = 0; p < l; p++) { - if (p & 1) + if (p & 1) { *o++ = (i[2 + p / 2] >> 4) + '0'; - else + } else { *o++ = (i[2 + p / 2] & 0xF) + '0'; + } } *o = 0; return (l + 5) / 2; @@ -694,19 +726,23 @@ if (*i == '+') { /* record as bit 0 in byte 1 */ i++; o[1] = 0x91; - } else + } else { o[1] = 0x81; + } for ( ; *i ; i++) { - if (!isdigit(*i)) /* ignore non-digits */ + if (!isdigit(*i)) { /* ignore non-digits */ continue; - if (o[0] & 1) + } + if (o[0] & 1) { o[p++] |= ((*i & 0xF) << 4); - else + } else { o[p] = (*i & 0xF); + } o[0]++; } - if (o[0] & 1) + if (o[0] & 1) { o[p++] |= 0xF0; /* pad */ + } return p; } @@ -715,16 +751,18 @@ { int o; - if (*h->oa == '\0' && *h->da == '\0') + if (*h->oa == '\0' && *h->da == '\0') { return; + } o = open(log_file, O_CREAT | O_APPEND | O_WRONLY, AST_FILE_MODE); if (o >= 0) { char line[1000], mrs[3] = "", *p; char buf[30]; unsigned char n; - if (h->mr >= 0) + if (h->mr >= 0) { snprintf(mrs, sizeof(mrs), "%02X", h->mr); + } snprintf(line, sizeof(line), "%s %c%c%c%s %s %s %s ", isodate(time(NULL), buf, sizeof(buf)), status, h->rx ? 'I' : 'O', h->smsc ? 'S' : 'M', mrs, h->queue, @@ -740,10 +778,11 @@ } else if (h->ud[n] == '\r') { *p++ = '\\'; *p++ = 'r'; - } else if (h->ud[n] < 32 || h->ud[n] == 127) + } else if (h->ud[n] < 32 || h->ud[n] == 127) { *p++ = 191; - else + } else { *p++ = h->ud[n]; + } } *p++ = '\n'; *p = 0; @@ -776,45 +815,50 @@ for (p = line; *p && *p != '\n' && *p != '\r'; p++); *p = 0; /* strip eoln */ p = line; - if (!*p || *p == ';') + if (!*p || *p == ';') { continue; /* blank line or comment, ignore */ + } while (isalnum(*p)) { *p = tolower (*p); p++; } - while (isspace (*p)) + while (isspace (*p)) { *p++ = 0; + } if (*p == '=') { *p++ = 0; if (!strcmp(line, "ud")) { /* parse message (UTF-8) */ unsigned char o = 0; memcpy(h->udtxt, p, SMSLEN); /* for protocol 2 */ - while (*p && o < SMSLEN) + while (*p && o < SMSLEN) { h->ud[o++] = utf8decode(pp); + } h->udl = o; - if (*p) + if (*p) { ast_log(LOG_WARNING, "UD too long in %s\n", fn); + } } else { - while (isspace (*p)) + while (isspace (*p)) { p++; - if (!strcmp(line, "oa") && strlen(p) < sizeof(h->oa)) + } + if (!strcmp(line, "oa") && strlen(p) < sizeof(h->oa)) { numcpy (h->oa, p); - else if (!strcmp(line, "da") && strlen(p) < sizeof(h->oa)) + } else if (!strcmp(line, "da") && strlen(p) < sizeof(h->oa)) { numcpy (h->da, p); - else if (!strcmp(line, "pid")) + } else if (!strcmp(line, "pid")) { h->pid = atoi(p); - else if (!strcmp(line, "dcs")) { + } else if (!strcmp(line, "dcs")) { h->dcs = atoi(p); dcsset = 1; - } else if (!strcmp(line, "mr")) + } else if (!strcmp(line, "mr")) { h->mr = atoi(p); - else if (!strcmp(line, "srr")) + } else if (!strcmp(line, "srr")) { h->srr = (atoi(p) ? 1 : 0); - else if (!strcmp(line, "vp")) + } else if (!strcmp(line, "vp")) { h->vp = atoi(p); - else if (!strcmp(line, "rp")) + } else if (!strcmp(line, "rp")) { h->rp = (atoi(p) ? 1 : 0); - else if (!strcmp(line, "scts")) { /* get date/time */ + } else if (!strcmp(line, "scts")) { /* get date/time */ int Y, m, d, @@ -831,11 +875,13 @@ t.tm_sec = S; t.tm_isdst = -1; h->scts = ast_mktime(&t, NULL); - if (h->scts.tv_sec == 0) + if (h->scts.tv_sec == 0) { ast_log(LOG_WARNING, "Bad date/timein %s: %s", fn, p); + } } - } else + } else { ast_log(LOG_WARNING, "Cannot parse in %s: %s=%si\n", fn, line, p); + } } } else if (*p == '#') { /* raw hex format */ *p++ = 0; @@ -850,26 +896,31 @@ (((isalpha(p[1]) ? 9 : 0) + (p[1] & 0xF)) << 8) + (((isalpha(p[2]) ? 9 : 0) + (p[2] & 0xF)) << 4) + ((isalpha(p[3]) ? 9 : 0) + (p[3] & 0xF)); p += 4; - } else + } else { break; + } } h->udl = o; - if (*p) + if (*p) { ast_log(LOG_WARNING, "UD too long / invalid UCS-2 hex in %s\n", fn); - } else + } + } else { ast_log(LOG_WARNING, "Only ud can use ## format, %s\n", fn); + } } else if (!strcmp(line, "ud")) { /* user data */ int o = 0; while (*p && o < SMSLEN) { if (isxdigit(*p) && isxdigit(p[1])) { h->ud[o++] = (((isalpha(*p) ? 9 : 0) + (*p & 0xF)) << 4) + ((isalpha(p[1]) ? 9 : 0) + (p[1] & 0xF)); p += 2; - } else + } else { break; + } } h->udl = o; - if (*p) + if (*p) { ast_log(LOG_WARNING, "UD too long / invalid UCS-1 hex in %s\n", fn); + } } else if (!strcmp(line, "udh")) { /* user data header */ unsigned char o = 0; h->udhi = 1; @@ -878,23 +929,27 @@ h->udh[o] = (((isalpha(*p) ? 9 : 0) + (*p & 0xF)) << 4) + ((isalpha(p[1]) ? 9 : 0) + (p[1] & 0xF)); o++; p += 2; - } else + } else { break; + } } h->udhl = o; - if (*p) + if (*p) { ast_log(LOG_WARNING, "UDH too long / invalid hex in %s\n", fn); - } else + } + } else { ast_log(LOG_WARNING, "Only ud and udh can use # format, %s\n", fn); - } else + } + } else { ast_log(LOG_WARNING, "Cannot parse in %s: %s\n", fn, line); + } } fclose(s); if (!dcsset && packsms7(0, h->udhl, h->udh, h->udl, h->ud) < 0) { if (packsms8(0, h->udhl, h->udh, h->udl, h->ud) < 0) { - if (packsms16(0, h->udhl, h->udh, h->udl, h->ud) < 0) + if (packsms16(0, h->udhl, h->udh, h->udl, h->ud) < 0) { ast_log(LOG_WARNING, "Invalid UTF-8 message even for UCS-2 (%s)\n", fn); - else { + } else { h->dcs = 0x08; /* default to 16 bit */ ast_log(LOG_WARNING, "Sending in 16 bit format(%s)\n", fn); } @@ -903,12 +958,15 @@ ast_log(LOG_WARNING, "Sending in 8 bit format(%s)\n", fn); } } - if (is7bit(h->dcs) && packsms7(0, h->udhl, h->udh, h->udl, h->ud) < 0) + if (is7bit(h->dcs) && packsms7(0, h->udhl, h->udh, h->udl, h->ud) < 0) { ast_log(LOG_WARNING, "Invalid 7 bit GSM data %s\n", fn); - if (is8bit(h->dcs) && packsms8(0, h->udhl, h->udh, h->udl, h->ud) < 0) + } + if (is8bit(h->dcs) && packsms8(0, h->udhl, h->udh, h->udl, h->ud) < 0) { ast_log(LOG_WARNING, "Invalid 8 bit data %s\n", fn); - if (is16bit(h->dcs) && packsms16(0, h->udhl, h->udh, h->udl, h->ud) < 0) + } + if (is16bit(h->dcs) && packsms16(0, h->udhl, h->udh, h->udl, h->ud) < 0) { ast_log(LOG_WARNING, "Invalid 16 bit data %s\n", fn); + } } } @@ -928,38 +986,41 @@ snprintf(fn2 + strlen(fn2), sizeof(fn2) - strlen(fn2), "/%s.%s-%d", h->queue, isodate(h->scts.tv_sec, buf, sizeof(buf)), seq++); snprintf(fn + strlen(fn), sizeof(fn) - strlen(fn), "/.%s", fn2 + strlen(fn) + 1); o = fopen(fn, "w"); - if (o == NULL) + if (o == NULL) { return; + } - if (*h->oa) + if (*h->oa) { fprintf(o, "oa=%s\n", h->oa); - if (*h->da) + } + if (*h->da) { fprintf(o, "da=%s\n", h->da); + } if (h->udhi) { unsigned int p; fprintf(o, "udh#"); - for (p = 0; p < h->udhl; p++) + for (p = 0; p < h->udhl; p++) { fprintf(o, "%02X", h->udh[p]); + } fprintf(o, "\n"); } if (h->udl) { unsigned int p; for (p = 0; p < h->udl && h->ud[p] >= ' '; p++); - if (p < h->udl) + if (p < h->udl) { fputc(';', o); /* cannot use ud=, but include as a comment for human readable */ + } fprintf(o, "ud="); for (p = 0; p < h->udl; p++) { unsigned short v = h->ud[p]; - if (v < 32) + if (v < 32) { fputc(191, o); - else if (v < 0x80) + } else if (v < 0x80) { fputc(v, o); - else if (v < 0x800) - { + } else if (v < 0x800) { fputc(0xC0 + (v >> 6), o); fputc(0x80 + (v & 0x3F), o); - } else - { + } else { fputc(0xE0 + (v >> 12), o); fputc(0x80 + ((v >> 6) & 0x3F), o); fputc(0x80 + (v & 0x3F), o); @@ -971,13 +1032,15 @@ for (p = 0; p < h->udl && h->ud[p] < 0x100; p++); if (p == h->udl) { /* can write in ucs-1 hex */ fprintf(o, "ud#"); - for (p = 0; p < h->udl; p++) + for (p = 0; p < h->udl; p++) { fprintf(o, "%02X", h->ud[p]); + } fprintf(o, "\n"); } else { /* write in UCS-2 */ fprintf(o, "ud##"); - for (p = 0; p < h->udl; p++) + for (p = 0; p < h->udl; p++) { fprintf(o, "%04X", h->ud[p]); + } fprintf(o, "\n"); } } @@ -986,23 +1049,30 @@ char datebuf[30]; fprintf(o, "scts=%s\n", isodate(h->scts.tv_sec, datebuf, sizeof(datebuf))); } - if (h->pid) + if (h->pid) { fprintf(o, "pid=%d\n", h->pid); - if (h->dcs != 0xF1) + } + if (h->dcs != 0xF1) { fprintf(o, "dcs=%d\n", h->dcs); - if (h->vp) + } + if (h->vp) { fprintf(o, "vp=%d\n", h->vp); - if (h->srr) + } + if (h->srr) { fprintf(o, "srr=1\n"); - if (h->mr >= 0) + } + if (h->mr >= 0) { fprintf(o, "mr=%d\n", h->mr); - if (h->rp) + } + if (h->rp) { fprintf(o, "rp=1\n"); + } fclose(o); - if (rename(fn, fn2)) + if (rename(fn, fn2)) { unlink(fn); - else + } else { ast_log(LOG_EVENT, "Received to %s\n", fn2); + } } /*! \brief read dir skipping dot files... */ @@ -1033,17 +1103,19 @@ h->pid = h->imsg[p++]; h->dcs = h->imsg[p++]; if ((h->imsg[2] & 0x18) == 0x10) { /* relative VP */ - if (h->imsg[p] < 144) + if (h->imsg[p] < 144) { h->vp = (h->imsg[p] + 1) * 5; - else if (h->imsg[p] < 168) + } else if (h->imsg[p] < 168) { h->vp = 720 + (h->imsg[p] - 143) * 30; - else if (h->imsg[p] < 197) + } else if (h->imsg[p] < 197) { h->vp = (h->imsg[p] - 166) * 1440; - else + } else { h->vp = (h->imsg[p] - 192) * 10080; + } p++; - } else if (h->imsg[2] & 0x18) + } else if (h->imsg[2] & 0x18) { p += 7; /* ignore enhanced / absolute VP */ + } p += unpacksms(h->dcs, h->imsg + p, h->udh, &h->udhl, h->ud, &h->udl, h->udhi); h->rx = 1; /* received message */ sms_writefile(h); /* write the file */ @@ -1093,13 +1165,15 @@ static void adddata_proto2(sms_t *h, unsigned char msg, char *data, int size) { int x = h->omsg[1]+2; /* Get current position */ - if (x == 2) + if (x == 2) { x += 2; /* First: skip Payload length (set later) */ + } h->omsg[x++] = msg; /* Message code */ h->omsg[x++] = (unsigned char)size; /* Data size Low */ h->omsg[x++] = 0; /* Data size Hi */ - for (; size > 0 ; size--) + for (; size > 0 ; size--) { h->omsg[x++] = *data++; + } h->omsg[1] = x - 2; /* Frame size */ h->omsg[2] = x - 4; /* Payload length (Lo) */ h->omsg[3] = 0; /* Payload length (Hi) */ @@ -1128,16 +1202,18 @@ ast_localtime(&now, &tm, NULL); sprintf(stm, "%02d%02d%02d%02d", tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min); /* Date mmddHHMM */ adddata_proto2(h, 0x14, stm, 8); /* Date */ - if (*h->oa == 0) + if (*h->oa == 0) { strcpy(h->oa, "00000000"); + } adddata_proto2(h, 0x15, h->oa, strlen(h->oa)); /* Originator */ adddata_proto2(h, 0x17, "\1", 1); /* Calling Terminal ID */ } else { /* submit */ h->omsg[0] = 0x10; /* SMS_SUBMIT */ /* Required: 10 11 12 13 17 18 1B 1C (seems they must be ordered!) */ adddata_proto2(h, 0x17, "\1", 1); /* Calling Terminal ID */ - if (*h->da == 0) + if (*h->da == 0) { strcpy(h->da, "00000000"); + } adddata_proto2(h, 0x18, h->da, strlen(h->da)); /* Originator */ adddata_proto2(h, 0x1B, "\1", 1); /* Called Terminal ID */ adddata_proto2(h, 0x1C, "\0\0\0", 3); /* Notification */ @@ -1152,8 +1228,9 @@ char *p; int f; - for (p = s, f = 0; f < size && f < MAX_DEBUG_LEN; f++, p += 3) + for (p = s, f = 0; f < size && f < MAX_DEBUG_LEN; f++, p += 3) { sprintf(p, "%02X ", (unsigned char)buf[f]); + } return(s); } @@ -1251,8 +1328,9 @@ case DLL2_SMS_INFO_MO: /* transport SMS_SUBMIT */ case DLL2_SMS_INFO_MT: /* transport SMS_DELIVERY */ cause = sms_handleincoming_proto2(h); - if (!cause) /* ACK */ + if (!cause) { /* ACK */ sms_log(h, 'Y'); + } h->omsg[0] = DLL2_ACK(h); h->omsg[1] = 0x06; /* msg len */ h->omsg[2] = 0x04; /* payload len */ @@ -1308,23 +1386,25 @@ } else { /* submit */ h->omsg[p++] = 0x01 + (more ? 4 : 0) + (h->srr ? 0x20 : 0) + (h->rp ? 0x80 : 0) + (h->vp ? 0x10 : 0) + (h->udhi ? 0x40 : 0); - if (h->mr < 0) + if (h->mr < 0) { h->mr = message_ref++; + } h->omsg[p++] = h->mr; p += packaddress(h->omsg + p, h->da); h->omsg[p++] = h->pid; h->omsg[p++] = h->dcs; if (h->vp) { /* relative VP */ - if (h->vp < 720) + if (h->vp < 720) { h->omsg[p++] = (h->vp + 4) / 5 - 1; - else if (h->vp < 1440) + } else if (h->vp < 1440) { h->omsg[p++] = (h->vp - 720 + 29) / 30 + 143; - else if (h->vp < 43200) + } else if (h->vp < 43200) { h->omsg[p++] = (h->vp + 1439) / 1440 + 166; - else if (h->vp < 635040) + } else if (h->vp < 635040) { h->omsg[p++] = (h->vp + 10079) / 10080 + 192; - else + } else { h->omsg[p++] = 255; /* max */ + } } p += packsms(h->dcs, h->omsg + p, h->udhl, h->udh, h->udl, h->ud); } @@ -1348,16 +1428,18 @@ if (f) { snprintf(fn + strlen(fn), sizeof(fn) - strlen(fn), "/%s", f->d_name); sms_readfile(h, fn); - if (readdirqueue(d, h->queue)) + if (readdirqueue(d, h->queue)) { more = 1; /* more to send */ + } } closedir(d); } if (*h->da || *h->oa) { /* message to send */ - if (h->protocol == 2) + if (h->protocol == 2) { sms_compose2(h, more); - else + } else { sms_compose1(h, more); + } } else { /* no message */ if (h->protocol == 2) { h->omsg[0] = 0x17; /* SMS_REL */ @@ -1383,8 +1465,9 @@ sprintf(p, " %02X", msg[q++]); p += 3; } - if (q < n) + if (q < n) { sprintf(p, "..."); + } ast_verb(3, "SMS %s%s\n", dir == DIR_RX ? "RX" : "TX", txt); } @@ -1452,8 +1535,9 @@ unsigned char c = 0, p; int len = h->omsg[1] + 2; /* total message length excluding checksum */ - for (p = 0; p < len; p++) /* compute checksum */ + for (p = 0; p < len; p++) { /* compute checksum */ c += h->omsg[p]; + } h->omsg[len] = 0 - c; /* actually, (256 - (c & 0fxx)) & 0xff) */ sms_debug(DIR_TX, h); h->framenumber++; /* Proto 2 */ @@ -1463,8 +1547,9 @@ * but for others this might be way too much and the phone * could time out. XXX make it configurable. */ - if (h->omsg[0] == 0x93) + if (h->omsg[0] == 0x93) { h->opause = 8 * h->opause_0; /* initial message delay */ + } h->obytep = 0; h->obitp = 0; if (h->protocol == 2) { @@ -1511,13 +1596,14 @@ for (i = 0; i < samples; i++) { buf[i] = wave_out[0]; /* default is silence */ - if (h->opause) + if (h->opause) { h->opause--; - else if (h->obyten || h->osync) { /* sending data */ + } else if (h->obyten || h->osync) { /* sending data */ buf[i] = wave_out[h->ophase]; h->ophase += (h->obyte & 1) ? 13 : 21; /* compute next phase */ - if (h->ophase >= 80) + if (h->ophase >= 80) { h->ophase -= 80; + } if ((h->ophasep += 12) >= 80) { /* time to send the next bit */ h->ophasep -= 80; if (h->oseizure > 0) { /* sending channel seizure (proto 2) */ @@ -1531,11 +1617,11 @@ } } else { h->obitp++; - if (h->obitp == 1) + if (h->obitp == 1) { h->obyte = 0; /* start bit; */ - else if (h->obitp == 2) + } else if (h->obitp == 2) { h->obyte = h->omsg[h->obytep]; - else if (h->obitp == 10) { + } else if (h->obitp == 10) { h->obyte = 1; /* stop bit */ h->obitp = 0; h->obytep++; @@ -1543,8 +1629,9 @@ h->obytep = h->obyten = 0; /* sent */ h->osync = 10; /* trailing marks */ } - } else + } else { h->obyte >>= 1; + } } } } @@ -1599,14 +1686,16 @@ * transmission overlap (which is an error condition anyways), * we may miss some data and this makes debugging harder. */ - if (h->obyten || h->osync) + if (h->obyten || h->osync) { return; + } for ( ; samples-- ; data++) { unsigned long long m0, m1; - if (abs(*data) > h->imag) + if (abs(*data) > h->imag) { h->imag = abs(*data); - else + } else { h->imag = h->imag * 7 / 8; + } if (h->imag <= 500) { /* below [arbitrary] threahold: lost carrier */ if (h->idle++ == 80000) { /* nothing happening */ ast_log(LOG_NOTICE, "No data, hanging up\n"); @@ -1637,28 +1726,36 @@ m1 = h->ims1 * h->ims1 + h->imc1 * h->imc1; /* advance the sin/cos pointers */ - if ((h->ips0 += 21) >= 80) + if ((h->ips0 += 21) >= 80) { h->ips0 -= 80; - if ((h->ipc0 += 21) >= 80) + } + if ((h->ipc0 += 21) >= 80) { h->ipc0 -= 80; - if ((h->ips1 += 13) >= 80) + } + if ((h->ips1 += 13) >= 80) { h->ips1 -= 80; - if ((h->ipc1 += 13) >= 80) + } + if ((h->ipc1 += 13) >= 80) { h->ipc1 -= 80; + } /* set new bit to 1 or 0 depending on which value is stronger */ h->ibith <<= 1; - if (m1 > m0) + if (m1 > m0) { h->ibith |= 1; - if (h->ibith & 8) + } + if (h->ibith & 8) { h->ibitt--; - if (h->ibith & 1) + } + if (h->ibith & 1) { h->ibitt++; + } bit = ((h->ibitt > 1) ? 1 : 0); - if (bit != h->ibitl) + if (bit != h->ibitl) { h->ibitc = 1; - else + } else { h->ibitc++; + } h->ibitl = bit; if (!h->ibitn && h->ibitc == 4 && !bit) { h->ibitn = 1; @@ -1695,9 +1792,9 @@ h->ierr = 2; /* bad message length */ } if (h->ibytep > 1 && h->ibytep == 3 + h->imsg[1] && !h->ierr) { - if (!h->ibytec) + if (!h->ibytec) { sms_messagerx(h); - else { + } else { ast_log(LOG_NOTICE, "bad checksum"); h->ierr = 1; /* bad checksum */ } @@ -1763,8 +1860,9 @@ parse = ast_strdupa(data); /* create a local copy */ AST_STANDARD_APP_ARGS(sms_args, parse); - if (sms_args.argc > 1) + if (sms_args.argc > 1) { ast_app_parse_options(sms_options, &flags, sms_opts, sms_args.options); + } ast_verb(1, "sms argc %d queue <%s> opts <%s> addr <%s> body <%s>\n", sms_args.argc, S_OR(sms_args.queue, ""), @@ -1775,8 +1873,9 @@ h.ipc0 = h.ipc1 = 20; /* phase for cosine */ h.dcs = 0xF1; /* default */ - if (chan->cid.cid_num) + if (chan->cid.cid_num) { ast_copy_string(h.cli, chan->cid.cid_num, sizeof(h.cli)); + } if (ast_strlen_zero(sms_args.queue)) { ast_log(LOG_ERROR, "Requires queue name\n"); @@ -1788,24 +1887,30 @@ } ast_copy_string(h.queue, sms_args.queue, sizeof(h.queue)); - for (p = h.queue; *p; p++) - if (!isalnum(*p)) + for (p = h.queue; *p; p++) { + if (!isalnum(*p)) { *p = '-'; /* make very safe for filenames */ + } + } h.smsc = ast_test_flag(&flags, OPTION_BE_SMSC); h.protocol = ast_test_flag(&flags, OPTION_TWO) ? 2 : 1; - if (!ast_strlen_zero(sms_opts[OPTION_ARG_PAUSE])) + if (!ast_strlen_zero(sms_opts[OPTION_ARG_PAUSE])) { h.opause_0 = atoi(sms_opts[OPTION_ARG_PAUSE]); - if (h.opause_0 < 25 || h.opause_0 > 2000) + } + if (h.opause_0 < 25 || h.opause_0 > 2000) { h.opause_0 = 300; /* default 300ms */ + } ast_verb(1, "initial delay %dms\n", h.opause_0); /* the following apply if there is an arg3/4 and apply to the created message file */ - if (ast_test_flag(&flags, OPTION_SRR)) + if (ast_test_flag(&flags, OPTION_SRR)) { h.srr = 1; - if (ast_test_flag(&flags, OPTION_DCS)) + } + if (ast_test_flag(&flags, OPTION_DCS)) { h.dcs = 1; + } #if 0 case '1': case '2': @@ -1828,9 +1933,9 @@ ast_log(LOG_ERROR, "Address too long %s\n", sms_args.addr); goto done; } - if (h.smsc) + if (h.smsc) { ast_copy_string(h.oa, sms_args.addr, sizeof(h.oa)); - else { + } else { ast_copy_string(h.da, sms_args.addr, sizeof(h.da)); ast_copy_string(h.oa, h.cli, sizeof(h.oa)); } @@ -1840,8 +1945,9 @@ goto done; } up = (unsigned char *)sms_args.body; - while (*up && h.udl < SMSLEN) + while (*up && h.udl < SMSLEN) { h.ud[h.udl++] = utf8decode(&up); + } if (is7bit(h.dcs) && packsms7(0, h.udhl, h.udh, h.udl, h.ud) < 0) { ast_log(LOG_WARNING, "Invalid 7 bit GSM data\n"); goto done; @@ -1874,12 +1980,14 @@ sms_messagetx(&h); } - if (chan->_state != AST_STATE_UP) + if (chan->_state != AST_STATE_UP) { ast_answer(chan); + } res = ast_set_write_format(chan, __OUT_FMT); - if (res >= 0) + if (res >= 0) { res = ast_set_read_format(chan, AST_FORMAT_SLINEAR); + } if (res < 0) { ast_log(LOG_ERROR, "Unable to set to linear mode, giving up\n"); goto done; @@ -1929,8 +2037,9 @@ { #ifdef OUTALAW int p; - for (p = 0; p < 80; p++) + for (p = 0; p < 80; p++) { wavea[p] = AST_LIN2A (wave[p]); + } #endif snprintf(log_file, sizeof(log_file), "%s/sms", ast_config_AST_LOG_DIR); return ast_register_application(app, sms_exec, synopsis, descrip);