[svn-commits] murf: branch murf/bug6334 r48765 - in /team/murf/bug6334: ./ apps/app_sms.c

svn-commits at lists.digium.com svn-commits at lists.digium.com
Thu Dec 21 12:18:20 MST 2006


Author: murf
Date: Thu Dec 21 13:18:19 2006
New Revision: 48765

URL: http://svn.digium.com/view/asterisk?view=rev&rev=48765
Log:
Merged revisions 48750,48752 via svnmerge from 
https://origsvn.digium.com/svn/asterisk/trunk

........
r48750 | rizzo | 2006-12-21 11:35:22 -0700 (Thu, 21 Dec 2006) | 3 lines

restructure a block to reduce nesting


........
r48752 | rizzo | 2006-12-21 11:54:17 -0700 (Thu, 21 Dec 2006) | 3 lines

reduce indentation


........

Modified:
    team/murf/bug6334/   (props changed)
    team/murf/bug6334/apps/app_sms.c

Propchange: team/murf/bug6334/
------------------------------------------------------------------------------
--- svnmerge-integrated (original)
+++ svnmerge-integrated Thu Dec 21 13:18:19 2006
@@ -1,1 +1,1 @@
-/trunk:1-48748
+/trunk:1-48752

Modified: team/murf/bug6334/apps/app_sms.c
URL: http://svn.digium.com/view/asterisk/team/murf/bug6334/apps/app_sms.c?view=diff&rev=48765&r1=48764&r2=48765
==============================================================================
--- team/murf/bug6334/apps/app_sms.c (original)
+++ team/murf/bug6334/apps/app_sms.c Thu Dec 21 13:18:19 2006
@@ -189,8 +189,7 @@
 
 #define SMSLEN 160              /*!< max SMS length */
 
