1 /* -------------------------------------------------------------------- */
2 /* SMS Client, send messages to mobile phones and pagers		*/
3 /*									*/
4 /* mobistar.c								*/
5 /*									*/
6 /*  Copyright (C) 1999,2000 yves.seynaeve@eyc.be 			*/
7 /*									*/
8 /*  This library is free software; you can redistribute it and/or	*/
9 /*  modify it under the terms of the GNU Library General Public		*/
10 /*  License as published by the Free Software Foundation; either	*/
11 /*  version 2 of the License, or (at your option) any later version.	*/
12 /*									*/
13 /*  This library is distributed in the hope that it will be useful,	*/
14 /*  but WITHOUT ANY WARRANTY; without even the implied warranty of	*/
15 /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU	*/
16 /*  Library General Public License for more details.			*/
17 /*									*/
18 /*  You should have received a copy of the GNU Library General Public	*/
19 /*  License along with this library; if not, write to the Free		*/
20 /*  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.	*/
21 /*									*/
22 /*  You can contact the author at this e-mail address:			*/
23 /*									*/
24 /*  yves.seynaeve@eyc.be 	 					*/
25 /*									*/
26 /* -------------------------------------------------------------------- */
27 /* $Id$
28    -------------------------------------------------------------------- */
29 
30 #include <stdio.h>
31 #include <string.h>
32 
33 #include "common/common.h"
34 #include "logfile/logfile.h"
35 #include "driver.h"
36 #include "error.h"
37 #include "comms/comms.h"
38 #include "resource/resource.h"
39 
40 /* -------------------------------------------------------------------- */
41 
42 static char ACK1[] = "\n";
43 
44 /* -------------------------------------------------------------------- */
45 /* -------------------------------------------------------------------- */
46 
47 static struct proximus_env
48 {
49 	DRIVER_DEFAULT_ENV def;
50 
51 } driver_env;
52 
53 
54 static 	RESOURCE resource_list[] =
55 	{
56 		{ RESOURCE_STRING,  "SMS_comms_params", 	0, 1, NULL, 0,  "8N1",  0, 	 &(driver_env.def.comms_params)  	},
57 		{ RESOURCE_STRING,  "SMS_centre_number", 	0, 1, NULL, 0,  NULL,   0, 	 &(driver_env.def.centre_number)  	},
58 		{ RESOURCE_NUMERIC, "SMS_baud", 		0, 1, NULL, 0,  NULL,   9600, 	 &(driver_env.def.baud)  		},
59 		{ RESOURCE_NUMERIC, "SMS_deliver_timeout", 	0, 0, NULL, 0,  NULL,   30, 	 &(driver_env.def.deliver_timeout)  	},
60 		{ RESOURCE_NUMERIC, "SMS_timeout", 		0, 0, NULL, 0,  NULL,   10, 	 &(driver_env.def.timeout)  		},
61 		{ RESOURCE_NUMERIC, "SMS_write_timeout", 	0, 0, NULL, 0,  NULL,   10, 	 &(driver_env.def.write_timeout)  	},
62 		{ RESOURCE_NUMERIC, "SMS_max_deliver", 		0, 0, NULL, 0,  NULL,   0, 	 &(driver_env.def.max_deliver) 	        },
63 		{ RESOURCE_NULL,     NULL, 			0, 1, NULL, 0,  NULL,   0, 	 NULL  					}
64 	};
65 
66 /* -------------------------------------------------------------------- */
67 
68 #define DELIVERTIMEOUT 		(driver_env.def.deliver_timeout)
69 #define TIMEOUT 		(driver_env.def.timeout)
70 #define WRITETIMEOUT 		(driver_env.def.write_timeout)
71 
72 /* -------------------------------------------------------------------- */
73 
74 #define FD			(driver_env.def.fd)
75 
76 /* -------------------------------------------------------------------- */
77 
78 #define STX "\02"          /* start character                                */
79 #define ETX "\03"          /* stop character                                 */
80 #define MAXPHONELEN 30     /* phone number maximum length                    */
81 #define MAXBUFLEN   300    /* maximum output buffer length                   */
82 #define MAXTEXTLEN  160     /* maximum SMS message length                     */
83 #define MODEM_NUMBER "026585382"
84 #define MAX_BUFSIZE 1024
85 #define EMOBISTAR_NORESPONSE -1
86 
87 #define CMDFORMAT "00/00000/O/51/%s/%s/////////////////3//%s///0//////////"
88 
89 
90 /* -------------------------------------------------------------------- */
91 
92 static char buf[MAX_BUFSIZE +1];
93 
94 /* -------------------------------------------------------------------- */
95 
96 static int MOBISTAR_login(void);
97 static int MOBISTAR_strtohex(char * , char *);
98 static int MOBISTAR_checksum(char *);
99 static void MOBISTAR_buildmessage(char *, char *, char *, char *);
100 static int  MOBISTAR_sendmessage(char *, char *);
101 static void MOBISTAR_hangup(void);
102 
103 /* -------------------------------------------------------------------- */
104 /* -------------------------------------------------------------------- */
MOBISTAR_login(void)105 static int MOBISTAR_login(void)
106 {
107 	char buf[MAX_RESPONSE_BUFSIZE];
108 
109 
110 	if (expstr(FD, buf, ACK1, MAX_RESPONSE_BUFSIZE, TIMEOUT) == 0)
111 	{
112 		lprintf(LOG_STANDARD, "Mobistar Service Login\n");
113 	}
114 	else
115 	{	lprintf(LOG_STANDARD, "No Mobistar Service Login\n");
116 
117 		MOBISTAR_hangup();
118 		return EMOBISTAR_NORESPONSE;
119 	}
120 
121 	return 0;
122 }
123 
124 /* -------------------------------------------------------------------- */
125 /* -------------------------------------------------------------------- */
126 
MOBISTAR_hangup(void)127 static void MOBISTAR_hangup(void)
128 {	default_hangup((DRIVER_DEFAULT_ENV *)(&driver_env));
129 }
130 
131 /* -------------------------------------------------------------------- */
132 /* Convert a string to its  hexadecimal representation                  */
133 /* Returns the length of the string                                     */
134 /* -------------------------------------------------------------------- */
135 
MOBISTAR_strtohex(char * strout,char * strin)136 static int MOBISTAR_strtohex(char * strout , char * strin)
137 { int i;
138 
139   for (i=0 ; i < strlen(strin); i++ )
140   { sms_snprintf(&strout[2*i],2,"%2X",strin[i]);
141   }
142   return strlen(strout);
143 }
144 
145 /* -------------------------------------------------------------------- */
146 /*   Compute checksum                                                   */
147 /* -------------------------------------------------------------------- */
148 
MOBISTAR_checksum(char * msg)149 static int MOBISTAR_checksum(char * msg)
150 { int cpt;
151   int i;
152 
153   cpt=0;
154   for (i=0;i<strlen(msg);i++)
155   {  cpt=cpt+msg[i];
156   }
157 
158   return cpt%256;
159 }
160 
161 
162 /* -------------------------------------------------------------------- */
163 /* Build message with stx and etx                                       */
164 /* -------------------------------------------------------------------- */
165 
MOBISTAR_buildmessage(char * outmsg,char * msgtext,char * phcaller,char * phdest)166 static void  MOBISTAR_buildmessage(char * outmsg , char * msgtext , char * phcaller , char * phdest)
167 { char  hexmsg[2*MAXTEXTLEN+1];     /* Message in hex representation */
168   int   hexmsglen;                  /* Message in hex representation length */
169   int   outmsglen;                  /* Length of the message to be sent     */
170   int   chksum;                     /* Checksum of the message              */
171   char  coutmsglen[6];              /* Output buffer length with checksum   */
172   char  outmsgtmp[MAXBUFLEN+1];
173 
174   hexmsglen=MOBISTAR_strtohex(hexmsg,msgtext);
175 
176   outmsglen=sms_snprintf(outmsgtmp,MAXBUFLEN,CMDFORMAT,phdest,phcaller,hexmsg);
177 
178   sms_snprintf(coutmsglen,5,"%05d",outmsglen+2); /* +2 chksum character */
179   memcpy(outmsgtmp+3,coutmsglen,5);
180 
181   chksum=MOBISTAR_checksum(outmsgtmp);
182   sms_snprintf(outmsg,MAXBUFLEN,"%s%s%2X%s",STX,outmsgtmp,chksum,ETX);
183 }
184 
185 
186 
187 /* -------------------------------------------------------------------- */
188 /* ------------------------------------------------------------------- */
MOBISTAR_sendmessage(char * msisdn,char * message)189 static int MOBISTAR_sendmessage(char *msisdn, char *message)
190 {
191 	char 	mobistar_message[MAXBUFLEN+1];
192 
193 
194 	MOBISTAR_buildmessage(mobistar_message, message,MODEM_NUMBER,msisdn);
195 
196 	twrite(FD, mobistar_message, strlen(mobistar_message), WRITETIMEOUT);
197 
198 	if (expstr(FD, buf, ETX, MAX_BUFSIZE, DELIVERTIMEOUT) == 0)
199 	{
200 		lprintf(LOG_STANDARD, "Received Message Response\n"
201 		                      "SMSC Respsonse: %s\n", buf);
202 	}
203 	else
204 	{	lprintf(LOG_STANDARD, "No Message or Wrong Response\n");
205 
206 		MOBISTAR_hangup();
207 		return EMOBISTAR_NORESPONSE;
208 	}
209 
210 	return 0;
211 }
212 
213 /* -------------------------------------------------------------------- */
214 /* -------------------------------------------------------------------- */
215 DEVICE_ENTRY mobistar_device = {
216 
217 	"MOBISTAR",
218 	"1.0",
219 	resource_list,
220 	(DRIVER_DEFAULT_ENV *)(&driver_env),
221 
222 	default_init,
223 	default_main,
224 	default_validate_numeric_id,
225 	default_dial,
226 	default_hangup,
227 	default_send_disconnect,
228 	default_single_deliver,
229 	MOBISTAR_sendmessage,
230 	MOBISTAR_login
231 };
232 
233