├── README └── app_softmodem.c /README: -------------------------------------------------------------------------------- 1 | 2 | app_softmodem.c - V.21/V.22/V.23 modem for Asterisk 3 | =============================================================================== 4 | 5 | This Asterisk application provides a softmodem for Asterisk. It supports 6 | the FSK standards V.23, V.21 and Bell 103, but also V.22 and V.22bis. 7 | Byte-size, number of stopbits and bitordering is configurable. 8 | A TCP connection comparable to a Telnet session is established to the 9 | host specified. 10 | 11 | 12 | Dependencies and Installation 13 | ------------------------------------------------------------------------------- 14 | 15 | app_softmodem.c depends on the spandsp library 16 | (http://www.soft-switch.org/). 17 | 18 | It is tested and working with Asterisk 13t. 19 | 20 | To compile put app_softmodem.c in the apps/ dir of your Asterisk source. 21 | Compile with 'make apps'. 22 | 23 | To use do a 'make install' on your asterisk source or copy 24 | apps/app_softmodem.so into your asterisk module directory 25 | (/lib/asterisk/mosules). 26 | 27 | 28 | Dialplan 29 | ------------------------------------------------------------------------------- 30 | 31 | exten => btx,1,Answer() 32 | exten => btx,n,Softmodem(host, port, options) 33 | exten => btx,n,Hangup() 34 | 35 | Without any arguments the application acts as a V.23 modem and connects to a 36 | Telnet server (port 23) on localhost. 37 | 38 | Options are: 39 | r(...): rx cutoff (dBi, float, default: -35) 40 | t(...): tx power (dBi, float, default: -28) 41 | v(...): modem version (default: V23): 42 | V21 - 300/300 baud 43 | V23 - 1200/75 baud 44 | Bell103 - 300/300 baud 45 | V22 - 1200/1200 baud 46 | V22bis - 2400/2400 baud 47 | l or m: least or most significant bit first (default: m) 48 | d(...): amount of data bits (5-8, default: 8) 49 | s(...): amount of stop bits (1-2, default: 1) 50 | e or o: add even or odd parity bit 51 | u: Send Ulm Relay Protocol header to Telnet server 52 | n: Send NULL-Byte to modem after carrier detection (Btx specific) 53 | 54 | To act as a "Btx Vermittlungsstelle" (German videotex system) using V.23, 8 data bits, 1 stop bit and sending a NULL byte upon carrier detection and connecting to a "twistedUlm" server on port 8289 (thus sending the ULM header): 55 | 56 | exten => 190,1,Softmodem(localhost, 8289, v(V23)ld(8)s(1)un) 57 | 58 | To accept calls fom UK Prestel terminals, using V.23, 7 data bits, Even parity, 1 stop bit - 59 | 60 | exten => 618,n,Softmodem(localhost, 6502, v(V23),ld(7)es(1)t(-8)) 61 | 62 | 63 | 64 | The modem seems to work fine with VOIP as long as you use a codec like G.711 65 | (alaw/ulaw). Please deactivate any echo cancellation you might use. 66 | 67 | 68 | 69 | Btx, the Ulm Relay Protocol, general description of operation 70 | ------------------------------------------------------------------------------- 71 | 72 | After the softmodem picks up the line a TCP connection to the specified host 73 | and port is established, the call is dropped when this wasn't successfull. 74 | 75 | When the "n" option is specified a NULL byte is send to the caller upon the detection of a carrier signal from the caller. The Btx system in Germany did this to request the caller's identification. 76 | 77 | With the "u" option specified a header is send to the TCP server at this 78 | point. The header consists of some "name: value" pairs, separated by a CR+LF newline, terminated by a double newline. 79 | 80 | At the moment this header looks like (for V.23): 81 | Version: 1 82 | TXspeed: 120 83 | RXspeed: 7.5 84 | Speeds are in bytes-per-second and may be used by the server to estimate 85 | the length of the transmission. 86 | 87 | After the header all data received from the terminal is put onto the socket 88 | and all data received from the socket is put on the line. 89 | 90 | The line is hung up when the server drops the connection, just as the 91 | connection to the server is dropped when the line is hung up. 92 | 93 | You find my "Ulm Relay Protocol" server here: 94 | http://github.com/proquar/twistedUlm 95 | It sends CEPT T/CD 6-1 encoded data, which it retrieves from a webserver, 96 | to the terminal and provides the characteristic BTX look-and-feel. 97 | 98 | You may also be interested in: 99 | http://runningserver.com/?page=runningserver.content.thelab.bildschirmtrix 100 | And you find a description of BTX MarkupLanguage here: 101 | http://runningserver.com/?page=runningserver.content.thelab.mikropad 102 | 103 | 104 | Authors 105 | ------------------------------------------------------------------------------- 106 | 107 | Christian Groeger 108 | 109 | Based on code by: 110 | Dmitry Andrianov 111 | Steve Underwood 112 | 113 | 114 | 115 | 116 | 117 | 118 | =============================================================================== 119 | 120 | GNU GENERAL PUBLIC LICENSE 121 | Version 3, 29 June 2007 122 | 123 | Copyright (C) 2007 Free Software Foundation, Inc. 124 | Everyone is permitted to copy and distribute verbatim copies 125 | of this license document, but changing it is not allowed. 126 | 127 | Preamble 128 | 129 | The GNU General Public License is a free, copyleft license for 130 | software and other kinds of works. 131 | 132 | The licenses for most software and other practical works are designed 133 | to take away your freedom to share and change the works. By contrast, 134 | the GNU General Public License is intended to guarantee your freedom to 135 | share and change all versions of a program--to make sure it remains free 136 | software for all its users. We, the Free Software Foundation, use the 137 | GNU General Public License for most of our software; it applies also to 138 | any other work released this way by its authors. You can apply it to 139 | your programs, too. 140 | 141 | When we speak of free software, we are referring to freedom, not 142 | price. Our General Public Licenses are designed to make sure that you 143 | have the freedom to distribute copies of free software (and charge for 144 | them if you wish), that you receive source code or can get it if you 145 | want it, that you can change the software or use pieces of it in new 146 | free programs, and that you know you can do these things. 147 | 148 | To protect your rights, we need to prevent others from denying you 149 | these rights or asking you to surrender the rights. Therefore, you have 150 | certain responsibilities if you distribute copies of the software, or if 151 | you modify it: responsibilities to respect the freedom of others. 152 | 153 | For example, if you distribute copies of such a program, whether 154 | gratis or for a fee, you must pass on to the recipients the same 155 | freedoms that you received. You must make sure that they, too, receive 156 | or can get the source code. And you must show them these terms so they 157 | know their rights. 158 | 159 | Developers that use the GNU GPL protect your rights with two steps: 160 | (1) assert copyright on the software, and (2) offer you this License 161 | giving you legal permission to copy, distribute and/or modify it. 162 | 163 | For the developers' and authors' protection, the GPL clearly explains 164 | that there is no warranty for this free software. For both users' and 165 | authors' sake, the GPL requires that modified versions be marked as 166 | changed, so that their problems will not be attributed erroneously to 167 | authors of previous versions. 168 | 169 | Some devices are designed to deny users access to install or run 170 | modified versions of the software inside them, although the manufacturer 171 | can do so. This is fundamentally incompatible with the aim of 172 | protecting users' freedom to change the software. The systematic 173 | pattern of such abuse occurs in the area of products for individuals to 174 | use, which is precisely where it is most unacceptable. Therefore, we 175 | have designed this version of the GPL to prohibit the practice for those 176 | products. If such problems arise substantially in other domains, we 177 | stand ready to extend this provision to those domains in future versions 178 | of the GPL, as needed to protect the freedom of users. 179 | 180 | Finally, every program is threatened constantly by software patents. 181 | States should not allow patents to restrict development and use of 182 | software on general-purpose computers, but in those that do, we wish to 183 | avoid the special danger that patents applied to a free program could 184 | make it effectively proprietary. To prevent this, the GPL assures that 185 | patents cannot be used to render the program non-free. 186 | 187 | The precise terms and conditions for copying, distribution and 188 | modification follow. 189 | 190 | TERMS AND CONDITIONS 191 | 192 | 0. Definitions. 193 | 194 | "This License" refers to version 3 of the GNU General Public License. 195 | 196 | "Copyright" also means copyright-like laws that apply to other kinds of 197 | works, such as semiconductor masks. 198 | 199 | "The Program" refers to any copyrightable work licensed under this 200 | License. Each licensee is addressed as "you". "Licensees" and 201 | "recipients" may be individuals or organizations. 202 | 203 | To "modify" a work means to copy from or adapt all or part of the work 204 | in a fashion requiring copyright permission, other than the making of an 205 | exact copy. The resulting work is called a "modified version" of the 206 | earlier work or a work "based on" the earlier work. 207 | 208 | A "covered work" means either the unmodified Program or a work based 209 | on the Program. 210 | 211 | To "propagate" a work means to do anything with it that, without 212 | permission, would make you directly or secondarily liable for 213 | infringement under applicable copyright law, except executing it on a 214 | computer or modifying a private copy. Propagation includes copying, 215 | distribution (with or without modification), making available to the 216 | public, and in some countries other activities as well. 217 | 218 | To "convey" a work means any kind of propagation that enables other 219 | parties to make or receive copies. Mere interaction with a user through 220 | a computer network, with no transfer of a copy, is not conveying. 221 | 222 | An interactive user interface displays "Appropriate Legal Notices" 223 | to the extent that it includes a convenient and prominently visible 224 | feature that (1) displays an appropriate copyright notice, and (2) 225 | tells the user that there is no warranty for the work (except to the 226 | extent that warranties are provided), that licensees may convey the 227 | work under this License, and how to view a copy of this License. If 228 | the interface presents a list of user commands or options, such as a 229 | menu, a prominent item in the list meets this criterion. 230 | 231 | 1. Source Code. 232 | 233 | The "source code" for a work means the preferred form of the work 234 | for making modifications to it. "Object code" means any non-source 235 | form of a work. 236 | 237 | A "Standard Interface" means an interface that either is an official 238 | standard defined by a recognized standards body, or, in the case of 239 | interfaces specified for a particular programming language, one that 240 | is widely used among developers working in that language. 241 | 242 | The "System Libraries" of an executable work include anything, other 243 | than the work as a whole, that (a) is included in the normal form of 244 | packaging a Major Component, but which is not part of that Major 245 | Component, and (b) serves only to enable use of the work with that 246 | Major Component, or to implement a Standard Interface for which an 247 | implementation is available to the public in source code form. A 248 | "Major Component", in this context, means a major essential component 249 | (kernel, window system, and so on) of the specific operating system 250 | (if any) on which the executable work runs, or a compiler used to 251 | produce the work, or an object code interpreter used to run it. 252 | 253 | The "Corresponding Source" for a work in object code form means all 254 | the source code needed to generate, install, and (for an executable 255 | work) run the object code and to modify the work, including scripts to 256 | control those activities. However, it does not include the work's 257 | System Libraries, or general-purpose tools or generally available free 258 | programs which are used unmodified in performing those activities but 259 | which are not part of the work. For example, Corresponding Source 260 | includes interface definition files associated with source files for 261 | the work, and the source code for shared libraries and dynamically 262 | linked subprograms that the work is specifically designed to require, 263 | such as by intimate data communication or control flow between those 264 | subprograms and other parts of the work. 265 | 266 | The Corresponding Source need not include anything that users 267 | can regenerate automatically from other parts of the Corresponding 268 | Source. 269 | 270 | The Corresponding Source for a work in source code form is that 271 | same work. 272 | 273 | 2. Basic Permissions. 274 | 275 | All rights granted under this License are granted for the term of 276 | copyright on the Program, and are irrevocable provided the stated 277 | conditions are met. This License explicitly affirms your unlimited 278 | permission to run the unmodified Program. The output from running a 279 | covered work is covered by this License only if the output, given its 280 | content, constitutes a covered work. This License acknowledges your 281 | rights of fair use or other equivalent, as provided by copyright law. 282 | 283 | You may make, run and propagate covered works that you do not 284 | convey, without conditions so long as your license otherwise remains 285 | in force. You may convey covered works to others for the sole purpose 286 | of having them make modifications exclusively for you, or provide you 287 | with facilities for running those works, provided that you comply with 288 | the terms of this License in conveying all material for which you do 289 | not control copyright. Those thus making or running the covered works 290 | for you must do so exclusively on your behalf, under your direction 291 | and control, on terms that prohibit them from making any copies of 292 | your copyrighted material outside their relationship with you. 293 | 294 | Conveying under any other circumstances is permitted solely under 295 | the conditions stated below. Sublicensing is not allowed; section 10 296 | makes it unnecessary. 297 | 298 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 299 | 300 | No covered work shall be deemed part of an effective technological 301 | measure under any applicable law fulfilling obligations under article 302 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 303 | similar laws prohibiting or restricting circumvention of such 304 | measures. 305 | 306 | When you convey a covered work, you waive any legal power to forbid 307 | circumvention of technological measures to the extent such circumvention 308 | is effected by exercising rights under this License with respect to 309 | the covered work, and you disclaim any intention to limit operation or 310 | modification of the work as a means of enforcing, against the work's 311 | users, your or third parties' legal rights to forbid circumvention of 312 | technological measures. 313 | 314 | 4. Conveying Verbatim Copies. 315 | 316 | You may convey verbatim copies of the Program's source code as you 317 | receive it, in any medium, provided that you conspicuously and 318 | appropriately publish on each copy an appropriate copyright notice; 319 | keep intact all notices stating that this License and any 320 | non-permissive terms added in accord with section 7 apply to the code; 321 | keep intact all notices of the absence of any warranty; and give all 322 | recipients a copy of this License along with the Program. 323 | 324 | You may charge any price or no price for each copy that you convey, 325 | and you may offer support or warranty protection for a fee. 326 | 327 | 5. Conveying Modified Source Versions. 328 | 329 | You may convey a work based on the Program, or the modifications to 330 | produce it from the Program, in the form of source code under the 331 | terms of section 4, provided that you also meet all of these conditions: 332 | 333 | a) The work must carry prominent notices stating that you modified 334 | it, and giving a relevant date. 335 | 336 | b) The work must carry prominent notices stating that it is 337 | released under this License and any conditions added under section 338 | 7. This requirement modifies the requirement in section 4 to 339 | "keep intact all notices". 340 | 341 | c) You must license the entire work, as a whole, under this 342 | License to anyone who comes into possession of a copy. This 343 | License will therefore apply, along with any applicable section 7 344 | additional terms, to the whole of the work, and all its parts, 345 | regardless of how they are packaged. This License gives no 346 | permission to license the work in any other way, but it does not 347 | invalidate such permission if you have separately received it. 348 | 349 | d) If the work has interactive user interfaces, each must display 350 | Appropriate Legal Notices; however, if the Program has interactive 351 | interfaces that do not display Appropriate Legal Notices, your 352 | work need not make them do so. 353 | 354 | A compilation of a covered work with other separate and independent 355 | works, which are not by their nature extensions of the covered work, 356 | and which are not combined with it such as to form a larger program, 357 | in or on a volume of a storage or distribution medium, is called an 358 | "aggregate" if the compilation and its resulting copyright are not 359 | used to limit the access or legal rights of the compilation's users 360 | beyond what the individual works permit. Inclusion of a covered work 361 | in an aggregate does not cause this License to apply to the other 362 | parts of the aggregate. 363 | 364 | 6. Conveying Non-Source Forms. 365 | 366 | You may convey a covered work in object code form under the terms 367 | of sections 4 and 5, provided that you also convey the 368 | machine-readable Corresponding Source under the terms of this License, 369 | in one of these ways: 370 | 371 | a) Convey the object code in, or embodied in, a physical product 372 | (including a physical distribution medium), accompanied by the 373 | Corresponding Source fixed on a durable physical medium 374 | customarily used for software interchange. 375 | 376 | b) Convey the object code in, or embodied in, a physical product 377 | (including a physical distribution medium), accompanied by a 378 | written offer, valid for at least three years and valid for as 379 | long as you offer spare parts or customer support for that product 380 | model, to give anyone who possesses the object code either (1) a 381 | copy of the Corresponding Source for all the software in the 382 | product that is covered by this License, on a durable physical 383 | medium customarily used for software interchange, for a price no 384 | more than your reasonable cost of physically performing this 385 | conveying of source, or (2) access to copy the 386 | Corresponding Source from a network server at no charge. 387 | 388 | c) Convey individual copies of the object code with a copy of the 389 | written offer to provide the Corresponding Source. This 390 | alternative is allowed only occasionally and noncommercially, and 391 | only if you received the object code with such an offer, in accord 392 | with subsection 6b. 393 | 394 | d) Convey the object code by offering access from a designated 395 | place (gratis or for a charge), and offer equivalent access to the 396 | Corresponding Source in the same way through the same place at no 397 | further charge. You need not require recipients to copy the 398 | Corresponding Source along with the object code. If the place to 399 | copy the object code is a network server, the Corresponding Source 400 | may be on a different server (operated by you or a third party) 401 | that supports equivalent copying facilities, provided you maintain 402 | clear directions next to the object code saying where to find the 403 | Corresponding Source. Regardless of what server hosts the 404 | Corresponding Source, you remain obligated to ensure that it is 405 | available for as long as needed to satisfy these requirements. 406 | 407 | e) Convey the object code using peer-to-peer transmission, provided 408 | you inform other peers where the object code and Corresponding 409 | Source of the work are being offered to the general public at no 410 | charge under subsection 6d. 411 | 412 | A separable portion of the object code, whose source code is excluded 413 | from the Corresponding Source as a System Library, need not be 414 | included in conveying the object code work. 415 | 416 | A "User Product" is either (1) a "consumer product", which means any 417 | tangible personal property which is normally used for personal, family, 418 | or household purposes, or (2) anything designed or sold for incorporation 419 | into a dwelling. In determining whether a product is a consumer product, 420 | doubtful cases shall be resolved in favor of coverage. For a particular 421 | product received by a particular user, "normally used" refers to a 422 | typical or common use of that class of product, regardless of the status 423 | of the particular user or of the way in which the particular user 424 | actually uses, or expects or is expected to use, the product. A product 425 | is a consumer product regardless of whether the product has substantial 426 | commercial, industrial or non-consumer uses, unless such uses represent 427 | the only significant mode of use of the product. 428 | 429 | "Installation Information" for a User Product means any methods, 430 | procedures, authorization keys, or other information required to install 431 | and execute modified versions of a covered work in that User Product from 432 | a modified version of its Corresponding Source. The information must 433 | suffice to ensure that the continued functioning of the modified object 434 | code is in no case prevented or interfered with solely because 435 | modification has been made. 436 | 437 | If you convey an object code work under this section in, or with, or 438 | specifically for use in, a User Product, and the conveying occurs as 439 | part of a transaction in which the right of possession and use of the 440 | User Product is transferred to the recipient in perpetuity or for a 441 | fixed term (regardless of how the transaction is characterized), the 442 | Corresponding Source conveyed under this section must be accompanied 443 | by the Installation Information. But this requirement does not apply 444 | if neither you nor any third party retains the ability to install 445 | modified object code on the User Product (for example, the work has 446 | been installed in ROM). 447 | 448 | The requirement to provide Installation Information does not include a 449 | requirement to continue to provide support service, warranty, or updates 450 | for a work that has been modified or installed by the recipient, or for 451 | the User Product in which it has been modified or installed. Access to a 452 | network may be denied when the modification itself materially and 453 | adversely affects the operation of the network or violates the rules and 454 | protocols for communication across the network. 455 | 456 | Corresponding Source conveyed, and Installation Information provided, 457 | in accord with this section must be in a format that is publicly 458 | documented (and with an implementation available to the public in 459 | source code form), and must require no special password or key for 460 | unpacking, reading or copying. 461 | 462 | 7. Additional Terms. 463 | 464 | "Additional permissions" are terms that supplement the terms of this 465 | License by making exceptions from one or more of its conditions. 466 | Additional permissions that are applicable to the entire Program shall 467 | be treated as though they were included in this License, to the extent 468 | that they are valid under applicable law. If additional permissions 469 | apply only to part of the Program, that part may be used separately 470 | under those permissions, but the entire Program remains governed by 471 | this License without regard to the additional permissions. 472 | 473 | When you convey a copy of a covered work, you may at your option 474 | remove any additional permissions from that copy, or from any part of 475 | it. (Additional permissions may be written to require their own 476 | removal in certain cases when you modify the work.) You may place 477 | additional permissions on material, added by you to a covered work, 478 | for which you have or can give appropriate copyright permission. 479 | 480 | Notwithstanding any other provision of this License, for material you 481 | add to a covered work, you may (if authorized by the copyright holders of 482 | that material) supplement the terms of this License with terms: 483 | 484 | a) Disclaiming warranty or limiting liability differently from the 485 | terms of sections 15 and 16 of this License; or 486 | 487 | b) Requiring preservation of specified reasonable legal notices or 488 | author attributions in that material or in the Appropriate Legal 489 | Notices displayed by works containing it; or 490 | 491 | c) Prohibiting misrepresentation of the origin of that material, or 492 | requiring that modified versions of such material be marked in 493 | reasonable ways as different from the original version; or 494 | 495 | d) Limiting the use for publicity purposes of names of licensors or 496 | authors of the material; or 497 | 498 | e) Declining to grant rights under trademark law for use of some 499 | trade names, trademarks, or service marks; or 500 | 501 | f) Requiring indemnification of licensors and authors of that 502 | material by anyone who conveys the material (or modified versions of 503 | it) with contractual assumptions of liability to the recipient, for 504 | any liability that these contractual assumptions directly impose on 505 | those licensors and authors. 506 | 507 | All other non-permissive additional terms are considered "further 508 | restrictions" within the meaning of section 10. If the Program as you 509 | received it, or any part of it, contains a notice stating that it is 510 | governed by this License along with a term that is a further 511 | restriction, you may remove that term. If a license document contains 512 | a further restriction but permits relicensing or conveying under this 513 | License, you may add to a covered work material governed by the terms 514 | of that license document, provided that the further restriction does 515 | not survive such relicensing or conveying. 516 | 517 | If you add terms to a covered work in accord with this section, you 518 | must place, in the relevant source files, a statement of the 519 | additional terms that apply to those files, or a notice indicating 520 | where to find the applicable terms. 521 | 522 | Additional terms, permissive or non-permissive, may be stated in the 523 | form of a separately written license, or stated as exceptions; 524 | the above requirements apply either way. 525 | 526 | 8. Termination. 527 | 528 | You may not propagate or modify a covered work except as expressly 529 | provided under this License. Any attempt otherwise to propagate or 530 | modify it is void, and will automatically terminate your rights under 531 | this License (including any patent licenses granted under the third 532 | paragraph of section 11). 533 | 534 | However, if you cease all violation of this License, then your 535 | license from a particular copyright holder is reinstated (a) 536 | provisionally, unless and until the copyright holder explicitly and 537 | finally terminates your license, and (b) permanently, if the copyright 538 | holder fails to notify you of the violation by some reasonable means 539 | prior to 60 days after the cessation. 540 | 541 | Moreover, your license from a particular copyright holder is 542 | reinstated permanently if the copyright holder notifies you of the 543 | violation by some reasonable means, this is the first time you have 544 | received notice of violation of this License (for any work) from that 545 | copyright holder, and you cure the violation prior to 30 days after 546 | your receipt of the notice. 547 | 548 | Termination of your rights under this section does not terminate the 549 | licenses of parties who have received copies or rights from you under 550 | this License. If your rights have been terminated and not permanently 551 | reinstated, you do not qualify to receive new licenses for the same 552 | material under section 10. 553 | 554 | 9. Acceptance Not Required for Having Copies. 555 | 556 | You are not required to accept this License in order to receive or 557 | run a copy of the Program. Ancillary propagation of a covered work 558 | occurring solely as a consequence of using peer-to-peer transmission 559 | to receive a copy likewise does not require acceptance. However, 560 | nothing other than this License grants you permission to propagate or 561 | modify any covered work. These actions infringe copyright if you do 562 | not accept this License. Therefore, by modifying or propagating a 563 | covered work, you indicate your acceptance of this License to do so. 564 | 565 | 10. Automatic Licensing of Downstream Recipients. 566 | 567 | Each time you convey a covered work, the recipient automatically 568 | receives a license from the original licensors, to run, modify and 569 | propagate that work, subject to this License. You are not responsible 570 | for enforcing compliance by third parties with this License. 571 | 572 | An "entity transaction" is a transaction transferring control of an 573 | organization, or substantially all assets of one, or subdividing an 574 | organization, or merging organizations. If propagation of a covered 575 | work results from an entity transaction, each party to that 576 | transaction who receives a copy of the work also receives whatever 577 | licenses to the work the party's predecessor in interest had or could 578 | give under the previous paragraph, plus a right to possession of the 579 | Corresponding Source of the work from the predecessor in interest, if 580 | the predecessor has it or can get it with reasonable efforts. 581 | 582 | You may not impose any further restrictions on the exercise of the 583 | rights granted or affirmed under this License. For example, you may 584 | not impose a license fee, royalty, or other charge for exercise of 585 | rights granted under this License, and you may not initiate litigation 586 | (including a cross-claim or counterclaim in a lawsuit) alleging that 587 | any patent claim is infringed by making, using, selling, offering for 588 | sale, or importing the Program or any portion of it. 589 | 590 | 11. Patents. 591 | 592 | A "contributor" is a copyright holder who authorizes use under this 593 | License of the Program or a work on which the Program is based. The 594 | work thus licensed is called the contributor's "contributor version". 595 | 596 | A contributor's "essential patent claims" are all patent claims 597 | owned or controlled by the contributor, whether already acquired or 598 | hereafter acquired, that would be infringed by some manner, permitted 599 | by this License, of making, using, or selling its contributor version, 600 | but do not include claims that would be infringed only as a 601 | consequence of further modification of the contributor version. For 602 | purposes of this definition, "control" includes the right to grant 603 | patent sublicenses in a manner consistent with the requirements of 604 | this License. 605 | 606 | Each contributor grants you a non-exclusive, worldwide, royalty-free 607 | patent license under the contributor's essential patent claims, to 608 | make, use, sell, offer for sale, import and otherwise run, modify and 609 | propagate the contents of its contributor version. 610 | 611 | In the following three paragraphs, a "patent license" is any express 612 | agreement or commitment, however denominated, not to enforce a patent 613 | (such as an express permission to practice a patent or covenant not to 614 | sue for patent infringement). To "grant" such a patent license to a 615 | party means to make such an agreement or commitment not to enforce a 616 | patent against the party. 617 | 618 | If you convey a covered work, knowingly relying on a patent license, 619 | and the Corresponding Source of the work is not available for anyone 620 | to copy, free of charge and under the terms of this License, through a 621 | publicly available network server or other readily accessible means, 622 | then you must either (1) cause the Corresponding Source to be so 623 | available, or (2) arrange to deprive yourself of the benefit of the 624 | patent license for this particular work, or (3) arrange, in a manner 625 | consistent with the requirements of this License, to extend the patent 626 | license to downstream recipients. "Knowingly relying" means you have 627 | actual knowledge that, but for the patent license, your conveying the 628 | covered work in a country, or your recipient's use of the covered work 629 | in a country, would infringe one or more identifiable patents in that 630 | country that you have reason to believe are valid. 631 | 632 | If, pursuant to or in connection with a single transaction or 633 | arrangement, you convey, or propagate by procuring conveyance of, a 634 | covered work, and grant a patent license to some of the parties 635 | receiving the covered work authorizing them to use, propagate, modify 636 | or convey a specific copy of the covered work, then the patent license 637 | you grant is automatically extended to all recipients of the covered 638 | work and works based on it. 639 | 640 | A patent license is "discriminatory" if it does not include within 641 | the scope of its coverage, prohibits the exercise of, or is 642 | conditioned on the non-exercise of one or more of the rights that are 643 | specifically granted under this License. You may not convey a covered 644 | work if you are a party to an arrangement with a third party that is 645 | in the business of distributing software, under which you make payment 646 | to the third party based on the extent of your activity of conveying 647 | the work, and under which the third party grants, to any of the 648 | parties who would receive the covered work from you, a discriminatory 649 | patent license (a) in connection with copies of the covered work 650 | conveyed by you (or copies made from those copies), or (b) primarily 651 | for and in connection with specific products or compilations that 652 | contain the covered work, unless you entered into that arrangement, 653 | or that patent license was granted, prior to 28 March 2007. 654 | 655 | Nothing in this License shall be construed as excluding or limiting 656 | any implied license or other defenses to infringement that may 657 | otherwise be available to you under applicable patent law. 658 | 659 | 12. No Surrender of Others' Freedom. 660 | 661 | If conditions are imposed on you (whether by court order, agreement or 662 | otherwise) that contradict the conditions of this License, they do not 663 | excuse you from the conditions of this License. If you cannot convey a 664 | covered work so as to satisfy simultaneously your obligations under this 665 | License and any other pertinent obligations, then as a consequence you may 666 | not convey it at all. For example, if you agree to terms that obligate you 667 | to collect a royalty for further conveying from those to whom you convey 668 | the Program, the only way you could satisfy both those terms and this 669 | License would be to refrain entirely from conveying the Program. 670 | 671 | 13. Use with the GNU Affero General Public License. 672 | 673 | Notwithstanding any other provision of this License, you have 674 | permission to link or combine any covered work with a work licensed 675 | under version 3 of the GNU Affero General Public License into a single 676 | combined work, and to convey the resulting work. The terms of this 677 | License will continue to apply to the part which is the covered work, 678 | but the special requirements of the GNU Affero General Public License, 679 | section 13, concerning interaction through a network will apply to the 680 | combination as such. 681 | 682 | 14. Revised Versions of this License. 683 | 684 | The Free Software Foundation may publish revised and/or new versions of 685 | the GNU General Public License from time to time. Such new versions will 686 | be similar in spirit to the present version, but may differ in detail to 687 | address new problems or concerns. 688 | 689 | Each version is given a distinguishing version number. If the 690 | Program specifies that a certain numbered version of the GNU General 691 | Public License "or any later version" applies to it, you have the 692 | option of following the terms and conditions either of that numbered 693 | version or of any later version published by the Free Software 694 | Foundation. If the Program does not specify a version number of the 695 | GNU General Public License, you may choose any version ever published 696 | by the Free Software Foundation. 697 | 698 | If the Program specifies that a proxy can decide which future 699 | versions of the GNU General Public License can be used, that proxy's 700 | public statement of acceptance of a version permanently authorizes you 701 | to choose that version for the Program. 702 | 703 | Later license versions may give you additional or different 704 | permissions. However, no additional obligations are imposed on any 705 | author or copyright holder as a result of your choosing to follow a 706 | later version. 707 | 708 | 15. Disclaimer of Warranty. 709 | 710 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 711 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 712 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 713 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 714 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 715 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 716 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 717 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 718 | 719 | 16. Limitation of Liability. 720 | 721 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 722 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 723 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 724 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 725 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 726 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 727 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 728 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 729 | SUCH DAMAGES. 730 | 731 | 17. Interpretation of Sections 15 and 16. 732 | 733 | If the disclaimer of warranty and limitation of liability provided 734 | above cannot be given local legal effect according to their terms, 735 | reviewing courts shall apply local law that most closely approximates 736 | an absolute waiver of all civil liability in connection with the 737 | Program, unless a warranty or assumption of liability accompanies a 738 | copy of the Program in return for a fee. 739 | 740 | END OF TERMS AND CONDITIONS 741 | 742 | How to Apply These Terms to Your New Programs 743 | 744 | If you develop a new program, and you want it to be of the greatest 745 | possible use to the public, the best way to achieve this is to make it 746 | free software which everyone can redistribute and change under these terms. 747 | 748 | To do so, attach the following notices to the program. It is safest 749 | to attach them to the start of each source file to most effectively 750 | state the exclusion of warranty; and each file should have at least 751 | the "copyright" line and a pointer to where the full notice is found. 752 | 753 | 754 | Copyright (C) 755 | 756 | This program is free software: you can redistribute it and/or modify 757 | it under the terms of the GNU General Public License as published by 758 | the Free Software Foundation, either version 3 of the License, or 759 | (at your option) any later version. 760 | 761 | This program is distributed in the hope that it will be useful, 762 | but WITHOUT ANY WARRANTY; without even the implied warranty of 763 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 764 | GNU General Public License for more details. 765 | 766 | You should have received a copy of the GNU General Public License 767 | along with this program. If not, see . 768 | 769 | Also add information on how to contact you by electronic and paper mail. 770 | 771 | If the program does terminal interaction, make it output a short 772 | notice like this when it starts in an interactive mode: 773 | 774 | Copyright (C) 775 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 776 | This is free software, and you are welcome to redistribute it 777 | under certain conditions; type `show c' for details. 778 | 779 | The hypothetical commands `show w' and `show c' should show the appropriate 780 | parts of the General Public License. Of course, your program's commands 781 | might be different; for a GUI interface, you would use an "about box". 782 | 783 | You should also get your employer (if you work as a programmer) or school, 784 | if any, to sign a "copyright disclaimer" for the program, if necessary. 785 | For more information on this, and how to apply and follow the GNU GPL, see 786 | . 787 | 788 | The GNU General Public License does not permit incorporating your program 789 | into proprietary programs. If your program is a subroutine library, you 790 | may consider it more useful to permit linking proprietary applications with 791 | the library. If this is what you want to do, use the GNU Lesser General 792 | Public License instead of this License. But first, please read 793 | . -------------------------------------------------------------------------------- /app_softmodem.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Softmodem for Asterisk 3 | * 4 | * 2010, Christian Groeger 5 | * 6 | * Based on app_fax.c by Dmitry Andrianov 7 | * and Steve Underwood 8 | * 9 | * Parity options added 2018 Rob O'Donnell 10 | * 11 | * This program is free software, distributed under the terms of 12 | * the GNU General Public License 13 | * 14 | */ 15 | 16 | /*** MODULEINFO 17 | spandsp 18 | extended 19 | ***/ 20 | 21 | #include "asterisk.h" 22 | 23 | //ASTERISK_FILE_VERSION(__FILE__, "$Revision$") 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | 34 | #include 35 | #ifdef HAVE_SPANDSP_EXPOSE_H 36 | #include 37 | #endif 38 | #include 39 | 40 | #include "asterisk/file.h" 41 | #include "asterisk/module.h" 42 | #include "asterisk/channel.h" 43 | #include "asterisk/strings.h" 44 | #include "asterisk/lock.h" 45 | #include "asterisk/app.h" 46 | #include "asterisk/pbx.h" 47 | #include "asterisk/format_cache.h" 48 | #include "asterisk/logger.h" 49 | #include "asterisk/utils.h" 50 | #include "asterisk/dsp.h" 51 | #include "asterisk/manager.h" 52 | 53 | #ifndef AST_MODULE 54 | #define AST_MODULE "app_softmodem" 55 | #endif 56 | 57 | /*** DOCUMENTATION 58 | 59 | 60 | A Softmodem that connects the caller to a Telnet server. 61 | 62 | 63 | 64 | Softmodem(hostname,port,options): Simulates a FSK(V.23) or V.22bis modem. The modem on the other end is connected to the specified server using a simple TCP connection (like Telnet). 65 | 66 | 67 | ***/ 68 | 69 | /** 70 | * "Options:" 71 | " r(...): rx cutoff (dBi, float, default: -35)\n" 72 | " t(...): tx power (dBi, float, default: -28)\n" 73 | " v(...): modem version (default: V23):\n" 74 | " V21 - 300/300 baud\n" 75 | " V23 - 1200/75 baud\n" 76 | " Bell103 - 300/300 baud\n" 77 | " V22 - 1200/1200 baud\n" 78 | " V22bis - 2400/2400 baud\n" 79 | " l or m: least or most significant bit first (default: m)\n" 80 | " d(...): amount of data bits (5-8, default: 8)\n" 81 | " s(...): amount of stop bits (1-2, default: 1)\n" 82 | " e or o: add even or odd parity bit 83 | " u: Send ULM header to Telnet server (Btx)\n" 84 | " n: Send NULL-Byte to modem after carrier detection (Btx)\n" 85 | 86 | 87 | **/ 88 | 89 | static const char app[] = "Softmodem"; 90 | 91 | enum { 92 | OPT_RX_CUTOFF = (1 << 0), 93 | OPT_TX_POWER = (1 << 1), 94 | OPT_MODEM_VERSION = (1 << 2), 95 | OPT_LSB_FIRST = (1 << 3), 96 | OPT_MSB_FIRST = (1 << 4), 97 | OPT_DATABITS = (1 << 5), 98 | OPT_STOPBITS = (1 << 6), 99 | OPT_ULM_HEADER = (1 << 7), 100 | OPT_NULL = (1 << 8), 101 | OPT_EVEN_PARITY = (1 << 9), 102 | OPT_ODD_PARITY = (1 << 10) 103 | }; 104 | 105 | enum { 106 | OPT_ARG_RX_CUTOFF, 107 | OPT_ARG_TX_POWER, 108 | OPT_ARG_MODEM_VERSION, 109 | OPT_ARG_DATABITS, 110 | OPT_ARG_STOPBITS, 111 | 112 | /* Must be the last element */ 113 | OPT_ARG_ARRAY_SIZE, 114 | }; 115 | 116 | AST_APP_OPTIONS(additional_options, BEGIN_OPTIONS 117 | AST_APP_OPTION_ARG('r', OPT_RX_CUTOFF, OPT_ARG_RX_CUTOFF), 118 | AST_APP_OPTION_ARG('t', OPT_TX_POWER, OPT_ARG_TX_POWER), 119 | AST_APP_OPTION_ARG('v', OPT_MODEM_VERSION, OPT_ARG_MODEM_VERSION), 120 | AST_APP_OPTION('l', OPT_LSB_FIRST), 121 | AST_APP_OPTION('m', OPT_MSB_FIRST), 122 | AST_APP_OPTION_ARG('d', OPT_DATABITS, OPT_ARG_DATABITS), 123 | AST_APP_OPTION_ARG('s', OPT_STOPBITS, OPT_ARG_STOPBITS), 124 | AST_APP_OPTION('e', OPT_EVEN_PARITY), 125 | AST_APP_OPTION('o', OPT_ODD_PARITY), 126 | AST_APP_OPTION('u', OPT_ULM_HEADER), 127 | AST_APP_OPTION('n', OPT_NULL), 128 | END_OPTIONS ); 129 | 130 | 131 | #define MAX_SAMPLES 240 132 | 133 | enum { 134 | VERSION_V21, 135 | VERSION_V23, 136 | VERSION_BELL103, 137 | VERSION_V22, 138 | VERSION_V22BIS, 139 | }; 140 | 141 | typedef struct { 142 | struct ast_channel *chan; 143 | const char *host; //telnetd host+port 144 | int port; 145 | float txpower; 146 | float rxcutoff; 147 | int version; 148 | int lsb; 149 | int databits; 150 | int stopbits; 151 | int ulmheader; 152 | int sendnull; 153 | volatile int finished; 154 | int paritytype; 155 | } modem_session; 156 | 157 | #define MODEM_BITBUFFER_SIZE 16 158 | typedef struct { 159 | int answertone; // terminal is active (=sends data) 160 | int nulsent; // we sent a NULL as very first character (at least the DBT03 expects this) 161 | } connection_state; 162 | typedef struct { 163 | int sock; 164 | int bitbuffer[MODEM_BITBUFFER_SIZE]; 165 | int writepos; 166 | int readpos; 167 | int fill; 168 | connection_state *state; 169 | modem_session *session; 170 | } modem_data; 171 | 172 | 173 | // this is called by spandsp whenever it filtered a new bit from the line 174 | static void modem_put_bit(void *user_data, int bit) { 175 | int stop, stop2, i; 176 | 177 | modem_data *rx = (modem_data*) user_data; 178 | 179 | int databits = rx->session->databits; 180 | int stopbits = rx->session->stopbits; 181 | int paritybits = 0; 182 | 183 | if (rx->session->paritytype) 184 | paritybits = 1; 185 | 186 | // modem recognised us and starts responding through sending it's pilot signal 187 | if (rx->state->answertone<=0) { 188 | if (bit==SIG_STATUS_CARRIER_UP) { 189 | rx->state->answertone=0; 190 | } 191 | else if (bit==1 && rx->state->answertone==0) { 192 | rx->state->answertone=1; 193 | } 194 | } 195 | else { 196 | // ignore other spandsp-stuff 197 | if (bit==1 || bit==0) { 198 | 199 | // insert bit into our bitbuffer 200 | rx->bitbuffer[rx->writepos]=bit; 201 | rx->writepos++; 202 | if (rx->writepos>=MODEM_BITBUFFER_SIZE) rx->writepos=0; 203 | 204 | if (rx->fillfill++; 206 | } else { 207 | // our bitbuffer is full, this probably won't happen 208 | // printf("full buffer\n"); 209 | rx->readpos++; 210 | if (rx->readpos>=MODEM_BITBUFFER_SIZE) rx->readpos=0; 211 | } 212 | 213 | // full byte = 1 startbit + databits + paritybits + stopbits 214 | while (rx->fill>=(1+databits+paritybits+stopbits)) { 215 | if (rx->bitbuffer[rx->readpos]==0) { // check for startbit 216 | stop=(rx->readpos+1+paritybits+databits)%MODEM_BITBUFFER_SIZE; 217 | stop2=(rx->readpos+2+paritybits+databits)%MODEM_BITBUFFER_SIZE; 218 | if ( (rx->bitbuffer[stop]==1) && 219 | (stopbits==1 || (stopbits==2 && rx->bitbuffer[stop2]==1)) ) 220 | { // check for stopbit -> valid framing 221 | 222 | 223 | char byte=0; 224 | 225 | for(i=0; isession->lsb) { //lsb first 227 | if (rx->bitbuffer[(rx->readpos+1+i)%MODEM_BITBUFFER_SIZE]) 228 | byte |= (1<bitbuffer[(rx->readpos+databits-i)%MODEM_BITBUFFER_SIZE]) 231 | byte |= (1<bitbuffer[(rx->readpos + databits + 1)%MODEM_BITBUFFER_SIZE] == 237 | ( (rx->session->paritytype == 2) ^ __builtin_parity(byte) ) ) ) ) { 238 | 239 | send(rx->sock, &byte, 1, 0); 240 | } // else invalid parity, ignore byte 241 | // TODO - why does this increment by 10? 242 | rx->readpos=(rx->readpos+10)%MODEM_BITBUFFER_SIZE; 243 | rx->fill-=10; 244 | } else { // no valid framing (no stopbit), remove first bit and maybe try again 245 | rx->fill--; 246 | rx->readpos++; 247 | rx->readpos %= MODEM_BITBUFFER_SIZE; 248 | } 249 | } else { // no valid framing either (no startbit) 250 | rx->fill--; 251 | rx->readpos++; 252 | rx->readpos %= MODEM_BITBUFFER_SIZE; 253 | } 254 | } 255 | } 256 | } 257 | 258 | 259 | 260 | return; 261 | } 262 | 263 | // spandsp asks us for a bit to send onto the line 264 | static int modem_get_bit(void *user_data) { 265 | modem_data *tx = (modem_data*) user_data; 266 | char byte=0; 267 | int i, rc; 268 | 269 | int databits=tx->session->databits; 270 | int stopbits=tx->session->stopbits; 271 | int paritybits = 0; 272 | 273 | if (tx->session->paritytype) 274 | paritybits = 1; 275 | 276 | 277 | 278 | // no new data in send (bit)buffer, 279 | // either we just picked up the line, the terminal started to respond, 280 | // than we check for new data on the socket 281 | // or there's no new data, so we send 1s (mark) 282 | if (tx->writepos==tx->readpos) { 283 | if(tx->state->nulsent>0) { // connection is established, look for data on socket 284 | rc=recv(tx->sock,&byte, 1, 0); 285 | if (rc>0) { 286 | // new data on socket, we put that byte into our bitbuffer 287 | for (i=0; i<(databits+paritybits+stopbits); i++) { 288 | if (paritybits && (i == databits) ) { 289 | tx->bitbuffer[tx->writepos] = (tx->session->paritytype == 2) ^ __builtin_parity( byte); 290 | } else if ( i >= databits ) 291 | tx->bitbuffer[tx->writepos]=1; // stopbits 292 | else { // databits 293 | if (tx->session->lsb) { 294 | if (byte & (1<bitbuffer[tx->writepos]=1; 295 | else tx->bitbuffer[tx->writepos]=0; 296 | } else { 297 | if (byte & (1<<(databits-1-i))) tx->bitbuffer[tx->writepos]=1; 298 | else tx->bitbuffer[tx->writepos]=0; 299 | } 300 | } 301 | tx->writepos++; 302 | if (tx->writepos>=MODEM_BITBUFFER_SIZE) tx->writepos=0; 303 | } 304 | return 0; // return startbit immediately 305 | } 306 | else if (rc==0) { 307 | ast_log(LOG_WARNING,"Socket seems closed. Will hangup.\n"); 308 | tx->session->finished=1; 309 | } 310 | } 311 | else { 312 | // check if socket was closed before connection was terminated 313 | if ( recv(tx->sock,&byte, 1, MSG_PEEK) == 0 ) { 314 | tx->session->finished=1; 315 | return 1; 316 | } 317 | if ( tx->state->answertone>0 ) { 318 | // ast_log(LOG_WARNING,"Got TE's tone, will send null-byte.\n"); 319 | 320 | if (tx->session->sendnull) { // send null byte 321 | for (i=0; i<(databits+paritybits+stopbits); i++) { 322 | if (paritybits && (i == databits) ) 323 | tx->bitbuffer[tx->writepos] = (tx->session->paritytype == 2) ^ __builtin_parity( 0 ); // yes I know! 324 | else if (i>=databits) tx->bitbuffer[tx->writepos]=1; //stopbits 325 | else tx->bitbuffer[tx->writepos]=0; //databits 326 | tx->writepos++; 327 | if (tx->writepos>=MODEM_BITBUFFER_SIZE) tx->writepos=0; 328 | } 329 | } 330 | tx->state->nulsent=1; 331 | 332 | if (tx->session->ulmheader) { 333 | // send ULM relay protocol header, include connection speed 334 | float tx_baud,rx_baud; 335 | if (tx->session->version==VERSION_V21) { 336 | tx_baud=300; 337 | rx_baud=300; 338 | } else if (tx->session->version==VERSION_V23) { 339 | tx_baud=1200; 340 | rx_baud=75; 341 | } else if (tx->session->version==VERSION_BELL103) { 342 | tx_baud=300; 343 | rx_baud=300; 344 | } else if (tx->session->version==VERSION_V22) { 345 | tx_baud=1200; 346 | rx_baud=1200; 347 | } else if (tx->session->version==VERSION_V22BIS) { 348 | tx_baud=2400; 349 | rx_baud=2400; 350 | } else { 351 | tx_baud=0; 352 | rx_baud=0; 353 | } 354 | 355 | char header[60]; 356 | int headerlength=sprintf(header, 357 | "Version: 1\r\nTXspeed: %.2f\r\nRXspeed: %.2f\r\n\r\n", 358 | tx_baud/(1+databits+stopbits), rx_baud/(1+databits+stopbits)); 359 | send(tx->sock, header, headerlength, 0); 360 | } 361 | 362 | if (tx->session->sendnull) 363 | return 0; 364 | else 365 | return 1; 366 | } 367 | } 368 | 369 | // no new data on socket, NULL-byte already sent, send mark-frequency 370 | return 1; 371 | 372 | } else { 373 | // there still is data in the bitbuffer, so we just send that out 374 | i=tx->bitbuffer[tx->readpos]; 375 | tx->readpos++; 376 | if (tx->readpos>=MODEM_BITBUFFER_SIZE) tx->readpos=0; 377 | // printf("b%i",i); 378 | return i; 379 | } 380 | } 381 | 382 | static void *modem_generator_alloc(struct ast_channel *chan, void *params) 383 | { 384 | return params; 385 | } 386 | 387 | static int fsk_generator_generate(struct ast_channel *chan, void *data, int len, int samples) 388 | { 389 | fsk_tx_state_t *tx = (fsk_tx_state_t*) data; 390 | uint8_t buffer[AST_FRIENDLY_OFFSET + MAX_SAMPLES * sizeof(uint16_t)]; 391 | int16_t *buf = (int16_t *) (buffer + AST_FRIENDLY_OFFSET); 392 | 393 | struct ast_frame outf = { 394 | .frametype = AST_FRAME_VOICE, 395 | .subclass.format = ast_format_slin, 396 | .src = __FUNCTION__, 397 | }; 398 | 399 | if (samples > MAX_SAMPLES) { 400 | ast_log(LOG_WARNING, "Only generating %d samples, where %d requested\n", MAX_SAMPLES, samples); 401 | samples = MAX_SAMPLES; 402 | } 403 | 404 | 405 | if ((len = fsk_tx(tx, buf, samples)) > 0) { 406 | outf.samples = len; 407 | AST_FRAME_SET_BUFFER(&outf, buffer, AST_FRIENDLY_OFFSET, len * sizeof(int16_t)); 408 | 409 | if (ast_write(chan, &outf) < 0) { 410 | ast_log(LOG_WARNING, "Failed to write frame to %s: %s\n", ast_channel_name(chan), strerror(errno)); 411 | return -1; 412 | } 413 | } 414 | 415 | return 0; 416 | } 417 | 418 | 419 | static int v22_generator_generate(struct ast_channel *chan, void *data, int len, int samples) 420 | { 421 | v22bis_state_t *tx = (v22bis_state_t*) data; 422 | uint8_t buffer[AST_FRIENDLY_OFFSET + MAX_SAMPLES * sizeof(uint16_t)]; 423 | int16_t *buf = (int16_t *) (buffer + AST_FRIENDLY_OFFSET); 424 | 425 | struct ast_frame outf = { 426 | .frametype = AST_FRAME_VOICE, 427 | .subclass.format = ast_format_slin, 428 | .src = __FUNCTION__, 429 | }; 430 | 431 | if (samples > MAX_SAMPLES) { 432 | ast_log(LOG_WARNING, "Only generating %d samples, where %d requested\n", MAX_SAMPLES, samples); 433 | samples = MAX_SAMPLES; 434 | } 435 | 436 | 437 | if ((len = v22bis_tx(tx, buf, samples)) > 0) { 438 | outf.samples = len; 439 | AST_FRAME_SET_BUFFER(&outf, buffer, AST_FRIENDLY_OFFSET, len * sizeof(int16_t)); 440 | 441 | if (ast_write(chan, &outf) < 0) { 442 | ast_log(LOG_WARNING, "Failed to write frame to %s: %s\n", ast_channel_name(chan), strerror(errno)); 443 | return -1; 444 | } 445 | } 446 | 447 | return 0; 448 | } 449 | 450 | struct ast_generator fsk_generator = { 451 | alloc: modem_generator_alloc, 452 | generate: fsk_generator_generate, 453 | }; 454 | struct ast_generator v22_generator = { 455 | alloc: modem_generator_alloc, 456 | generate: v22_generator_generate, 457 | }; 458 | 459 | static int softmodem_communicate(modem_session *s) { 460 | int res = -1; 461 | struct ast_format *original_read_fmt; 462 | struct ast_format *original_write_fmt; 463 | 464 | modem_data rxdata, txdata; 465 | 466 | struct ast_frame *inf = NULL; 467 | 468 | fsk_tx_state_t *modem_tx; 469 | fsk_rx_state_t *modem_rx; 470 | 471 | v22bis_state_t *v22_modem; 472 | 473 | 474 | original_read_fmt = ast_channel_readformat(s->chan); 475 | if (original_read_fmt != ast_format_slin) { 476 | res=ast_set_read_format(s->chan, ast_format_slin); 477 | if (res < 0) { 478 | ast_log(LOG_WARNING, "Unable to set to linear read mode, giving up\n"); 479 | return res; 480 | } 481 | } 482 | 483 | original_write_fmt = ast_channel_writeformat(s->chan); 484 | if (original_write_fmt != ast_format_slin) { 485 | res=ast_set_write_format(s->chan, ast_format_slin); 486 | if (res < 0) { 487 | ast_log(LOG_WARNING, "Unable to set to linear write mode, giving up\n"); 488 | return res; 489 | } 490 | } 491 | 492 | int sock; 493 | struct sockaddr_in server; 494 | struct hostent *hp; 495 | struct ast_hostent ahp; 496 | 497 | sock = socket(AF_INET, SOCK_STREAM, 0); 498 | if(sock < 0) { 499 | ast_log(LOG_WARNING, "Could not create socket.\n"); 500 | return res; 501 | } 502 | 503 | server.sin_family=AF_INET; 504 | hp=ast_gethostbyname(s->host, &ahp); 505 | memcpy( (char *)&server.sin_addr, hp->h_addr, hp->h_length); 506 | //bcopy ( hp->h_addr, &(server.sin_addr.s_addr), hp->h_length); 507 | server.sin_port=htons(s->port); 508 | 509 | if (connect(sock, (struct sockaddr*)&server, sizeof(server)) < 0) { 510 | ast_log(LOG_WARNING, "Cannot connect to remote host.\n"); 511 | return res; 512 | } 513 | 514 | fcntl(sock, F_SETFL, O_NONBLOCK); 515 | 516 | 517 | connection_state state; 518 | state.answertone=-1; //no carrier yet 519 | state.nulsent=0; 520 | 521 | rxdata.sock=sock; 522 | rxdata.writepos=0; 523 | rxdata.readpos=0; 524 | rxdata.fill=0; 525 | rxdata.state= &state; 526 | rxdata.session=s; 527 | 528 | txdata.sock=sock; 529 | txdata.writepos=0; 530 | txdata.readpos=0; 531 | txdata.fill=0; 532 | txdata.state= &state; 533 | txdata.session=s; 534 | 535 | // initialise spandsp-stuff, give it our callback-functions 536 | if (s->version==VERSION_V21) { 537 | modem_tx = fsk_tx_init(NULL, &preset_fsk_specs[FSK_V21CH2], modem_get_bit, &txdata); 538 | modem_rx = fsk_rx_init(NULL, &preset_fsk_specs[FSK_V21CH1], TRUE, modem_put_bit, &rxdata); 539 | } 540 | else if (s->version==VERSION_V23) { 541 | modem_tx = fsk_tx_init(NULL, &preset_fsk_specs[FSK_V23CH1], modem_get_bit, &txdata); 542 | modem_rx = fsk_rx_init(NULL, &preset_fsk_specs[FSK_V23CH2], TRUE, modem_put_bit, &rxdata); 543 | } 544 | else if (s->version==VERSION_BELL103) { 545 | modem_tx = fsk_tx_init(NULL, &preset_fsk_specs[FSK_BELL103CH1], modem_get_bit, &txdata); 546 | modem_rx = fsk_rx_init(NULL, &preset_fsk_specs[FSK_BELL103CH2], TRUE, modem_put_bit, &rxdata); 547 | } 548 | else if (s->version==VERSION_V22) { 549 | v22_modem = v22bis_init(NULL, 1200, 0, FALSE, modem_get_bit, &txdata, modem_put_bit, &rxdata); 550 | } 551 | else if (s->version==VERSION_V22BIS) { 552 | v22_modem = v22bis_init(NULL, 2400, 0, FALSE, modem_get_bit, &txdata, modem_put_bit, &rxdata); 553 | } 554 | else { 555 | ast_log(LOG_ERROR,"Unsupported modem type. Sorry.\n"); 556 | return res; 557 | } 558 | 559 | if (s->version==VERSION_V21 || s->version==VERSION_V23 || s->version==VERSION_BELL103) { 560 | fsk_tx_power (modem_tx, s->txpower); 561 | fsk_rx_signal_cutoff(modem_rx, s->rxcutoff); 562 | } 563 | else if (s->version==VERSION_V22 || s->version==VERSION_V22BIS) { 564 | v22bis_tx_power(v22_modem, s->txpower); 565 | v22bis_rx_signal_cutoff(v22_modem, s->rxcutoff); 566 | } 567 | 568 | //printf("comm: baud %i\n",btx_tx->baud_rate); 569 | if (s->version==VERSION_V21 || s->version==VERSION_V23 || s->version==VERSION_BELL103) 570 | ast_activate_generator(s->chan, &fsk_generator, modem_tx); 571 | else if (s->version==VERSION_V22 || s->version==VERSION_V22BIS) 572 | ast_activate_generator(s->chan, &v22_generator, v22_modem); 573 | 574 | while (!s->finished) { 575 | res = ast_waitfor(s->chan, 20); 576 | if (res < 0) 577 | break; 578 | else if (res > 0) 579 | res = 0; 580 | 581 | inf = ast_read(s->chan); 582 | if (inf == NULL) { 583 | ast_debug(1, "Channel hangup\n"); 584 | res = -1; 585 | break; 586 | } 587 | 588 | /* Check the frame type. Format also must be checked because there is a chance 589 | that a frame in old format was already queued before we set chanel format 590 | to slinear so it will still be received by ast_read */ 591 | if (inf->frametype == AST_FRAME_VOICE && inf->subclass.format == ast_format_slin) { 592 | if (s->version==VERSION_V21 || s->version==VERSION_V23 || s->version==VERSION_BELL103) { 593 | if (fsk_rx(modem_rx, inf->data.ptr, inf->samples) < 0) { 594 | /* I know fax_rx never returns errors. The check here is for good style only */ 595 | ast_log(LOG_WARNING, "softmodem returned error\n"); 596 | res = -1; 597 | break; 598 | } 599 | } 600 | else if (s->version==VERSION_V22 || s->version==VERSION_V22BIS) { 601 | if (v22bis_rx(v22_modem, inf->data.ptr, inf->samples) < 0) { 602 | ast_log(LOG_WARNING, "softmodem returned error\n"); 603 | res = -1; 604 | break; 605 | } 606 | } 607 | } 608 | 609 | ast_frfree(inf); 610 | inf = NULL; 611 | } 612 | 613 | close(sock); 614 | 615 | if (s->version==VERSION_V22 || s->version==VERSION_V22BIS) { 616 | v22bis_release(v22_modem); 617 | v22bis_free(v22_modem); 618 | } 619 | 620 | if (original_write_fmt != ast_format_slin) { 621 | if (ast_set_write_format(s->chan, original_write_fmt) < 0) 622 | ast_log(LOG_WARNING, "Unable to restore write format on '%s'\n", ast_channel_name(s->chan)); 623 | } 624 | 625 | if (original_read_fmt != ast_format_slin) { 626 | if (ast_set_read_format(s->chan, original_read_fmt) < 0) 627 | ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", ast_channel_name(s->chan)); 628 | } 629 | 630 | return res; 631 | 632 | } 633 | 634 | static int softmodem_exec(struct ast_channel *chan, const char *data) { 635 | int res = 0; 636 | char *parse; 637 | modem_session session; 638 | struct ast_flags options = { 0, }; 639 | char *option_args[OPT_ARG_ARRAY_SIZE]; 640 | 641 | AST_DECLARE_APP_ARGS(args, 642 | AST_APP_ARG(host); 643 | AST_APP_ARG(port); 644 | AST_APP_ARG(options); 645 | ); 646 | 647 | if (chan == NULL) { 648 | ast_log(LOG_ERROR, "Channel is NULL. Giving up.\n"); 649 | return -1; 650 | } 651 | 652 | /* answer channel if not already answered */ 653 | if (ast_channel_state(chan) != AST_STATE_UP) { 654 | res = ast_answer(chan); 655 | if (res) { 656 | ast_log(LOG_WARNING, "Could not answer channel '%s'\n", ast_channel_name(chan)); 657 | return res; 658 | } 659 | } 660 | 661 | session.chan=chan; 662 | session.finished=0; 663 | session.rxcutoff=-35.0f; 664 | session.txpower=-28.0f; 665 | session.version=VERSION_V23; 666 | session.lsb=0; 667 | session.databits=8; 668 | session.stopbits=1; 669 | session.paritytype=0; 670 | session.ulmheader=0; 671 | session.sendnull=0; 672 | 673 | parse=ast_strdupa(data); 674 | AST_STANDARD_APP_ARGS(args,parse); 675 | 676 | if (args.host) 677 | session.host=args.host; 678 | else 679 | session.host="localhost"; 680 | 681 | if (args.port) { 682 | session.port=atoi(args.port); 683 | if ((session.port<0) || (session.port>65535)) { 684 | ast_log(LOG_ERROR, "Please specify a valid port number.\n"); 685 | return -1; 686 | } 687 | } else 688 | session.port=23; 689 | 690 | 691 | if (args.options) { 692 | ast_app_parse_options(additional_options, &options, option_args, args.options); 693 | 694 | if (ast_test_flag(&options, OPT_RX_CUTOFF)) { 695 | if (!ast_strlen_zero(option_args[OPT_ARG_RX_CUTOFF])) 696 | session.rxcutoff=atof(option_args[OPT_ARG_RX_CUTOFF]); 697 | } 698 | 699 | if (ast_test_flag(&options, OPT_TX_POWER)) { 700 | if (!ast_strlen_zero(option_args[OPT_ARG_TX_POWER])) 701 | session.txpower=atof(option_args[OPT_ARG_TX_POWER]); 702 | } 703 | 704 | if (ast_test_flag(&options, OPT_MODEM_VERSION)) { 705 | if (!ast_strlen_zero(option_args[OPT_ARG_MODEM_VERSION])) { 706 | if (strcmp(option_args[OPT_ARG_MODEM_VERSION],"V21")==0) 707 | session.version=VERSION_V21; 708 | else if (strcmp(option_args[OPT_ARG_MODEM_VERSION],"V23")==0) 709 | session.version=VERSION_V23; 710 | else if (strcmp(option_args[OPT_ARG_MODEM_VERSION],"Bell103")==0) 711 | session.version=VERSION_BELL103; 712 | else if (strcmp(option_args[OPT_ARG_MODEM_VERSION],"V22")==0) 713 | session.version=VERSION_V22; 714 | else if (strcmp(option_args[OPT_ARG_MODEM_VERSION],"V22bis")==0) 715 | session.version=VERSION_V22BIS; 716 | } 717 | } 718 | 719 | if (ast_test_flag(&options, OPT_LSB_FIRST)) { 720 | if (ast_test_flag(&options, OPT_MSB_FIRST)) { 721 | ast_log(LOG_ERROR, "Please only set l or m flag, not both.\n"); 722 | return -1; 723 | } 724 | session.lsb=1; 725 | } 726 | 727 | if (ast_test_flag(&options, OPT_DATABITS)) { 728 | if (!ast_strlen_zero(option_args[OPT_ARG_DATABITS])) { 729 | session.databits = atoi(option_args[OPT_ARG_DATABITS]); 730 | 731 | if ((session.databits<5) || (session.databits>8)) { 732 | ast_log(LOG_ERROR, "Only 5-8 data bits are supported.\n"); 733 | return -1; 734 | } 735 | } 736 | } 737 | 738 | if (ast_test_flag(&options, OPT_STOPBITS)) { 739 | if (!ast_strlen_zero(option_args[OPT_ARG_STOPBITS])) { 740 | session.stopbits = atoi(option_args[OPT_ARG_STOPBITS]); 741 | if ((session.stopbits<1) || (session.stopbits>2)) { 742 | ast_log(LOG_ERROR, "Only 1-2 stop bits are supported.\n"); 743 | return -1; 744 | } 745 | } 746 | } 747 | 748 | if (ast_test_flag(&options, OPT_EVEN_PARITY)) { 749 | if (ast_test_flag(&options, OPT_ODD_PARITY)) { 750 | ast_log(LOG_ERROR, "Please only set e or o (parity) flag, not both.\n"); 751 | return -1; 752 | } 753 | session.paritytype = 1; 754 | } 755 | if (ast_test_flag(&options, OPT_ODD_PARITY)) { 756 | session.paritytype = 2; 757 | } 758 | 759 | if (ast_test_flag(&options, OPT_ULM_HEADER)) { 760 | session.ulmheader = 1; 761 | } 762 | if (ast_test_flag(&options, OPT_NULL)) { 763 | session.sendnull = 1; 764 | } 765 | } 766 | 767 | res=softmodem_communicate(&session); 768 | 769 | return res; 770 | } 771 | 772 | 773 | 774 | static int unload_module(void) { 775 | return ast_unregister_application(app); 776 | } 777 | 778 | static int load_module(void) { 779 | return ast_register_application_xml(app, softmodem_exec); 780 | } 781 | 782 | 783 | AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Softmodem v22"); --------------------------------------------------------------------------------