FW:[PATCH] timestamps, scripts and nokia 7110

"Oded Arbel" <[email protected]>
Newsgroups gmane.comp.mobile.kannel.devel
Message-ID <[email protected]>
Don't send it to me - send it to the list :-)

If I'm already forwarding stuff - I like the way Dennis fixed the time
reading from the PDU ( I know I should have fixed it earlier, but I had
other things on my mind ;-), so I ported it to AT2 on the CVS. I also
changed the way he handles the time zone to get it to work.
Now decode_deliver decodes time correctly, but I still get  bogus time
stamps with the messages - I guess it's date_convert_universal()
fault's. l'll look into it next.
All the patches below are Dennis Malmstrom's, except the smsc_at2 patch
which is my own's. 
I hadn't managed to apply all of Dennis' patches, them being against
1.0.3 (Dennis - could you please try your changes on the CVS version ?
patche 1.0.3 doesn't really help the development much..) but I like most
of his chnages.

P.S.
Dennis - The current AT2 module have some comments about things that has
to do with Nokia 7110 - maybe you could try to use that with your Nokia
? Using AT2 is preferd over AT anyway.

Oded Arbel
m-Wise Inc.
[email protected]

--
I haven't failed, I've found 10,000 ways that don't work.
	-- Ben Franklin

-----Original Message-----
From: [email protected] [mailto:[email protected]]
Sent: Thursday, February 07, 2002 5:01 PM
To: Oded Arbel
Subject: RE: timestamps, scripts and nokia 7110


Ok, lets try it again.

 /Dennis


-----Original Message-----
From:	Oded Arbel [mailto:[email protected]]
Sent:	Thu 2002-02-07 15:05
To:	Malmström, Dennis
Cc:	
Subject:	RE: timestamps, scripts and nokia 7110


Major oops :-) your patches are reversed - it's a patch of how to
downgrade from your changes back to the original..

Oded Arbel
m-Wise Inc.
[email protected]

--
Elwood:  What kind of music do you get here ma'am?
Barmaid: Why, we get both kinds of music, Country and Western.


On Thu, 2002-02-07 at 15:17, "Malmström, Dennis" wrote:
> 
> Oops, I forgot to eclose the changes.
> 
> 
> /Dennis
> 
> 
> -----Original Message-----
> From:	Malmström, Dennis
> Sent:	Thu 2002-02-07 14:01
> To:	[email protected]
> Cc:	
> Subject:	timestamps, scripts and nokia 7110
> 
> Hello
> 
>   I've been using kannel (version 1.0.3) to implement a prototype sms
gateway. This has required some changes to the source code. I case any
of them are of
>   value to the kannel project I hereby submit them.
> 
>   Be aware that I have not gained overall understanding of the code.
The purpose of the changes have been to solve my particular problems as
quickly as
>   possible. This should be taken into account when considering the
changes (no guarantees).
> 
> 
> 
>   The changes I made are:
> 
>   1) Added three escape codes:
>           %z, skips a word.
>           %A, same as %a but without url encoding.
>           %R, same as %r but without url encoding.
> 
>   2) Added the possibility of executing a command line instead of
fetching an url.
>      Configuration file example:
>           group = sms-service
>           keyword = default
>           execute = <command line>
> 
>   3) Added modemtype nokia7110.
>      I started out using the modemtype premicell (which I somehow
gathered was nokia related).
>      Getting it to work required a few changes to the GSM modem
handling code (gw/smsc_at.c).
>      I created a new modemtype so I wouldn't change the definition of
an existing one.
> 
>   4) Fixed broken timestamp parser.
>      I am not sure wether this is a general issue or specific to nokia
7110.
>      Anyway, the existing code produced nonsense values for the data
sent from a nokia 7110.
> 
> 
> 
>   The following source code files where changed:
> 
>   file                    change
>   ----                    ------
>   gwlib/octstr.h
>   gwlib/octstr.c          Added function for reading Octstr from
FILE*. Used by gw/smsbox_req.c.
> 
>   gw/smsbox_req.c         (2)
> 
>   gw/smsc_at.c            (3)
>                           (4)
> 
>   gw/urltrans.c           (1)
>                           (2)
> 
> 
>           /Dennis
> 
>   Dennis Malmström
>   Erda Technology AB
>   S:t Larsgatan 12, 582 24 Linköping, Sweden
>   http://www.erda.se
>   mail: [email protected]
>   phone: +46 (0)13 377218
>   gsm: +46 (0)706483090
> 
> 
> 
> 
> 
> 
-- 
Oded Arbel
m-Wise inc.
[email protected]

