67ecf16f8803742bdace36d917bcecf2b58c67f9
[asterisk/asterisk.git] / apps / app_sendfax.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2008, Digium, Inc.
5  *
6  * Dwayne M. Hubbard <dhubbard@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief SendFax application for res_fax
22  *
23  * \author\verbatim Dwayne M. Hubbard <dhubbard@digium.com> \endverbatim
24  * 
25  * This is a SendFax application for use with res_fax.  This is probably
26  * a throw away application, once res_fax works, it should probably be
27  * merged with app_fax.c
28  *
29  * \ingroup applications
30  */
31
32 /*** MODULEINFO
33         <defaultenabled>yes</defaultenabled>
34  ***/
35
36 #include "asterisk.h"
37
38 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
39
40 #include "asterisk/file.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/pbx.h"
43 #include "asterisk/module.h"
44 #include "asterisk/lock.h"
45 #include "asterisk/app.h"
46 #include "asterisk/res_fax.h"
47
48 static char *app = "SendFax";
49 static char *synopsis = 
50 "SendFax application for res_fax.";
51 static char *descrip = "This application is a ?temporary? SendFax application.\n"
52  " It uses the res_fax module.\n";
53
54 enum {
55         OPTION_A = (1 << 0),
56         OPTION_B = (1 << 1),
57         OPTION_C = (1 << 2),
58 } option_flags;
59
60 enum {
61         OPTION_ARG_B = 0,
62         OPTION_ARG_C = 1,
63         /* This *must* be the last value in this enum! */
64         OPTION_ARG_ARRAY_SIZE = 2,
65 } option_args;
66
67 AST_APP_OPTIONS(app_opts,{
68         AST_APP_OPTION('a', OPTION_A),
69         AST_APP_OPTION_ARG('b', OPTION_B, OPTION_ARG_B),
70         AST_APP_OPTION_ARG('c', OPTION_C, OPTION_ARG_C),
71 });
72
73
74 static int app_exec(struct ast_channel *chan, void *data)
75 {
76         int ms = 1000;
77         int timeout = 5000; /* 5 seconds */
78         int res = 0;
79         struct ast_frame *frame = NULL;
80         struct ast_fax_session *session;
81         struct ast_fax_requirements *sessionreqs;
82         enum ast_control_t38 t38control;
83         struct ast_flags flags;
84         char *parse, *opts[OPTION_ARG_ARRAY_SIZE];
85         AST_DECLARE_APP_ARGS(args,
86                 AST_APP_ARG(dummy);
87                 AST_APP_ARG(options);
88         );
89
90         if (ast_strlen_zero(data)) {
91                 ast_log(LOG_WARNING, "%s requires an argument (dummy[,options])\n", app);
92                 return -1;
93         }
94
95         /* set the fax requirements */  
96         sessionreqs = ast_calloc(1, sizeof(*sessionreqs));
97         if (!sessionreqs) {
98                 ast_log(LOG_ERROR, "system error\n");
99                 return -1;
100         }
101         sessionreqs->type = FAX_SESSION_MULAW;
102         sessionreqs->operation = FAX_SESSION_OP_SEND;
103
104         /* obtain a fax resource that can handle said requirements */
105         session = ast_fax_session_get(sessionreqs);
106         if (!session) {
107                 ast_log(LOG_ERROR, "failed to retrieve a fax session that is capable of the session requirements.\n");
108                 ast_free(sessionreqs);
109                 return -1;
110         }
111         session->headerinfo = ast_strdup(pbx_builtin_getvar_helper(chan, "LOCALHEADERINFO"));
112         session->localstationid = ast_strdup(pbx_builtin_getvar_helper(chan, "LOCALSTATIONID"));
113         
114         t38control = AST_T38_REQUEST_NEGOTIATE;
115         ast_indicate_data(chan, AST_CONTROL_T38, &t38control, sizeof(t38control));
116
117         /* if you feed it, it will fax */       
118         while (ms > 0) {
119                 ms = ast_waitfor(chan, ms);
120                 if (ms < 0) {
121                         /* bad stuff happened */
122                         res = ms;
123                         break;
124                 }
125                 if (!ms) {
126                         /* nothing happened */
127                         if (timeout > 0) {
128                                 timeout -= 1000;
129                                 ms = 1000;
130                                 continue;
131                         } else {
132                                 ast_log(LOG_WARNING, "channel '%s' timed-out waiting to receive the fax.\n", chan->name);
133                                 break;
134                         }
135                 }
136                 if (!(frame = ast_read(chan))) {
137                         ast_log(LOG_DEBUG, "channel '%s' failed to read frames.\n", chan->name);
138                         res = -1;
139                         break;
140                 }
141                 if (frame->frametype == AST_FRAME_CONTROL && frame->subclass == AST_CONTROL_T38 && frame->datalen == sizeof(enum ast_control_t38)) {
142                         t38control =*((enum ast_control_t38 *) frame->data.ptr);
143                         if (t38control == AST_T38_NEGOTIATED) {
144                                 ast_log(LOG_NOTICE, "yay T38 negotiated!\n");
145                                 session->start(session);
146                         } else ast_log(LOG_NOTICE, "T38 control frame: %d\n", t38control);
147                 }
148                 ast_frfree(frame);
149         }
150         /* We need to make a copy of the input string if we are going to modify it! */
151         parse = ast_strdupa(data);
152
153         AST_STANDARD_APP_ARGS(args, parse);
154
155         if (args.argc == 2)
156                 ast_app_parse_options(app_opts, &flags, opts, args.options);
157
158         if (!ast_strlen_zero(args.dummy)) 
159                 ast_log(LOG_NOTICE, "Dummy value is : %s\n", args.dummy);
160
161         if (ast_test_flag(&flags, OPTION_A))
162                 ast_log(LOG_NOTICE, "Option A is set\n");
163
164         if (ast_test_flag(&flags, OPTION_B))
165                 ast_log(LOG_NOTICE, "Option B is set with : %s\n", opts[OPTION_ARG_B] ? opts[OPTION_ARG_B] : "<unspecified>");
166
167         if (ast_test_flag(&flags, OPTION_C))
168                 ast_log(LOG_NOTICE, "Option C is set with : %s\n", opts[OPTION_ARG_C] ? opts[OPTION_ARG_C] : "<unspecified>");
169
170         return res;
171 }
172
173 static int unload_module(void)
174 {
175         return ast_unregister_application(app);
176 }
177
178 static int load_module(void)
179 {
180         return ast_register_application(app, app_exec, synopsis, descrip) ? 
181                 AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
182 }
183
184 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SendFax Application for res_fax");