app_sms.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2004 - 2005, Adrian Kennard, rights assigned to Digium
  5. *
  6. * See http://www.asterisk.org for more information about
  7. * the Asterisk project. Please do not directly contact
  8. * any of the maintainers of this project for assistance;
  9. * the project provides a web site, mailing lists and IRC
  10. * channels for your use.
  11. *
  12. * This program is free software, distributed under the terms of
  13. * the GNU General Public License Version 2. See the LICENSE file
  14. * at the top of the source tree.
  15. */
  16. /*! \file
  17. *
  18. * \brief SMS application - ETSI ES 201 912 protocol 1 implementation
  19. *
  20. * \par Development notes
  21. * \note The ETSI standards are available free of charge from ETSI at
  22. * http://pda.etsi.org/pda/queryform.asp
  23. * Among the relevant documents here we have:
  24. *
  25. * ES 201 912 SMS for PSTN/ISDN
  26. * TS 123 040 Technical realization of SMS
  27. *
  28. *
  29. * \ingroup applications
  30. *
  31. * \author Adrian Kennard (for the original protocol 1 code)
  32. * \author Filippo Grassilli (Hyppo) - protocol 2 support
  33. * Not fully tested, under development
  34. */
  35. /*** MODULEINFO
  36. <support_level>extended</support_level>
  37. ***/
  38. #include "asterisk.h"
  39. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  40. #include <dirent.h>
  41. #include <ctype.h>
  42. #include <sys/stat.h>
  43. #include "asterisk/paths.h" /* use ast_config_AST_SPOOL_DIR and LOG_DIR */
  44. #include "asterisk/lock.h"
  45. #include "asterisk/file.h"
  46. #include "asterisk/channel.h"
  47. #include "asterisk/pbx.h"
  48. #include "asterisk/module.h"
  49. #include "asterisk/alaw.h"
  50. #include "asterisk/callerid.h"
  51. #include "asterisk/utils.h"
  52. #include "asterisk/app.h"
  53. #include "asterisk/format_cache.h"
  54. /*** DOCUMENTATION
  55. <application name="SMS" language="en_US">
  56. <synopsis>
  57. Communicates with SMS service centres and SMS capable analogue phones.
  58. </synopsis>
  59. <syntax>
  60. <parameter name="name" required="true">
  61. <para>The name of the queue used in <filename>/var/spool/asterisk/sms</filename></para>
  62. </parameter>
  63. <parameter name="options">
  64. <optionlist>
  65. <option name="a">
  66. <para>Answer, i.e. send initial FSK packet.</para>
  67. </option>
  68. <option name="s">
  69. <para>Act as service centre talking to a phone.</para>
  70. </option>
  71. <option name="t">
  72. <para>Use protocol 2 (default used is protocol 1).</para>
  73. </option>
  74. <option name="p">
  75. <para>Set the initial delay to N ms (default is <literal>300</literal>).
  76. addr and body are a deprecated format to send messages out.</para>
  77. </option>
  78. <option name="r">
  79. <para>Set the Status Report Request (SRR) bit.</para>
  80. </option>
  81. <option name="o">
  82. <para>The body should be coded as octets not 7-bit symbols.</para>
  83. </option>
  84. <option name="n">
  85. <para>Do not log any SMS content to log file (privacy).</para>
  86. </option>
  87. </optionlist>
  88. </parameter>
  89. <parameter name="addr" />
  90. <parameter name="body" />
  91. </syntax>
  92. <description>
  93. <para>SMS handles exchange of SMS data with a call to/from SMS capable phone or SMS PSTN service center.
  94. Can send and/or receive SMS messages. Works to ETSI ES 201 912; compatible with BT SMS PSTN service in
  95. UK and Telecom Italia in Italy.</para>
  96. <para>Typical usage is to use to handle calls from the SMS service centre CLI, or to set up a call using
  97. <literal>outgoing</literal> or manager interface to connect service centre to SMS().</para>
  98. <para>"Messages are processed as per text file message queues. smsq (a separate software) is a command to
  99. generate message queues and send messages.</para>
  100. <note><para>The protocol has tight delay bounds. Please use short frames and disable/keep short the
  101. jitter buffer on the ATA to make sure that respones (ACK etc.) are received in time.</para></note>
  102. </description>
  103. </application>
  104. ***/
  105. /* #define OUTALAW */ /* enable this to output Alaw rather than linear */
  106. /* ToDo */
  107. /* Add full VP support */
  108. /* Handle status report messages (generation and reception) */
  109. /* Time zones on time stamps */
  110. /* user ref field */
  111. static volatile unsigned char message_ref; /* arbitary message ref */
  112. static volatile unsigned int seq; /* arbitrary message sequence number for unqiue files */
  113. static char log_file[255];
  114. static char *app = "SMS";
  115. /*
  116. * 80 samples of a single period of the wave. At 8000 Hz, it means these
  117. * are the samples of a 100 Hz signal.
  118. * To pick the two carriers (1300Hz for '1' and 2100 Hz for '0') used by
  119. * the modulation, we should take one every 13 and 21 samples respectively.
  120. */
  121. static const signed short wave[] = {
  122. 0, 392, 782, 1167, 1545, 1913, 2270, 2612, 2939, 3247, 3536, 3802, 4045, 4263, 4455, 4619, 4755, 4862, 4938, 4985,
  123. 5000, 4985, 4938, 4862, 4755, 4619, 4455, 4263, 4045, 3802, 3536, 3247, 2939, 2612, 2270, 1913, 1545, 1167, 782, 392,
  124. 0, -392, -782, -1167,
  125. -1545, -1913, -2270, -2612, -2939, -3247, -3536, -3802, -4045, -4263, -4455, -4619, -4755, -4862, -4938, -4985, -5000,
  126. -4985, -4938, -4862,
  127. -4755, -4619, -4455, -4263, -4045, -3802, -3536, -3247, -2939, -2612, -2270, -1913, -1545, -1167, -782, -392
  128. };
  129. #ifdef OUTALAW
  130. static unsigned char wavea[80];
  131. typedef unsigned char output_t;
  132. static const output_t *wave_out = wavea; /* outgoing samples */
  133. #define __OUT_FMT ast_format_alaw
  134. #else
  135. typedef signed short output_t;
  136. static const output_t *wave_out = wave; /* outgoing samples */
  137. #define __OUT_FMT ast_format_slin
  138. #endif
  139. #define OSYNC_BITS 80 /* initial sync bits */
  140. /*!
  141. * The SMS spec ETSI ES 201 912 defines two protocols with different message types.
  142. * Also note that the high bit is used to indicate whether the message
  143. * is complete or not, but in two opposite ways:
  144. * for Protocol 1, 0x80 means that the message is complete;
  145. * for Protocol 2, 0x00 means that the message is complete;
  146. */
  147. enum message_types {
  148. DLL_SMS_MASK = 0x7f, /* mask for the valid bits */
  149. /* Protocol 1 values */
  150. DLL1_SMS_DATA = 0x11, /* data packet */
  151. DLL1_SMS_ERROR = 0x12,
  152. DLL1_SMS_EST = 0x13, /* start the connection */
  153. DLL1_SMS_REL = 0x14, /* end the connection */
  154. DLL1_SMS_ACK = 0x15,
  155. DLL1_SMS_NACK = 0x16,
  156. DLL1_SMS_COMPLETE = 0x80, /* packet is complete */
  157. DLL1_SMS_MORE = 0x00, /* more data to follow */
  158. /* Protocol 2 values */
  159. DLL2_SMS_EST = 0x7f, /* magic number. No message body */
  160. DLL2_SMS_INFO_MO = 0x10,
  161. DLL2_SMS_INFO_MT = 0x11,
  162. DLL2_SMS_INFO_STA = 0x12,
  163. DLL2_SMS_NACK = 0x13,
  164. DLL2_SMS_ACK0 = 0x14, /* ack even-numbered frame */
  165. DLL2_SMS_ACK1 = 0x15, /* ack odd-numbered frame */
  166. DLL2_SMS_ENQ = 0x16,
  167. DLL2_SMS_REL = 0x17, /* end the connection */
  168. DLL2_SMS_COMPLETE = 0x00, /* packet is complete */
  169. DLL2_SMS_MORE = 0x80, /* more data to follow */
  170. };
  171. /* SMS 7 bit character mapping to UCS-2 */
  172. static const unsigned short defaultalphabet[] = {
  173. 0x0040, 0x00A3, 0x0024, 0x00A5, 0x00E8, 0x00E9, 0x00F9, 0x00EC,
  174. 0x00F2, 0x00E7, 0x000A, 0x00D8, 0x00F8, 0x000D, 0x00C5, 0x00E5,
  175. 0x0394, 0x005F, 0x03A6, 0x0393, 0x039B, 0x03A9, 0x03A0, 0x03A8,
  176. 0x03A3, 0x0398, 0x039E, 0x00A0, 0x00C6, 0x00E6, 0x00DF, 0x00C9,
  177. ' ', '!', '"', '#', 164, '%', '&', 39, '(', ')', '*', '+', ',', '-', '.', '/',
  178. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?',
  179. 161, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
  180. 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 196, 214, 209, 220, 167,
  181. 191, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  182. 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 228, 246, 241, 252, 224,
  183. };
  184. static const unsigned short escapes[] = {
  185. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x000C, 0, 0, 0, 0, 0,
  186. 0, 0, 0, 0, 0x005E, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  187. 0, 0, 0, 0, 0, 0, 0, 0, 0x007B, 0x007D, 0, 0, 0, 0, 0, 0x005C,
  188. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x005B, 0x007E, 0x005D, 0,
  189. 0x007C, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  190. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  191. 0, 0, 0, 0, 0, 0x20AC, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  192. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  193. };
  194. #define SMSLEN 160 /*!< max SMS length */
  195. #define SMSLEN_8 140 /*!< max SMS length for 8-bit char */
  196. typedef struct sms_s {
  197. unsigned char hangup; /*!< we are done... */
  198. unsigned char err; /*!< set for any errors */
  199. unsigned char sent_rel:1; /*!< have sent REL message... */
  200. unsigned char smsc:1; /*!< we are SMSC */
  201. unsigned char rx:1; /*!< this is a received message */
  202. unsigned char nolog:1; /*!< do not log plain text SMS content (privacy) */
  203. char queue[30]; /*!< queue name */
  204. char oa[20]; /*!< originating address */
  205. char da[20]; /*!< destination address */
  206. struct timeval scts; /*!< time stamp, UTC */
  207. unsigned char pid; /*!< protocol ID */
  208. unsigned char dcs; /*!< data coding scheme */
  209. short mr; /*!< message reference - actually a byte, but use -1 for not set */
  210. int udl; /*!< user data length */
  211. int udhl; /*!< user data header length */
  212. unsigned char srr:1; /*!< Status Report request */
  213. unsigned char udhi:1; /*!< User Data Header required, even if length 0 */
  214. unsigned char rp:1; /*!< Reply Path */
  215. unsigned int vp; /*!< validity period in minutes, 0 for not set */
  216. unsigned short ud[SMSLEN]; /*!< user data (message), UCS-2 coded */
  217. unsigned char udh[SMSLEN]; /*!< user data header */
  218. char cli[20]; /*!< caller ID */
  219. unsigned char ophase; /*!< phase (0-79) for 0 and 1 frequencies (1300Hz and 2100Hz) */
  220. unsigned char ophasep; /*!< phase (0-79) for 1200 bps */
  221. unsigned char obyte; /*!< byte being sent */
  222. unsigned int opause; /*!< silent pause before sending (in sample periods) */
  223. unsigned char obitp; /*!< bit in byte */
  224. unsigned char osync; /*!< sync bits to send */
  225. unsigned char obytep; /*!< byte in data */
  226. unsigned char obyten; /*!< bytes in data */
  227. unsigned char omsg[256]; /*!< data buffer (out) */
  228. unsigned char imsg[250]; /*!< data buffer (in) */
  229. signed long long ims0,
  230. imc0,
  231. ims1,
  232. imc1; /*!< magnitude averages sin/cos 0/1 */
  233. unsigned int idle;
  234. unsigned short imag; /*!< signal level */
  235. unsigned char ips0; /*!< phase sin for bit 0, start at 0 inc by 21 mod 80 */
  236. unsigned char ips1; /*!< phase cos for bit 0, start at 20 inc by 21 mod 80 */
  237. unsigned char ipc0; /*!< phase sin for bit 1, start at 0 inc by 13 mod 80 */
  238. unsigned char ipc1; /*!< phase cos for bit 1, start at 20 inc by 13 mod 80 */
  239. unsigned char ibitl; /*!< last bit */
  240. unsigned char ibitc; /*!< bit run length count */
  241. unsigned char iphasep; /*!< bit phase (0-79) for 1200 bps */
  242. unsigned char ibitn; /*!< bit number in byte being received */
  243. unsigned char ibytev; /*!< byte value being received */
  244. unsigned char ibytep; /*!< byte pointer in message */
  245. unsigned char ibytec; /*!< byte checksum for message */
  246. unsigned char ierr; /*!< error flag */
  247. unsigned char ibith; /*!< history of last bits */
  248. unsigned char ibitt; /*!< total of 1's in last 3 bytes */
  249. /* more to go here */
  250. int opause_0; /*!< initial delay in ms, p() option */
  251. int protocol; /*!< ETSI SMS protocol to use (passed at app call) */
  252. int oseizure; /*!< protocol 2: channel seizure bits to send */
  253. int framenumber; /*!< protocol 2: frame number (for sending ACK0 or ACK1) */
  254. char udtxt[SMSLEN]; /*!< user data (message), PLAIN text */
  255. } sms_t;
  256. /* different types of encoding */
  257. #define is7bit(dcs) ( ((dcs) & 0xC0) ? (!((dcs) & 4) ) : (((dcs) & 0xc) == 0) )
  258. #define is8bit(dcs) ( ((dcs) & 0xC0) ? ( ((dcs) & 4) ) : (((dcs) & 0xc) == 4) )
  259. #define is16bit(dcs) ( ((dcs) & 0xC0) ? 0 : (((dcs) & 0xc) == 8) )
  260. static void sms_messagetx(sms_t *h);
  261. /*! \brief copy number, skipping non digits apart from leading + */
  262. static void numcpy(char *d, char *s)
  263. {
  264. if (*s == '+') {
  265. *d++ = *s++;
  266. }
  267. while (*s) {
  268. if (isdigit(*s)) {
  269. *d++ = *s;
  270. }
  271. s++;
  272. }
  273. *d = 0;
  274. }
  275. /*! \brief static, return a date/time in ISO format */
  276. static char *isodate(time_t t, char *buf, int len)
  277. {
  278. struct ast_tm tm;
  279. struct timeval local = { t, 0 };
  280. ast_localtime(&local, &tm, NULL);
  281. ast_strftime(buf, len, "%Y-%m-%dT%H:%M:%S", &tm);
  282. return buf;
  283. }
  284. /*! \brief Reads next UCS character from NUL terminated UTF-8 string and advance pointer */
  285. /* for non valid UTF-8 sequences, returns character as is */
  286. /* Does not advance pointer for null termination */
  287. static long utf8decode(unsigned char **pp)
  288. {
  289. unsigned char *p = *pp;
  290. if (!*p) {
  291. return 0; /* null termination of string */
  292. }
  293. (*pp)++;
  294. if (*p < 0xC0) {
  295. return *p; /* ascii or continuation character */
  296. }
  297. if (*p < 0xE0) {
  298. if (*p < 0xC2 || (p[1] & 0xC0) != 0x80) {
  299. return *p; /* not valid UTF-8 */
  300. }
  301. (*pp)++;
  302. return ((*p & 0x1F) << 6) + (p[1] & 0x3F);
  303. }
  304. if (*p < 0xF0) {
  305. if ((*p == 0xE0 && p[1] < 0xA0) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80) {
  306. return *p; /* not valid UTF-8 */
  307. }
  308. (*pp) += 2;
  309. return ((*p & 0x0F) << 12) + ((p[1] & 0x3F) << 6) + (p[2] & 0x3F);
  310. }
  311. if (*p < 0xF8) {
  312. if ((*p == 0xF0 && p[1] < 0x90) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80 || (p[3] & 0xC0) != 0x80) {
  313. return *p; /* not valid UTF-8 */
  314. }
  315. (*pp) += 3;
  316. return ((*p & 0x07) << 18) + ((p[1] & 0x3F) << 12) + ((p[2] & 0x3F) << 6) + (p[3] & 0x3F);
  317. }
  318. if (*p < 0xFC) {
  319. if ((*p == 0xF8 && p[1] < 0x88) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80 || (p[3] & 0xC0) != 0x80
  320. || (p[4] & 0xC0) != 0x80) {
  321. return *p; /* not valid UTF-8 */
  322. }
  323. (*pp) += 4;
  324. return ((*p & 0x03) << 24) + ((p[1] & 0x3F) << 18) + ((p[2] & 0x3F) << 12) + ((p[3] & 0x3F) << 6) + (p[4] & 0x3F);
  325. }
  326. if (*p < 0xFE) {
  327. if ((*p == 0xFC && p[1] < 0x84) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80 || (p[3] & 0xC0) != 0x80
  328. || (p[4] & 0xC0) != 0x80 || (p[5] & 0xC0) != 0x80) {
  329. return *p; /* not valid UTF-8 */
  330. }
  331. (*pp) += 5;
  332. return ((*p & 0x01) << 30) + ((p[1] & 0x3F) << 24) + ((p[2] & 0x3F) << 18) + ((p[3] & 0x3F) << 12) + ((p[4] & 0x3F) << 6) + (p[5] & 0x3F);
  333. }
  334. return *p; /* not sensible */
  335. }
  336. /*! \brief takes a binary header (udhl bytes at udh) and UCS-2 message (udl characters at ud) and packs in to o using SMS 7 bit character codes */
  337. /* The return value is the number of septets packed in to o, which is internally limited to SMSLEN */
  338. /* o can be null, in which case this is used to validate or count only */
  339. /* if the input contains invalid characters then the return value is -1 */
  340. static int packsms7(unsigned char *o, int udhl, unsigned char *udh, int udl, unsigned short *ud)
  341. {
  342. unsigned char p = 0; /* output pointer (bytes) */
  343. unsigned char b = 0; /* bit position */
  344. unsigned char n = 0; /* output character count */
  345. unsigned char dummy[SMSLEN];
  346. if (o == NULL) { /* output to a dummy buffer if o not set */
  347. o = dummy;
  348. }
  349. if (udhl) { /* header */
  350. o[p++] = udhl;
  351. b = 1;
  352. n = 1;
  353. while (udhl--) {
  354. o[p++] = *udh++;
  355. b += 8;
  356. while (b >= 7) {
  357. b -= 7;
  358. n++;
  359. }
  360. if (n >= SMSLEN)
  361. return n;
  362. }
  363. if (b) {
  364. b = 7 - b;
  365. if (++n >= SMSLEN)
  366. return n;
  367. } /* filling to septet boundary */
  368. }
  369. o[p] = 0;
  370. /* message */
  371. while (udl--) {
  372. long u;
  373. unsigned char v;
  374. u = *ud++;
  375. /* XXX 0 is invalid ? */
  376. /* look up in defaultalphabet[]. If found, v is the 7-bit code */
  377. for (v = 0; v < 128 && defaultalphabet[v] != u; v++);
  378. if (v == 128 /* not found */ && u && n + 1 < SMSLEN) {
  379. /* if not found, look in the escapes table (we need 2 bytes) */
  380. for (v = 0; v < 128 && escapes[v] != u; v++);
  381. if (v < 128) { /* escaped sequence, esc + v */
  382. /* store the low (8-b) bits in o[p], the remaining bits in o[p+1] */
  383. o[p] |= (27 << b); /* the low bits go into o[p] */
  384. b += 7;
  385. if (b >= 8) {
  386. b -= 8;
  387. p++;
  388. o[p] = (27 >> (7 - b));
  389. }
  390. n++;
  391. }
  392. }
  393. if (v == 128)
  394. return -1; /* invalid character */
  395. /* store, same as above */
  396. o[p] |= (v << b);
  397. b += 7;
  398. if (b >= 8) {
  399. b -= 8;
  400. p++;
  401. o[p] = (v >> (7 - b));
  402. }
  403. if (++n >= SMSLEN)
  404. return n;
  405. }
  406. return n;
  407. }
  408. /*! \brief takes a binary header (udhl bytes at udh) and UCS-2 message (udl characters at ud)
  409. * and packs in to o using 8 bit character codes.
  410. * The return value is the number of bytes packed in to o, which is internally limited to 140.
  411. * o can be null, in which case this is used to validate or count only.
  412. * if the input contains invalid characters then the return value is -1
  413. */
  414. static int packsms8(unsigned char *o, int udhl, unsigned char *udh, int udl, unsigned short *ud)
  415. {
  416. unsigned char p = 0;
  417. unsigned char dummy[SMSLEN_8];
  418. if (o == NULL)
  419. o = dummy;
  420. /* header - no encoding */
  421. if (udhl) {
  422. o[p++] = udhl;
  423. while (udhl--) {
  424. o[p++] = *udh++;
  425. if (p >= SMSLEN_8) {
  426. return p;
  427. }
  428. }
  429. }
  430. while (udl--) {
  431. long u;
  432. u = *ud++;
  433. if (u < 0 || u > 0xFF) {
  434. return -1; /* not valid */
  435. }
  436. o[p++] = u;
  437. if (p >= SMSLEN_8) {
  438. return p;
  439. }
  440. }
  441. return p;
  442. }
  443. /*! \brief takes a binary header (udhl bytes at udh) and UCS-2
  444. message (udl characters at ud) and packs in to o using 16 bit
  445. UCS-2 character codes
  446. The return value is the number of bytes packed in to o, which is
  447. internally limited to 140
  448. o can be null, in which case this is used to validate or count
  449. only if the input contains invalid characters then
  450. the return value is -1 */
  451. static int packsms16(unsigned char *o, int udhl, unsigned char *udh, int udl, unsigned short *ud)
  452. {
  453. unsigned char p = 0;
  454. unsigned char dummy[SMSLEN_8];
  455. if (o == NULL) {
  456. o = dummy;
  457. }
  458. /* header - no encoding */
  459. if (udhl) {
  460. o[p++] = udhl;
  461. while (udhl--) {
  462. o[p++] = *udh++;
  463. if (p >= SMSLEN_8) {
  464. return p;
  465. }
  466. }
  467. }
  468. while (udl--) {
  469. long u;
  470. u = *ud++;
  471. o[p++] = (u >> 8);
  472. if (p >= SMSLEN_8) {
  473. return p - 1; /* could not fit last character */
  474. }
  475. o[p++] = u;
  476. if (p >= SMSLEN_8) {
  477. return p;
  478. }
  479. }
  480. return p;
  481. }
  482. /*! \brief general pack, with length and data,
  483. returns number of bytes of target used */
  484. static int packsms(unsigned char dcs, unsigned char *base, unsigned int udhl, unsigned char *udh, int udl, unsigned short *ud)
  485. {
  486. unsigned char *p = base;
  487. if (udl == 0) {
  488. *p++ = 0; /* no user data */
  489. } else {
  490. int l = 0;
  491. if (is7bit(dcs)) { /* 7 bit */
  492. if ((l = packsms7(p + 1, udhl, udh, udl, ud)) < 0) {
  493. l = 0;
  494. }
  495. *p++ = l;
  496. p += (l * 7 + 7) / 8;
  497. } else if (is8bit(dcs)) { /* 8 bit */
  498. if ((l = packsms8(p + 1, udhl, udh, udl, ud)) < 0) {
  499. l = 0;
  500. }
  501. *p++ = l;
  502. p += l;
  503. } else { /* UCS-2 */
  504. if ((l = packsms16(p + 1, udhl, udh, udl, ud)) < 0) {
  505. l = 0;
  506. }
  507. *p++ = l;
  508. p += l;
  509. }
  510. }
  511. return p - base;
  512. }
  513. /*! \brief pack a date and return */
  514. static void packdate(unsigned char *o, time_t w)
  515. {
  516. struct ast_tm t;
  517. struct timeval topack = { w, 0 };
  518. int z;
  519. ast_localtime(&topack, &t, NULL);
  520. #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined( __NetBSD__ ) || defined(__APPLE__) || defined(__CYGWIN__)
  521. z = -t.tm_gmtoff / 60 / 15;
  522. #else
  523. z = timezone / 60 / 15;
  524. #endif
  525. *o++ = ((t.tm_year % 10) << 4) + (t.tm_year % 100) / 10;
  526. *o++ = (((t.tm_mon + 1) % 10) << 4) + (t.tm_mon + 1) / 10;
  527. *o++ = ((t.tm_mday % 10) << 4) + t.tm_mday / 10;
  528. *o++ = ((t.tm_hour % 10) << 4) + t.tm_hour / 10;
  529. *o++ = ((t.tm_min % 10) << 4) + t.tm_min / 10;
  530. *o++ = ((t.tm_sec % 10) << 4) + t.tm_sec / 10;
  531. if (z < 0) {
  532. *o++ = (((-z) % 10) << 4) + (-z) / 10 + 0x08;
  533. } else {
  534. *o++ = ((z % 10) << 4) + z / 10;
  535. }
  536. }
  537. /*! \brief unpack a date and return */
  538. static struct timeval unpackdate(unsigned char *i)
  539. {
  540. struct ast_tm t;
  541. t.tm_year = 100 + (i[0] & 0xF) * 10 + (i[0] >> 4);
  542. t.tm_mon = (i[1] & 0xF) * 10 + (i[1] >> 4) - 1;
  543. t.tm_mday = (i[2] & 0xF) * 10 + (i[2] >> 4);
  544. t.tm_hour = (i[3] & 0xF) * 10 + (i[3] >> 4);
  545. t.tm_min = (i[4] & 0xF) * 10 + (i[4] >> 4);
  546. t.tm_sec = (i[5] & 0xF) * 10 + (i[5] >> 4);
  547. t.tm_isdst = 0;
  548. if (i[6] & 0x08) {
  549. t.tm_min += 15 * ((i[6] & 0x7) * 10 + (i[6] >> 4));
  550. } else {
  551. t.tm_min -= 15 * ((i[6] & 0x7) * 10 + (i[6] >> 4));
  552. }
  553. return ast_mktime(&t, NULL);
  554. }
  555. /*! \brief unpacks bytes (7 bit encoding) at i, len l septets,
  556. and places in udh and ud setting udhl and udl. udh not used
  557. if udhi not set */
  558. static void unpacksms7(unsigned char *i, unsigned char l, unsigned char *udh, int *udhl, unsigned short *ud, int *udl, char udhi)
  559. {
  560. unsigned char b = 0, p = 0;
  561. unsigned short *o = ud;
  562. *udhl = 0;
  563. if (udhi && l) { /* header */
  564. int h = i[p];
  565. *udhl = h;
  566. if (h) {
  567. b = 1;
  568. p++;
  569. l--;
  570. while (h-- && l) {
  571. *udh++ = i[p++];
  572. b += 8;
  573. while (b >= 7) {
  574. b -= 7;
  575. l--;
  576. if (!l) {
  577. break;
  578. }
  579. }
  580. }
  581. /* adjust for fill, septets */
  582. if (b) {
  583. b = 7 - b;
  584. l--;
  585. }
  586. }
  587. }
  588. while (l--) {
  589. unsigned char v;
  590. if (b < 2) {
  591. v = ((i[p] >> b) & 0x7F); /* everything in one byte */
  592. } else {
  593. v = ((((i[p] >> b) + (i[p + 1] << (8 - b)))) & 0x7F);
  594. }
  595. b += 7;
  596. if (b >= 8) {
  597. b -= 8;
  598. p++;
  599. }
  600. /* 0x00A0 is the encoding of ESC (27) in defaultalphabet */
  601. if (o > ud && o[-1] == 0x00A0 && escapes[v]) {
  602. o[-1] = escapes[v];
  603. } else {
  604. *o++ = defaultalphabet[v];
  605. }
  606. }
  607. *udl = (o - ud);
  608. }
  609. /*! \brief unpacks bytes (8 bit encoding) at i, len l septets,
  610. * and places in udh and ud setting udhl and udl. udh not used
  611. * if udhi not set.
  612. */
  613. static void unpacksms8(unsigned char *i, unsigned char l, unsigned char *udh, int *udhl, unsigned short *ud, int *udl, char udhi)
  614. {
  615. unsigned short *o = ud;
  616. *udhl = 0;
  617. if (udhi) {
  618. int n = *i;
  619. *udhl = n;
  620. if (n) {
  621. i++;
  622. l--;
  623. while (l && n) {
  624. l--;
  625. n--;
  626. *udh++ = *i++;
  627. }
  628. }
  629. }
  630. while (l--) {
  631. *o++ = *i++; /* not to UTF-8 as explicitly 8 bit coding in DCS */
  632. }
  633. *udl = (o - ud);
  634. }
  635. /*! \brief unpacks bytes (16 bit encoding) at i, len l septets,
  636. and places in udh and ud setting udhl and udl.
  637. udh not used if udhi not set */
  638. static void unpacksms16(unsigned char *i, unsigned char l, unsigned char *udh, int *udhl, unsigned short *ud, int *udl, char udhi)
  639. {
  640. unsigned short *o = ud;
  641. *udhl = 0;
  642. if (udhi) {
  643. int n = *i;
  644. *udhl = n;
  645. if (n) {
  646. i++;
  647. l--;
  648. while (l && n) {
  649. l--;
  650. n--;
  651. *udh++ = *i++;
  652. }
  653. }
  654. }
  655. while (l--) {
  656. int v = *i++;
  657. if (l && l--) {
  658. v = (v << 8) + *i++;
  659. }
  660. *o++ = v;
  661. }
  662. *udl = (o - ud);
  663. }
  664. /*! \brief general unpack - starts with length byte (octet or septet) and returns number of bytes used, inc length */
  665. static int unpacksms(unsigned char dcs, unsigned char *i, unsigned char *udh, int *udhl, unsigned short *ud, int *udl, char udhi)
  666. {
  667. int l = *i++;
  668. if (is7bit(dcs)) {
  669. unpacksms7(i, l, udh, udhl, ud, udl, udhi);
  670. l = (l * 7 + 7) / 8; /* adjust length to return */
  671. } else if (is8bit(dcs)) {
  672. unpacksms8(i, l, udh, udhl, ud, udl, udhi);
  673. } else {
  674. l += l % 2;
  675. unpacksms16(i, l, udh, udhl, ud, udl, udhi);
  676. }
  677. return l + 1;
  678. }
  679. /*! \brief unpack an address from i, return byte length, unpack to o */
  680. static unsigned char unpackaddress(char *o, unsigned char *i)
  681. {
  682. unsigned char l = i[0], p;
  683. if (i[1] == 0x91) {
  684. *o++ = '+';
  685. }
  686. for (p = 0; p < l; p++) {
  687. if (p & 1) {
  688. *o++ = (i[2 + p / 2] >> 4) + '0';
  689. } else {
  690. *o++ = (i[2 + p / 2] & 0xF) + '0';
  691. }
  692. }
  693. *o = 0;
  694. return (l + 5) / 2;
  695. }
  696. /*! \brief store an address at o, and return number of bytes used */
  697. static unsigned char packaddress(unsigned char *o, char *i)
  698. {
  699. unsigned char p = 2;
  700. o[0] = 0; /* number of bytes */
  701. if (*i == '+') { /* record as bit 0 in byte 1 */
  702. i++;
  703. o[1] = 0x91;
  704. } else {
  705. o[1] = 0x81;
  706. }
  707. for ( ; *i ; i++) {
  708. if (!isdigit(*i)) { /* ignore non-digits */
  709. continue;
  710. }
  711. if (o[0] & 1) {
  712. o[p++] |= ((*i & 0xF) << 4);
  713. } else {
  714. o[p] = (*i & 0xF);
  715. }
  716. o[0]++;
  717. }
  718. if (o[0] & 1) {
  719. o[p++] |= 0xF0; /* pad */
  720. }
  721. return p;
  722. }
  723. /*! \brief Log the output, and remove file */
  724. static void sms_log(sms_t * h, char status)
  725. {
  726. int o;
  727. if (*h->oa == '\0' && *h->da == '\0') {
  728. return;
  729. }
  730. o = open(log_file, O_CREAT | O_APPEND | O_WRONLY, AST_FILE_MODE);
  731. if (o >= 0) {
  732. char line[1000], mrs[3] = "", *p;
  733. char buf[30];
  734. unsigned char n;
  735. if (h->mr >= 0) {
  736. snprintf(mrs, sizeof(mrs), "%02hhX", (unsigned char)h->mr);
  737. }
  738. snprintf(line, sizeof(line), "%s %c%c%c%s %s %s %s ",
  739. isodate(time(NULL), buf, sizeof(buf)),
  740. status, h->rx ? 'I' : 'O', h->smsc ? 'S' : 'M', mrs, h->queue,
  741. S_OR(h->oa, "-"), S_OR(h->da, "-") );
  742. p = line + strlen(line);
  743. if (h->nolog) {
  744. p += snprintf(p, 1000 - strlen(line), "udl=%d", h->udl);
  745. } else {
  746. for (n = 0; n < h->udl; n++) {
  747. if (h->ud[n] == '\\') {
  748. *p++ = '\\';
  749. *p++ = '\\';
  750. } else if (h->ud[n] == '\n') {
  751. *p++ = '\\';
  752. *p++ = 'n';
  753. } else if (h->ud[n] == '\r') {
  754. *p++ = '\\';
  755. *p++ = 'r';
  756. } else if (h->ud[n] < 32 || h->ud[n] == 127) {
  757. *p++ = 191;
  758. } else {
  759. *p++ = h->ud[n];
  760. }
  761. }
  762. }
  763. *p++ = '\n';
  764. *p = 0;
  765. if (write(o, line, strlen(line)) < 0) {
  766. ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
  767. }
  768. close(o);
  769. }
  770. *h->oa = *h->da = h->udl = 0;
  771. }
  772. /*! \brief parse and delete a file */
  773. static void sms_readfile(sms_t * h, char *fn)
  774. {
  775. char line[1000];
  776. FILE *s;
  777. char dcsset = 0; /* if DSC set */
  778. ast_log(LOG_NOTICE, "Sending %s\n", fn);
  779. h->rx = h->udl = *h->oa = *h->da = h->pid = h->srr = h->udhi = h->rp = h->vp = h->udhl = 0;
  780. h->mr = -1;
  781. h->dcs = 0xF1; /* normal messages class 1 */
  782. h->scts = ast_tvnow();
  783. s = fopen(fn, "r");
  784. if (s) {
  785. if (unlink(fn)) { /* concurrent access, we lost */
  786. fclose(s);
  787. return;
  788. }
  789. while (fgets (line, sizeof(line), s)) { /* process line in file */
  790. char *p;
  791. void *pp = &p;
  792. for (p = line; *p && *p != '\n' && *p != '\r'; p++);
  793. *p = 0; /* strip eoln */
  794. p = line;
  795. if (!*p || *p == ';') {
  796. continue; /* blank line or comment, ignore */
  797. }
  798. while (isalnum(*p)) {
  799. *p = tolower (*p);
  800. p++;
  801. }
  802. while (isspace (*p)) {
  803. *p++ = 0;
  804. }
  805. if (*p == '=') {
  806. *p++ = 0;
  807. if (!strcmp(line, "ud")) { /* parse message (UTF-8) */
  808. unsigned char o = 0;
  809. memcpy(h->udtxt, p, SMSLEN); /* for protocol 2 */
  810. while (*p && o < SMSLEN) {
  811. h->ud[o++] = utf8decode(pp);
  812. }
  813. h->udl = o;
  814. if (*p) {
  815. ast_log(LOG_WARNING, "UD too long in %s\n", fn);
  816. }
  817. } else {
  818. while (isspace (*p)) {
  819. p++;
  820. }
  821. if (!strcmp(line, "oa") && strlen(p) < sizeof(h->oa)) {
  822. numcpy (h->oa, p);
  823. } else if (!strcmp(line, "da") && strlen(p) < sizeof(h->oa)) {
  824. numcpy (h->da, p);
  825. } else if (!strcmp(line, "pid")) {
  826. h->pid = atoi(p);
  827. } else if (!strcmp(line, "dcs")) {
  828. h->dcs = atoi(p);
  829. dcsset = 1;
  830. } else if (!strcmp(line, "mr")) {
  831. h->mr = atoi(p);
  832. } else if (!strcmp(line, "srr")) {
  833. h->srr = (atoi(p) ? 1 : 0);
  834. } else if (!strcmp(line, "vp")) {
  835. h->vp = atoi(p);
  836. } else if (!strcmp(line, "rp")) {
  837. h->rp = (atoi(p) ? 1 : 0);
  838. } else if (!strcmp(line, "scts")) { /* get date/time */
  839. int Y, m, d, H, M, S;
  840. /* XXX Why aren't we using ast_strptime here? */
  841. if (sscanf(p, "%4d-%2d-%2dT%2d:%2d:%2d", &Y, &m, &d, &H, &M, &S) == 6) {
  842. struct ast_tm t = { 0, };
  843. t.tm_year = Y - 1900;
  844. t.tm_mon = m - 1;
  845. t.tm_mday = d;
  846. t.tm_hour = H;
  847. t.tm_min = M;
  848. t.tm_sec = S;
  849. t.tm_isdst = -1;
  850. h->scts = ast_mktime(&t, NULL);
  851. if (h->scts.tv_sec == 0) {
  852. ast_log(LOG_WARNING, "Bad date/timein %s: %s", fn, p);
  853. }
  854. }
  855. } else {
  856. ast_log(LOG_WARNING, "Cannot parse in %s: %s=%si\n", fn, line, p);
  857. }
  858. }
  859. } else if (*p == '#') { /* raw hex format */
  860. *p++ = 0;
  861. if (*p == '#') {
  862. p++;
  863. if (!strcmp(line, "ud")) { /* user data */
  864. int o = 0;
  865. while (*p && o < SMSLEN) {
  866. if (isxdigit(*p) && isxdigit(p[1]) && isxdigit(p[2]) && isxdigit(p[3])) {
  867. h->ud[o++] =
  868. (((isalpha(*p) ? 9 : 0) + (*p & 0xF)) << 12) +
  869. (((isalpha(p[1]) ? 9 : 0) + (p[1] & 0xF)) << 8) +
  870. (((isalpha(p[2]) ? 9 : 0) + (p[2] & 0xF)) << 4) + ((isalpha(p[3]) ? 9 : 0) + (p[3] & 0xF));
  871. p += 4;
  872. } else
  873. break;
  874. }
  875. h->udl = o;
  876. if (*p)
  877. ast_log(LOG_WARNING, "UD too long / invalid UCS-2 hex in %s\n", fn);
  878. } else
  879. ast_log(LOG_WARNING, "Only ud can use ## format, %s\n", fn);
  880. } else if (!strcmp(line, "ud")) { /* user data */
  881. int o = 0;
  882. while (*p && o < SMSLEN) {
  883. if (isxdigit(*p) && isxdigit(p[1])) {
  884. h->ud[o++] = (((isalpha(*p) ? 9 : 0) + (*p & 0xF)) << 4) + ((isalpha(p[1]) ? 9 : 0) + (p[1] & 0xF));
  885. p += 2;
  886. } else {
  887. break;
  888. }
  889. }
  890. h->udl = o;
  891. if (*p) {
  892. ast_log(LOG_WARNING, "UD too long / invalid UCS-1 hex in %s\n", fn);
  893. }
  894. } else if (!strcmp(line, "udh")) { /* user data header */
  895. unsigned char o = 0;
  896. h->udhi = 1;
  897. while (*p && o < SMSLEN) {
  898. if (isxdigit(*p) && isxdigit(p[1])) {
  899. h->udh[o] = (((isalpha(*p) ? 9 : 0) + (*p & 0xF)) << 4) + ((isalpha(p[1]) ? 9 : 0) + (p[1] & 0xF));
  900. o++;
  901. p += 2;
  902. } else {
  903. break;
  904. }
  905. }
  906. h->udhl = o;
  907. if (*p) {
  908. ast_log(LOG_WARNING, "UDH too long / invalid hex in %s\n", fn);
  909. }
  910. } else {
  911. ast_log(LOG_WARNING, "Only ud and udh can use # format, %s\n", fn);
  912. }
  913. } else {
  914. ast_log(LOG_WARNING, "Cannot parse in %s: %s\n", fn, line);
  915. }
  916. }
  917. fclose(s);
  918. if (!dcsset && packsms7(0, h->udhl, h->udh, h->udl, h->ud) < 0) {
  919. if (packsms8(0, h->udhl, h->udh, h->udl, h->ud) < 0) {
  920. if (packsms16(0, h->udhl, h->udh, h->udl, h->ud) < 0) {
  921. ast_log(LOG_WARNING, "Invalid UTF-8 message even for UCS-2 (%s)\n", fn);
  922. } else {
  923. h->dcs = 0x08; /* default to 16 bit */
  924. ast_log(LOG_WARNING, "Sending in 16 bit format(%s)\n", fn);
  925. }
  926. } else {
  927. h->dcs = 0xF5; /* default to 8 bit */
  928. ast_log(LOG_WARNING, "Sending in 8 bit format(%s)\n", fn);
  929. }
  930. }
  931. if (is7bit(h->dcs) && packsms7(0, h->udhl, h->udh, h->udl, h->ud) < 0) {
  932. ast_log(LOG_WARNING, "Invalid 7 bit GSM data %s\n", fn);
  933. }
  934. if (is8bit(h->dcs) && packsms8(0, h->udhl, h->udh, h->udl, h->ud) < 0) {
  935. ast_log(LOG_WARNING, "Invalid 8 bit data %s\n", fn);
  936. }
  937. if (is16bit(h->dcs) && packsms16(0, h->udhl, h->udh, h->udl, h->ud) < 0) {
  938. ast_log(LOG_WARNING, "Invalid 16 bit data %s\n", fn);
  939. }
  940. }
  941. }
  942. /*! \brief white a received text message to a file */
  943. static void sms_writefile(sms_t * h)
  944. {
  945. char fn[200] = "", fn2[200] = "";
  946. char buf[30];
  947. FILE *o;
  948. if (ast_tvzero(h->scts)) {
  949. h->scts = ast_tvnow();
  950. }
  951. snprintf(fn, sizeof(fn), "%s/sms/%s", ast_config_AST_SPOOL_DIR, h->smsc ? h->rx ? "morx" : "mttx" : h->rx ? "mtrx" : "motx");
  952. ast_mkdir(fn, 0777); /* ensure it exists */
  953. ast_copy_string(fn2, fn, sizeof(fn2));
  954. snprintf(fn2 + strlen(fn2), sizeof(fn2) - strlen(fn2), "/%s.%s-%u", h->queue, isodate(h->scts.tv_sec, buf, sizeof(buf)), seq++);
  955. snprintf(fn + strlen(fn), sizeof(fn) - strlen(fn), "/.%s", fn2 + strlen(fn) + 1);
  956. if ((o = fopen(fn, "w")) == NULL) {
  957. return;
  958. }
  959. if (*h->oa) {
  960. fprintf(o, "oa=%s\n", h->oa);
  961. }
  962. if (*h->da) {
  963. fprintf(o, "da=%s\n", h->da);
  964. }
  965. if (h->udhi) {
  966. unsigned int p;
  967. fprintf(o, "udh#");
  968. for (p = 0; p < h->udhl; p++) {
  969. fprintf(o, "%02hhX", (unsigned char)h->udh[p]);
  970. }
  971. fprintf(o, "\n");
  972. }
  973. if (h->udl) {
  974. unsigned int p;
  975. for (p = 0; p < h->udl && h->ud[p] >= ' '; p++);
  976. if (p < h->udl) {
  977. fputc(';', o); /* cannot use ud=, but include as a comment for human readable */
  978. }
  979. fprintf(o, "ud=");
  980. for (p = 0; p < h->udl; p++) {
  981. unsigned short v = h->ud[p];
  982. if (v < 32) {
  983. fputc(191, o);
  984. } else if (v < 0x80) {
  985. fputc(v, o);
  986. } else if (v < 0x800) {
  987. fputc(0xC0 + (v >> 6), o);
  988. fputc(0x80 + (v & 0x3F), o);
  989. } else {
  990. fputc(0xE0 + (v >> 12), o);
  991. fputc(0x80 + ((v >> 6) & 0x3F), o);
  992. fputc(0x80 + (v & 0x3F), o);
  993. }
  994. }
  995. fprintf(o, "\n");
  996. for (p = 0; p < h->udl && h->ud[p] >= ' '; p++);
  997. if (p < h->udl) {
  998. for (p = 0; p < h->udl && h->ud[p] < 0x100; p++);
  999. if (p == h->udl) { /* can write in ucs-1 hex */
  1000. fprintf(o, "ud#");
  1001. for (p = 0; p < h->udl; p++) {
  1002. fprintf(o, "%02hhX", (unsigned char)h->ud[p]);
  1003. }
  1004. fprintf(o, "\n");
  1005. } else { /* write in UCS-2 */
  1006. fprintf(o, "ud##");
  1007. for (p = 0; p < h->udl; p++) {
  1008. fprintf(o, "%04X", (unsigned)h->ud[p]);
  1009. }
  1010. fprintf(o, "\n");
  1011. }
  1012. }
  1013. }
  1014. if (h->scts.tv_sec) {
  1015. char datebuf[30];
  1016. fprintf(o, "scts=%s\n", isodate(h->scts.tv_sec, datebuf, sizeof(datebuf)));
  1017. }
  1018. if (h->pid) {
  1019. fprintf(o, "pid=%d\n", h->pid);
  1020. }
  1021. if (h->dcs != 0xF1) {
  1022. fprintf(o, "dcs=%d\n", h->dcs);
  1023. }
  1024. if (h->vp) {
  1025. fprintf(o, "vp=%u\n", h->vp);
  1026. }
  1027. if (h->srr) {
  1028. fprintf(o, "srr=1\n");
  1029. }
  1030. if (h->mr >= 0) {
  1031. fprintf(o, "mr=%d\n", h->mr);
  1032. }
  1033. if (h->rp) {
  1034. fprintf(o, "rp=1\n");
  1035. }
  1036. fclose(o);
  1037. if (rename(fn, fn2)) {
  1038. unlink(fn);
  1039. } else {
  1040. ast_log(LOG_NOTICE, "Received to %s\n", fn2);
  1041. }
  1042. }
  1043. /*! \brief read dir skipping dot files... */
  1044. static struct dirent *readdirqueue(DIR *d, char *queue)
  1045. {
  1046. struct dirent *f;
  1047. do {
  1048. f = readdir(d);
  1049. } while (f && (*f->d_name == '.' || strncmp(f->d_name, queue, strlen(queue)) || f->d_name[strlen(queue)] != '.'));
  1050. return f;
  1051. }
  1052. /*! \brief handle the incoming message */
  1053. static unsigned char sms_handleincoming (sms_t * h)
  1054. {
  1055. unsigned char p = 3;
  1056. if (h->smsc) { /* SMSC */
  1057. if ((h->imsg[2] & 3) == 1) { /* SMS-SUBMIT */
  1058. h->udhl = h->udl = 0;
  1059. h->vp = 0;
  1060. h->srr = ((h->imsg[2] & 0x20) ? 1 : 0);
  1061. h->udhi = ((h->imsg[2] & 0x40) ? 1 : 0);
  1062. h->rp = ((h->imsg[2] & 0x80) ? 1 : 0);
  1063. ast_copy_string(h->oa, h->cli, sizeof(h->oa));
  1064. h->scts = ast_tvnow();
  1065. h->mr = h->imsg[p++];
  1066. p += unpackaddress(h->da, h->imsg + p);
  1067. h->pid = h->imsg[p++];
  1068. h->dcs = h->imsg[p++];
  1069. if ((h->imsg[2] & 0x18) == 0x10) { /* relative VP */
  1070. if (h->imsg[p] < 144) {
  1071. h->vp = (h->imsg[p] + 1) * 5;
  1072. } else if (h->imsg[p] < 168) {
  1073. h->vp = 720 + (h->imsg[p] - 143) * 30;
  1074. } else if (h->imsg[p] < 197) {
  1075. h->vp = (h->imsg[p] - 166) * 1440;
  1076. } else {
  1077. h->vp = (h->imsg[p] - 192) * 10080;
  1078. }
  1079. p++;
  1080. } else if (h->imsg[2] & 0x18) {
  1081. p += 7; /* ignore enhanced / absolute VP */
  1082. }
  1083. p += unpacksms(h->dcs, h->imsg + p, h->udh, &h->udhl, h->ud, &h->udl, h->udhi);
  1084. h->rx = 1; /* received message */
  1085. sms_writefile(h); /* write the file */
  1086. if (p != h->imsg[1] + 2) {
  1087. ast_log(LOG_WARNING, "Mismatch receive unpacking %d/%d\n", p, h->imsg[1] + 2);
  1088. return 0xFF; /* duh! */
  1089. }
  1090. } else {
  1091. ast_log(LOG_WARNING, "Unknown message type %02hhX\n", h->imsg[2]);
  1092. return 0xFF;
  1093. }
  1094. } else { /* client */
  1095. if (!(h->imsg[2] & 3)) { /* SMS-DELIVER */
  1096. *h->da = h->srr = h->rp = h->vp = h->udhi = h->udhl = h->udl = 0;
  1097. h->srr = ((h->imsg[2] & 0x20) ? 1 : 0);
  1098. h->udhi = ((h->imsg[2] & 0x40) ? 1 : 0);
  1099. h->rp = ((h->imsg[2] & 0x80) ? 1 : 0);
  1100. h->mr = -1;
  1101. p += unpackaddress(h->oa, h->imsg + p);
  1102. h->pid = h->imsg[p++];
  1103. h->dcs = h->imsg[p++];
  1104. h->scts = unpackdate(h->imsg + p);
  1105. p += 7;
  1106. p += unpacksms(h->dcs, h->imsg + p, h->udh, &h->udhl, h->ud, &h->udl, h->udhi);
  1107. h->rx = 1; /* received message */
  1108. sms_writefile(h); /* write the file */
  1109. if (p != h->imsg[1] + 2) {
  1110. ast_log(LOG_WARNING, "Mismatch receive unpacking %d/%d\n", p, h->imsg[1] + 2);
  1111. return 0xFF; /* duh! */
  1112. }
  1113. } else {
  1114. ast_log(LOG_WARNING, "Unknown message type %02hhX\n", h->imsg[2]);
  1115. return 0xFF;
  1116. }
  1117. }
  1118. return 0; /* no error */
  1119. }
  1120. #ifdef SOLARIS
  1121. #define NAME_MAX 1024
  1122. #endif
  1123. /*!
  1124. * Add data to a protocol 2 message.
  1125. * Use the length field (h->omsg[1]) as a pointer to the next free position.
  1126. */
  1127. static void adddata_proto2(sms_t *h, unsigned char msg, char *data, int size)
  1128. {
  1129. int x = h->omsg[1] + 2; /* Get current position */
  1130. if (x == 2) {
  1131. x += 2; /* First: skip Payload length (set later) */
  1132. }
  1133. h->omsg[x++] = msg; /* Message code */
  1134. h->omsg[x++] = (unsigned char)size; /* Data size Low */
  1135. h->omsg[x++] = 0; /* Data size Hi */
  1136. for (; size > 0 ; size--) {
  1137. h->omsg[x++] = *data++;
  1138. }
  1139. h->omsg[1] = x - 2; /* Frame size */
  1140. h->omsg[2] = x - 4; /* Payload length (Lo) */
  1141. h->omsg[3] = 0; /* Payload length (Hi) */
  1142. }
  1143. static void putdummydata_proto2(sms_t *h)
  1144. {
  1145. adddata_proto2(h, 0x10, "\0", 1); /* Media Identifier > SMS */
  1146. adddata_proto2(h, 0x11, "\0\0\0\0\0\0", 6); /* Firmware version */
  1147. adddata_proto2(h, 0x12, "\2\0\4", 3); /* SMS provider ID */
  1148. adddata_proto2(h, 0x13, h->udtxt, h->udl); /* Body */
  1149. }
  1150. static void sms_compose2(sms_t *h, int more)
  1151. {
  1152. struct ast_tm tm;
  1153. struct timeval now = h->scts;
  1154. char stm[9];
  1155. h->omsg[0] = 0x00; /* set later... */
  1156. h->omsg[1] = 0;
  1157. putdummydata_proto2(h);
  1158. if (h->smsc) { /* deliver */
  1159. h->omsg[0] = 0x11; /* SMS_DELIVERY */
  1160. /* Required: 10 11 12 13 14 15 17 (seems they must be ordered!) */
  1161. ast_localtime(&now, &tm, NULL);
  1162. sprintf(stm, "%02d%02d%02d%02d", tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min); /* Date mmddHHMM */
  1163. adddata_proto2(h, 0x14, stm, 8); /* Date */
  1164. if (*h->oa == 0) {
  1165. strcpy(h->oa, "00000000");
  1166. }
  1167. adddata_proto2(h, 0x15, h->oa, strlen(h->oa)); /* Originator */
  1168. adddata_proto2(h, 0x17, "\1", 1); /* Calling Terminal ID */
  1169. } else { /* submit */
  1170. h->omsg[0] = 0x10; /* SMS_SUBMIT */
  1171. /* Required: 10 11 12 13 17 18 1B 1C (seems they must be ordered!) */
  1172. adddata_proto2(h, 0x17, "\1", 1); /* Calling Terminal ID */
  1173. if (*h->da == 0) {
  1174. strcpy(h->da, "00000000");
  1175. }
  1176. adddata_proto2(h, 0x18, h->da, strlen(h->da)); /* Originator */
  1177. adddata_proto2(h, 0x1B, "\1", 1); /* Called Terminal ID */
  1178. adddata_proto2(h, 0x1C, "\0\0\0", 3); /* Notification */
  1179. }
  1180. }
  1181. static void putdummydata_proto2(sms_t *h);
  1182. #define MAX_DEBUG_LEN 300
  1183. static char *sms_hexdump(unsigned char buf[], int size, char *s /* destination */)
  1184. {
  1185. char *p;
  1186. int f;
  1187. for (p = s, f = 0; f < size && f < MAX_DEBUG_LEN; f++, p += 3) {
  1188. sprintf(p, "%02hhX ", (unsigned char)buf[f]);
  1189. }
  1190. return(s);
  1191. }
  1192. /*! \brief sms_handleincoming_proto2: handle the incoming message */
  1193. static int sms_handleincoming_proto2(sms_t *h)
  1194. {
  1195. int f, i, sz = 0;
  1196. int msg, msgsz;
  1197. struct ast_tm tm;
  1198. struct timeval now = { 0, 0 };
  1199. char debug_buf[MAX_DEBUG_LEN * 3 + 1];
  1200. sz = h->imsg[1] + 2;
  1201. /* ast_verb(3, "SMS-P2 Frame: %s\n", sms_hexdump(h->imsg, sz, debug_buf)); */
  1202. /* Parse message body (called payload) */
  1203. now = h->scts = ast_tvnow();
  1204. for (f = 4; f < sz; ) {
  1205. msg = h->imsg[f++];
  1206. msgsz = h->imsg[f++];
  1207. msgsz += (h->imsg[f++] * 256);
  1208. switch (msg) {
  1209. case 0x13: /* Body */
  1210. ast_verb(3, "SMS-P2 Body#%02X=[%.*s]\n", (unsigned)msg, msgsz, &h->imsg[f]);
  1211. if (msgsz >= sizeof(h->ud)) {
  1212. msgsz = sizeof(h->ud) - 1;
  1213. }
  1214. for (i = 0; i < msgsz; i++) {
  1215. h->ud[i] = h->imsg[f + i];
  1216. }
  1217. h->udl = msgsz;
  1218. break;
  1219. case 0x14: /* Date SCTS */
  1220. now = h->scts = ast_tvnow();
  1221. ast_localtime(&now, &tm, NULL);
  1222. tm.tm_mon = ( (h->imsg[f] * 10) + h->imsg[f + 1] ) - 1;
  1223. tm.tm_mday = ( (h->imsg[f + 2] * 10) + h->imsg[f + 3] );
  1224. tm.tm_hour = ( (h->imsg[f + 4] * 10) + h->imsg[f + 5] );
  1225. tm.tm_min = ( (h->imsg[f + 6] * 10) + h->imsg[f + 7] );
  1226. tm.tm_sec = 0;
  1227. h->scts = ast_mktime(&tm, NULL);
  1228. ast_verb(3, "SMS-P2 Date#%02X=%02d/%02d %02d:%02d\n", (unsigned)msg, tm.tm_mday, tm.tm_mon + 1, tm.tm_hour, tm.tm_min);
  1229. break;
  1230. case 0x15: /* Calling line (from SMSC) */
  1231. if (msgsz >= 20) {
  1232. msgsz = 20 - 1;
  1233. }
  1234. ast_verb(3, "SMS-P2 Origin#%02X=[%.*s]\n", (unsigned)msg, msgsz, &h->imsg[f]);
  1235. ast_copy_string(h->oa, (char *)(&h->imsg[f]), msgsz + 1);
  1236. break;
  1237. case 0x18: /* Destination(from TE/phone) */
  1238. if (msgsz >= 20) {
  1239. msgsz = 20 - 1;
  1240. }
  1241. ast_verb(3, "SMS-P2 Destination#%02X=[%.*s]\n", (unsigned)msg, msgsz, &h->imsg[f]);
  1242. ast_copy_string(h->da, (char *)(&h->imsg[f]), msgsz + 1);
  1243. break;
  1244. case 0x1C: /* Notify */
  1245. ast_verb(3, "SMS-P2 Notify#%02X=%s\n", (unsigned)msg, sms_hexdump(&h->imsg[f], 3, debug_buf));
  1246. break;
  1247. default:
  1248. ast_verb(3, "SMS-P2 Par#%02X [%d]: %s\n", (unsigned)msg, msgsz, sms_hexdump(&h->imsg[f], msgsz, debug_buf));
  1249. break;
  1250. }
  1251. f+=msgsz; /* Skip to next */
  1252. }
  1253. h->rx = 1; /* received message */
  1254. sms_writefile(h); /* write the file */
  1255. return 0; /* no error */
  1256. }
  1257. #if 0
  1258. static void smssend(sms_t *h, char *c)
  1259. {
  1260. int f, x;
  1261. for (f = 0; f < strlen(c); f++) {
  1262. sscanf(&c[f*3], "%x", &x);
  1263. h->omsg[f] = x;
  1264. }
  1265. sms_messagetx(h);
  1266. }
  1267. #endif
  1268. static void sms_nextoutgoing (sms_t *h);
  1269. static void sms_messagerx2(sms_t * h)
  1270. {
  1271. int p = h->imsg[0] & DLL_SMS_MASK ; /* mask the high bit */
  1272. int cause;
  1273. #define DLL2_ACK(h) ((h->framenumber & 1) ? DLL2_SMS_ACK1: DLL2_SMS_ACK1)
  1274. switch (p) {
  1275. case DLL2_SMS_EST: /* Protocol 2: Connection ready (fake): send message */
  1276. sms_nextoutgoing (h);
  1277. /* smssend(h,"11 29 27 00 10 01 00 00 11 06 00 00 00 00 00 00 00 12 03 00 02 00 04 13 01 00 41 14 08 00 30 39 31 35 30 02 30 02 15 02 00 39 30 "); */
  1278. break;
  1279. case DLL2_SMS_INFO_MO: /* transport SMS_SUBMIT */
  1280. case DLL2_SMS_INFO_MT: /* transport SMS_DELIVERY */
  1281. cause = sms_handleincoming_proto2(h);
  1282. if (!cause) { /* ACK */
  1283. sms_log(h, 'Y');
  1284. }
  1285. h->omsg[0] = DLL2_ACK(h);
  1286. h->omsg[1] = 0x06; /* msg len */
  1287. h->omsg[2] = 0x04; /* payload len */
  1288. h->omsg[3] = 0x00; /* payload len */
  1289. h->omsg[4] = 0x1f; /* Response type */
  1290. h->omsg[5] = 0x01; /* parameter len */
  1291. h->omsg[6] = 0x00; /* parameter len */
  1292. h->omsg[7] = cause; /* CONFIRM or error */
  1293. sms_messagetx(h);
  1294. break;
  1295. case DLL2_SMS_NACK: /* Protocol 2: SMS_NAK */
  1296. h->omsg[0] = DLL2_SMS_REL; /* SMS_REL */
  1297. h->omsg[1] = 0x00; /* msg len */
  1298. sms_messagetx(h);
  1299. break;
  1300. case DLL2_SMS_ACK0:
  1301. case DLL2_SMS_ACK1:
  1302. /* SMS_ACK also transport SMS_SUBMIT or SMS_DELIVERY */
  1303. if ( (h->omsg[0] & DLL_SMS_MASK) == DLL2_SMS_REL) {
  1304. /* a response to our Release, just hangup */
  1305. h->hangup = 1; /* hangup */
  1306. } else {
  1307. /* XXX depending on what we are.. */
  1308. ast_log(LOG_NOTICE, "SMS_SUBMIT or SMS_DELIVERY\n");
  1309. sms_nextoutgoing (h);
  1310. }
  1311. break;
  1312. case DLL2_SMS_REL: /* Protocol 2: SMS_REL (hangup req) */
  1313. h->omsg[0] = DLL2_ACK(h);
  1314. h->omsg[1] = 0;
  1315. sms_messagetx(h);
  1316. break;
  1317. }
  1318. }
  1319. /*! \brief compose a message for protocol 1 */
  1320. static void sms_compose1(sms_t *h, int more)
  1321. {
  1322. unsigned int p = 2; /* next byte to write. Skip type and len */
  1323. h->omsg[0] = 0x91; /* SMS_DATA */
  1324. if (h->smsc) { /* deliver */
  1325. h->omsg[p++] = (more ? 4 : 0) + ((h->udhl > 0) ? 0x40 : 0);
  1326. p += packaddress(h->omsg + p, h->oa);
  1327. h->omsg[p++] = h->pid;
  1328. h->omsg[p++] = h->dcs;
  1329. packdate(h->omsg + p, h->scts.tv_sec);
  1330. p += 7;
  1331. p += packsms(h->dcs, h->omsg + p, h->udhl, h->udh, h->udl, h->ud);
  1332. } else { /* submit */
  1333. h->omsg[p++] =
  1334. 0x01 + (more ? 4 : 0) + (h->srr ? 0x20 : 0) + (h->rp ? 0x80 : 0) + (h->vp ? 0x10 : 0) + (h->udhi ? 0x40 : 0);
  1335. if (h->mr < 0) {
  1336. h->mr = message_ref++;
  1337. }
  1338. h->omsg[p++] = h->mr;
  1339. p += packaddress(h->omsg + p, h->da);
  1340. h->omsg[p++] = h->pid;
  1341. h->omsg[p++] = h->dcs;
  1342. if (h->vp) { /* relative VP */
  1343. if (h->vp < 720) {
  1344. h->omsg[p++] = (h->vp + 4) / 5 - 1;
  1345. } else if (h->vp < 1440) {
  1346. h->omsg[p++] = (h->vp - 720 + 29) / 30 + 143;
  1347. } else if (h->vp < 43200) {
  1348. h->omsg[p++] = (h->vp + 1439) / 1440 + 166;
  1349. } else if (h->vp < 635040) {
  1350. h->omsg[p++] = (h->vp + 10079) / 10080 + 192;
  1351. } else {
  1352. h->omsg[p++] = 255; /* max */
  1353. }
  1354. }
  1355. p += packsms(h->dcs, h->omsg + p, h->udhl, h->udh, h->udl, h->ud);
  1356. }
  1357. h->omsg[1] = p - 2;
  1358. }
  1359. /*! \brief find and fill in next message, or send a REL if none waiting */
  1360. static void sms_nextoutgoing (sms_t * h)
  1361. {
  1362. char fn[100 + NAME_MAX] = "";
  1363. DIR *d;
  1364. char more = 0;
  1365. *h->da = *h->oa = '\0'; /* clear destinations */
  1366. h->rx = 0; /* outgoing message */
  1367. snprintf(fn, sizeof(fn), "%s/sms/%s", ast_config_AST_SPOOL_DIR, h->smsc ? "mttx" : "motx");
  1368. ast_mkdir(fn, 0777); /* ensure it exists */
  1369. d = opendir(fn);
  1370. if (d) {
  1371. struct dirent *f = readdirqueue(d, h->queue);
  1372. if (f) {
  1373. snprintf(fn + strlen(fn), sizeof(fn) - strlen(fn), "/%s", f->d_name);
  1374. sms_readfile(h, fn);
  1375. if (readdirqueue(d, h->queue)) {
  1376. more = 1; /* more to send */
  1377. }
  1378. }
  1379. closedir(d);
  1380. }
  1381. if (*h->da || *h->oa) { /* message to send */
  1382. if (h->protocol == 2) {
  1383. sms_compose2(h, more);
  1384. } else {
  1385. sms_compose1(h, more);
  1386. }
  1387. } else { /* no message */
  1388. if (h->protocol == 2) {
  1389. h->omsg[0] = 0x17; /* SMS_REL */
  1390. h->omsg[1] = 0;
  1391. } else {
  1392. h->omsg[0] = 0x94; /* SMS_REL */
  1393. h->omsg[1] = 0;
  1394. h->sent_rel = 1;
  1395. }
  1396. }
  1397. sms_messagetx(h);
  1398. }
  1399. #define DIR_RX 1
  1400. #define DIR_TX 2
  1401. static void sms_debug (int dir, sms_t *h)
  1402. {
  1403. char txt[259 * 3 + 1];
  1404. char *p = txt; /* always long enough */
  1405. unsigned char *msg = (dir == DIR_RX) ? h->imsg : h->omsg;
  1406. int n = (dir == DIR_RX) ? h->ibytep : msg[1] + 2;
  1407. int q = 0;
  1408. while (q < n && q < 30) {
  1409. sprintf(p, " %02hhX", msg[q++]);
  1410. p += 3;
  1411. }
  1412. if (q < n) {
  1413. sprintf(p, "...");
  1414. }
  1415. ast_verb(3, "SMS %s%s\n", dir == DIR_RX ? "RX" : "TX", txt);
  1416. }
  1417. static void sms_messagerx(sms_t * h)
  1418. {
  1419. int cause;
  1420. sms_debug (DIR_RX, h);
  1421. if (h->protocol == 2) {
  1422. sms_messagerx2(h);
  1423. return;
  1424. }
  1425. /* parse incoming message for Protocol 1 */
  1426. switch (h->imsg[0]) {
  1427. case 0x91: /* SMS_DATA */
  1428. cause = sms_handleincoming (h);
  1429. if (!cause) {
  1430. sms_log(h, 'Y');
  1431. h->omsg[0] = 0x95; /* SMS_ACK */
  1432. h->omsg[1] = 0x02;
  1433. h->omsg[2] = 0x00; /* deliver report */
  1434. h->omsg[3] = 0x00; /* no parameters */
  1435. } else { /* NACK */
  1436. sms_log(h, 'N');
  1437. h->omsg[0] = 0x96; /* SMS_NACK */
  1438. h->omsg[1] = 3;
  1439. h->omsg[2] = 0; /* delivery report */
  1440. h->omsg[3] = cause; /* cause */
  1441. h->omsg[4] = 0; /* no parameters */
  1442. }
  1443. sms_messagetx(h);
  1444. break;
  1445. case 0x92: /* SMS_ERROR */
  1446. h->err = 1;
  1447. sms_messagetx(h); /* send whatever we sent again */
  1448. break;
  1449. case 0x93: /* SMS_EST */
  1450. sms_nextoutgoing (h);
  1451. break;
  1452. case 0x94: /* SMS_REL */
  1453. h->hangup = 1; /* hangup */
  1454. break;
  1455. case 0x95: /* SMS_ACK */
  1456. sms_log(h, 'Y');
  1457. sms_nextoutgoing (h);
  1458. break;
  1459. case 0x96: /* SMS_NACK */
  1460. h->err = 1;
  1461. sms_log(h, 'N');
  1462. sms_nextoutgoing (h);
  1463. break;
  1464. default: /* Unknown */
  1465. h->omsg[0] = 0x92; /* SMS_ERROR */
  1466. h->omsg[1] = 1;
  1467. h->omsg[2] = 3; /* unknown message type */
  1468. sms_messagetx(h);
  1469. break;
  1470. }
  1471. }
  1472. static void sms_messagetx(sms_t * h)
  1473. {
  1474. unsigned char c = 0, p;
  1475. int len = h->omsg[1] + 2; /* total message length excluding checksum */
  1476. for (p = 0; p < len; p++) { /* compute checksum */
  1477. c += h->omsg[p];
  1478. }
  1479. h->omsg[len] = 0 - c; /* actually, (256 - (c & 0fxx)) & 0xff) */
  1480. sms_debug(DIR_TX, h);
  1481. h->framenumber++; /* Proto 2 */
  1482. h->obytep = 0;
  1483. h->obitp = 0;
  1484. if (h->protocol == 2) { /* Proto 2: */
  1485. h->oseizure = 300; /* 300bits (or more ?) */
  1486. h->obyte = 0; /* Seizure starts with space (0) */
  1487. if (h->omsg[0] == 0x7F) {
  1488. h->opause = 8 * h->opause_0; /* initial message delay */
  1489. } else {
  1490. h->opause = 400;
  1491. }
  1492. } else { /* Proto 1: */
  1493. h->oseizure = 0; /* No seizure */
  1494. h->obyte = 1; /* send mark ('1') at the beginning */
  1495. /* Change the initial message delay. BT requires 300ms,
  1496. * but for others this might be way too much and the phone
  1497. * could time out. XXX make it configurable.
  1498. */
  1499. if (h->omsg[0] == 0x93) {
  1500. h->opause = 8 * h->opause_0; /* initial message delay */
  1501. } else {
  1502. h->opause = 200;
  1503. }
  1504. }
  1505. /* Note - setting osync triggers the generator */
  1506. h->osync = OSYNC_BITS; /* 80 sync bits */
  1507. h->obyten = len + 1; /* bytes to send (including checksum) */
  1508. }
  1509. /*!
  1510. * outgoing data are produced by this generator function, that reads from
  1511. * the descriptor whether it has data to send and which ones.
  1512. */
  1513. static int sms_generate(struct ast_channel *chan, void *data, int len, int samples)
  1514. {
  1515. struct ast_frame f = { 0 };
  1516. #define MAXSAMPLES (800)
  1517. output_t *buf;
  1518. sms_t *h = data;
  1519. int i, res;
  1520. if (samples > MAXSAMPLES) {
  1521. ast_log(LOG_WARNING, "Only doing %d samples (%d requested)\n",
  1522. MAXSAMPLES, samples);
  1523. samples = MAXSAMPLES;
  1524. }
  1525. len = samples * sizeof(*buf) + AST_FRIENDLY_OFFSET;
  1526. buf = ast_alloca(len);
  1527. f.frametype = AST_FRAME_VOICE;
  1528. f.subclass.format = __OUT_FMT;
  1529. f.datalen = samples * sizeof(*buf);
  1530. f.offset = AST_FRIENDLY_OFFSET;
  1531. f.mallocd = 0;
  1532. f.data.ptr = buf;
  1533. f.samples = samples;
  1534. f.src = "app_sms";
  1535. /* create a buffer containing the digital sms pattern */
  1536. for (i = 0; i < samples; i++) {
  1537. buf[i] = wave_out[0]; /* default is silence */
  1538. if (h->opause) {
  1539. h->opause--;
  1540. } else if (h->obyten || h->osync) { /* sending data */
  1541. buf[i] = wave_out[h->ophase];
  1542. h->ophase += (h->obyte & 1) ? 13 : 21; /* compute next phase */
  1543. if (h->ophase >= 80)
  1544. h->ophase -= 80;
  1545. if ((h->ophasep += 12) >= 80) { /* time to send the next bit */
  1546. h->ophasep -= 80;
  1547. if (h->oseizure > 0) { /* sending channel seizure (proto 2) */
  1548. h->oseizure--;
  1549. h->obyte ^= 1; /* toggle low bit */
  1550. } else if (h->osync) {
  1551. h->obyte = 1; /* send mark as sync bit */
  1552. h->osync--; /* sending sync bits */
  1553. if (h->osync == 0 && h->protocol == 2 && h->omsg[0] == DLL2_SMS_EST) {
  1554. h->obytep = h->obyten = 0; /* we are done */
  1555. }
  1556. } else {
  1557. h->obitp++;
  1558. if (h->obitp == 1) {
  1559. h->obyte = 0; /* start bit; */
  1560. } else if (h->obitp == 2) {
  1561. h->obyte = h->omsg[h->obytep];
  1562. } else if (h->obitp == 10) {
  1563. h->obyte = 1; /* stop bit */
  1564. h->obitp = 0;
  1565. h->obytep++;
  1566. if (h->obytep == h->obyten) {
  1567. h->obytep = h->obyten = 0; /* sent */
  1568. h->osync = 10; /* trailing marks */
  1569. }
  1570. } else {
  1571. h->obyte >>= 1;
  1572. }
  1573. }
  1574. }
  1575. }
  1576. }
  1577. res = ast_write(chan, &f);
  1578. ast_frfree(&f);
  1579. if (res < 0) {
  1580. ast_log(LOG_WARNING, "Failed to write frame to '%s': %s\n", ast_channel_name(chan), strerror(errno));
  1581. return -1;
  1582. }
  1583. return 0;
  1584. #undef MAXSAMPLES
  1585. }
  1586. /*!
  1587. * Just return the pointer to the descriptor that we received.
  1588. */
  1589. static void *sms_alloc(struct ast_channel *chan, void *sms_t_ptr)
  1590. {
  1591. return sms_t_ptr;
  1592. }
  1593. static void sms_release(struct ast_channel *chan, void *data)
  1594. {
  1595. return; /* nothing to do here. */
  1596. }
  1597. static struct ast_generator smsgen = {
  1598. .alloc = sms_alloc,
  1599. .release = sms_release,
  1600. .generate = sms_generate,
  1601. };
  1602. /*!
  1603. * Process an incoming frame, trying to detect the carrier and
  1604. * decode the message. The two frequencies are 1300 and 2100 Hz.
  1605. * The decoder detects the amplitude of the signal over the last
  1606. * few samples, filtering the absolute values with a lowpass filter.
  1607. * If the magnitude (h->imag) is large enough, multiply the signal
  1608. * by the two carriers, and compute the amplitudes m0 and m1.
  1609. * Record the current sample as '0' or '1' depending on which one is greater.
  1610. * The last 3 bits are stored in h->ibith, with the count of '1'
  1611. * bits in h->ibitt.
  1612. * XXX the rest is to be determined.
  1613. */
  1614. static void sms_process(sms_t * h, int samples, signed short *data)
  1615. {
  1616. int bit;
  1617. /*
  1618. * Ignore incoming audio while a packet is being transmitted,
  1619. * the protocol is half-duplex.
  1620. * Unfortunately this means that if the outbound and incoming
  1621. * transmission overlap (which is an error condition anyways),
  1622. * we may miss some data and this makes debugging harder.
  1623. */
  1624. if (h->obyten || h->osync) {
  1625. return;
  1626. }
  1627. for ( ; samples-- ; data++) {
  1628. unsigned long long m0, m1;
  1629. if (abs(*data) > h->imag) {
  1630. h->imag = abs(*data);
  1631. } else {
  1632. h->imag = h->imag * 7 / 8;
  1633. }
  1634. if (h->imag <= 500) { /* below [arbitrary] threahold: lost carrier */
  1635. if (h->idle++ == 80000) { /* nothing happening */
  1636. ast_log(LOG_NOTICE, "No data, hanging up\n");
  1637. h->hangup = 1;
  1638. h->err = 1;
  1639. }
  1640. if (h->ierr) { /* error */
  1641. ast_log(LOG_NOTICE, "Error %d, hanging up\n", h->ierr);
  1642. /* Protocol 1 */
  1643. h->err = 1;
  1644. h->omsg[0] = 0x92; /* error */
  1645. h->omsg[1] = 1;
  1646. h->omsg[2] = h->ierr;
  1647. sms_messagetx(h); /* send error */
  1648. }
  1649. h->ierr = h->ibitn = h->ibytep = h->ibytec = 0;
  1650. continue;
  1651. }
  1652. h->idle = 0;
  1653. /* multiply signal by the two carriers. */
  1654. h->ims0 = (h->ims0 * 6 + *data * wave[h->ips0]) / 7;
  1655. h->imc0 = (h->imc0 * 6 + *data * wave[h->ipc0]) / 7;
  1656. h->ims1 = (h->ims1 * 6 + *data * wave[h->ips1]) / 7;
  1657. h->imc1 = (h->imc1 * 6 + *data * wave[h->ipc1]) / 7;
  1658. /* compute the amplitudes */
  1659. m0 = h->ims0 * h->ims0 + h->imc0 * h->imc0;
  1660. m1 = h->ims1 * h->ims1 + h->imc1 * h->imc1;
  1661. /* advance the sin/cos pointers */
  1662. if ((h->ips0 += 21) >= 80) {
  1663. h->ips0 -= 80;
  1664. }
  1665. if ((h->ipc0 += 21) >= 80) {
  1666. h->ipc0 -= 80;
  1667. }
  1668. if ((h->ips1 += 13) >= 80) {
  1669. h->ips1 -= 80;
  1670. }
  1671. if ((h->ipc1 += 13) >= 80) {
  1672. h->ipc1 -= 80;
  1673. }
  1674. /* set new bit to 1 or 0 depending on which value is stronger */
  1675. h->ibith <<= 1;
  1676. if (m1 > m0) {
  1677. h->ibith |= 1;
  1678. }
  1679. if (h->ibith & 8) {
  1680. h->ibitt--;
  1681. }
  1682. if (h->ibith & 1) {
  1683. h->ibitt++;
  1684. }
  1685. bit = ((h->ibitt > 1) ? 1 : 0);
  1686. if (bit != h->ibitl) {
  1687. h->ibitc = 1;
  1688. } else {
  1689. h->ibitc++;
  1690. }
  1691. h->ibitl = bit;
  1692. if (!h->ibitn && h->ibitc == 4 && !bit) {
  1693. h->ibitn = 1;
  1694. h->iphasep = 0;
  1695. }
  1696. if (bit && h->ibitc == 200) { /* sync, restart message */
  1697. /* Protocol 2: empty connection ready (I am master) */
  1698. if (h->framenumber < 0 && h->ibytec >= 160 && !memcmp(h->imsg, "UUUUUUUUUUUUUUUUUUUU", 20)) {
  1699. h->framenumber = 1;
  1700. ast_verb(3, "SMS protocol 2 detected\n");
  1701. h->protocol = 2;
  1702. h->imsg[0] = 0xff; /* special message (fake) */
  1703. h->imsg[1] = h->imsg[2] = 0x00;
  1704. h->ierr = h->ibitn = h->ibytep = h->ibytec = 0;
  1705. sms_messagerx(h);
  1706. }
  1707. h->ierr = h->ibitn = h->ibytep = h->ibytec = 0;
  1708. }
  1709. if (h->ibitn) {
  1710. h->iphasep += 12;
  1711. if (h->iphasep >= 80) { /* next bit */
  1712. h->iphasep -= 80;
  1713. if (h->ibitn++ == 9) { /* end of byte */
  1714. if (!bit) { /* bad stop bit */
  1715. if (h->sent_rel) {
  1716. h->hangup = 1;
  1717. } else {
  1718. ast_log(LOG_NOTICE, "Bad stop bit\n");
  1719. h->ierr = 0xFF; /* unknown error */
  1720. }
  1721. } else {
  1722. if (h->ibytep < sizeof(h->imsg)) {
  1723. h->imsg[h->ibytep] = h->ibytev;
  1724. h->ibytec += h->ibytev;
  1725. h->ibytep++;
  1726. } else if (h->ibytep == sizeof(h->imsg)) {
  1727. ast_log(LOG_NOTICE, "msg too large\n");
  1728. h->ierr = 2; /* bad message length */
  1729. }
  1730. if (h->ibytep > 1 && h->ibytep == 3 + h->imsg[1] && !h->ierr) {
  1731. if (!h->ibytec) {
  1732. sms_messagerx(h);
  1733. } else {
  1734. ast_log(LOG_NOTICE, "bad checksum\n");
  1735. h->ierr = 1; /* bad checksum */
  1736. }
  1737. }
  1738. }
  1739. h->ibitn = 0;
  1740. }
  1741. h->ibytev = (h->ibytev >> 1) + (bit ? 0x80 : 0);
  1742. }
  1743. }
  1744. }
  1745. }
  1746. /*
  1747. * Standard argument parsing:
  1748. * - one enum for the flags we recognise,
  1749. * - one enum for argument indexes
  1750. * - AST_APP_OPTIONS() to drive the parsing routine
  1751. * - in the function, AST_DECLARE_APP_ARGS(...) for the arguments.
  1752. */
  1753. enum sms_flags {
  1754. OPTION_BE_SMSC = (1 << 0), /* act as sms center */
  1755. OPTION_ANSWER = (1 << 1), /* answer on incoming calls */
  1756. OPTION_TWO = (1 << 2), /* Use Protocol Two */
  1757. OPTION_PAUSE = (1 << 3), /* pause before sending data, in ms */
  1758. OPTION_SRR = (1 << 4), /* set srr */
  1759. OPTION_DCS = (1 << 5), /* set dcs */
  1760. OPTIONS_NO_LOG = (1 << 6), /* Don't log SMS content */
  1761. };
  1762. enum sms_opt_args {
  1763. OPTION_ARG_PAUSE = 0,
  1764. OPTION_ARG_ARRAY_SIZE
  1765. };
  1766. AST_APP_OPTIONS(sms_options, {
  1767. AST_APP_OPTION('s', OPTION_BE_SMSC),
  1768. AST_APP_OPTION('a', OPTION_ANSWER),
  1769. AST_APP_OPTION('t', OPTION_TWO),
  1770. AST_APP_OPTION('r', OPTION_SRR),
  1771. AST_APP_OPTION('o', OPTION_DCS),
  1772. AST_APP_OPTION('n', OPTIONS_NO_LOG),
  1773. AST_APP_OPTION_ARG('p', OPTION_PAUSE, OPTION_ARG_PAUSE),
  1774. } );
  1775. static int sms_exec(struct ast_channel *chan, const char *data)
  1776. {
  1777. int res = -1;
  1778. sms_t h = { 0 };
  1779. /* argument parsing support */
  1780. struct ast_flags flags = { 0 };
  1781. char *parse, *sms_opts[OPTION_ARG_ARRAY_SIZE] = { 0, };
  1782. char *p;
  1783. AST_DECLARE_APP_ARGS(sms_args,
  1784. AST_APP_ARG(queue);
  1785. AST_APP_ARG(options);
  1786. AST_APP_ARG(addr);
  1787. AST_APP_ARG(body);
  1788. );
  1789. if (!data) {
  1790. ast_log(LOG_ERROR, "Requires queue name at least\n");
  1791. return -1;
  1792. }
  1793. parse = ast_strdupa(data); /* create a local copy */
  1794. AST_STANDARD_APP_ARGS(sms_args, parse);
  1795. if (sms_args.argc > 1) {
  1796. ast_app_parse_options(sms_options, &flags, sms_opts, sms_args.options);
  1797. }
  1798. ast_verb(1, "sms argc %u queue <%s> opts <%s> addr <%s> body <%s>\n",
  1799. sms_args.argc, S_OR(sms_args.queue, ""),
  1800. S_OR(sms_args.options, ""),
  1801. S_OR(sms_args.addr, ""),
  1802. S_OR(sms_args.body, "") );
  1803. h.ipc0 = h.ipc1 = 20; /* phase for cosine */
  1804. h.dcs = 0xF1; /* default */
  1805. ast_copy_string(h.cli,
  1806. S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, ""),
  1807. sizeof(h.cli));
  1808. if (ast_strlen_zero(sms_args.queue)) {
  1809. ast_log(LOG_ERROR, "Requires queue name\n");
  1810. goto done;
  1811. }
  1812. if (strlen(sms_args.queue) >= sizeof(h.queue)) {
  1813. ast_log(LOG_ERROR, "Queue name too long\n");
  1814. goto done;
  1815. }
  1816. ast_copy_string(h.queue, sms_args.queue, sizeof(h.queue));
  1817. for (p = h.queue; *p; p++) {
  1818. if (!isalnum(*p)) {
  1819. *p = '-'; /* make very safe for filenames */
  1820. }
  1821. }
  1822. h.smsc = ast_test_flag(&flags, OPTION_BE_SMSC);
  1823. h.protocol = ast_test_flag(&flags, OPTION_TWO) ? 2 : 1;
  1824. h.nolog = ast_test_flag(&flags, OPTIONS_NO_LOG) ? 1 : 0;
  1825. if (!ast_strlen_zero(sms_opts[OPTION_ARG_PAUSE])) {
  1826. h.opause_0 = atoi(sms_opts[OPTION_ARG_PAUSE]);
  1827. }
  1828. if (h.opause_0 < 25 || h.opause_0 > 2000) {
  1829. h.opause_0 = 300; /* default 300ms */
  1830. }
  1831. ast_verb(1, "initial delay %dms\n", h.opause_0);
  1832. /* the following apply if there is an arg3/4 and apply to the created message file */
  1833. if (ast_test_flag(&flags, OPTION_SRR)) {
  1834. h.srr = 1;
  1835. }
  1836. if (ast_test_flag(&flags, OPTION_DCS)) {
  1837. h.dcs = 1;
  1838. }
  1839. #if 0
  1840. case '1':
  1841. case '2':
  1842. case '3':
  1843. case '4':
  1844. case '5':
  1845. case '6':
  1846. case '7': /* set the pid for saved local message */
  1847. h.pid = 0x40 + (*d & 0xF);
  1848. break;
  1849. }
  1850. #endif
  1851. if (sms_args.argc > 2) {
  1852. unsigned char *up;
  1853. /* submitting a message, not taking call. */
  1854. /* deprecated, use smsq instead */
  1855. h.scts = ast_tvnow();
  1856. if (ast_strlen_zero(sms_args.addr) || strlen(sms_args.addr) >= sizeof(h.oa)) {
  1857. ast_log(LOG_ERROR, "Address too long %s\n", sms_args.addr);
  1858. goto done;
  1859. }
  1860. if (h.smsc) {
  1861. ast_copy_string(h.oa, sms_args.addr, sizeof(h.oa));
  1862. } else {
  1863. ast_copy_string(h.da, sms_args.addr, sizeof(h.da));
  1864. ast_copy_string(h.oa, h.cli, sizeof(h.oa));
  1865. }
  1866. h.udl = 0;
  1867. if (ast_strlen_zero(sms_args.body)) {
  1868. ast_log(LOG_ERROR, "Missing body for %s\n", sms_args.addr);
  1869. goto done;
  1870. }
  1871. up = (unsigned char *)sms_args.body;
  1872. while (*up && h.udl < SMSLEN) {
  1873. h.ud[h.udl++] = utf8decode(&up);
  1874. }
  1875. if (is7bit(h.dcs) && packsms7(0, h.udhl, h.udh, h.udl, h.ud) < 0) {
  1876. ast_log(LOG_WARNING, "Invalid 7 bit GSM data\n");
  1877. goto done;
  1878. }
  1879. if (is8bit(h.dcs) && packsms8(0, h.udhl, h.udh, h.udl, h.ud) < 0) {
  1880. ast_log(LOG_WARNING, "Invalid 8 bit data\n");
  1881. goto done;
  1882. }
  1883. if (is16bit(h.dcs) && packsms16(0, h.udhl, h.udh, h.udl, h.ud) < 0) {
  1884. ast_log(LOG_WARNING, "Invalid 16 bit data\n");
  1885. goto done;
  1886. }
  1887. h.rx = 0; /* sent message */
  1888. h.mr = -1;
  1889. sms_writefile(&h);
  1890. res = h.err;
  1891. goto done;
  1892. }
  1893. if (ast_channel_state(chan) != AST_STATE_UP) { /* make sure channel is answered before any TX */
  1894. ast_answer(chan);
  1895. }
  1896. if (ast_test_flag(&flags, OPTION_ANSWER)) {
  1897. h.framenumber = 1; /* Proto 2 */
  1898. /* set up SMS_EST initial message */
  1899. if (h.protocol == 2) {
  1900. h.omsg[0] = DLL2_SMS_EST;
  1901. h.omsg[1] = 0;
  1902. } else {
  1903. h.omsg[0] = DLL1_SMS_EST | DLL1_SMS_COMPLETE;
  1904. h.omsg[1] = 0;
  1905. }
  1906. sms_messagetx(&h);
  1907. }
  1908. res = ast_set_write_format(chan, __OUT_FMT);
  1909. if (res >= 0) {
  1910. res = ast_set_read_format(chan, ast_format_slin);
  1911. }
  1912. if (res < 0) {
  1913. ast_log(LOG_ERROR, "Unable to set to linear mode, giving up\n");
  1914. goto done;
  1915. }
  1916. if ( (res = ast_activate_generator(chan, &smsgen, &h)) < 0) {
  1917. ast_log(LOG_ERROR, "Failed to activate generator on '%s'\n", ast_channel_name(chan));
  1918. goto done;
  1919. }
  1920. /* Do our thing here */
  1921. for (;;) {
  1922. struct ast_frame *f;
  1923. int i = ast_waitfor(chan, -1);
  1924. if (i < 0) {
  1925. ast_log(LOG_NOTICE, "waitfor failed\n");
  1926. break;
  1927. }
  1928. if (h.hangup) {
  1929. ast_log(LOG_NOTICE, "channel hangup\n");
  1930. break;
  1931. }
  1932. f = ast_read(chan);
  1933. if (!f) {
  1934. ast_log(LOG_NOTICE, "ast_read failed\n");
  1935. break;
  1936. }
  1937. if (f->frametype == AST_FRAME_VOICE) {
  1938. sms_process(&h, f->samples, f->data.ptr);
  1939. }
  1940. ast_frfree(f);
  1941. }
  1942. res = h.err; /* XXX */
  1943. /*
  1944. * The SMS generator data is on the stack. We _MUST_ make sure the generator
  1945. * is stopped before returning from this function.
  1946. */
  1947. ast_deactivate_generator(chan);
  1948. sms_log(&h, '?'); /* log incomplete message */
  1949. done:
  1950. return (res);
  1951. }
  1952. static int unload_module(void)
  1953. {
  1954. return ast_unregister_application(app);
  1955. }
  1956. static int load_module(void)
  1957. {
  1958. #ifdef OUTALAW
  1959. int p;
  1960. for (p = 0; p < 80; p++) {
  1961. wavea[p] = AST_LIN2A(wave[p]);
  1962. }
  1963. #endif
  1964. snprintf(log_file, sizeof(log_file), "%s/sms", ast_config_AST_LOG_DIR);
  1965. return ast_register_application_xml(app, sms_exec);
  1966. }
  1967. AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "SMS/PSTN handler");