app_sms.c 62 KB

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