--
Politicians are like diapers. They both need changing regularly and for
the same reason.
octstr.c.diff (application/octet-stream, 1.2 KB)
--- ../org/gateway-1.0.3/gwlib/octstr.c	Mon Feb  5 11:26:21 2001
+++ ../gateway-1.0.3/gwlib/octstr.c	Wed Feb  6 09:54:12 2002
@@ -1202,22 +1202,12 @@
 }
 
 
-
-Octstr *octstr_read_file(const char *filename)
+Octstr *octstr_read(FILE *f)
 {
-    FILE *f;
     Octstr *os;
     char buf[128*1024];
     long n;
 
-    gw_assert(filename != NULL);
-
-    f = fopen(filename, "r");
-    if (f == NULL) {
-        error(errno, "fopen failed: couldn't open `%s'", filename);
-        return NULL;
-    }
-
     os = octstr_create("");
     if (os == NULL)
         goto error;
@@ -1225,16 +1215,34 @@
     while ((n = fread(buf, 1, sizeof(buf), f)) > 0)
         octstr_insert_data(os, octstr_len(os), buf, n);
 
-    (void) fclose(f);
     return os;
 
 error:
-    (void) fclose(f);
     octstr_destroy(os);
     return NULL;
 }
 
 
+Octstr *octstr_read_file(const char *filename)
+{
+    FILE *f;
+    Octstr *ret;
+
+    gw_assert(filename != NULL);
+
+    f = fopen(filename, "r");
+    if (f == NULL) {
+        error(errno, "fopen failed: couldn't open `%s'", filename);
+        return NULL;
+    }
+
+    ret = octstr_read( f );
+
+    (void) fclose(f);
+
+    return ret;
+}
+
 
 List *octstr_split_words(Octstr *ostr)
 {
octstr.h.diff (application/octet-stream, 386 B)
--- ../org/gateway-1.0.3/gwlib/octstr.h	Thu Oct  5 18:01:22 2000
+++ ../gateway-1.0.3/gwlib/octstr.h	Wed Feb  6 09:52:57 2002
@@ -399,6 +399,13 @@
 
 
 /*
+ * Read the contents of a file to an octet string. Return pointer to
+ * octet string.
+ */
+Octstr *octstr_read(FILE* f);
+
+
+/*
  * Read the contents of a named file to an octet string. Return pointer to
  * octet string.
  */
smsbox_req.c.diff (application/octet-stream, 1 KB)
--- ../org/gateway-1.0.3/gw/smsbox_req.c	Fri Oct  6 12:59:27 2000
+++ ../gateway-1.0.3/gw/smsbox_req.c	Wed Feb  6 10:00:42 2002
@@ -87,6 +87,7 @@
 		*temp, *replytext;
 	List *request_headers, *reply_headers;
 	int status;
+  FILE *f;
 
 	gw_assert(msg != NULL);
 	gw_assert(msg_type(msg) == smart_sms);
@@ -115,6 +116,28 @@
 		     ret);
 		break;
 
+	case TRANSTYPE_EXECUTE:
+		alog("SMS request sender:%s request: '%s' processed by '%s'",
+		     octstr_get_cstr(msg->smart_sms.receiver),
+		     octstr_get_cstr(msg->smart_sms.msgdata),
+		     pattern);
+
+		info(0, "executing %s", pattern);
+
+    f = popen(pattern, "r");
+		gw_free(pattern);
+
+    if (f == 0) {
+        info(0, "popen failed %d %s", errno, strerror(errno));
+        ret = octstr_create("");
+    } else {
+        replytext = octstr_read(f);
+        ret = gw_strdup(octstr_get_cstr(replytext));
+        octstr_destroy(replytext);
+        pclose(f);
+    }
+		break;
+
 	case TRANSTYPE_URL:
 		url = octstr_create(pattern);
 		request_headers = list_create();
