app_rpt.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. /** @file app_rpt.c
  2. *
  3. * Asterisk -- A telephony toolkit for Linux.
  4. *
  5. * Radio Repeater program
  6. *
  7. * Copyright (C) 2002, Jim Dixon
  8. *
  9. * Jim Dixon <jim@lambdatel.com>
  10. *
  11. * This program is free software, distributed under the terms of
  12. * the GNU General Public License
  13. *
  14. */
  15. #include <asterisk/lock.h>
  16. #include <asterisk/file.h>
  17. #include <asterisk/logger.h>
  18. #include <asterisk/channel.h>
  19. #include <asterisk/pbx.h>
  20. #include <asterisk/module.h>
  21. #include <asterisk/translate.h>
  22. #include <asterisk/options.h>
  23. #include <asterisk/config.h>
  24. #include <stdio.h>
  25. #include <unistd.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include <pthread.h>
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <errno.h>
  32. #include <dirent.h>
  33. #include <ctype.h>
  34. #include <sys/stat.h>
  35. #include <sys/time.h>
  36. #include <sys/file.h>
  37. #include <sys/ioctl.h>
  38. #include <math.h>
  39. #include <tonezone.h>
  40. #include <linux/zaptel.h>
  41. static char *tdesc = "Radio Repeater";
  42. static int debug = 0;
  43. STANDARD_LOCAL_USER;
  44. LOCAL_USER_DECL;
  45. #define MSWAIT 200
  46. #define HANGTIME 5000
  47. #define TOTIME 180000
  48. #define IDTIME 300000
  49. #define MAXRPTS 20
  50. static pthread_t rpt_master_thread;
  51. static struct rpt
  52. {
  53. char *name;
  54. char *rxchanname;
  55. char *txchanname;
  56. char *ourcontext;
  57. char *ourcallerid;
  58. char *acctcode;
  59. char *idrecording;
  60. int hangtime;
  61. int totime;
  62. int idtime;
  63. struct ast_channel *rxchannel,*txchannel,*pchannel;
  64. int tailtimer,totimer,idtimer,txconf,pconf,callmode,cidx;
  65. pthread_t rpt_id_thread,rpt_term_thread,rpt_proc_thread,rpt_call_thread;
  66. char mydtmf,iding,terming;
  67. char exten[AST_MAX_EXTENSION];
  68. } rpt_vars[MAXRPTS];
  69. static void *rpt_id(void *this)
  70. {
  71. ZT_CONFINFO ci; /* conference info */
  72. int res;
  73. struct rpt *myrpt = (struct rpt *)this;
  74. struct ast_channel *mychannel;
  75. /* allocate a pseudo-channel thru asterisk */
  76. mychannel = ast_request("zap",AST_FORMAT_SLINEAR,"pseudo");
  77. if (!mychannel)
  78. {
  79. fprintf(stderr,"rpt:Sorry unable to obtain pseudo channel\n");
  80. pthread_exit(NULL);
  81. }
  82. /* make a conference for the tx */
  83. ci.chan = 0;
  84. ci.confno = myrpt->txconf; /* use the tx conference */
  85. ci.confmode = ZT_CONF_CONFANN;
  86. /* first put the channel on the conference in announce mode */
  87. if (ioctl(mychannel->fds[0],ZT_SETCONF,&ci) == -1)
  88. {
  89. ast_log(LOG_WARNING, "Unable to set conference mode to Announce\n");
  90. pthread_exit(NULL);
  91. }
  92. myrpt->iding = 1;
  93. ast_stopstream(mychannel);
  94. res = ast_streamfile(mychannel, myrpt->idrecording, mychannel->language);
  95. if (!res)
  96. res = ast_waitstream(mychannel, "");
  97. else {
  98. ast_log(LOG_WARNING, "ast_streamfile failed on %s\n", mychannel->name);
  99. res = 0;
  100. }
  101. myrpt->iding = 0;
  102. ast_stopstream(mychannel);
  103. ast_hangup(mychannel);
  104. pthread_exit(NULL);
  105. }
  106. static void *rpt_proc(void *this)
  107. {
  108. ZT_CONFINFO ci; /* conference info */
  109. int res;
  110. struct rpt *myrpt = (struct rpt *)this;
  111. struct ast_channel *mychannel;
  112. /* wait a little bit */
  113. usleep(1500000);
  114. /* allocate a pseudo-channel thru asterisk */
  115. mychannel = ast_request("zap",AST_FORMAT_SLINEAR,"pseudo");
  116. if (!mychannel)
  117. {
  118. fprintf(stderr,"rpt:Sorry unable to obtain pseudo channel\n");
  119. pthread_exit(NULL);
  120. }
  121. /* make a conference for the tx */
  122. ci.chan = 0;
  123. ci.confno = myrpt->pconf; /* use the tx conference */
  124. ci.confmode = ZT_CONF_CONFANN;
  125. /* first put the channel on the conference in announce mode */
  126. if (ioctl(mychannel->fds[0],ZT_SETCONF,&ci) == -1)
  127. {
  128. ast_log(LOG_WARNING, "Unable to set conference mode to Announce\n");
  129. pthread_exit(NULL);
  130. }
  131. myrpt->terming = 1;
  132. ast_stopstream(mychannel);
  133. res = ast_streamfile(mychannel, "callproceeding", mychannel->language);
  134. if (!res)
  135. res = ast_waitstream(mychannel, "");
  136. else {
  137. ast_log(LOG_WARNING, "ast_streamfile failed on %s\n", mychannel->name);
  138. res = 0;
  139. }
  140. myrpt->terming = 0;
  141. ast_stopstream(mychannel);
  142. ast_hangup(mychannel);
  143. pthread_exit(NULL);
  144. }
  145. static void *rpt_term(void *this)
  146. {
  147. ZT_CONFINFO ci; /* conference info */
  148. int res;
  149. struct rpt *myrpt = (struct rpt *)this;
  150. struct ast_channel *mychannel;
  151. /* wait a little bit */
  152. usleep(1500000);
  153. /* allocate a pseudo-channel thru asterisk */
  154. mychannel = ast_request("zap",AST_FORMAT_SLINEAR,"pseudo");
  155. if (!mychannel)
  156. {
  157. fprintf(stderr,"rpt:Sorry unable to obtain pseudo channel\n");
  158. pthread_exit(NULL);
  159. }
  160. /* make a conference for the tx */
  161. ci.chan = 0;
  162. ci.confno = myrpt->pconf; /* use the tx conference */
  163. ci.confmode = ZT_CONF_CONFANN;
  164. /* first put the channel on the conference in announce mode */
  165. if (ioctl(mychannel->fds[0],ZT_SETCONF,&ci) == -1)
  166. {
  167. ast_log(LOG_WARNING, "Unable to set conference mode to Announce\n");
  168. pthread_exit(NULL);
  169. }
  170. myrpt->terming = 1;
  171. ast_stopstream(mychannel);
  172. res = ast_streamfile(mychannel, "callterminated", mychannel->language);
  173. if (!res)
  174. res = ast_waitstream(mychannel, "");
  175. else {
  176. ast_log(LOG_WARNING, "ast_streamfile failed on %s\n", mychannel->name);
  177. res = 0;
  178. }
  179. myrpt->terming = 0;
  180. ast_stopstream(mychannel);
  181. ast_hangup(mychannel);
  182. pthread_exit(NULL);
  183. }
  184. static void *rpt_call(void *this)
  185. {
  186. ZT_CONFINFO ci; /* conference info */
  187. struct rpt *myrpt = (struct rpt *)this;
  188. int res;
  189. struct ast_frame *f,wf;
  190. int stopped,congstarted;
  191. struct ast_channel *mychannel,*genchannel;
  192. myrpt->mydtmf = 0;
  193. /* allocate a pseudo-channel thru asterisk */
  194. mychannel = ast_request("zap",AST_FORMAT_SLINEAR,"pseudo");
  195. if (!mychannel)
  196. {
  197. fprintf(stderr,"rpt:Sorry unable to obtain pseudo channel\n");
  198. pthread_exit(NULL);
  199. }
  200. ci.chan = 0;
  201. ci.confno = myrpt->pconf; /* use the pseudo conference */
  202. ci.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER
  203. | ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
  204. /* first put the channel on the conference */
  205. if (ioctl(mychannel->fds[0],ZT_SETCONF,&ci) == -1)
  206. {
  207. ast_log(LOG_WARNING, "Unable to set conference mode to Announce\n");
  208. ast_hangup(mychannel);
  209. myrpt->callmode = 0;
  210. pthread_exit(NULL);
  211. }
  212. /* allocate a pseudo-channel thru asterisk */
  213. genchannel = ast_request("zap",AST_FORMAT_SLINEAR,"pseudo");
  214. if (!genchannel)
  215. {
  216. fprintf(stderr,"rpt:Sorry unable to obtain pseudo channel\n");
  217. ast_hangup(mychannel);
  218. pthread_exit(NULL);
  219. }
  220. ci.chan = 0;
  221. ci.confno = myrpt->pconf;
  222. ci.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER
  223. | ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
  224. /* first put the channel on the conference */
  225. if (ioctl(genchannel->fds[0],ZT_SETCONF,&ci) == -1)
  226. {
  227. ast_log(LOG_WARNING, "Unable to set conference mode to Announce\n");
  228. ast_hangup(mychannel);
  229. ast_hangup(genchannel);
  230. myrpt->callmode = 0;
  231. pthread_exit(NULL);
  232. }
  233. /* start dialtone */
  234. if (tone_zone_play_tone(mychannel->fds[0],ZT_TONE_DIALTONE) < 0)
  235. {
  236. ast_log(LOG_WARNING, "Cannot start dialtone\n");
  237. ast_hangup(mychannel);
  238. ast_hangup(genchannel);
  239. myrpt->callmode = 0;
  240. pthread_exit(NULL);
  241. }
  242. stopped = 0;
  243. congstarted = 0;
  244. while ((myrpt->callmode == 1) || (myrpt->callmode == 4))
  245. {
  246. if ((myrpt->callmode == 1) && (myrpt->cidx > 0) && (!stopped))
  247. {
  248. stopped = 1;
  249. /* stop dial tone */
  250. tone_zone_play_tone(mychannel->fds[0],-1);
  251. }
  252. if ((myrpt->callmode == 4) && (!congstarted))
  253. {
  254. congstarted = 1;
  255. /* start congestion tone */
  256. tone_zone_play_tone(mychannel->fds[0],ZT_TONE_CONGESTION);
  257. }
  258. res = ast_waitfor(mychannel, MSWAIT);
  259. if (res < 0)
  260. {
  261. ast_hangup(mychannel);
  262. ast_hangup(genchannel);
  263. myrpt->callmode = 0;
  264. pthread_exit(NULL);
  265. }
  266. if (res == 0) continue;
  267. f = ast_read(mychannel);
  268. if (f == NULL)
  269. {
  270. ast_hangup(mychannel);
  271. ast_hangup(genchannel);
  272. myrpt->callmode = 0;
  273. pthread_exit(NULL);
  274. }
  275. if ((f->frametype == AST_FRAME_CONTROL) &&
  276. (f->subclass == AST_CONTROL_HANGUP))
  277. {
  278. ast_frfree(f);
  279. ast_hangup(mychannel);
  280. ast_hangup(genchannel);
  281. myrpt->callmode = 0;
  282. pthread_exit(NULL);
  283. }
  284. ast_frfree(f);
  285. }
  286. /* stop any tone generation */
  287. tone_zone_play_tone(mychannel->fds[0],-1);
  288. /* end if done */
  289. if (!myrpt->callmode)
  290. {
  291. ast_hangup(mychannel);
  292. ast_hangup(genchannel);
  293. myrpt->callmode = 0;
  294. pthread_exit(NULL);
  295. }
  296. if (myrpt->ourcallerid && *myrpt->ourcallerid)
  297. {
  298. if (mychannel->callerid) free(mychannel->callerid);
  299. mychannel->callerid = strdup(myrpt->ourcallerid);
  300. }
  301. strcpy(mychannel->exten,myrpt->exten);
  302. strcpy(mychannel->context,myrpt->ourcontext);
  303. if (myrpt->acctcode)
  304. strcpy(mychannel->accountcode,myrpt->acctcode);
  305. mychannel->priority = 1;
  306. ast_channel_undefer_dtmf(mychannel);
  307. if (ast_pbx_start(mychannel) < 0)
  308. {
  309. ast_log(LOG_WARNING, "Unable to start PBX!!\n");
  310. ast_hangup(mychannel);
  311. ast_hangup(genchannel);
  312. myrpt->callmode = 0;
  313. pthread_exit(NULL);
  314. }
  315. myrpt->callmode = 3;
  316. while(myrpt->callmode)
  317. {
  318. if ((!mychannel->pvt) && (myrpt->callmode != 4))
  319. {
  320. myrpt->callmode = 4;
  321. /* start congestion tone */
  322. tone_zone_play_tone(genchannel->fds[0],ZT_TONE_CONGESTION);
  323. }
  324. if (myrpt->mydtmf)
  325. {
  326. wf.frametype = AST_FRAME_DTMF;
  327. wf.subclass = myrpt->mydtmf;
  328. wf.offset = 0;
  329. wf.mallocd = 0;
  330. wf.data = NULL;
  331. wf.datalen = 0;
  332. wf.samples = 0;
  333. ast_write(genchannel,&wf);
  334. myrpt->mydtmf = 0;
  335. }
  336. usleep(25000);
  337. }
  338. tone_zone_play_tone(genchannel->fds[0],-1);
  339. if (mychannel->pvt) ast_softhangup(mychannel,AST_SOFTHANGUP_DEV);
  340. ast_hangup(genchannel);
  341. myrpt->callmode = 0;
  342. pthread_exit(NULL);
  343. }
  344. /* single thread with one file (request) to dial */
  345. static void *rpt(void *this)
  346. {
  347. struct rpt *myrpt = (struct rpt *)this;
  348. char *tele;
  349. int ms = MSWAIT,lasttx,keyed,val;
  350. struct ast_channel *who;
  351. ZT_CONFINFO ci; /* conference info */
  352. pthread_attr_t attr;
  353. tele = strchr(myrpt->rxchanname,'/');
  354. if (!tele)
  355. {
  356. fprintf(stderr,"rpt:Dial number must be in format tech/number\n");
  357. pthread_exit(NULL);
  358. }
  359. *tele++ = 0;
  360. myrpt->rxchannel = ast_request(myrpt->rxchanname,AST_FORMAT_SLINEAR,tele);
  361. if (myrpt->rxchannel)
  362. {
  363. ast_set_read_format(myrpt->rxchannel,AST_FORMAT_SLINEAR);
  364. ast_set_write_format(myrpt->rxchannel,AST_FORMAT_SLINEAR);
  365. myrpt->rxchannel->whentohangup = 0;
  366. myrpt->rxchannel->appl = "Apprpt";
  367. myrpt->rxchannel->data = "(Repeater Rx)";
  368. if (option_verbose > 2)
  369. ast_verbose(VERBOSE_PREFIX_3 "rpt (Rx) initiating call to %s/%s on %s\n",
  370. myrpt->rxchanname,tele,myrpt->rxchannel->name);
  371. ast_call(myrpt->rxchannel,tele,999);
  372. }
  373. else
  374. {
  375. fprintf(stderr,"rpt:Sorry unable to obtain channel\n");
  376. pthread_exit(NULL);
  377. }
  378. if (myrpt->txchanname)
  379. {
  380. tele = strchr(myrpt->txchanname,'/');
  381. if (!tele)
  382. {
  383. fprintf(stderr,"rpt:Dial number must be in format tech/number\n");
  384. pthread_exit(NULL);
  385. }
  386. *tele++ = 0;
  387. myrpt->txchannel = ast_request(myrpt->txchanname,AST_FORMAT_SLINEAR,tele);
  388. if (myrpt->txchannel)
  389. {
  390. ast_set_read_format(myrpt->txchannel,AST_FORMAT_SLINEAR);
  391. ast_set_write_format(myrpt->txchannel,AST_FORMAT_SLINEAR);
  392. myrpt->txchannel->whentohangup = 0;
  393. myrpt->txchannel->appl = "Apprpt";
  394. myrpt->txchannel->data = "(Repeater Rx)";
  395. if (option_verbose > 2)
  396. ast_verbose(VERBOSE_PREFIX_3 "rpt (Tx) initiating call to %s/%s on %s\n",
  397. myrpt->txchanname,tele,myrpt->txchannel->name);
  398. ast_call(myrpt->txchannel,tele,999);
  399. }
  400. else
  401. {
  402. fprintf(stderr,"rpt:Sorry unable to obtain channel\n");
  403. pthread_exit(NULL);
  404. }
  405. }
  406. else
  407. {
  408. myrpt->txchannel = myrpt->rxchannel;
  409. }
  410. /* allocate a pseudo-channel thru asterisk */
  411. myrpt->pchannel = ast_request("zap",AST_FORMAT_SLINEAR,"pseudo");
  412. if (!myrpt->pchannel)
  413. {
  414. fprintf(stderr,"rpt:Sorry unable to obtain pseudo channel\n");
  415. pthread_exit(NULL);
  416. }
  417. /* make a conference for the tx */
  418. ci.chan = 0;
  419. ci.confno = -1; /* make a new conf */
  420. ci.confmode = ZT_CONF_CONF | ZT_CONF_LISTENER;
  421. /* first put the channel on the conference in announce mode */
  422. if (ioctl(myrpt->txchannel->fds[0],ZT_SETCONF,&ci) == -1)
  423. {
  424. ast_log(LOG_WARNING, "Unable to set conference mode to Announce\n");
  425. pthread_exit(NULL);
  426. }
  427. /* save tx conference number */
  428. myrpt->txconf = ci.confno;
  429. /* make a conference for the pseudo */
  430. ci.chan = 0;
  431. ci.confno = -1; /* make a new conf */
  432. ci.confmode = ZT_CONF_CONFANNMON;
  433. /* first put the channel on the conference in announce mode */
  434. if (ioctl(myrpt->pchannel->fds[0],ZT_SETCONF,&ci) == -1)
  435. {
  436. ast_log(LOG_WARNING, "Unable to set conference mode to Announce\n");
  437. pthread_exit(NULL);
  438. }
  439. /* save pseudo channel conference number */
  440. myrpt->pconf = ci.confno;
  441. /* Now, the idea here is to copy from the physical rx channel buffer
  442. into the pseudo tx buffer, and from the pseudo rx buffer into the
  443. tx channel buffer */
  444. myrpt->tailtimer = 0;
  445. myrpt->totimer = 0;
  446. myrpt->idtimer = 0;
  447. lasttx = 0;
  448. keyed = 0;
  449. myrpt->callmode = 0;
  450. val = 0;
  451. ast_channel_setoption(myrpt->rxchannel,AST_OPTION_TONE_VERIFY,&val,sizeof(char),0);
  452. val = 1;
  453. ast_channel_setoption(myrpt->rxchannel,AST_OPTION_RELAXDTMF,&val,sizeof(char),0);
  454. while (ms >= 0)
  455. {
  456. struct ast_frame *f;
  457. struct ast_channel *cs[3];
  458. int totx,elap;
  459. totx = (keyed || myrpt->callmode || myrpt->iding || myrpt->terming);
  460. if (!totx) myrpt->totimer = myrpt->totime;
  461. else myrpt->tailtimer = myrpt->hangtime;
  462. totx = (totx || myrpt->tailtimer) && myrpt->totimer;
  463. /* if wants to transmit and in phone call, but timed out,
  464. reset time-out timer if keyed */
  465. if ((!totx) && (!myrpt->totimer) && myrpt->callmode && keyed)
  466. {
  467. myrpt->totimer = myrpt->totime;
  468. continue;
  469. }
  470. if (totx && (!myrpt->idtimer))
  471. {
  472. myrpt->idtimer = myrpt->idtime;
  473. pthread_attr_init(&attr);
  474. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  475. pthread_create(&myrpt->rpt_id_thread,&attr,rpt_id,(void *) myrpt);
  476. }
  477. if (totx && (!lasttx))
  478. {
  479. lasttx = 1;
  480. ast_indicate(myrpt->txchannel,AST_CONTROL_RADIO_KEY);
  481. }
  482. if ((!totx) && lasttx)
  483. {
  484. lasttx = 0;
  485. ast_indicate(myrpt->txchannel,AST_CONTROL_RADIO_UNKEY);
  486. }
  487. cs[0] = myrpt->rxchannel;
  488. cs[1] = myrpt->pchannel;
  489. cs[2] = myrpt->txchannel;
  490. ms = MSWAIT;
  491. who = ast_waitfor_n(cs,3,&ms);
  492. if (who == NULL) ms = 0;
  493. elap = MSWAIT - ms;
  494. if (myrpt->tailtimer) myrpt->tailtimer -= elap;
  495. if (myrpt->tailtimer < 0) myrpt->tailtimer = 0;
  496. if (myrpt->totimer) myrpt->totimer -= elap;
  497. if (myrpt->totimer < 0) myrpt->totimer = 0;
  498. if (myrpt->idtimer) myrpt->idtimer -= elap;
  499. if (myrpt->idtimer < 0) myrpt->idtimer = 0;
  500. if (!ms) continue;
  501. if (who == myrpt->rxchannel) /* if it was a read from rx */
  502. {
  503. f = ast_read(myrpt->rxchannel);
  504. if (!f)
  505. {
  506. if (debug) printf("@@@@ rpt:Hung Up\n");
  507. break;
  508. }
  509. if (f->frametype == AST_FRAME_VOICE)
  510. {
  511. ast_write(myrpt->pchannel,f);
  512. }
  513. else if (f->frametype == AST_FRAME_DTMF)
  514. {
  515. char c;
  516. c = (char) f->subclass; /* get DTMF char */
  517. if ((!myrpt->callmode) && (c == '*'))
  518. {
  519. myrpt->callmode = 1;
  520. myrpt->cidx = 0;
  521. myrpt->exten[myrpt->cidx] = 0;
  522. pthread_attr_init(&attr);
  523. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  524. pthread_create(&myrpt->rpt_call_thread,&attr,rpt_call,(void *)myrpt);
  525. continue;
  526. }
  527. if (myrpt->callmode && (c == '#'))
  528. {
  529. myrpt->callmode = 0;
  530. pthread_attr_init(&attr);
  531. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  532. pthread_create(&myrpt->rpt_term_thread,&attr,rpt_term,(void *) myrpt);
  533. continue;
  534. }
  535. if (myrpt->callmode == 1)
  536. {
  537. myrpt->exten[myrpt->cidx++] = c;
  538. myrpt->exten[myrpt->cidx] = 0;
  539. /* if this exists */
  540. if (ast_exists_extension(myrpt->pchannel,myrpt->ourcontext,myrpt->exten,1,NULL))
  541. {
  542. myrpt->callmode = 2;
  543. pthread_attr_init(&attr);
  544. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  545. pthread_create(&myrpt->rpt_proc_thread,&attr,rpt_proc,(void *) myrpt);
  546. }
  547. /* if can continue, do so */
  548. if (ast_canmatch_extension(myrpt->pchannel,myrpt->ourcontext,myrpt->exten,1,NULL)) continue;
  549. /* call has failed, inform user */
  550. myrpt->callmode = 4;
  551. continue;
  552. }
  553. if ((myrpt->callmode == 2) || (myrpt->callmode == 3))
  554. {
  555. myrpt->mydtmf = f->subclass;
  556. }
  557. }
  558. else if (f->frametype == AST_FRAME_CONTROL)
  559. {
  560. if (f->subclass == AST_CONTROL_HANGUP)
  561. {
  562. if (debug) printf("@@@@ rpt:Hung Up\n");
  563. ast_frfree(f);
  564. break;
  565. }
  566. /* if RX key */
  567. if (f->subclass == AST_CONTROL_RADIO_KEY)
  568. {
  569. if (debug) printf("@@@@ rx key\n");
  570. keyed = 1;
  571. }
  572. /* if RX un-key */
  573. if (f->subclass == AST_CONTROL_RADIO_UNKEY)
  574. {
  575. if (debug) printf("@@@@ rx un-key\n");
  576. keyed = 0;
  577. }
  578. }
  579. ast_frfree(f);
  580. }
  581. if (who == myrpt->pchannel) /* if it was a read from pseudo */
  582. {
  583. f = ast_read(myrpt->pchannel);
  584. if (!f)
  585. {
  586. if (debug) printf("@@@@ rpt:Hung Up\n");
  587. break;
  588. }
  589. if (f->frametype == AST_FRAME_VOICE)
  590. {
  591. ast_write(myrpt->txchannel,f);
  592. }
  593. if (f->frametype == AST_FRAME_CONTROL)
  594. {
  595. if (f->subclass == AST_CONTROL_HANGUP)
  596. {
  597. if (debug) printf("@@@@ rpt:Hung Up\n");
  598. ast_frfree(f);
  599. break;
  600. }
  601. }
  602. ast_frfree(f);
  603. }
  604. if (who == myrpt->txchannel) /* if it was a read from tx */
  605. {
  606. f = ast_read(myrpt->txchannel);
  607. if (!f)
  608. {
  609. if (debug) printf("@@@@ rpt:Hung Up\n");
  610. break;
  611. }
  612. if (f->frametype == AST_FRAME_CONTROL)
  613. {
  614. if (f->subclass == AST_CONTROL_HANGUP)
  615. {
  616. if (debug) printf("@@@@ rpt:Hung Up\n");
  617. ast_frfree(f);
  618. break;
  619. }
  620. }
  621. ast_frfree(f);
  622. }
  623. }
  624. ast_hangup(myrpt->pchannel);
  625. ast_hangup(myrpt->rxchannel);
  626. ast_hangup(myrpt->txchannel);
  627. if (debug) printf("@@@@ rpt:Hung up channel\n");
  628. pthread_exit(NULL);
  629. return NULL;
  630. }
  631. static void *rpt_master(void *ignore)
  632. {
  633. struct ast_config *cfg;
  634. char *this,*val;
  635. int i,n;
  636. /* start with blank config */
  637. memset(&rpt_vars,0,sizeof(rpt_vars));
  638. cfg = ast_load("rpt.conf");
  639. if (!cfg) {
  640. ast_log(LOG_NOTICE, "Unable to open radio repeater configuration rpt.conf. Radio Repeater disabled.\n");
  641. pthread_exit(NULL);
  642. }
  643. /* go thru all the specified repeaters */
  644. this = NULL;
  645. n = 0;
  646. while((this = ast_category_browse(cfg,this)) != NULL)
  647. {
  648. ast_log(LOG_DEBUG,"Loading config for repeater %s\n",this);
  649. rpt_vars[n].name = this;
  650. rpt_vars[n].rxchanname = ast_variable_retrieve(cfg,this,"rxchannel");
  651. rpt_vars[n].txchanname = ast_variable_retrieve(cfg,this,"txchannel");
  652. rpt_vars[n].ourcontext = ast_variable_retrieve(cfg,this,"context");
  653. if (!rpt_vars[n].ourcontext) rpt_vars[n].ourcontext = this;
  654. rpt_vars[n].ourcallerid = ast_variable_retrieve(cfg,this,"callerid");
  655. rpt_vars[n].acctcode = ast_variable_retrieve(cfg,this,"accountcode");
  656. rpt_vars[n].idrecording = ast_variable_retrieve(cfg,this,"idrecording");
  657. val = ast_variable_retrieve(cfg,this,"hangtime");
  658. if (val) rpt_vars[n].hangtime = atoi(val);
  659. else rpt_vars[n].hangtime = HANGTIME;
  660. val = ast_variable_retrieve(cfg,this,"totime");
  661. if (val) rpt_vars[n].totime = atoi(val);
  662. else rpt_vars[n].totime = TOTIME;
  663. val = ast_variable_retrieve(cfg,this,"idtime");
  664. if (val) rpt_vars[n].idtime = atoi(val);
  665. else rpt_vars[n].idtime = IDTIME;
  666. n++;
  667. }
  668. ast_log(LOG_DEBUG, "Total of %d repeaters configured.\n",n);
  669. /* start em all */
  670. for(i = 0; i < n; i++)
  671. {
  672. if (!rpt_vars[i].rxchanname)
  673. {
  674. ast_log(LOG_WARNING,"Did not specify rxchanname for repeater %s\n",rpt_vars[i].name);
  675. pthread_exit(NULL);
  676. }
  677. if (!rpt_vars[i].idrecording)
  678. {
  679. ast_log(LOG_WARNING,"Did not specify idrecording for repeater %s\n",rpt_vars[i].name);
  680. pthread_exit(NULL);
  681. }
  682. pthread_create(&rpt_vars[i].rpt_id_thread,NULL,rpt,(void *) &rpt_vars[i]);
  683. }
  684. /* wait for first one to die (should be never) */
  685. pthread_join(rpt_vars[0].rpt_id_thread,NULL);
  686. pthread_exit(NULL);
  687. }
  688. int unload_module(void)
  689. {
  690. STANDARD_HANGUP_LOCALUSERS;
  691. return 0;
  692. }
  693. int load_module(void)
  694. {
  695. pthread_create(&rpt_master_thread,NULL,rpt_master,NULL);
  696. return 0;
  697. }
  698. char *description(void)
  699. {
  700. return tdesc;
  701. }
  702. int usecount(void)
  703. {
  704. int res;
  705. STANDARD_USECOUNT(res);
  706. return res;
  707. }
  708. char *key()
  709. {
  710. return ASTERISK_GPL_KEY;
  711. }