-typedef struct sms_s
-{
+typedef struct sms_s {
 	unsigned char hangup;        /*!< we are done... */
 	unsigned char err;           /*!< set for any errors */
 	unsigned char smsc:1;        /*!< we are SMSC */
@@ -1556,110 +1555,26 @@
  * bits in h->ibitt.
  * XXX the rest is to be determined.
  */
-static void sms_process (sms_t * h, int samples, signed short *data)
-{
-#if 1
-	/* Do we really need to remain deaf while a packet is
-	 * being transmitted ?
+static void sms_process(sms_t * h, int samples, signed short *data)
+{
+	int bit;
+
+	/*
+	 * Ignore incoming audio while a packet is being transmitted,
+	 * the protocol is half-duplex.
+	 * Unfortunately this means that if the outbound and incoming
+	 * transmission overlap (which is an error condition anyways),
+	 * we may miss some data and this makes debugging harder.
 	 */
 	if (h->obyten || h->osync)
-		return;						 /* sending */
-#endif
-	while (samples--) {
+		return;
+	for ( ; samples-- ; data++) {
 		unsigned long long m0, m1;
 		if (abs (*data) > h->imag)
 			h->imag = abs (*data);
 		else
 			h->imag = h->imag * 7 / 8;
-		if (h->imag > 500) {
-			h->idle = 0;
-
-			/* multiply signal by the two carriers. */
-			h->ims0 = (h->ims0 * 6 + *data * wave[h->ips0]) / 7;
-			h->imc0 = (h->imc0 * 6 + *data * wave[h->ipc0]) / 7;
-			h->ims1 = (h->ims1 * 6 + *data * wave[h->ips1]) / 7;
-			h->imc1 = (h->imc1 * 6 + *data * wave[h->ipc1]) / 7;
-			/* compute the amplitudes */
-			m0 = h->ims0 * h->ims0 + h->imc0 * h->imc0;
-			m1 = h->ims1 * h->ims1 + h->imc1 * h->imc1;
-
-			/* advance the sin/cos pointers */
-			if ((h->ips0 += 21) >= 80)
-				h->ips0 -= 80;
-			if ((h->ipc0 += 21) >= 80)
-				h->ipc0 -= 80;
-			if ((h->ips1 += 13) >= 80)
-				h->ips1 -= 80;
-			if ((h->ipc1 += 13) >= 80)
-				h->ipc1 -= 80;
-			{
-				char bit;
-
-				/* set new bit to 1 or 0 depending on which value is stronger */
-				h->ibith <<= 1;
-				if (m1 > m0)
-					h->ibith |= 1;
-				if (h->ibith & 8)
-					h->ibitt--;
-				if (h->ibith & 1)
-					h->ibitt++;
-				bit = ((h->ibitt > 1) ? 1 : 0);
-				if (bit != h->ibitl)
-					h->ibitc = 1;
-				else
-					h->ibitc++;
-				h->ibitl = bit;
-				if (!h->ibitn && h->ibitc == 4 && !bit) {
-					h->ibitn = 1;
-					h->iphasep = 0;
-				}
-				if (bit && h->ibitc == 200) {						 /* sync, restart message */
-					/* Protocol 2: empty connnection ready (I am master) */
-					if(h->framenumber<0 && h->ibytec>=160 && !memcmp(h->imsg,"UUUUUUUUUUUUUUUUUUUU",20)) {
-						h->framenumber = 1;
-						if (option_verbose > 2)
-							ast_verbose (VERBOSE_PREFIX_3 "SMS protocol 2 detected\n");
-						h->protocol = 2;
-						h->imsg[0] = 0xff;      /* special message (fake) */
-						h->imsg[1] = h->imsg[2] = 0x00;
-						h->ierr = h->ibitn = h->ibytep = h->ibytec = 0;
-						sms_messagerx (h);
-					}
-					h->ierr = h->ibitn = h->ibytep = h->ibytec = 0;
-				}
-				if (h->ibitn) {
-					h->iphasep += 12;
-					if (h->iphasep >= 80) {			 		/* next bit */
-						h->iphasep -= 80;
-						if (h->ibitn++ == 9) {		 		/* end of byte */
-							if (!bit) { /* bad stop bit */
-								ast_log(LOG_NOTICE, "bad stop bit");
-								h->ierr = 0xFF; /* unknown error */
-							} else {
-								if (h->ibytep < sizeof (h->imsg)) {
-									h->imsg[h->ibytep] = h->ibytev;
-									h->ibytec += h->ibytev;
-									h->ibytep++;
-								} else if (h->ibytep == sizeof (h->imsg)) {
-									ast_log(LOG_NOTICE, "msg too large");
-									h->ierr = 2; /* bad message length */
-								}
-								if (h->ibytep > 1 && h->ibytep == 3 + h->imsg[1] && !h->ierr) {
-									if (!h->ibytec)
-										sms_messagerx (h);
-									else {
-										ast_log(LOG_NOTICE, "bad checksum");
-										h->ierr = 1;		/* bad checksum */
-									}
-								}
-							}
-							h->ibitn = 0;
-						}
-						h->ibytev = (h->ibytev >> 1) + (bit ? 0x80 : 0);
-					}
-				}
-			}
-		} else {			 /* lost carrier */
+		if (h->imag <= 500) {		/* below [arbitrary] threahold: lost carrier */
 			if (h->idle++ == 80000) {		 /* nothing happening */
 				ast_log (LOG_NOTICE, "No data, hanging up\n");
 				h->hangup = 1;
@@ -1675,8 +1590,92 @@
 				sms_messagetx (h);  /* send error */
 			}
 			h->ierr = h->ibitn = h->ibytep = h->ibytec = 0;
-		}
-		data++;
+			continue;
+		}
+		h->idle = 0;
+
+		/* multiply signal by the two carriers. */
+		h->ims0 = (h->ims0 * 6 + *data * wave[h->ips0]) / 7;
+		h->imc0 = (h->imc0 * 6 + *data * wave[h->ipc0]) / 7;
+		h->ims1 = (h->ims1 * 6 + *data * wave[h->ips1]) / 7;
+		h->imc1 = (h->imc1 * 6 + *data * wave[h->ipc1]) / 7;
+		/* compute the amplitudes */
+		m0 = h->ims0 * h->ims0 + h->imc0 * h->imc0;
+		m1 = h->ims1 * h->ims1 + h->imc1 * h->imc1;
+
+		/* advance the sin/cos pointers */
+		if ((h->ips0 += 21) >= 80)
+			h->ips0 -= 80;
+		if ((h->ipc0 += 21) >= 80)
+			h->ipc0 -= 80;
+		if ((h->ips1 += 13) >= 80)
+			h->ips1 -= 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)
+			h->ibith |= 1;
+		if (h->ibith & 8)
+			h->ibitt--;
+		if (h->ibith & 1)
+			h->ibitt++;
+		bit = ((h->ibitt > 1) ? 1 : 0);
+		if (bit != h->ibitl)
+			h->ibitc = 1;
+		else
+			h->ibitc++;
+		h->ibitl = bit;
+		if (!h->ibitn && h->ibitc == 4 && !bit) {
+			h->ibitn = 1;
+			h->iphasep = 0;
+		}
+		if (bit && h->ibitc == 200) {						 /* sync, restart message */
+			/* Protocol 2: empty connnection ready (I am master) */
+			if(h->framenumber<0 && h->ibytec>=160 && !memcmp(h->imsg,"UUUUUUUUUUUUUUUUUUUU",20)) {
+				h->framenumber = 1;
+				if (option_verbose > 2)
+					ast_verbose (VERBOSE_PREFIX_3 "SMS protocol 2 detected\n");
+				h->protocol = 2;
+				h->imsg[0] = 0xff;      /* special message (fake) */
+				h->imsg[1] = h->imsg[2] = 0x00;
+				h->ierr = h->ibitn = h->ibytep = h->ibytec = 0;
+				sms_messagerx (h);
+			}
+			h->ierr = h->ibitn = h->ibytep = h->ibytec = 0;
+		}
+		if (h->ibitn) {
+			h->iphasep += 12;
+			if (h->iphasep >= 80) {			 		/* next bit */
+				h->iphasep -= 80;
+				if (h->ibitn++ == 9) {		 		/* end of byte */
+					if (!bit) { /* bad stop bit */
+						ast_log(LOG_NOTICE, "bad stop bit");
+						h->ierr = 0xFF; /* unknown error */
+					} else {
+						if (h->ibytep < sizeof (h->imsg)) {
+							h->imsg[h->ibytep] = h->ibytev;
+							h->ibytec += h->ibytev;
+							h->ibytep++;
+						} else if (h->ibytep == sizeof (h->imsg)) {
+							ast_log(LOG_NOTICE, "msg too large");
+							h->ierr = 2; /* bad message length */
+						}
+						if (h->ibytep > 1 && h->ibytep == 3 + h->imsg[1] && !h->ierr) {
+							if (!h->ibytec)
+								sms_messagerx (h);
+							else {
+								ast_log(LOG_NOTICE, "bad checksum");
+								h->ierr = 1;		/* bad checksum */
+							}
+						}
+					}
+					h->ibitn = 0;
+				}
+				h->ibytev = (h->ibytev >> 1) + (bit ? 0x80 : 0);
+			}
+		}
 	}
 }
 
@@ -1692,121 +1691,118 @@
 	struct ast_module_user *u;
 	struct ast_frame *f;
 	sms_t h = { 0 };
+	unsigned char *p;
+	unsigned char *d = data;
+	int answer = 0;
 	
-	u = ast_module_user_add(chan);
-
-	h.ipc0 = h.ipc1 = 20;		  /* phase for cosine */
-	h.dcs = 0xF1;					 /* default */
 	if (!data) {
 		ast_log (LOG_ERROR, "Requires queue name at least\n");
+		return -1;
+	}
+
+	u = ast_module_user_add(chan);
+	h.ipc0 = h.ipc1 = 20;		/* phase for cosine */
+	h.dcs = 0xF1;			/* default */
+
+	if (chan->cid.cid_num)
+		ast_copy_string (h.cli, chan->cid.cid_num, sizeof (h.cli));
+
+	if (!*d || *d == '|') {
+		ast_log (LOG_ERROR, "Requires queue name\n");
 		ast_module_user_remove(u);
 		return -1;
 	}
-
-	if (chan->cid.cid_num)
-		ast_copy_string (h.cli, chan->cid.cid_num, sizeof (h.cli));
-
-	{
-		unsigned char *p;
-		unsigned char *d = data,
-			answer = 0;
-		if (!*d || *d == '|') {
-			ast_log (LOG_ERROR, "Requires queue name\n");
-			ast_module_user_remove(u);
-			return -1;
-		}
+	for (p = d; *p && *p != '|'; p++);
+	if (p - d >= sizeof (h.queue)) {
+		ast_log (LOG_ERROR, "Queue name too long\n");
+		ast_module_user_remove(u);
+		return -1;
+	}
+	strncpy(h.queue, (char *)d, p - d);
+	if (*p == '|')
+		p++;
+	d = p;
+	for (p = (unsigned char *)h.queue; *p; p++)
+		if (!isalnum (*p))
+			*p = '-';			  /* make very safe for filenames */
+
+	while (*d && *d != '|') {
+		switch (*d) {
+		case 'a':				 /* we have to send the initial FSK sequence */
+			answer = 1;
+			break;
+		case 's':				 /* we are acting as a service centre talking to a phone */
+			h.smsc = 1;
+			break;
+		case 't':                                /* use protocol 2 ([t]wo)! couldn't use numbers *!* */
+			h.protocol = 2;
+			break;
+			/* the following apply if there is an arg3/4 and apply to the created message file */
+		case 'r':
+			h.srr = 1;
+			break;
+		case 'o':
+			h.dcs |= 4;			/* octets */
+			break;
+		case '1':
+		case '2':
+		case '3':
+		case '4':
+		case '5':
+		case '6':
+		case '7':				 /* set the pid for saved local message */
+			h.pid = 0x40 + (*d & 0xF);
+			break;
+		}
+		d++;
+	}
+	if (*d == '|') {
+		/* submitting a message, not taking call. */
+		/* deprecated, use smsq instead */
+		d++;
+		h.scts = time (0);
 		for (p = d; *p && *p != '|'; p++);
-		if (p - d >= sizeof (h.queue)) {
-			ast_log (LOG_ERROR, "Queue name too long\n");
-			ast_module_user_remove(u);
-			return -1;
-		}
-		strncpy(h.queue, (char *)d, p - d);
-		if (*p == '|')
-			p++;
+		if (*p)
+			*p++ = 0;
+		if (strlen ((char *)d) >= sizeof (h.oa)) {
+			ast_log (LOG_ERROR, "Address too long %s\n", d);
+			return 0;
+		}
+		if (h.smsc) {
+			ast_copy_string (h.oa, (char *)d, sizeof (h.oa));
+		} else {
+			ast_copy_string (h.da, (char *)d, sizeof (h.da));
+		}
+		if (!h.smsc)
+			ast_copy_string (h.oa, h.cli, sizeof (h.oa));
 		d = p;
-		for (p = (unsigned char *)h.queue; *p; p++)
-			if (!isalnum (*p))
-				*p = '-';			  /* make very safe for filenames */
-ast_log(LOG_NOTICE, "sms to queue %s\n", h.queue);
-		while (*d && *d != '|') {
-			switch (*d) {
-			case 'a':				 /* we have to send the initial FSK sequence */
-				answer = 1;
-				break;
-			case 's':				 /* we are acting as a service centre talking to a phone */
-				h.smsc = 1;
-				break;
-			case 't':                                /* use protocol 2 ([t]wo)! couldn't use numbers *!* */
-				h.protocol = 2;
-				break;
-				/* the following apply if there is an arg3/4 and apply to the created message file */
-			case 'r':
-				h.srr = 1;
-				break;
-			case 'o':
-				h.dcs |= 4;			/* octets */
-				break;
-			case '1':
-			case '2':
-			case '3':
-			case '4':
-			case '5':
-			case '6':
-			case '7':				 /* set the pid for saved local message */
-				h.pid = 0x40 + (*d & 0xF);
-				break;
-			}
-			d++;
-		}
-		if (*d == '|') {
-			/* submitting a message, not taking call. */
-			/* deprecated, use smsq instead */
-			d++;
-			h.scts = time (0);
-			for (p = d; *p && *p != '|'; p++);
-			if (*p)
-				*p++ = 0;
-			if (strlen ((char *)d) >= sizeof (h.oa)) {
-				ast_log (LOG_ERROR, "Address too long %s\n", d);
-				return 0;
-			}
-			if (h.smsc) {
-				ast_copy_string (h.oa, (char *)d, sizeof (h.oa));
-			} else {
-				ast_copy_string (h.da, (char *)d, sizeof (h.da));
-			}
-			if (!h.smsc)
-				ast_copy_string (h.oa, h.cli, sizeof (h.oa));
-			d = p;
-			h.udl = 0;
-			while (*p && h.udl < SMSLEN)
-				h.ud[h.udl++] = utf8decode(&p);
-			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");
-			if (is8bit (h.dcs) && packsms8 (0, h.udhl, h.udh, h.udl, h.ud) < 0)
-				ast_log (LOG_WARNING, "Invalid 8 bit data\n");
-			if (is16bit (h.dcs) && packsms16 (0, h.udhl, h.udh, h.udl, h.ud) < 0)
-				ast_log (LOG_WARNING, "Invalid 16 bit data\n");
-			h.rx = 0;				  /* sent message */
-			h.mr = -1;
-			sms_writefile (&h);
-			ast_module_user_remove(u);
-			return 0;
-		}
-
-		if (answer) {
-			h.framenumber = 1;             /* Proto 2 */
-			/* set up SMS_EST initial message */
-			if (h.protocol == 2) {
-				h.omsg[0] = DLL2_SMS_EST;
-				h.omsg[1] = 0;
-			} else {
-				h.omsg[0] = DLL1_SMS_EST | DLL1_SMS_COMPLETE;
-				h.omsg[1] = 0;
-			}
-			sms_messagetx (&h);
-		}
+		h.udl = 0;
+		while (*p && h.udl < SMSLEN)
+			h.ud[h.udl++] = utf8decode(&p);
+		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");
+		if (is8bit (h.dcs) && packsms8 (0, h.udhl, h.udh, h.udl, h.ud) < 0)
+			ast_log (LOG_WARNING, "Invalid 8 bit data\n");
+		if (is16bit (h.dcs) && packsms16 (0, h.udhl, h.udh, h.udl, h.ud) < 0)
+			ast_log (LOG_WARNING, "Invalid 16 bit data\n");
+		h.rx = 0;				  /* sent message */
+		h.mr = -1;
+		sms_writefile (&h);
+		ast_module_user_remove(u);
+		return 0;
+	}
+
+	if (answer) {
+		h.framenumber = 1;             /* Proto 2 */
+		/* set up SMS_EST initial message */
+		if (h.protocol == 2) {
+			h.omsg[0] = DLL2_SMS_EST;
+			h.omsg[1] = 0;
+		} else {
+			h.omsg[0] = DLL1_SMS_EST | DLL1_SMS_COMPLETE;
+			h.omsg[1] = 0;
+		}
+		sms_messagetx (&h);
 	}
 
 	if (chan->_state != AST_STATE_UP)



More information about the svn-commits mailing list