smsc_at.c.diff (application/octet-stream, 3 KB)
--- ../org/gateway-1.0.3/gw/smsc_at.c	Tue Mar 27 15:40:38 2001
+++ ../gateway-1.0.3/gw/smsc_at.c	Wed Feb  6 13:39:18 2002
@@ -62,6 +62,7 @@
 #define WAVECOM		"wavecom"
 #define PREMICELL	"premicell"
 #define SIEMENS		"siemens"
+#define NOKIA7110	"nokia7110"
 
 /******************************************************************************
  * Open the connection
@@ -81,7 +82,10 @@
 	}
 
 	tcgetattr(fd, &tios);
-	if(strcmp(smsc->at_modemtype, SIEMENS) == 0) {
+	if ( strcmp( smsc->at_modemtype, SIEMENS ) == 0 ) {
+		cfsetospeed(&tios, B19200);  /* check radio pad parameter*/
+		cfsetispeed(&tios, B19200);
+	} else if ( strcmp( smsc->at_modemtype, NOKIA7110 ) == 0 ) {
 		cfsetospeed(&tios, B19200);  /* check radio pad parameter*/
 		cfsetispeed(&tios, B19200);
 	} else {
@@ -136,12 +140,14 @@
 	if (smsc->at_fd < 0)
 		goto error;
 
+  if ( strcmp( smsc->at_modemtype, NOKIA7110 ) == 0 ) sleep( 1 ); // wait for the modem
+
 	/* Turn Echo off on the modem: we don't need it */
 	if(send_modem_command(smsc->at_fd, "ATE0", 0) == -1)
 		goto error;
 	/* Check does the modem require a PIN and, if so, send it
 	 * This is not supported by the Nokia Premicell */
-	if(strcmp(smsc->at_modemtype, PREMICELL) != 0) {
+	if ( ( strcmp( smsc->at_modemtype, SIEMENS ) == 0 ) || ( strcmp( smsc->at_modemtype, WAVECOM ) == 0 ) ) {
 		ret = send_modem_command(smsc->at_fd, "AT+CPIN?", 0); 
 		if(ret == -1)
 			goto error;
@@ -476,7 +482,8 @@
 	
 	/* skip the SMSC address on the Wavecom (don't know about other modems -
 	 * Premicell doesn't need it) */
-	if(strcmp(smsc->at_modemtype, WAVECOM) == 0 
+	if(strcmp(smsc->at_modemtype, NOKIA7110) == 0 
+	    || strcmp(smsc->at_modemtype, WAVECOM) == 0 
 	    || strcmp(smsc->at_modemtype, SIEMENS) == 0) {
 		tmp = hexchar(octstr_get_char(buffer, pos))*16
 		    + hexchar(octstr_get_char(buffer, pos+1));
@@ -582,14 +589,15 @@
 	pos++;
 	
 	/* get the timestamp */
-	mtime.tm_year = octstr_get_char(pdu, pos) + 100; pos++;
-	mtime.tm_mon  = octstr_get_char(pdu, pos); pos++;
-	mtime.tm_mday = octstr_get_char(pdu, pos); pos++;
-	mtime.tm_hour = octstr_get_char(pdu, pos); pos++;
-	mtime.tm_min  = octstr_get_char(pdu, pos); pos++;
-	mtime.tm_sec = octstr_get_char(pdu, pos); pos++;
+	mtime.tm_year = ( ( octstr_get_char(pdu, pos) & 15 ) * 10 ) + ( octstr_get_char(pdu, pos) >> 4 ) + 100; pos++;
+	mtime.tm_mon  = ( ( octstr_get_char(pdu, pos) & 15 ) * 10 ) + ( octstr_get_char(pdu, pos) >> 4 ) - 1; pos++;
+	mtime.tm_mday = ( ( octstr_get_char(pdu, pos) & 15 ) * 10 ) + ( octstr_get_char(pdu, pos) >> 4 ); pos++;
+	mtime.tm_hour = ( ( octstr_get_char(pdu, pos) & 15 ) * 10 ) + ( octstr_get_char(pdu, pos) >> 4 ); pos++;
+	mtime.tm_min  = ( ( octstr_get_char(pdu, pos) & 15 ) * 10 ) + ( octstr_get_char(pdu, pos) >> 4 ); pos++;
+	mtime.tm_sec  = ( ( octstr_get_char(pdu, pos) & 15 ) * 10 ) + ( octstr_get_char(pdu, pos) >> 4 ); pos++;
+
 	/* time zone: */
-	mtime.tm_hour += octstr_get_char(pdu, pos); pos++;
+	mtime.tm_hour += ( ( octstr_get_char(pdu, pos) & 15 ) * 10 ) + ( octstr_get_char(pdu, pos) >> 4 ); pos++;
 	stime = mktime(&mtime);
 	
 	/* get data length */
urltrans.c.diff (application/octet-stream, 3.1 KB)
--- ../org/gateway-1.0.3/gw/urltrans.c	Wed Oct 25 14:26:05 2000
+++ ../gateway-1.0.3/gw/urltrans.c	Wed Feb  6 10:03:57 2002
@@ -208,10 +208,14 @@
 	len = strlen(pattern);
 	len += count_occurences(pattern, "%s") * maxword * ENCODED_LEN;
 	len += count_occurences(pattern, "%S") * maxword * ENCODED_LEN;
+	len += count_occurences(pattern, "%A") * 
+			(maxword + 1) * n * ENCODED_LEN;
 	len += count_occurences(pattern, "%a") * 
 			(maxword + 1) * n * ENCODED_LEN;
 	len += count_occurences(pattern, "%r") * 
 			(maxword + 1) * n * ENCODED_LEN;
+	len += count_occurences(pattern, "%R") * 
+			(maxword + 1) * n * ENCODED_LEN;
 	len += count_occurences(pattern, "%p") * 
 			octstr_len(request->smart_sms.receiver) * ENCODED_LEN;
 	len += count_occurences(pattern, "%P") * 
@@ -239,6 +243,7 @@
 		sprintf(s, "%.*s", (int) (p - pattern), pattern);
 		s = strchr(s, '\0');
 		switch (p[1]) {
+		case 'z': break; /* skip a word */
 		case 's':
 			encode_for_url(enc, words[nextarg]);
 			sprintf(s, "%s", enc);
@@ -262,6 +267,15 @@
 				s = strchr(s, '\0');
 			}
 			break;
+		case 'R': /* like %r but without url formatting */
+			for (j = nextarg; j < n; ++j) {
+				if (j == nextarg)
+					sprintf(s, "%s", words[j]);
+				else
+					sprintf(s, " %s", words[j]);
+				s = strchr(s, '\0');
+			}
+			break;
 
 			/* NOTE: the sender and receiver is already switched in
 			 *    message, so that's why we must use 'sender' when
@@ -306,6 +320,15 @@
 				s = strchr(s, '\0');
 			}
 			break;
+		case 'A': /* like %a but without url formatting */
+			for (j = 0; j < n; ++j) {
+				if (j > 0)
+					sprintf(s, " %s", words[j]);
+				else
+					sprintf(s, "%s", words[j]);
+				s = strchr(s, '\0');
+			}
+			break;
 		case 't':
 			tm = gw_gmtime(request->smart_sms.time);
 			sprintf(s, "%04d-%02d-%02d+%02d:%02d",
@@ -412,7 +435,7 @@
 static URLTranslation *create_onetrans(ConfigGroup *grp)
 {
     URLTranslation *ot;
-    char *keyword, *aliases, *url, *text, *file;
+    char *keyword, *aliases, *url, *text, *file, *execute;
     char *prefix, *suffix, *faked_sender, *max_msgs, *concatenation;
     char *split_chars, *split_suffix, *omit_empty;
     char *username, *password;
@@ -435,6 +458,7 @@
     aliases = config_get(grp, "aliases");
     url = config_get(grp, "url");
     text = config_get(grp, "text");
+    execute = config_get(grp, "execute");
     file = config_get(grp, "file");
     prefix = config_get(grp, "prefix");
     suffix = config_get(grp, "suffix");
@@ -465,6 +489,9 @@
     } else if (text) {
 	ot->type = TRANSTYPE_TEXT;
 	ot->pattern = gw_strdup(text);
+    } else if (execute) {
+	ot->type = TRANSTYPE_EXECUTE;
+	ot->pattern = gw_strdup(execute);
     } else if (username) {
 	ot->type = TRANSTYPE_SENDSMS;
 	ot->pattern = gw_strdup("");
@@ -518,7 +545,9 @@
 	ot->args = count_occurences(ot->pattern, "%s");
 	ot->args += count_occurences(ot->pattern, "%S");
 	ot->has_catchall_arg = (count_occurences(ot->pattern, "%r") > 0) ||
-	    (count_occurences(ot->pattern, "%a") > 0);
+	    (count_occurences(ot->pattern, "%a") > 0) ||
+	    (count_occurences(ot->pattern, "%A") > 0) ||
+	    (count_occurences(ot->pattern, "%R") > 0);
     }
     else { 		/* send-sms user */
 	ot->args = 0;
smsc_at2.patch (application/octet-stream, 1.8 KB)
--- gateway/gw/smsc_at2.c	Thu Feb  7 18:18:49 2002
+++ patched/gw/smsc_at2.c	Thu Feb  7 18:28:17 2002
@@ -1163,15 +1163,20 @@
         pos++;
         
         /* get the timestamp */
-        mtime.year   = octstr_get_char(pdu, pos) + 1900; pos++;
-        mtime.month  = octstr_get_char(pdu, pos); pos++;
-        mtime.day    = octstr_get_char(pdu, pos); pos++;
-        mtime.hour   = octstr_get_char(pdu, pos); pos++;
-        mtime.minute = octstr_get_char(pdu, pos); pos++;
-        mtime.second = octstr_get_char(pdu, pos); pos++;
+	mtime.year = ( ( octstr_get_char(pdu, pos) & 15 ) * 10 ) + ( octstr_get_char(pdu, pos) >> 4 ) + 100; pos++;
+	mtime.month  = ( ( octstr_get_char(pdu, pos) & 15 ) * 10 ) + ( octstr_get_char(pdu, pos) >> 4 ) - 1; pos++;
+	mtime.day = ( ( octstr_get_char(pdu, pos) & 15 ) * 10 ) + ( octstr_get_char(pdu, pos) >> 4 ); pos++;
+	mtime.hour = ( ( octstr_get_char(pdu, pos) & 15 ) * 10 ) + ( octstr_get_char(pdu, pos) >> 4 ); pos++;
+	mtime.minute  = ( ( octstr_get_char(pdu, pos) & 15 ) * 10 ) + ( octstr_get_char(pdu, pos) >> 4 ); pos++;
+	mtime.second  = ( ( octstr_get_char(pdu, pos) & 15 ) * 10 ) + ( octstr_get_char(pdu, pos) >> 4 ); pos++;
+
         /* time zone: */
         /* XXX handle negative time zones */
-        mtime.hour  += octstr_get_char(pdu, pos); pos++;
+        /* time zone is not "swapped nibble", with the MSB as the sign (1 is negative). the problem is that
+        +1 means that we have to substract 1 from the hour to get GMT. also remember that the time zone is measured
+        in quarters of the hour and not in full hours.
+        FIXME: we need to get the module of the division by 4 to the minute field */
+	mtime.hour += ((octstr_get_char(pdu, pos) >> 7) ? 1 : -1) * (octstr_get_char(pdu, pos) & 127) / 4; pos++;
         stime = date_convert_universal(&mtime);
         
         /* get data length */
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.