app_sms.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083
  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. /*** DOCUMENTATION
  54. <application name="SMS" language="en_US">
  55. <synopsis>
  56. Communicates with SMS service centres and SMS capable analogue phones.
  57. </synopsis>
  58. <syntax>
  59. <parameter name="name" required="true">
  60. <para>The name of the queue used in <filename>/var/spool/asterisk/sms</filename></para>
  61. </parameter>
  62. <parameter name="options">
  63. <optionlist>
  64. <option name="a">
  65. <para>Answer, i.e. send initial FSK packet.</para>
  66. </option>
  67. <option name="s">
  68. <para>Act as service centre talking to a phone.</para>
  69. </option>
  70. <option name="t">
  71. <para>Use protocol 2 (default used is protocol 1).</para>
  72. </option>
  73. <option name="p">
  74. <para>Set the initial delay to N ms (default is <literal>300</literal>).
  75. addr and body are a deprecated format to send messages out.</para>
  76. </option>
  77. <option name="r">
  78. <para>Set the Status Report Request (SRR) bit.</para>
  79. </option>
  80. <option name="o">
  81. <para>The body should be coded as octets not 7-bit symbols.</para>
  82. </option>
  83. </optionlist>
  84. </parameter>
  85. <parameter name="addr" />
  86. <parameter name="body" />
  87. </syntax>
  88. <description>
  89. <para>SMS handles exchange of SMS data with a call to/from SMS capable phone or SMS PSTN service center.
  90. Can send and/or receive SMS messages. Works to ETSI ES 201 912; compatible with BT SMS PSTN service in
  91. UK and Telecom Italia in Italy.</para>
  92. <para>Typical usage is to use to handle calls from the SMS service centre CLI, or to set up a call using
  93. <literal>outgoing</literal> or manager interface to connect service centre to SMS().</para>
  94. <para>"Messages are processed as per text file message queues. smsq (a separate software) is a command to
  95. generate message queues and send messages.</para>
  96. <note><para>The protocol has tight delay bounds. Please use short frames and disable/keep short the
  97. jitter buffer on the ATA to make sure that respones (ACK etc.) are received in time.</para></note>
  98. </description>
  99. </application>
  100. ***/
  101. /* #define OUTALAW */ /* enable this to output Alaw rather than linear */
  102. /* ToDo */
  103. /* Add full VP support */
  104. /* Handle status report messages (generation and reception) */
  105. /* Time zones on time stamps */
  106. /* user ref field */
  107. static volatile unsigned char message_ref; /* arbitary message ref */
  108. static volatile unsigned int seq; /* arbitrary message sequence number for unqiue files */
  109. static char log_file[255];
  110. static char *app = "SMS";
  111. /*
  112. * 80 samples of a single period of the wave. At 8000 Hz, it means these
  113. * are the samples of a 100 Hz signal.
  114. * To pick the two carriers (1300Hz for '1' and 2100 Hz for '0') used by
  115. * the modulation, we should take one every 13 and 21 samples respectively.
  116. */
  117. static const signed short wave[] = {
  118. 0, 392, 782, 1167, 1545, 1913, 2270, 2612, 2939, 3247, 3536, 3802, 4045, 4263, 4455, 4619, 4755, 4862, 4938, 4985,
  119. 5000, 4985, 4938, 4862, 4755, 4619, 4455, 4263, 4045, 3802, 3536, 3247, 2939, 2612, 2270, 1913, 1545, 1167, 782, 392,
  120. 0, -392, -782, -1167,
  121. -1545, -1913, -2270, -2612, -2939, -3247, -3536, -3802, -4045, -4263, -4455, -4619, -4755, -4862, -4938, -4985, -5000,
  122. -4985, -4938, -4862,
  123. -4755, -4619, -4455, -4263, -4045, -3802, -3536, -3247, -2939, -2612, -2270, -1913, -1545, -1167, -782, -392
  124. };
  125. #ifdef OUTALAW
  126. static unsigned char wavea[80];
  127. typedef unsigned char output_t;
  128. static const output_t *wave_out = wavea; /* outgoing samples */
  129. #define __OUT_FMT AST_FORMAT_ALAW;
  130. #else
  131. typedef signed short output_t;
  132. static const output_t *wave_out = wave; /* outgoing samples */
  133. #define __OUT_FMT AST_FORMAT_SLINEAR
  134. #endif
  135. #define OSYNC_BITS 80 /* initial sync bits */
  136. /*!
  137. * The SMS spec ETSI ES 201 912 defines two protocols with different message types.
  138. * Also note that the high bit is used to indicate whether the message
  139. * is complete or not, but in two opposite ways:
  140. * for Protocol 1, 0x80 means that the message is complete;
  141. * for Protocol 2, 0x00 means that the message is complete;
  142. */
  143. enum message_types {
  144. DLL_SMS_MASK = 0x7f, /* mask for the valid bits */
  145. /* Protocol 1 values */
  146. DLL1_SMS_DATA = 0x11, /* data packet */
  147. DLL1_SMS_ERROR = 0x12,
  148. DLL1_SMS_EST = 0x13, /* start the connection */
  149. DLL1_SMS_REL = 0x14, /* end the connection */
  150. DLL1_SMS_ACK = 0x15,
  151. DLL1_SMS_NACK = 0x16,
  152. DLL1_SMS_COMPLETE = 0x80, /* packet is complete */
  153. DLL1_SMS_MORE = 0x00, /* more data to follow */
  154. /* Protocol 2 values */
  155. DLL2_SMS_EST = 0x7f, /* magic number. No message body */
  156. DLL2_SMS_INFO_MO = 0x10,
  157. DLL2_SMS_INFO_MT = 0x11,
  158. DLL2_SMS_INFO_STA = 0x12,
  159. DLL2_SMS_NACK = 0x13,
  160. DLL2_SMS_ACK0 = 0x14, /* ack even-numbered frame */
  161. DLL2_SMS_ACK1 = 0x15, /* ack odd-numbered frame */
  162. DLL2_SMS_ENQ = 0x16,
  163. DLL2_SMS_REL = 0x17, /* end the connection */
  164. DLL2_SMS_COMPLETE = 0x00, /* packet is complete */
  165. DLL2_SMS_MORE = 0x80, /* more data to follow */
  166. };
  167. /* SMS 7 bit character mapping to UCS-2 */
  168. static const unsigned short defaultalphabet[] = {
  169. 0x0040, 0x00A3, 0x0024, 0x00A5, 0x00E8, 0x00E9, 0x00F9, 0x00EC,
  170. 0x00F2, 0x00E7, 0x000A, 0x00D8, 0x00F8, 0x000D, 0x00C5, 0x00E5,
  171. 0x0394, 0x005F, 0x03A6, 0x0393, 0x039B, 0x03A9, 0x03A0, 0x03A8,
  172. 0x03A3, 0x0398, 0x039E, 0x00A0, 0x00C6, 0x00E6, 0x00DF, 0x00C9,
  173. ' ', '!', '"', '#', 164, '%', '&', 39, '(', ')', '*', '+', ',', '-', '.', '/',
  174. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?',
  175. 161, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
  176. 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 196, 214, 209, 220, 167,
  177. 191, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  178. 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 228, 246, 241, 252, 224,
  179. };
  180. static const unsigned short escapes[] = {
  181. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x000C, 0, 0, 0, 0, 0,
  182. 0, 0, 0, 0, 0x005E, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  183. 0, 0, 0, 0, 0, 0, 0, 0, 0x007B, 0x007D, 0, 0, 0, 0, 0, 0x005C,
  184. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x005B, 0x007E, 0x005D, 0,
  185. 0x007C, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  186. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  187. 0, 0, 0, 0, 0, 0x20AC, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  188. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  189. };
  190. #define SMSLEN 160 /*!< max SMS length */
  191. #define SMSLEN_8 140 /*!< max SMS length for 8-bit char */
  192. typedef struct sms_s {
  193. unsigned char hangup; /*!< we are done... */
  194. unsigned char err; /*!< set for any errors */
  195. unsigned char sent_rel:1; /*!< have sent REL message... */
  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 && 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. l += l % 2;
  670. unpacksms16(i, l, udh, udhl, ud, udl, udhi);
  671. }
  672. return l + 1;
  673. }
  674. /*! \brief unpack an address from i, return byte length, unpack to o */
  675. static unsigned char unpackaddress(char *o, unsigned char *i)
  676. {
  677. unsigned char l = i[0], p;
  678. if (i[1] == 0x91) {
  679. *o++ = '+';
  680. }
  681. for (p = 0; p < l; p++) {
  682. if (p & 1) {
  683. *o++ = (i[2 + p / 2] >> 4) + '0';
  684. } else {
  685. *o++ = (i[2 + p / 2] & 0xF) + '0';
  686. }
  687. }
  688. *o = 0;
  689. return (l + 5) / 2;
  690. }
  691. /*! \brief store an address at o, and return number of bytes used */
  692. static unsigned char packaddress(unsigned char *o, char *i)
  693. {
  694. unsigned char p = 2;
  695. o[0] = 0; /* number of bytes */
  696. if (*i == '+') { /* record as bit 0 in byte 1 */
  697. i++;
  698. o[1] = 0x91;
  699. } else {
  700. o[1] = 0x81;
  701. }
  702. for ( ; *i ; i++) {
  703. if (!isdigit(*i)) { /* ignore non-digits */
  704. continue;
  705. }
  706. if (o[0] & 1) {
  707. o[p++] |= ((*i & 0xF) << 4);
  708. } else {
  709. o[p] = (*i & 0xF);
  710. }
  711. o[0]++;
  712. }
  713. if (o[0] & 1) {
  714. o[p++] |= 0xF0; /* pad */
  715. }
  716. return p;
  717. }
  718. /*! \brief Log the output, and remove file */
  719. static void sms_log(sms_t * h, char status)
  720. {
  721. int o;
  722. if (*h->oa == '\0' && *h->da == '\0') {
  723. return;
  724. }
  725. o = open(log_file, O_CREAT | O_APPEND | O_WRONLY, AST_FILE_MODE);
  726. if (o >= 0) {
  727. char line[1000], mrs[3] = "", *p;
  728. char buf[30];
  729. unsigned char n;
  730. if (h->mr >= 0) {
  731. snprintf(mrs, sizeof(mrs), "%02X", (unsigned)h->mr);
  732. }
  733. snprintf(line, sizeof(line), "%s %c%c%c%s %s %s %s ",
  734. isodate(time(NULL), buf, sizeof(buf)),
  735. status, h->rx ? 'I' : 'O', h->smsc ? 'S' : 'M', mrs, h->queue,
  736. S_OR(h->oa, "-"), S_OR(h->da, "-") );
  737. p = line + strlen(line);
  738. for (n = 0; n < h->udl; n++) {
  739. if (h->ud[n] == '\\') {
  740. *p++ = '\\';
  741. *p++ = '\\';
  742. } else if (h->ud[n] == '\n') {
  743. *p++ = '\\';
  744. *p++ = 'n';
  745. } else if (h->ud[n] == '\r') {
  746. *p++ = '\\';
  747. *p++ = 'r';
  748. } else if (h->ud[n] < 32 || h->ud[n] == 127) {
  749. *p++ = 191;
  750. } else {
  751. *p++ = h->ud[n];
  752. }
  753. }
  754. *p++ = '\n';
  755. *p = 0;
  756. if (write(o, line, strlen(line)) < 0) {
  757. ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
  758. }
  759. close(o);
  760. }
  761. *h->oa = *h->da = h->udl = 0;
  762. }
  763. /*! \brief parse and delete a file */
  764. static void sms_readfile(sms_t * h, char *fn)
  765. {
  766. char line[1000];
  767. FILE *s;
  768. char dcsset = 0; /* if DSC set */
  769. ast_log(LOG_NOTICE, "Sending %s\n", fn);
  770. h->rx = h->udl = *h->oa = *h->da = h->pid = h->srr = h->udhi = h->rp = h->vp = h->udhl = 0;
  771. h->mr = -1;
  772. h->dcs = 0xF1; /* normal messages class 1 */
  773. h->scts = ast_tvnow();
  774. s = fopen(fn, "r");
  775. if (s) {
  776. if (unlink(fn)) { /* concurrent access, we lost */
  777. fclose(s);
  778. return;
  779. }
  780. while (fgets (line, sizeof(line), s)) { /* process line in file */
  781. char *p;
  782. void *pp = &p;
  783. for (p = line; *p && *p != '\n' && *p != '\r'; p++);
  784. *p = 0; /* strip eoln */
  785. p = line;
  786. if (!*p || *p == ';') {
  787. continue; /* blank line or comment, ignore */
  788. }
  789. while (isalnum(*p)) {
  790. *p = tolower (*p);
  791. p++;
  792. }
  793. while (isspace (*p)) {
  794. *p++ = 0;
  795. }
  796. if (*p == '=') {
  797. *p++ = 0;
  798. if (!strcmp(line, "ud")) { /* parse message (UTF-8) */
  799. unsigned char o = 0;
  800. memcpy(h->udtxt, p, SMSLEN); /* for protocol 2 */
  801. while (*p && o < SMSLEN) {
  802. h->ud[o++] = utf8decode(pp);
  803. }
  804. h->udl = o;
  805. if (*p) {
  806. ast_log(LOG_WARNING, "UD too long in %s\n", fn);
  807. }
  808. } else {
  809. while (isspace (*p)) {
  810. p++;
  811. }
  812. if (!strcmp(line, "oa") && strlen(p) < sizeof(h->oa)) {
  813. numcpy (h->oa, p);
  814. } else if (!strcmp(line, "da") && strlen(p) < sizeof(h->oa)) {
  815. numcpy (h->da, p);
  816. } else if (!strcmp(line, "pid")) {
  817. h->pid = atoi(p);
  818. } else if (!strcmp(line, "dcs")) {
  819. h->dcs = atoi(p);
  820. dcsset = 1;
  821. } else if (!strcmp(line, "mr")) {
  822. h->mr = atoi(p);
  823. } else if (!strcmp(line, "srr")) {
  824. h->srr = (atoi(p) ? 1 : 0);
  825. } else if (!strcmp(line, "vp")) {
  826. h->vp = atoi(p);
  827. } else if (!strcmp(line, "rp")) {
  828. h->rp = (atoi(p) ? 1 : 0);
  829. } else if (!strcmp(line, "scts")) { /* get date/time */
  830. int Y, m, d, H, M, S;
  831. /* XXX Why aren't we using ast_strptime here? */
  832. if (sscanf(p, "%4d-%2d-%2dT%2d:%2d:%2d", &Y, &m, &d, &H, &M, &S) == 6) {
  833. struct ast_tm t = { 0, };
  834. t.tm_year = Y - 1900;
  835. t.tm_mon = m - 1;
  836. t.tm_mday = d;
  837. t.tm_hour = H;
  838. t.tm_min = M;
  839. t.tm_sec = S;
  840. t.tm_isdst = -1;
  841. h->scts = ast_mktime(&t, NULL);
  842. if (h->scts.tv_sec == 0) {
  843. ast_log(LOG_WARNING, "Bad date/timein %s: %s", fn, p);
  844. }
  845. }
  846. } else {
  847. ast_log(LOG_WARNING, "Cannot parse in %s: %s=%si\n", fn, line, p);
  848. }
  849. }
  850. } else if (*p == '#') { /* raw hex format */
  851. *p++ = 0;
  852. if (*p == '#') {
  853. p++;
  854. if (!strcmp(line, "ud")) { /* user data */
  855. int o = 0;
  856. while (*p && o < SMSLEN) {
  857. if (isxdigit(*p) && isxdigit(p[1]) && isxdigit(p[2]) && isxdigit(p[3])) {
  858. h->ud[o++] =
  859. (((isalpha(*p) ? 9 : 0) + (*p & 0xF)) << 12) +
  860. (((isalpha(p[1]) ? 9 : 0) + (p[1] & 0xF)) << 8) +
  861. (((isalpha(p[2]) ? 9 : 0) + (p[2] & 0xF)) << 4) + ((isalpha(p[3]) ? 9 : 0) + (p[3] & 0xF));
  862. p += 4;
  863. } else
  864. break;
  865. }
  866. h->udl = o;
  867. if (*p)
  868. ast_log(LOG_WARNING, "UD too long / invalid UCS-2 hex in %s\n", fn);
  869. } else
  870. ast_log(LOG_WARNING, "Only ud can use ## format, %s\n", fn);
  871. } else if (!strcmp(line, "ud")) { /* user data */
  872. int o = 0;
  873. while (*p && o < SMSLEN) {
  874. if (isxdigit(*p) && isxdigit(p[1])) {
  875. h->ud[o++] = (((isalpha(*p) ? 9 : 0) + (*p & 0xF)) << 4) + ((isalpha(p[1]) ? 9 : 0) + (p[1] & 0xF));
  876. p += 2;
  877. } else {
  878. break;
  879. }
  880. }
  881. h->udl = o;
  882. if (*p) {
  883. ast_log(LOG_WARNING, "UD too long / invalid UCS-1 hex in %s\n", fn);
  884. }
  885. } else if (!strcmp(line, "udh")) { /* user data header */
  886. unsigned char o = 0;
  887. h->udhi = 1;
  888. while (*p && o < SMSLEN) {
  889. if (isxdigit(*p) && isxdigit(p[1])) {
  890. h->udh[o] = (((isalpha(*p) ? 9 : 0) + (*p & 0xF)) << 4) + ((isalpha(p[1]) ? 9 : 0) + (p[1] & 0xF));
  891. o++;
  892. p += 2;
  893. } else {
  894. break;
  895. }
  896. }
  897. h->udhl = o;
  898. if (*p) {
  899. ast_log(LOG_WARNING, "UDH too long / invalid hex in %s\n", fn);
  900. }
  901. } else {
  902. ast_log(LOG_WARNING, "Only ud and udh can use # format, %s\n", fn);
  903. }
  904. } else {
  905. ast_log(LOG_WARNING, "Cannot parse in %s: %s\n", fn, line);
  906. }
  907. }
  908. fclose(s);
  909. if (!dcsset && packsms7(0, h->udhl, h->udh, h->udl, h->ud) < 0) {
  910. if (packsms8(0, h->udhl, h->udh, h->udl, h->ud) < 0) {
  911. if (packsms16(0, h->udhl, h->udh, h->udl, h->ud) < 0) {
  912. ast_log(LOG_WARNING, "Invalid UTF-8 message even for UCS-2 (%s)\n", fn);
  913. } else {
  914. h->dcs = 0x08; /* default to 16 bit */
  915. ast_log(LOG_WARNING, "Sending in 16 bit format(%s)\n", fn);
  916. }
  917. } else {
  918. h->dcs = 0xF5; /* default to 8 bit */
  919. ast_log(LOG_WARNING, "Sending in 8 bit format(%s)\n", fn);
  920. }
  921. }
  922. if (is7bit(h->dcs) && packsms7(0, h->udhl, h->udh, h->udl, h->ud) < 0) {
  923. ast_log(LOG_WARNING, "Invalid 7 bit GSM data %s\n", fn);
  924. }
  925. if (is8bit(h->dcs) && packsms8(0, h->udhl, h->udh, h->udl, h->ud) < 0) {
  926. ast_log(LOG_WARNING, "Invalid 8 bit data %s\n", fn);
  927. }
  928. if (is16bit(h->dcs) && packsms16(0, h->udhl, h->udh, h->udl, h->ud) < 0) {
  929. ast_log(LOG_WARNING, "Invalid 16 bit data %s\n", fn);
  930. }
  931. }
  932. }
  933. /*! \brief white a received text message to a file */
  934. static void sms_writefile(sms_t * h)
  935. {
  936. char fn[200] = "", fn2[200] = "";
  937. char buf[30];
  938. FILE *o;
  939. if (ast_tvzero(h->scts)) {
  940. h->scts = ast_tvnow();
  941. }
  942. snprintf(fn, sizeof(fn), "%s/sms/%s", ast_config_AST_SPOOL_DIR, h->smsc ? h->rx ? "morx" : "mttx" : h->rx ? "mtrx" : "motx");
  943. ast_mkdir(fn, 0777); /* ensure it exists */
  944. ast_copy_string(fn2, fn, sizeof(fn2));
  945. snprintf(fn2 + strlen(fn2), sizeof(fn2) - strlen(fn2), "/%s.%s-%u", h->queue, isodate(h->scts.tv_sec, buf, sizeof(buf)), seq++);
  946. snprintf(fn + strlen(fn), sizeof(fn) - strlen(fn), "/.%s", fn2 + strlen(fn) + 1);
  947. if ((o = fopen(fn, "w")) == NULL) {
  948. return;
  949. }
  950. if (*h->oa) {
  951. fprintf(o, "oa=%s\n", h->oa);
  952. }
  953. if (*h->da) {
  954. fprintf(o, "da=%s\n", h->da);
  955. }
  956. if (h->udhi) {
  957. unsigned int p;
  958. fprintf(o, "udh#");
  959. for (p = 0; p < h->udhl; p++) {
  960. fprintf(o, "%02X", (unsigned)h->udh[p]);
  961. }
  962. fprintf(o, "\n");
  963. }
  964. if (h->udl) {
  965. unsigned int p;
  966. for (p = 0; p < h->udl && h->ud[p] >= ' '; p++);
  967. if (p < h->udl) {
  968. fputc(';', o); /* cannot use ud=, but include as a comment for human readable */
  969. }
  970. fprintf(o, "ud=");
  971. for (p = 0; p < h->udl; p++) {
  972. unsigned short v = h->ud[p];
  973. if (v < 32) {
  974. fputc(191, o);
  975. } else if (v < 0x80) {
  976. fputc(v, o);
  977. } else if (v < 0x800) {
  978. fputc(0xC0 + (v >> 6), o);
  979. fputc(0x80 + (v & 0x3F), o);
  980. } else {
  981. fputc(0xE0 + (v >> 12), o);
  982. fputc(0x80 + ((v >> 6) & 0x3F), o);
  983. fputc(0x80 + (v & 0x3F), o);
  984. }
  985. }
  986. fprintf(o, "\n");
  987. for (p = 0; p < h->udl && h->ud[p] >= ' '; p++);
  988. if (p < h->udl) {
  989. for (p = 0; p < h->udl && h->ud[p] < 0x100; p++);
  990. if (p == h->udl) { /* can write in ucs-1 hex */
  991. fprintf(o, "ud#");
  992. for (p = 0; p < h->udl; p++) {
  993. fprintf(o, "%02X", (unsigned)h->ud[p]);
  994. }
  995. fprintf(o, "\n");
  996. } else { /* write in UCS-2 */
  997. fprintf(o, "ud##");
  998. for (p = 0; p < h->udl; p++) {
  999. fprintf(o, "%04X", (unsigned)h->ud[p]);
  1000. }
  1001. fprintf(o, "\n");
  1002. }
  1003. }
  1004. }
  1005. if (h->scts.tv_sec) {
  1006. char datebuf[30];
  1007. fprintf(o, "scts=%s\n", isodate(h->scts.tv_sec, datebuf, sizeof(datebuf)));
  1008. }
  1009. if (h->pid) {
  1010. fprintf(o, "pid=%d\n", h->pid);
  1011. }
  1012. if (h->dcs != 0xF1) {
  1013. fprintf(o, "dcs=%d\n", h->dcs);
  1014. }
  1015. if (h->vp) {
  1016. fprintf(o, "vp=%u\n", h->vp);
  1017. }
  1018. if (h->srr) {
  1019. fprintf(o, "srr=1\n");
  1020. }
  1021. if (h->mr >= 0) {
  1022. fprintf(o, "mr=%d\n", h->mr);
  1023. }
  1024. if (h->rp) {
  1025. fprintf(o, "rp=1\n");
  1026. }
  1027. fclose(o);
  1028. if (rename(fn, fn2)) {
  1029. unlink(fn);
  1030. } else {
  1031. ast_log(LOG_NOTICE, "Received to %s\n", fn2);
  1032. }
  1033. }
  1034. /*! \brief read dir skipping dot files... */
  1035. static struct dirent *readdirqueue(DIR *d, char *queue)
  1036. {
  1037. struct dirent *f;
  1038. do {
  1039. f = readdir(d);
  1040. } while (f && (*f->d_name == '.' || strncmp(f->d_name, queue, strlen(queue)) || f->d_name[strlen(queue)] != '.'));
  1041. return f;
  1042. }
  1043. /*! \brief handle the incoming message */
  1044. static unsigned char sms_handleincoming (sms_t * h)
  1045. {
  1046. unsigned char p = 3;
  1047. if (h->smsc) { /* SMSC */
  1048. if ((h->imsg[2] & 3) == 1) { /* SMS-SUBMIT */
  1049. h->udhl = h->udl = 0;
  1050. h->vp = 0;
  1051. h->srr = ((h->imsg[2] & 0x20) ? 1 : 0);
  1052. h->udhi = ((h->imsg[2] & 0x40) ? 1 : 0);
  1053. h->rp = ((h->imsg[2] & 0x80) ? 1 : 0);
  1054. ast_copy_string(h->oa, h->cli, sizeof(h->oa));
  1055. h->scts = ast_tvnow();
  1056. h->mr = h->imsg[p++];
  1057. p += unpackaddress(h->da, h->imsg + p);
  1058. h->pid = h->imsg[p++];
  1059. h->dcs = h->imsg[p++];
  1060. if ((h->imsg[2] & 0x18) == 0x10) { /* relative VP */
  1061. if (h->imsg[p] < 144) {
  1062. h->vp = (h->imsg[p] + 1) * 5;
  1063. } else if (h->imsg[p] < 168) {
  1064. h->vp = 720 + (h->imsg[p] - 143) * 30;
  1065. } else if (h->imsg[p] < 197) {
  1066. h->vp = (h->imsg[p] - 166) * 1440;
  1067. } else {
  1068. h->vp = (h->imsg[p] - 192) * 10080;
  1069. }
  1070. p++;
  1071. } else if (h->imsg[2] & 0x18) {
  1072. p += 7; /* ignore enhanced / absolute VP */
  1073. }
  1074. p += unpacksms(h->dcs, h->imsg + p, h->udh, &h->udhl, h->ud, &h->udl, h->udhi);
  1075. h->rx = 1; /* received message */
  1076. sms_writefile(h); /* write the file */
  1077. if (p != h->imsg[1] + 2) {
  1078. ast_log(LOG_WARNING, "Mismatch receive unpacking %d/%d\n", p, h->imsg[1] + 2);
  1079. return 0xFF; /* duh! */
  1080. }
  1081. } else {
  1082. ast_log(LOG_WARNING, "Unknown message type %02X\n", (unsigned)h->imsg[2]);
  1083. return 0xFF;
  1084. }
  1085. } else { /* client */
  1086. if (!(h->imsg[2] & 3)) { /* SMS-DELIVER */
  1087. *h->da = h->srr = h->rp = h->vp = h->udhi = h->udhl = h->udl = 0;
  1088. h->srr = ((h->imsg[2] & 0x20) ? 1 : 0);
  1089. h->udhi = ((h->imsg[2] & 0x40) ? 1 : 0);
  1090. h->rp = ((h->imsg[2] & 0x80) ? 1 : 0);
  1091. h->mr = -1;
  1092. p += unpackaddress(h->oa, h->imsg + p);
  1093. h->pid = h->imsg[p++];
  1094. h->dcs = h->imsg[p++];
  1095. h->scts = unpackdate(h->imsg + p);
  1096. p += 7;
  1097. p += unpacksms(h->dcs, h->imsg + p, h->udh, &h->udhl, h->ud, &h->udl, h->udhi);
  1098. h->rx = 1; /* received message */
  1099. sms_writefile(h); /* write the file */
  1100. if (p != h->imsg[1] + 2) {
  1101. ast_log(LOG_WARNING, "Mismatch receive unpacking %d/%d\n", p, h->imsg[1] + 2);
  1102. return 0xFF; /* duh! */
  1103. }
  1104. } else {
  1105. ast_log(LOG_WARNING, "Unknown message type %02X\n", (unsigned)h->imsg[2]);
  1106. return 0xFF;
  1107. }
  1108. }
  1109. return 0; /* no error */
  1110. }
  1111. #ifdef SOLARIS
  1112. #define NAME_MAX 1024
  1113. #endif
  1114. /*!
  1115. * Add data to a protocol 2 message.
  1116. * Use the length field (h->omsg[1]) as a pointer to the next free position.
  1117. */
  1118. static void adddata_proto2(sms_t *h, unsigned char msg, char *data, int size)
  1119. {
  1120. int x = h->omsg[1] + 2; /* Get current position */
  1121. if (x == 2) {
  1122. x += 2; /* First: skip Payload length (set later) */
  1123. }
  1124. h->omsg[x++] = msg; /* Message code */
  1125. h->omsg[x++] = (unsigned char)size; /* Data size Low */
  1126. h->omsg[x++] = 0; /* Data size Hi */
  1127. for (; size > 0 ; size--) {
  1128. h->omsg[x++] = *data++;
  1129. }
  1130. h->omsg[1] = x - 2; /* Frame size */
  1131. h->omsg[2] = x - 4; /* Payload length (Lo) */
  1132. h->omsg[3] = 0; /* Payload length (Hi) */
  1133. }
  1134. static void putdummydata_proto2(sms_t *h)
  1135. {
  1136. adddata_proto2(h, 0x10, "\0", 1); /* Media Identifier > SMS */
  1137. adddata_proto2(h, 0x11, "\0\0\0\0\0\0", 6); /* Firmware version */
  1138. adddata_proto2(h, 0x12, "\2\0\4", 3); /* SMS provider ID */
  1139. adddata_proto2(h, 0x13, h->udtxt, h->udl); /* Body */
  1140. }
  1141. static void sms_compose2(sms_t *h, int more)
  1142. {
  1143. struct ast_tm tm;
  1144. struct timeval now = h->scts;
  1145. char stm[9];
  1146. h->omsg[0] = 0x00; /* set later... */
  1147. h->omsg[1] = 0;
  1148. putdummydata_proto2(h);
  1149. if (h->smsc) { /* deliver */
  1150. h->omsg[0] = 0x11; /* SMS_DELIVERY */
  1151. /* Required: 10 11 12 13 14 15 17 (seems they must be ordered!) */
  1152. ast_localtime(&now, &tm, NULL);
  1153. sprintf(stm, "%02d%02d%02d%02d", tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min); /* Date mmddHHMM */
  1154. adddata_proto2(h, 0x14, stm, 8); /* Date */
  1155. if (*h->oa == 0) {
  1156. strcpy(h->oa, "00000000");
  1157. }
  1158. adddata_proto2(h, 0x15, h->oa, strlen(h->oa)); /* Originator */
  1159. adddata_proto2(h, 0x17, "\1", 1); /* Calling Terminal ID */
  1160. } else { /* submit */
  1161. h->omsg[0] = 0x10; /* SMS_SUBMIT */
  1162. /* Required: 10 11 12 13 17 18 1B 1C (seems they must be ordered!) */
  1163. adddata_proto2(h, 0x17, "\1", 1); /* Calling Terminal ID */
  1164. if (*h->da == 0) {
  1165. strcpy(h->da, "00000000");
  1166. }
  1167. adddata_proto2(h, 0x18, h->da, strlen(h->da)); /* Originator */
  1168. adddata_proto2(h, 0x1B, "\1", 1); /* Called Terminal ID */
  1169. adddata_proto2(h, 0x1C, "\0\0\0", 3); /* Notification */
  1170. }
  1171. }
  1172. static void putdummydata_proto2(sms_t *h);
  1173. #define MAX_DEBUG_LEN 300
  1174. static char *sms_hexdump(unsigned char buf[], int size, char *s /* destination */)
  1175. {
  1176. char *p;
  1177. int f;
  1178. for (p = s, f = 0; f < size && f < MAX_DEBUG_LEN; f++, p += 3) {
  1179. sprintf(p, "%02X ", (unsigned)buf[f]);
  1180. }
  1181. return(s);
  1182. }
  1183. /*! \brief sms_handleincoming_proto2: handle the incoming message */
  1184. static int sms_handleincoming_proto2(sms_t *h)
  1185. {
  1186. int f, i, sz = 0;
  1187. int msg, msgsz;
  1188. struct ast_tm tm;
  1189. struct timeval now = { 0, 0 };
  1190. char debug_buf[MAX_DEBUG_LEN * 3 + 1];
  1191. sz = h->imsg[1] + 2;
  1192. /* ast_verb(3, "SMS-P2 Frame: %s\n", sms_hexdump(h->imsg, sz, debug_buf)); */
  1193. /* Parse message body (called payload) */
  1194. now = h->scts = ast_tvnow();
  1195. for (f = 4; f < sz; ) {
  1196. msg = h->imsg[f++];
  1197. msgsz = h->imsg[f++];
  1198. msgsz += (h->imsg[f++] * 256);
  1199. switch (msg) {
  1200. case 0x13: /* Body */
  1201. ast_verb(3, "SMS-P2 Body#%02X=[%.*s]\n", (unsigned)msg, msgsz, &h->imsg[f]);
  1202. if (msgsz >= sizeof(h->ud)) {
  1203. msgsz = sizeof(h->ud) - 1;
  1204. }
  1205. for (i = 0; i < msgsz; i++) {
  1206. h->ud[i] = h->imsg[f + i];
  1207. }
  1208. h->udl = msgsz;
  1209. break;
  1210. case 0x14: /* Date SCTS */
  1211. now = h->scts = ast_tvnow();
  1212. ast_localtime(&now, &tm, NULL);
  1213. tm.tm_mon = ( (h->imsg[f] * 10) + h->imsg[f + 1] ) - 1;
  1214. tm.tm_mday = ( (h->imsg[f + 2] * 10) + h->imsg[f + 3] );
  1215. tm.tm_hour = ( (h->imsg[f + 4] * 10) + h->imsg[f + 5] );
  1216. tm.tm_min = ( (h->imsg[f + 6] * 10) + h->imsg[f + 7] );
  1217. tm.tm_sec = 0;
  1218. h->scts = ast_mktime(&tm, NULL);
  1219. 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);
  1220. break;
  1221. case 0x15: /* Calling line (from SMSC) */
  1222. if (msgsz >= 20) {
  1223. msgsz = 20 - 1;
  1224. }
  1225. ast_verb(3, "SMS-P2 Origin#%02X=[%.*s]\n", (unsigned)msg, msgsz, &h->imsg[f]);
  1226. ast_copy_string(h->oa, (char *)(&h->imsg[f]), msgsz + 1);
  1227. break;
  1228. case 0x18: /* Destination(from TE/phone) */
  1229. if (msgsz >= 20) {
  1230. msgsz = 20 - 1;
  1231. }
  1232. ast_verb(3, "SMS-P2 Destination#%02X=[%.*s]\n", (unsigned)msg, msgsz, &h->imsg[f]);
  1233. ast_copy_string(h->da, (char *)(&h->imsg[f]), msgsz + 1);
  1234. break;
  1235. case 0x1C: /* Notify */
  1236. ast_verb(3, "SMS-P2 Notify#%02X=%s\n", (unsigned)msg, sms_hexdump(&h->imsg[f], 3, debug_buf));
  1237. break;
  1238. default:
  1239. ast_verb(3, "SMS-P2 Par#%02X [%d]: %s\n", (unsigned)msg, msgsz, sms_hexdump(&h->imsg[f], msgsz, debug_buf));
  1240. break;
  1241. }
  1242. f+=msgsz; /* Skip to next */
  1243. }
  1244. h->rx = 1; /* received message */
  1245. sms_writefile(h); /* write the file */
  1246. return 0; /* no error */
  1247. }
  1248. #if 0
  1249. static void smssend(sms_t *h, char *c)
  1250. {
  1251. int f, x;
  1252. for (f = 0; f < strlen(c); f++) {
  1253. sscanf(&c[f*3], "%x", &x);
  1254. h->omsg[f] = x;
  1255. }
  1256. sms_messagetx(h);
  1257. }
  1258. #endif
  1259. static void sms_nextoutgoing (sms_t *h);
  1260. static void sms_messagerx2(sms_t * h)
  1261. {
  1262. int p = h->imsg[0] & DLL_SMS_MASK ; /* mask the high bit */
  1263. int cause;
  1264. #define DLL2_ACK(h) ((h->framenumber & 1) ? DLL2_SMS_ACK1: DLL2_SMS_ACK1)
  1265. switch (p) {
  1266. case DLL2_SMS_EST: /* Protocol 2: Connection ready (fake): send message */
  1267. sms_nextoutgoing (h);
  1268. /* 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 "); */
  1269. break;
  1270. case DLL2_SMS_INFO_MO: /* transport SMS_SUBMIT */
  1271. case DLL2_SMS_INFO_MT: /* transport SMS_DELIVERY */
  1272. cause = sms_handleincoming_proto2(h);
  1273. if (!cause) { /* ACK */
  1274. sms_log(h, 'Y');
  1275. }
  1276. h->omsg[0] = DLL2_ACK(h);
  1277. h->omsg[1] = 0x06; /* msg len */
  1278. h->omsg[2] = 0x04; /* payload len */
  1279. h->omsg[3] = 0x00; /* payload len */
  1280. h->omsg[4] = 0x1f; /* Response type */
  1281. h->omsg[5] = 0x01; /* parameter len */
  1282. h->omsg[6] = 0x00; /* parameter len */
  1283. h->omsg[7] = cause; /* CONFIRM or error */
  1284. sms_messagetx(h);
  1285. break;
  1286. case DLL2_SMS_NACK: /* Protocol 2: SMS_NAK */
  1287. h->omsg[0] = DLL2_SMS_REL; /* SMS_REL */
  1288. h->omsg[1] = 0x00; /* msg len */
  1289. sms_messagetx(h);
  1290. break;
  1291. case DLL2_SMS_ACK0:
  1292. case DLL2_SMS_ACK1:
  1293. /* SMS_ACK also transport SMS_SUBMIT or SMS_DELIVERY */
  1294. if ( (h->omsg[0] & DLL_SMS_MASK) == DLL2_SMS_REL) {
  1295. /* a response to our Release, just hangup */
  1296. h->hangup = 1; /* hangup */
  1297. } else {
  1298. /* XXX depending on what we are.. */
  1299. ast_log(LOG_NOTICE, "SMS_SUBMIT or SMS_DELIVERY\n");
  1300. sms_nextoutgoing (h);
  1301. }
  1302. break;
  1303. case DLL2_SMS_REL: /* Protocol 2: SMS_REL (hangup req) */
  1304. h->omsg[0] = DLL2_ACK(h);
  1305. h->omsg[1] = 0;
  1306. sms_messagetx(h);
  1307. break;
  1308. }
  1309. }
  1310. /*! \brief compose a message for protocol 1 */
  1311. static void sms_compose1(sms_t *h, int more)
  1312. {
  1313. unsigned int p = 2; /* next byte to write. Skip type and len */
  1314. h->omsg[0] = 0x91; /* SMS_DATA */
  1315. if (h->smsc) { /* deliver */
  1316. h->omsg[p++] = (more ? 4 : 0) + ((h->udhl > 0) ? 0x40 : 0);
  1317. p += packaddress(h->omsg + p, h->oa);
  1318. h->omsg[p++] = h->pid;
  1319. h->omsg[p++] = h->dcs;
  1320. packdate(h->omsg + p, h->scts.tv_sec);
  1321. p += 7;
  1322. p += packsms(h->dcs, h->omsg + p, h->udhl, h->udh, h->udl, h->ud);
  1323. } else { /* submit */
  1324. h->omsg[p++] =
  1325. 0x01 + (more ? 4 : 0) + (h->srr ? 0x20 : 0) + (h->rp ? 0x80 : 0) + (h->vp ? 0x10 : 0) + (h->udhi ? 0x40 : 0);
  1326. if (h->mr < 0) {
  1327. h->mr = message_ref++;
  1328. }
  1329. h->omsg[p++] = h->mr;
  1330. p += packaddress(h->omsg + p, h->da);
  1331. h->omsg[p++] = h->pid;
  1332. h->omsg[p++] = h->dcs;
  1333. if (h->vp) { /* relative VP */
  1334. if (h->vp < 720) {
  1335. h->omsg[p++] = (h->vp + 4) / 5 - 1;
  1336. } else if (h->vp < 1440) {
  1337. h->omsg[p++] = (h->vp - 720 + 29) / 30 + 143;
  1338. } else if (h->vp < 43200) {
  1339. h->omsg[p++] = (h->vp + 1439) / 1440 + 166;
  1340. } else if (h->vp < 635040) {
  1341. h->omsg[p++] = (h->vp + 10079) / 10080 + 192;
  1342. } else {
  1343. h->omsg[p++] = 255; /* max */
  1344. }
  1345. }
  1346. p += packsms(h->dcs, h->omsg + p, h->udhl, h->udh, h->udl, h->ud);
  1347. }
  1348. h->omsg[1] = p - 2;
  1349. }
  1350. /*! \brief find and fill in next message, or send a REL if none waiting */
  1351. static void sms_nextoutgoing (sms_t * h)
  1352. {
  1353. char fn[100 + NAME_MAX] = "";
  1354. DIR *d;
  1355. char more = 0;
  1356. *h->da = *h->oa = '\0'; /* clear destinations */
  1357. h->rx = 0; /* outgoing message */
  1358. snprintf(fn, sizeof(fn), "%s/sms/%s", ast_config_AST_SPOOL_DIR, h->smsc ? "mttx" : "motx");
  1359. ast_mkdir(fn, 0777); /* ensure it exists */
  1360. d = opendir(fn);
  1361. if (d) {
  1362. struct dirent *f = readdirqueue(d, h->queue);
  1363. if (f) {
  1364. snprintf(fn + strlen(fn), sizeof(fn) - strlen(fn), "/%s", f->d_name);
  1365. sms_readfile(h, fn);
  1366. if (readdirqueue(d, h->queue)) {
  1367. more = 1; /* more to send */
  1368. }
  1369. }
  1370. closedir(d);
  1371. }
  1372. if (*h->da || *h->oa) { /* message to send */
  1373. if (h->protocol == 2) {
  1374. sms_compose2(h, more);
  1375. } else {
  1376. sms_compose1(h, more);
  1377. }
  1378. } else { /* no message */
  1379. if (h->protocol == 2) {
  1380. h->omsg[0] = 0x17; /* SMS_REL */
  1381. h->omsg[1] = 0;
  1382. } else {
  1383. h->omsg[0] = 0x94; /* SMS_REL */
  1384. h->omsg[1] = 0;
  1385. h->sent_rel = 1;
  1386. }
  1387. }
  1388. sms_messagetx(h);
  1389. }
  1390. #define DIR_RX 1
  1391. #define DIR_TX 2
  1392. static void sms_debug (int dir, sms_t *h)
  1393. {
  1394. char txt[259 * 3 + 1];
  1395. char *p = txt; /* always long enough */
  1396. unsigned char *msg = (dir == DIR_RX) ? h->imsg : h->omsg;
  1397. int n = (dir == DIR_RX) ? h->ibytep : msg[1] + 2;
  1398. int q = 0;
  1399. while (q < n && q < 30) {
  1400. sprintf(p, " %02X", (unsigned)msg[q++]);
  1401. p += 3;
  1402. }
  1403. if (q < n) {
  1404. sprintf(p, "...");
  1405. }
  1406. ast_verb(3, "SMS %s%s\n", dir == DIR_RX ? "RX" : "TX", txt);
  1407. }
  1408. static void sms_messagerx(sms_t * h)
  1409. {
  1410. int cause;
  1411. sms_debug (DIR_RX, h);
  1412. if (h->protocol == 2) {
  1413. sms_messagerx2(h);
  1414. return;
  1415. }
  1416. /* parse incoming message for Protocol 1 */
  1417. switch (h->imsg[0]) {
  1418. case 0x91: /* SMS_DATA */
  1419. cause = sms_handleincoming (h);
  1420. if (!cause) {
  1421. sms_log(h, 'Y');
  1422. h->omsg[0] = 0x95; /* SMS_ACK */
  1423. h->omsg[1] = 0x02;
  1424. h->omsg[2] = 0x00; /* deliver report */
  1425. h->omsg[3] = 0x00; /* no parameters */
  1426. } else { /* NACK */
  1427. sms_log(h, 'N');
  1428. h->omsg[0] = 0x96; /* SMS_NACK */
  1429. h->omsg[1] = 3;
  1430. h->omsg[2] = 0; /* delivery report */
  1431. h->omsg[3] = cause; /* cause */
  1432. h->omsg[4] = 0; /* no parameters */
  1433. }
  1434. sms_messagetx(h);
  1435. break;
  1436. case 0x92: /* SMS_ERROR */
  1437. h->err = 1;
  1438. sms_messagetx(h); /* send whatever we sent again */
  1439. break;
  1440. case 0x93: /* SMS_EST */
  1441. sms_nextoutgoing (h);
  1442. break;
  1443. case 0x94: /* SMS_REL */
  1444. h->hangup = 1; /* hangup */
  1445. break;
  1446. case 0x95: /* SMS_ACK */
  1447. sms_log(h, 'Y');
  1448. sms_nextoutgoing (h);
  1449. break;
  1450. case 0x96: /* SMS_NACK */
  1451. h->err = 1;
  1452. sms_log(h, 'N');
  1453. sms_nextoutgoing (h);
  1454. break;
  1455. default: /* Unknown */
  1456. h->omsg[0] = 0x92; /* SMS_ERROR */
  1457. h->omsg[1] = 1;
  1458. h->omsg[2] = 3; /* unknown message type */
  1459. sms_messagetx(h);
  1460. break;
  1461. }
  1462. }
  1463. static void sms_messagetx(sms_t * h)
  1464. {
  1465. unsigned char c = 0, p;
  1466. int len = h->omsg[1] + 2; /* total message length excluding checksum */
  1467. for (p = 0; p < len; p++) { /* compute checksum */
  1468. c += h->omsg[p];
  1469. }
  1470. h->omsg[len] = 0 - c; /* actually, (256 - (c & 0fxx)) & 0xff) */
  1471. sms_debug(DIR_TX, h);
  1472. h->framenumber++; /* Proto 2 */
  1473. h->obytep = 0;
  1474. h->obitp = 0;
  1475. if (h->protocol == 2) { /* Proto 2: */
  1476. h->oseizure = 300; /* 300bits (or more ?) */
  1477. h->obyte = 0; /* Seizure starts with space (0) */
  1478. if (h->omsg[0] == 0x7F) {
  1479. h->opause = 8 * h->opause_0; /* initial message delay */
  1480. } else {
  1481. h->opause = 400;
  1482. }
  1483. } else { /* Proto 1: */
  1484. h->oseizure = 0; /* No seizure */
  1485. h->obyte = 1; /* send mark ('1') at the beginning */
  1486. /* Change the initial message delay. BT requires 300ms,
  1487. * but for others this might be way too much and the phone
  1488. * could time out. XXX make it configurable.
  1489. */
  1490. if (h->omsg[0] == 0x93) {
  1491. h->opause = 8 * h->opause_0; /* initial message delay */
  1492. } else {
  1493. h->opause = 200;
  1494. }
  1495. }
  1496. /* Note - setting osync triggers the generator */
  1497. h->osync = OSYNC_BITS; /* 80 sync bits */
  1498. h->obyten = len + 1; /* bytes to send (including checksum) */
  1499. }
  1500. /*!
  1501. * outgoing data are produced by this generator function, that reads from
  1502. * the descriptor whether it has data to send and which ones.
  1503. */
  1504. static int sms_generate(struct ast_channel *chan, void *data, int len, int samples)
  1505. {
  1506. struct ast_frame f = { 0 };
  1507. #define MAXSAMPLES (800)
  1508. output_t *buf;
  1509. sms_t *h = data;
  1510. int i;
  1511. if (samples > MAXSAMPLES) {
  1512. ast_log(LOG_WARNING, "Only doing %d samples (%d requested)\n",
  1513. MAXSAMPLES, samples);
  1514. samples = MAXSAMPLES;
  1515. }
  1516. len = samples * sizeof(*buf) + AST_FRIENDLY_OFFSET;
  1517. buf = ast_alloca(len);
  1518. f.frametype = AST_FRAME_VOICE;
  1519. f.subclass.codec = __OUT_FMT;
  1520. f.datalen = samples * sizeof(*buf);
  1521. f.offset = AST_FRIENDLY_OFFSET;
  1522. f.mallocd = 0;
  1523. f.data.ptr = buf;
  1524. f.samples = samples;
  1525. f.src = "app_sms";
  1526. /* create a buffer containing the digital sms pattern */
  1527. for (i = 0; i < samples; i++) {
  1528. buf[i] = wave_out[0]; /* default is silence */
  1529. if (h->opause) {
  1530. h->opause--;
  1531. } else if (h->obyten || h->osync) { /* sending data */
  1532. buf[i] = wave_out[h->ophase];
  1533. h->ophase += (h->obyte & 1) ? 13 : 21; /* compute next phase */
  1534. if (h->ophase >= 80)
  1535. h->ophase -= 80;
  1536. if ((h->ophasep += 12) >= 80) { /* time to send the next bit */
  1537. h->ophasep -= 80;
  1538. if (h->oseizure > 0) { /* sending channel seizure (proto 2) */
  1539. h->oseizure--;
  1540. h->obyte ^= 1; /* toggle low bit */
  1541. } else if (h->osync) {
  1542. h->obyte = 1; /* send mark as sync bit */
  1543. h->osync--; /* sending sync bits */
  1544. if (h->osync == 0 && h->protocol == 2 && h->omsg[0] == DLL2_SMS_EST) {
  1545. h->obytep = h->obyten = 0; /* we are done */
  1546. }
  1547. } else {
  1548. h->obitp++;
  1549. if (h->obitp == 1) {
  1550. h->obyte = 0; /* start bit; */
  1551. } else if (h->obitp == 2) {
  1552. h->obyte = h->omsg[h->obytep];
  1553. } else if (h->obitp == 10) {
  1554. h->obyte = 1; /* stop bit */
  1555. h->obitp = 0;
  1556. h->obytep++;
  1557. if (h->obytep == h->obyten) {
  1558. h->obytep = h->obyten = 0; /* sent */
  1559. h->osync = 10; /* trailing marks */
  1560. }
  1561. } else {
  1562. h->obyte >>= 1;
  1563. }
  1564. }
  1565. }
  1566. }
  1567. }
  1568. if (ast_write(chan, &f) < 0) {
  1569. ast_log(LOG_WARNING, "Failed to write frame to '%s': %s\n", chan->name, strerror(errno));
  1570. return -1;
  1571. }
  1572. return 0;
  1573. #undef MAXSAMPLES
  1574. }
  1575. /*!
  1576. * Just return the pointer to the descriptor that we received.
  1577. */
  1578. static void *sms_alloc(struct ast_channel *chan, void *sms_t_ptr)
  1579. {
  1580. return sms_t_ptr;
  1581. }
  1582. static void sms_release(struct ast_channel *chan, void *data)
  1583. {
  1584. return; /* nothing to do here. */
  1585. }
  1586. static struct ast_generator smsgen = {
  1587. .alloc = sms_alloc,
  1588. .release = sms_release,
  1589. .generate = sms_generate,
  1590. };
  1591. /*!
  1592. * Process an incoming frame, trying to detect the carrier and
  1593. * decode the message. The two frequencies are 1300 and 2100 Hz.
  1594. * The decoder detects the amplitude of the signal over the last
  1595. * few samples, filtering the absolute values with a lowpass filter.
  1596. * If the magnitude (h->imag) is large enough, multiply the signal
  1597. * by the two carriers, and compute the amplitudes m0 and m1.
  1598. * Record the current sample as '0' or '1' depending on which one is greater.
  1599. * The last 3 bits are stored in h->ibith, with the count of '1'
  1600. * bits in h->ibitt.
  1601. * XXX the rest is to be determined.
  1602. */
  1603. static void sms_process(sms_t * h, int samples, signed short *data)
  1604. {
  1605. int bit;
  1606. /*
  1607. * Ignore incoming audio while a packet is being transmitted,
  1608. * the protocol is half-duplex.
  1609. * Unfortunately this means that if the outbound and incoming
  1610. * transmission overlap (which is an error condition anyways),
  1611. * we may miss some data and this makes debugging harder.
  1612. */
  1613. if (h->obyten || h->osync) {
  1614. return;
  1615. }
  1616. for ( ; samples-- ; data++) {
  1617. unsigned long long m0, m1;
  1618. if (abs(*data) > h->imag) {
  1619. h->imag = abs(*data);
  1620. } else {
  1621. h->imag = h->imag * 7 / 8;
  1622. }
  1623. if (h->imag <= 500) { /* below [arbitrary] threahold: lost carrier */
  1624. if (h->idle++ == 80000) { /* nothing happening */
  1625. ast_log(LOG_NOTICE, "No data, hanging up\n");
  1626. h->hangup = 1;
  1627. h->err = 1;
  1628. }
  1629. if (h->ierr) { /* error */
  1630. ast_log(LOG_NOTICE, "Error %d, hanging up\n", h->ierr);
  1631. /* Protocol 1 */
  1632. h->err = 1;
  1633. h->omsg[0] = 0x92; /* error */
  1634. h->omsg[1] = 1;
  1635. h->omsg[2] = h->ierr;
  1636. sms_messagetx(h); /* send error */
  1637. }
  1638. h->ierr = h->ibitn = h->ibytep = h->ibytec = 0;
  1639. continue;
  1640. }
  1641. h->idle = 0;
  1642. /* multiply signal by the two carriers. */
  1643. h->ims0 = (h->ims0 * 6 + *data * wave[h->ips0]) / 7;
  1644. h->imc0 = (h->imc0 * 6 + *data * wave[h->ipc0]) / 7;
  1645. h->ims1 = (h->ims1 * 6 + *data * wave[h->ips1]) / 7;
  1646. h->imc1 = (h->imc1 * 6 + *data * wave[h->ipc1]) / 7;
  1647. /* compute the amplitudes */
  1648. m0 = h->ims0 * h->ims0 + h->imc0 * h->imc0;
  1649. m1 = h->ims1 * h->ims1 + h->imc1 * h->imc1;
  1650. /* advance the sin/cos pointers */
  1651. if ((h->ips0 += 21) >= 80) {
  1652. h->ips0 -= 80;
  1653. }
  1654. if ((h->ipc0 += 21) >= 80) {
  1655. h->ipc0 -= 80;
  1656. }
  1657. if ((h->ips1 += 13) >= 80) {
  1658. h->ips1 -= 80;
  1659. }
  1660. if ((h->ipc1 += 13) >= 80) {
  1661. h->ipc1 -= 80;
  1662. }
  1663. /* set new bit to 1 or 0 depending on which value is stronger */
  1664. h->ibith <<= 1;
  1665. if (m1 > m0) {
  1666. h->ibith |= 1;
  1667. }
  1668. if (h->ibith & 8) {
  1669. h->ibitt--;
  1670. }
  1671. if (h->ibith & 1) {
  1672. h->ibitt++;
  1673. }
  1674. bit = ((h->ibitt > 1) ? 1 : 0);
  1675. if (bit != h->ibitl) {
  1676. h->ibitc = 1;
  1677. } else {
  1678. h->ibitc++;
  1679. }
  1680. h->ibitl = bit;
  1681. if (!h->ibitn && h->ibitc == 4 && !bit) {
  1682. h->ibitn = 1;
  1683. h->iphasep = 0;
  1684. }
  1685. if (bit && h->ibitc == 200) { /* sync, restart message */
  1686. /* Protocol 2: empty connection ready (I am master) */
  1687. if (h->framenumber < 0 && h->ibytec >= 160 && !memcmp(h->imsg, "UUUUUUUUUUUUUUUUUUUU", 20)) {
  1688. h->framenumber = 1;
  1689. ast_verb(3, "SMS protocol 2 detected\n");
  1690. h->protocol = 2;
  1691. h->imsg[0] = 0xff; /* special message (fake) */
  1692. h->imsg[1] = h->imsg[2] = 0x00;
  1693. h->ierr = h->ibitn = h->ibytep = h->ibytec = 0;
  1694. sms_messagerx(h);
  1695. }
  1696. h->ierr = h->ibitn = h->ibytep = h->ibytec = 0;
  1697. }
  1698. if (h->ibitn) {
  1699. h->iphasep += 12;
  1700. if (h->iphasep >= 80) { /* next bit */
  1701. h->iphasep -= 80;
  1702. if (h->ibitn++ == 9) { /* end of byte */
  1703. if (!bit) { /* bad stop bit */
  1704. if (h->sent_rel) {
  1705. h->hangup = 1;
  1706. } else {
  1707. ast_log(LOG_NOTICE, "Bad stop bit\n");
  1708. h->ierr = 0xFF; /* unknown error */
  1709. }
  1710. } else {
  1711. if (h->ibytep < sizeof(h->imsg)) {
  1712. h->imsg[h->ibytep] = h->ibytev;
  1713. h->ibytec += h->ibytev;
  1714. h->ibytep++;
  1715. } else if (h->ibytep == sizeof(h->imsg)) {
  1716. ast_log(LOG_NOTICE, "msg too large\n");
  1717. h->ierr = 2; /* bad message length */
  1718. }
  1719. if (h->ibytep > 1 && h->ibytep == 3 + h->imsg[1] && !h->ierr) {
  1720. if (!h->ibytec) {
  1721. sms_messagerx(h);
  1722. } else {
  1723. ast_log(LOG_NOTICE, "bad checksum\n");
  1724. h->ierr = 1; /* bad checksum */
  1725. }
  1726. }
  1727. }
  1728. h->ibitn = 0;
  1729. }
  1730. h->ibytev = (h->ibytev >> 1) + (bit ? 0x80 : 0);
  1731. }
  1732. }
  1733. }
  1734. }
  1735. /*
  1736. * Standard argument parsing:
  1737. * - one enum for the flags we recognise,
  1738. * - one enum for argument indexes
  1739. * - AST_APP_OPTIONS() to drive the parsing routine
  1740. * - in the function, AST_DECLARE_APP_ARGS(...) for the arguments.
  1741. */
  1742. enum sms_flags {
  1743. OPTION_BE_SMSC = (1 << 0), /* act as sms center */
  1744. OPTION_ANSWER = (1 << 1), /* answer on incoming calls */
  1745. OPTION_TWO = (1 << 2), /* Use Protocol Two */
  1746. OPTION_PAUSE = (1 << 3), /* pause before sending data, in ms */
  1747. OPTION_SRR = (1 << 4), /* set srr */
  1748. OPTION_DCS = (1 << 5), /* set dcs */
  1749. };
  1750. enum sms_opt_args {
  1751. OPTION_ARG_PAUSE = 0,
  1752. OPTION_ARG_ARRAY_SIZE
  1753. };
  1754. AST_APP_OPTIONS(sms_options, {
  1755. AST_APP_OPTION('s', OPTION_BE_SMSC),
  1756. AST_APP_OPTION('a', OPTION_ANSWER),
  1757. AST_APP_OPTION('t', OPTION_TWO),
  1758. AST_APP_OPTION('r', OPTION_SRR),
  1759. AST_APP_OPTION('o', OPTION_DCS),
  1760. AST_APP_OPTION_ARG('p', OPTION_PAUSE, OPTION_ARG_PAUSE),
  1761. } );
  1762. static int sms_exec(struct ast_channel *chan, const char *data)
  1763. {
  1764. int res = -1;
  1765. sms_t h = { 0 };
  1766. /* argument parsing support */
  1767. struct ast_flags flags = { 0 };
  1768. char *parse, *sms_opts[OPTION_ARG_ARRAY_SIZE] = { 0, };
  1769. char *p;
  1770. AST_DECLARE_APP_ARGS(sms_args,
  1771. AST_APP_ARG(queue);
  1772. AST_APP_ARG(options);
  1773. AST_APP_ARG(addr);
  1774. AST_APP_ARG(body);
  1775. );
  1776. if (!data) {
  1777. ast_log(LOG_ERROR, "Requires queue name at least\n");
  1778. return -1;
  1779. }
  1780. parse = ast_strdupa(data); /* create a local copy */
  1781. AST_STANDARD_APP_ARGS(sms_args, parse);
  1782. if (sms_args.argc > 1) {
  1783. ast_app_parse_options(sms_options, &flags, sms_opts, sms_args.options);
  1784. }
  1785. ast_verb(1, "sms argc %u queue <%s> opts <%s> addr <%s> body <%s>\n",
  1786. sms_args.argc, S_OR(sms_args.queue, ""),
  1787. S_OR(sms_args.options, ""),
  1788. S_OR(sms_args.addr, ""),
  1789. S_OR(sms_args.body, "") );
  1790. h.ipc0 = h.ipc1 = 20; /* phase for cosine */
  1791. h.dcs = 0xF1; /* default */
  1792. ast_copy_string(h.cli,
  1793. S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, ""),
  1794. sizeof(h.cli));
  1795. if (ast_strlen_zero(sms_args.queue)) {
  1796. ast_log(LOG_ERROR, "Requires queue name\n");
  1797. goto done;
  1798. }
  1799. if (strlen(sms_args.queue) >= sizeof(h.queue)) {
  1800. ast_log(LOG_ERROR, "Queue name too long\n");
  1801. goto done;
  1802. }
  1803. ast_copy_string(h.queue, sms_args.queue, sizeof(h.queue));
  1804. for (p = h.queue; *p; p++) {
  1805. if (!isalnum(*p)) {
  1806. *p = '-'; /* make very safe for filenames */
  1807. }
  1808. }
  1809. h.smsc = ast_test_flag(&flags, OPTION_BE_SMSC);
  1810. h.protocol = ast_test_flag(&flags, OPTION_TWO) ? 2 : 1;
  1811. if (!ast_strlen_zero(sms_opts[OPTION_ARG_PAUSE])) {
  1812. h.opause_0 = atoi(sms_opts[OPTION_ARG_PAUSE]);
  1813. }
  1814. if (h.opause_0 < 25 || h.opause_0 > 2000) {
  1815. h.opause_0 = 300; /* default 300ms */
  1816. }
  1817. ast_verb(1, "initial delay %dms\n", h.opause_0);
  1818. /* the following apply if there is an arg3/4 and apply to the created message file */
  1819. if (ast_test_flag(&flags, OPTION_SRR)) {
  1820. h.srr = 1;
  1821. }
  1822. if (ast_test_flag(&flags, OPTION_DCS)) {
  1823. h.dcs = 1;
  1824. }
  1825. #if 0
  1826. case '1':
  1827. case '2':
  1828. case '3':
  1829. case '4':
  1830. case '5':
  1831. case '6':
  1832. case '7': /* set the pid for saved local message */
  1833. h.pid = 0x40 + (*d & 0xF);
  1834. break;
  1835. }
  1836. #endif
  1837. if (sms_args.argc > 2) {
  1838. unsigned char *up;
  1839. /* submitting a message, not taking call. */
  1840. /* deprecated, use smsq instead */
  1841. h.scts = ast_tvnow();
  1842. if (ast_strlen_zero(sms_args.addr) || strlen(sms_args.addr) >= sizeof(h.oa)) {
  1843. ast_log(LOG_ERROR, "Address too long %s\n", sms_args.addr);
  1844. goto done;
  1845. }
  1846. if (h.smsc) {
  1847. ast_copy_string(h.oa, sms_args.addr, sizeof(h.oa));
  1848. } else {
  1849. ast_copy_string(h.da, sms_args.addr, sizeof(h.da));
  1850. ast_copy_string(h.oa, h.cli, sizeof(h.oa));
  1851. }
  1852. h.udl = 0;
  1853. if (ast_strlen_zero(sms_args.body)) {
  1854. ast_log(LOG_ERROR, "Missing body for %s\n", sms_args.addr);
  1855. goto done;
  1856. }
  1857. up = (unsigned char *)sms_args.body;
  1858. while (*up && h.udl < SMSLEN) {
  1859. h.ud[h.udl++] = utf8decode(&up);
  1860. }
  1861. if (is7bit(h.dcs) && packsms7(0, h.udhl, h.udh, h.udl, h.ud) < 0) {
  1862. ast_log(LOG_WARNING, "Invalid 7 bit GSM data\n");
  1863. goto done;
  1864. }
  1865. if (is8bit(h.dcs) && packsms8(0, h.udhl, h.udh, h.udl, h.ud) < 0) {
  1866. ast_log(LOG_WARNING, "Invalid 8 bit data\n");
  1867. goto done;
  1868. }
  1869. if (is16bit(h.dcs) && packsms16(0, h.udhl, h.udh, h.udl, h.ud) < 0) {
  1870. ast_log(LOG_WARNING, "Invalid 16 bit data\n");
  1871. goto done;
  1872. }
  1873. h.rx = 0; /* sent message */
  1874. h.mr = -1;
  1875. sms_writefile(&h);
  1876. res = h.err;
  1877. goto done;
  1878. }
  1879. if (chan->_state != AST_STATE_UP) { /* make sure channel is answered before any TX */
  1880. ast_answer(chan);
  1881. }
  1882. if (ast_test_flag(&flags, OPTION_ANSWER)) {
  1883. h.framenumber = 1; /* Proto 2 */
  1884. /* set up SMS_EST initial message */
  1885. if (h.protocol == 2) {
  1886. h.omsg[0] = DLL2_SMS_EST;
  1887. h.omsg[1] = 0;
  1888. } else {
  1889. h.omsg[0] = DLL1_SMS_EST | DLL1_SMS_COMPLETE;
  1890. h.omsg[1] = 0;
  1891. }
  1892. sms_messagetx(&h);
  1893. }
  1894. res = ast_set_write_format(chan, __OUT_FMT);
  1895. if (res >= 0) {
  1896. res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
  1897. }
  1898. if (res < 0) {
  1899. ast_log(LOG_ERROR, "Unable to set to linear mode, giving up\n");
  1900. goto done;
  1901. }
  1902. if ( (res = ast_activate_generator(chan, &smsgen, &h)) < 0) {
  1903. ast_log(LOG_ERROR, "Failed to activate generator on '%s'\n", chan->name);
  1904. goto done;
  1905. }
  1906. /* Do our thing here */
  1907. for (;;) {
  1908. struct ast_frame *f;
  1909. int i = ast_waitfor(chan, -1);
  1910. if (i < 0) {
  1911. ast_log(LOG_NOTICE, "waitfor failed\n");
  1912. break;
  1913. }
  1914. if (h.hangup) {
  1915. ast_log(LOG_NOTICE, "channel hangup\n");
  1916. break;
  1917. }
  1918. f = ast_read(chan);
  1919. if (!f) {
  1920. ast_log(LOG_NOTICE, "ast_read failed\n");
  1921. break;
  1922. }
  1923. if (f->frametype == AST_FRAME_VOICE) {
  1924. sms_process(&h, f->samples, f->data.ptr);
  1925. }
  1926. ast_frfree(f);
  1927. }
  1928. res = h.err; /* XXX */
  1929. /*
  1930. * The SMS generator data is on the stack. We _MUST_ make sure the generator
  1931. * is stopped before returning from this function.
  1932. */
  1933. ast_deactivate_generator(chan);
  1934. sms_log(&h, '?'); /* log incomplete message */
  1935. done:
  1936. return (res);
  1937. }
  1938. static int unload_module(void)
  1939. {
  1940. return ast_unregister_application(app);
  1941. }
  1942. static int load_module(void)
  1943. {
  1944. #ifdef OUTALAW
  1945. int p;
  1946. for (p = 0; p < 80; p++) {
  1947. wavea[p] = AST_LIN2A(wave[p]);
  1948. }
  1949. #endif
  1950. snprintf(log_file, sizeof(log_file), "%s/sms", ast_config_AST_LOG_DIR);
  1951. return ast_register_application_xml(app, sms_exec);
  1952. }
  1953. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SMS/PSTN handler");