1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033 |
- #define NEW_ASTERISK
- #include "asterisk.h"
- ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
- #include <stdio.h>
- #include <ctype.h>
- #include <math.h>
- #include <string.h>
- #include <unistd.h>
- #ifdef HAVE_SYS_IO_H
- #include <sys/io.h>
- #endif
- #include <sys/ioctl.h>
- #include <fcntl.h>
- #include <sys/time.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <usb.h>
- #include <alsa/asoundlib.h>
- #ifdef RADIO_XPMRX
- #define HAVE_XPMRX 1
- #endif
- #define CHAN_USBRADIO 1
- #define DEBUG_USBRADIO 0
- #define DEBUG_CAPTURES 1
- #define DEBUG_CAP_RX_OUT 0
- #define DEBUG_CAP_TX_OUT 0
- #define DEBUG_FILETEST 0
- #define RX_CAP_RAW_FILE "/tmp/rx_cap_in.pcm"
- #define RX_CAP_TRACE_FILE "/tmp/rx_trace.pcm"
- #define RX_CAP_OUT_FILE "/tmp/rx_cap_out.pcm"
- #define TX_CAP_RAW_FILE "/tmp/tx_cap_in.pcm"
- #define TX_CAP_TRACE_FILE "/tmp/tx_trace.pcm"
- #define TX_CAP_OUT_FILE "/tmp/tx_cap_out.pcm"
- #define MIXER_PARAM_MIC_PLAYBACK_SW "Mic Playback Switch"
- #define MIXER_PARAM_MIC_PLAYBACK_VOL "Mic Playback Volume"
- #define MIXER_PARAM_MIC_CAPTURE_SW "Mic Capture Switch"
- #define MIXER_PARAM_MIC_CAPTURE_VOL "Mic Capture Volume"
- #define MIXER_PARAM_MIC_BOOST "Auto Gain Control"
- #define MIXER_PARAM_SPKR_PLAYBACK_SW "Speaker Playback Switch"
- #define MIXER_PARAM_SPKR_PLAYBACK_VOL "Speaker Playback Volume"
- #define DELIMCHR ','
- #define QUOTECHR 34
- #define READERR_THRESHOLD 50
- #include "./xpmr/xpmr.h"
- #ifdef HAVE_XPMRX
- #include "./xpmrx/xpmrx.h"
- #include "./xpmrx/bitweight.h"
- #endif
- #if 0
- #define traceusb1(a) {printf a;}
- #else
- #define traceusb1(a)
- #endif
- #if 0
- #define traceusb2(a) {printf a;}
- #else
- #define traceusb2(a)
- #endif
- #ifdef __linux
- #include <linux/soundcard.h>
- #elif defined(__FreeBSD__)
- #include <sys/soundcard.h>
- #else
- #include <soundcard.h>
- #endif
- #include "asterisk/lock.h"
- #include "asterisk/frame.h"
- #include "asterisk/logger.h"
- #include "asterisk/callerid.h"
- #include "asterisk/channel.h"
- #include "asterisk/module.h"
- #include "asterisk/options.h"
- #include "asterisk/pbx.h"
- #include "asterisk/config.h"
- #include "asterisk/cli.h"
- #include "asterisk/utils.h"
- #include "asterisk/causes.h"
- #include "asterisk/endian.h"
- #include "asterisk/stringfields.h"
- #include "asterisk/abstract_jb.h"
- #include "asterisk/musiconhold.h"
- #include "asterisk/dsp.h"
- #ifndef NEW_ASTERISK
- #include "busy.h"
- #include "ringtone.h"
- #include "ring10.h"
- #include "answer.h"
- #endif
- #define C108_VENDOR_ID 0x0d8c
- #define C108_PRODUCT_ID 0x000c
- #define C108_HID_INTERFACE 3
- #define HID_REPORT_GET 0x01
- #define HID_REPORT_SET 0x09
- #define HID_RT_INPUT 0x01
- #define HID_RT_OUTPUT 0x02
- #define EEPROM_START_ADDR 6
- #define EEPROM_END_ADDR 63
- #define EEPROM_PHYSICAL_LEN 64
- #define EEPROM_TEST_ADDR EEPROM_END_ADDR
- #define EEPROM_MAGIC_ADDR 6
- #define EEPROM_MAGIC 34329
- #define EEPROM_CS_ADDR 62
- #define EEPROM_RXMIXERSET 8
- #define EEPROM_TXMIXASET 9
- #define EEPROM_TXMIXBSET 10
- #define EEPROM_RXVOICEADJ 11
- #define EEPROM_RXCTCSSADJ 13
- #define EEPROM_TXCTCSSADJ 15
- #define EEPROM_RXSQUELCHADJ 16
- static struct ast_jb_conf default_jbconf =
- {
- .flags = 0,
- .max_size = 200,
- .resync_threshold = 1000,
- .impl = "fixed",
- .target_extra = 40,
- };
- static struct ast_jb_conf global_jbconf;
- #define M_START(var, val) \
- char *__s = var; char *__val = val;
- #define M_END(x) x;
- #define M_F(tag, f) if (!strcasecmp((__s), tag)) { f; } else
- #define M_BOOL(tag, dst) M_F(tag, (dst) = ast_true(__val) )
- #define M_UINT(tag, dst) M_F(tag, (dst) = strtoul(__val, NULL, 0) )
- #define M_STR(tag, dst) M_F(tag, ast_copy_string(dst, __val, sizeof(dst)))
- #define FRAME_SIZE 160
- #define QUEUE_SIZE 2
- #if defined(__FreeBSD__)
- #define FRAGS 0x8
- #else
- #define FRAGS ( ( (6 * 5) << 16 ) | 0xc )
- #endif
- /*
- * XXX text message sizes are probably 256 chars, but i am
- * not sure if there is a suitable definition anywhere.
- */
- #define TEXT_SIZE 256
- #if 0
- #define TRYOPEN 1 /* try to open on startup */
- #endif
- #define O_CLOSE 0x444 /* special 'close' mode for device */
- /* Which device to use */
- #if defined( __OpenBSD__ ) || defined( __NetBSD__ )
- #define DEV_DSP "/dev/audio"
- #else
- #define DEV_DSP "/dev/dsp"
- #endif
- static const char *config = "usbradio.conf"; /* default config file */
- #define config1 "usbradio_tune_%s.conf" /* tune config file */
- static FILE *frxcapraw = NULL, *frxcaptrace = NULL, *frxoutraw = NULL;
- static FILE *ftxcapraw = NULL, *ftxcaptrace = NULL, *ftxoutraw = NULL;
- static char *usb_device_list = NULL;
- static int usb_device_list_size = 0;
- static int usbradio_debug;
- #if 0 //maw asdf sph
- static int usbradio_debug_level = 0;
- #endif
- enum {RX_AUDIO_NONE,RX_AUDIO_SPEAKER,RX_AUDIO_FLAT};
- enum {CD_IGNORE,CD_XPMR_NOISE,CD_XPMR_VOX,CD_HID,CD_HID_INVERT};
- enum {SD_IGNORE,SD_HID,SD_HID_INVERT,SD_XPMR}; // no,external,externalinvert,software
- enum {RX_KEY_CARRIER,RX_KEY_CARRIER_CODE};
- enum {TX_OUT_OFF,TX_OUT_VOICE,TX_OUT_LSD,TX_OUT_COMPOSITE,TX_OUT_AUX};
- enum {TOC_NONE,TOC_PHASE,TOC_NOTONE};
- /* DECLARE STRUCTURES */
- /*
- * Each sound is made of 'datalen' samples of sound, repeated as needed to
- * generate 'samplen' samples of data, then followed by 'silencelen' samples
- * of silence. The loop is repeated if 'repeat' is set.
- */
- struct sound {
- int ind;
- char *desc;
- short *data;
- int datalen;
- int samplen;
- int silencelen;
- int repeat;
- };
- #ifndef NEW_ASTERISK
- static struct sound sounds[] = {
- { AST_CONTROL_RINGING, "RINGING", ringtone, sizeof(ringtone)/2, 16000, 32000, 1 },
- { AST_CONTROL_BUSY, "BUSY", busy, sizeof(busy)/2, 4000, 4000, 1 },
- { AST_CONTROL_CONGESTION, "CONGESTION", busy, sizeof(busy)/2, 2000, 2000, 1 },
- { AST_CONTROL_RING, "RING10", ring10, sizeof(ring10)/2, 16000, 32000, 1 },
- { AST_CONTROL_ANSWER, "ANSWER", answer, sizeof(answer)/2, 2200, 0, 0 },
- { -1, NULL, 0, 0, 0, 0 }, /* end marker */
- };
- #endif
- /*
- * descriptor for one of our channels.
- * There is one used for 'default' values (from the [general] entry in
- * the configuration file), and then one instance for each device
- * (the default is cloned from [general], others are only created
- * if the relevant section exists).
- */
- struct chan_usbradio_pvt {
- struct chan_usbradio_pvt *next;
- char *name;
- #ifndef NEW_ASTERISK
- /*
- * cursound indicates which in struct sound we play. -1 means nothing,
- * any other value is a valid sound, in which case sampsent indicates
- * the next sample to send in [0..samplen + silencelen]
- * nosound is set to disable the audio data from the channel
- * (so we can play the tones etc.).
- */
- int sndcmd[2]; /* Sound command pipe */
- int cursound; /* index of sound to send */
- int sampsent; /* # of sound samples sent */
- int nosound; /* set to block audio from the PBX */
- #endif
- int pttkick[2];
- int total_blocks; /* total blocks in the output device */
- int sounddev;
- enum { M_UNSET, M_FULL, M_READ, M_WRITE } duplex;
- i16 cdMethod;
- int autoanswer;
- int autohangup;
- int hookstate;
- unsigned int queuesize; /* max fragments in queue */
- unsigned int frags; /* parameter for SETFRAGMENT */
- int warned; /* various flags used for warnings */
- #define WARN_used_blocks 1
- #define WARN_speed 2
- #define WARN_frag 4
- int w_errors; /* overfull in the write path */
- struct timeval lastopen;
- int overridecontext;
- int mute;
- /* boost support. BOOST_SCALE * 10 ^(BOOST_MAX/20) must
- * be representable in 16 bits to avoid overflows.
- */
- #define BOOST_SCALE (1<<9)
- #define BOOST_MAX 40 /* slightly less than 7 bits */
- int boost; /* input boost, scaled by BOOST_SCALE */
- char devicenum;
- char devstr[128];
- int spkrmax;
- int micmax;
- #ifndef NEW_ASTERISK
- pthread_t sthread;
- #endif
- pthread_t hidthread;
- int stophid;
- FILE *hkickhid;
- struct ast_channel *owner;
- char ext[AST_MAX_EXTENSION];
- char ctx[AST_MAX_CONTEXT];
- char language[MAX_LANGUAGE];
- char cid_name[256]; /*XXX */
- char cid_num[256]; /*XXX */
- char mohinterpret[MAX_MUSICCLASS];
- /* buffers used in usbradio_write, 2 per int by 2 channels by 6 times oversampling (48KS/s) */
- char usbradio_write_buf[FRAME_SIZE * 2 * 2 * 6];
- char usbradio_write_buf_1[FRAME_SIZE * 2 * 2* 6];
- int usbradio_write_dst;
- /* buffers used in usbradio_read - AST_FRIENDLY_OFFSET space for headers
- * plus enough room for a full frame
- */
- char usbradio_read_buf[FRAME_SIZE * (2 * 12) + AST_FRIENDLY_OFFSET];
- char usbradio_read_buf_8k[FRAME_SIZE * 2 + AST_FRIENDLY_OFFSET];
- int readpos; /* read position above */
- struct ast_frame read_f; /* returned by usbradio_read */
- char debuglevel;
- char radioduplex; //
- char wanteeprom;
- int tracetype;
- int tracelevel;
- char area;
- char rptnum;
- int idleinterval;
- int turnoffs;
- int txsettletime;
- char ukey[48];
- char lastrx;
- char rxhidsq;
- char rxcarrierdetect; // status from pmr channel
- char rxctcssdecode; // status from pmr channel
- int rxdcsdecode;
- int rxlsddecode;
- char rxkeytype;
- char rxkeyed; // indicates rx signal present
- char lasttx;
- char txkeyed; // tx key request from upper layers
- char txchankey;
- char txtestkey;
- time_t lasthidtime;
- struct ast_dsp *dsp;
- t_pmr_chan *pmrChan;
- char rxcpusaver;
- char txcpusaver;
- char rxdemod;
- float rxgain;
- char rxcdtype;
- char rxsdtype;
- int rxsquelchadj; /* this copy needs to be here for initialization */
- int rxsqvoxadj;
- char txtoctype;
- char txprelim;
- float txctcssgain;
- char txmixa;
- char txmixb;
- char invertptt;
- char rxctcssrelax;
- float rxctcssgain;
- char txctcssdefault[16]; // for repeater operation
- char rxctcssfreqs[512]; // a string
- char txctcssfreqs[512];
- char txctcssfreq[32]; // encode now
- char rxctcssfreq[32]; // decode now
- char numrxctcssfreqs; // how many
- char numtxctcssfreqs;
- char *rxctcss[CTCSS_NUM_CODES]; // pointers to strings
- char *txctcss[CTCSS_NUM_CODES];
- int txfreq; // in Hz
- int rxfreq;
- // start remote operation info
- char set_txctcssdefault[16]; // for remote operation
- char set_txctcssfreq[16]; // encode now
- char set_rxctcssfreq[16]; // decode now
- char set_numrxctcssfreqs; // how many
- char set_numtxctcssfreqs;
- char set_rxctcssfreqs[16]; // a string
- char set_txctcssfreqs[16];
- char *set_rxctcss; // pointers to strings
- char *set_txctcss;
- int set_txfreq; // in Hz
- int set_rxfreq;
- // end remote operation info
- int rxmixerset;
- int rxboostset;
- float rxvoiceadj;
- float rxctcssadj;
- int txmixaset;
- int txmixbset;
- int txctcssadj;
- int hdwtype;
- int hid_gpio_ctl;
- int hid_gpio_ctl_loc;
- int hid_io_cor;
- int hid_io_cor_loc;
- int hid_io_ctcss;
- int hid_io_ctcss_loc;
- int hid_io_ptt;
- int hid_gpio_loc;
- struct {
- unsigned rxcapraw:1;
- unsigned txcapraw:1;
- unsigned txcap2:1;
- unsigned rxcap2:1;
- unsigned rxplmon:1;
- unsigned remoted:1;
- unsigned txpolarity:1;
- unsigned rxpolarity:1;
- unsigned dcstxpolarity:1;
- unsigned dcsrxpolarity:1;
- unsigned lsdtxpolarity:1;
- unsigned lsdrxpolarity:1;
- unsigned loopback:1;
- unsigned radioactive:1;
- }b;
- unsigned short eeprom[EEPROM_PHYSICAL_LEN];
- char eepromctl;
- ast_mutex_t eepromlock;
- struct usb_dev_handle *usb_handle;
- int readerrs;
- };
- // maw add additional defaults !!!
- static struct chan_usbradio_pvt usbradio_default = {
- #ifndef NEW_ASTERISK
- .cursound = -1,
- #endif
- .sounddev = -1,
- .duplex = M_UNSET, /* XXX check this */
- .autoanswer = 1,
- .autohangup = 1,
- .queuesize = QUEUE_SIZE,
- .frags = FRAGS,
- .ext = "s",
- .ctx = "default",
- .readpos = AST_FRIENDLY_OFFSET, /* start here on reads */
- .lastopen = { 0, 0 },
- .boost = BOOST_SCALE,
- .wanteeprom = 1,
- .area = 0,
- .rptnum = 0,
- };
- /* DECLARE FUNCTION PROTOTYPES */
- static void store_txtoctype(struct chan_usbradio_pvt *o, const char *s);
- static int hidhdwconfig(struct chan_usbradio_pvt *o);
- static int set_txctcss_level(struct chan_usbradio_pvt *o);
- static void pmrdump(struct chan_usbradio_pvt *o);
- static void mult_set(struct chan_usbradio_pvt *o);
- static int mult_calc(int value);
- static void mixer_write(struct chan_usbradio_pvt *o);
- static void tune_rxinput(int fd, struct chan_usbradio_pvt *o);
- static void tune_rxvoice(int fd, struct chan_usbradio_pvt *o);
- static void tune_rxctcss(int fd, struct chan_usbradio_pvt *o);
- static void tune_txoutput(struct chan_usbradio_pvt *o, int value, int fd);
- static void tune_write(struct chan_usbradio_pvt *o);
- static char *usbradio_active; /* the active device */
- static int setformat(struct chan_usbradio_pvt *o, int mode);
- static struct ast_channel *usbradio_request(const char *type, format_t format,
- const struct ast_channel *requestor,
- void *data, int *cause);
- static int usbradio_digit_begin(struct ast_channel *c, char digit);
- static int usbradio_digit_end(struct ast_channel *c, char digit, unsigned int duration);
- static int usbradio_text(struct ast_channel *c, const char *text);
- static int usbradio_hangup(struct ast_channel *c);
- static int usbradio_answer(struct ast_channel *c);
- static struct ast_frame *usbradio_read(struct ast_channel *chan);
- static int usbradio_call(struct ast_channel *c, char *dest, int timeout);
- static int usbradio_write(struct ast_channel *chan, struct ast_frame *f);
- static int usbradio_indicate(struct ast_channel *chan, int cond, const void *data, size_t datalen);
- static int usbradio_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
- static int xpmr_config(struct chan_usbradio_pvt *o);
- #if DEBUG_FILETEST == 1
- static int RxTestIt(struct chan_usbradio_pvt *o);
- #endif
- static char tdesc[] = "USB (CM108) Radio Channel Driver";
- static const struct ast_channel_tech usbradio_tech = {
- .type = "Radio",
- .description = tdesc,
- .capabilities = AST_FORMAT_SLINEAR,
- .requester = usbradio_request,
- .send_digit_begin = usbradio_digit_begin,
- .send_digit_end = usbradio_digit_end,
- .send_text = usbradio_text,
- .hangup = usbradio_hangup,
- .answer = usbradio_answer,
- .read = usbradio_read,
- .call = usbradio_call,
- .write = usbradio_write,
- .indicate = usbradio_indicate,
- .fixup = usbradio_fixup,
- };
- /* Call with: devnum: alsa major device number, param: ascii Formal
- Parameter Name, val1, first or only value, val2 second value, or 0
- if only 1 value. Values: 0-99 (percent) or 0-1 for baboon.
- Note: must add -lasound to end of linkage */
- static int amixer_max(int devnum,char *param)
- {
- int rv,type;
- char str[100];
- snd_hctl_t *hctl;
- snd_ctl_elem_id_t *id;
- snd_hctl_elem_t *elem;
- snd_ctl_elem_info_t *info;
- sprintf(str,"hw:%d",devnum);
- if (snd_hctl_open(&hctl, str, 0)) return(-1);
- snd_hctl_load(hctl);
- snd_ctl_elem_id_alloca(&id);
- snd_ctl_elem_id_set_interface(id, SND_CTL_ELEM_IFACE_MIXER);
- snd_ctl_elem_id_set_name(id, param);
- elem = snd_hctl_find_elem(hctl, id);
- if (!elem)
- {
- snd_hctl_close(hctl);
- return(-1);
- }
- snd_ctl_elem_info_alloca(&info);
- snd_hctl_elem_info(elem,info);
- type = snd_ctl_elem_info_get_type(info);
- rv = 0;
- switch(type)
- {
- case SND_CTL_ELEM_TYPE_INTEGER:
- rv = snd_ctl_elem_info_get_max(info);
- break;
- case SND_CTL_ELEM_TYPE_BOOLEAN:
- rv = 1;
- break;
- }
- snd_hctl_close(hctl);
- return(rv);
- }
- /* Call with: devnum: alsa major device number, param: ascii Formal
- Parameter Name, val1, first or only value, val2 second value, or 0
- if only 1 value. Values: 0-99 (percent) or 0-1 for baboon.
- Note: must add -lasound to end of linkage */
- static int setamixer(int devnum,char *param, int v1, int v2)
- {
- int type;
- char str[100];
- snd_hctl_t *hctl;
- snd_ctl_elem_id_t *id;
- snd_ctl_elem_value_t *control;
- snd_hctl_elem_t *elem;
- snd_ctl_elem_info_t *info;
- sprintf(str,"hw:%d",devnum);
- if (snd_hctl_open(&hctl, str, 0)) return(-1);
- snd_hctl_load(hctl);
- snd_ctl_elem_id_alloca(&id);
- snd_ctl_elem_id_set_interface(id, SND_CTL_ELEM_IFACE_MIXER);
- snd_ctl_elem_id_set_name(id, param);
- elem = snd_hctl_find_elem(hctl, id);
- if (!elem)
- {
- snd_hctl_close(hctl);
- return(-1);
- }
- snd_ctl_elem_info_alloca(&info);
- snd_hctl_elem_info(elem,info);
- type = snd_ctl_elem_info_get_type(info);
- snd_ctl_elem_value_alloca(&control);
- snd_ctl_elem_value_set_id(control, id);
- switch(type)
- {
- case SND_CTL_ELEM_TYPE_INTEGER:
- snd_ctl_elem_value_set_integer(control, 0, v1);
- if (v2 > 0) snd_ctl_elem_value_set_integer(control, 1, v2);
- break;
- case SND_CTL_ELEM_TYPE_BOOLEAN:
- snd_ctl_elem_value_set_integer(control, 0, (v1 != 0));
- break;
- }
- if (snd_hctl_elem_write(elem, control))
- {
- snd_hctl_close(hctl);
- return(-1);
- }
- snd_hctl_close(hctl);
- return(0);
- }
- static void hid_set_outputs(struct usb_dev_handle *handle,
- unsigned char *outputs)
- {
- usleep(1500);
- usb_control_msg(handle,
- USB_ENDPOINT_OUT + USB_TYPE_CLASS + USB_RECIP_INTERFACE,
- HID_REPORT_SET,
- 0 + (HID_RT_OUTPUT << 8),
- C108_HID_INTERFACE,
- (char*)outputs, 4, 5000);
- }
- static void hid_get_inputs(struct usb_dev_handle *handle,
- unsigned char *inputs)
- {
- usleep(1500);
- usb_control_msg(handle,
- USB_ENDPOINT_IN + USB_TYPE_CLASS + USB_RECIP_INTERFACE,
- HID_REPORT_GET,
- 0 + (HID_RT_INPUT << 8),
- C108_HID_INTERFACE,
- (char*)inputs, 4, 5000);
- }
- static unsigned short read_eeprom(struct usb_dev_handle *handle, int addr)
- {
- unsigned char buf[4];
- buf[0] = 0x80;
- buf[1] = 0;
- buf[2] = 0;
- buf[3] = 0x80 | (addr & 0x3f);
- hid_set_outputs(handle,buf);
- memset(buf,0,sizeof(buf));
- hid_get_inputs(handle,buf);
- return(buf[1] + (buf[2] << 8));
- }
- static void write_eeprom(struct usb_dev_handle *handle, int addr,
- unsigned short data)
- {
- unsigned char buf[4];
- buf[0] = 0x80;
- buf[1] = data & 0xff;
- buf[2] = data >> 8;
- buf[3] = 0xc0 | (addr & 0x3f);
- hid_set_outputs(handle,buf);
- }
- static unsigned short get_eeprom(struct usb_dev_handle *handle,
- unsigned short *buf)
- {
- int i;
- unsigned short cs;
- cs = 0xffff;
- for(i = EEPROM_START_ADDR; i < EEPROM_END_ADDR; i++)
- {
- cs += buf[i] = read_eeprom(handle,i);
- }
- return(cs);
- }
- static void put_eeprom(struct usb_dev_handle *handle,unsigned short *buf)
- {
- int i;
- unsigned short cs;
- cs = 0xffff;
- buf[EEPROM_MAGIC_ADDR] = EEPROM_MAGIC;
- for(i = EEPROM_START_ADDR; i < EEPROM_CS_ADDR; i++)
- {
- write_eeprom(handle,i,buf[i]);
- cs += buf[i];
- }
- buf[EEPROM_CS_ADDR] = (65535 - cs) + 1;
- write_eeprom(handle,i,buf[EEPROM_CS_ADDR]);
- }
- static struct usb_device *hid_device_init(char *desired_device)
- {
- struct usb_bus *usb_bus;
- struct usb_device *dev;
- char devstr[200],str[200],desdev[200],*cp;
- int i;
- FILE *fp;
- usb_init();
- usb_find_busses();
- usb_find_devices();
- for (usb_bus = usb_busses;
- usb_bus;
- usb_bus = usb_bus->next) {
- for (dev = usb_bus->devices;
- dev;
- dev = dev->next) {
- if ((dev->descriptor.idVendor
- == C108_VENDOR_ID) &&
- (dev->descriptor.idProduct
- == C108_PRODUCT_ID))
- {
- sprintf(devstr,"%s/%s", usb_bus->dirname,dev->filename);
- for(i = 0; i < 32; i++)
- {
- sprintf(str,"/proc/asound/card%d/usbbus",i);
- fp = fopen(str,"r");
- if (!fp) continue;
- if ((!fgets(desdev,sizeof(desdev) - 1,fp)) || (!desdev[0]))
- {
- fclose(fp);
- continue;
- }
- fclose(fp);
- if (desdev[strlen(desdev) - 1] == '\n')
- desdev[strlen(desdev) -1 ] = 0;
- if (strcasecmp(desdev,devstr)) continue;
- if (i) sprintf(str,"/sys/class/sound/dsp%d/device",i);
- else strcpy(str,"/sys/class/sound/dsp/device");
- memset(desdev,0,sizeof(desdev));
- if (readlink(str,desdev,sizeof(desdev) - 1) == -1)
- {
- sprintf(str,"/sys/class/sound/controlC%d/device",i);
- memset(desdev,0,sizeof(desdev));
- if (readlink(str,desdev,sizeof(desdev) - 1) == -1) continue;
- }
- cp = strrchr(desdev,'/');
- if (cp) *cp = 0; else continue;
- cp = strrchr(desdev,'/');
- if (!cp) continue;
- cp++;
- break;
- }
- if (i >= 32) continue;
- if (!strcmp(cp,desired_device)) return dev;
- }
- }
- }
- return NULL;
- }
- static int hid_device_mklist(void)
- {
- struct usb_bus *usb_bus;
- struct usb_device *dev;
- char devstr[200],str[200],desdev[200],*cp;
- int i;
- FILE *fp;
- usb_device_list = ast_malloc(2);
- if (!usb_device_list) return -1;
- memset(usb_device_list,0,2);
- usb_init();
- usb_find_busses();
- usb_find_devices();
- for (usb_bus = usb_busses;
- usb_bus;
- usb_bus = usb_bus->next) {
- for (dev = usb_bus->devices;
- dev;
- dev = dev->next) {
- if ((dev->descriptor.idVendor
- == C108_VENDOR_ID) &&
- (dev->descriptor.idProduct
- == C108_PRODUCT_ID))
- {
- sprintf(devstr,"%s/%s", usb_bus->dirname,dev->filename);
- for(i = 0;i < 32; i++)
- {
- sprintf(str,"/proc/asound/card%d/usbbus",i);
- fp = fopen(str,"r");
- if (!fp) continue;
- if ((!fgets(desdev,sizeof(desdev) - 1,fp)) || (!desdev[0]))
- {
- fclose(fp);
- continue;
- }
- fclose(fp);
- if (desdev[strlen(desdev) - 1] == '\n')
- desdev[strlen(desdev) -1 ] = 0;
- if (strcasecmp(desdev,devstr)) continue;
- if (i) sprintf(str,"/sys/class/sound/dsp%d/device",i);
- else strcpy(str,"/sys/class/sound/dsp/device");
- memset(desdev,0,sizeof(desdev));
- if (readlink(str,desdev,sizeof(desdev) - 1) == -1)
- {
- sprintf(str,"/sys/class/sound/controlC%d/device",i);
- memset(desdev,0,sizeof(desdev));
- if (readlink(str,desdev,sizeof(desdev) - 1) == -1) continue;
- }
- cp = strrchr(desdev,'/');
- if (cp) *cp = 0; else continue;
- cp = strrchr(desdev,'/');
- if (!cp) continue;
- cp++;
- break;
- }
- if (i >= 32) return -1;
- usb_device_list = ast_realloc(usb_device_list,
- usb_device_list_size + 2 +
- strlen(cp));
- if (!usb_device_list) return -1;
- usb_device_list_size += strlen(cp) + 2;
- i = 0;
- while(usb_device_list[i])
- {
- i += strlen(usb_device_list + i) + 1;
- }
- strcat(usb_device_list + i,cp);
- usb_device_list[strlen(cp) + i + 1] = 0;
- }
- }
- }
- return 0;
- }
- static int usb_get_usbdev(char *devstr)
- {
- int i;
- char str[200],desdev[200],*cp;
- for(i = 0;i < 32; i++)
- {
- if (i) sprintf(str,"/sys/class/sound/dsp%d/device",i);
- else strcpy(str,"/sys/class/sound/dsp/device");
- memset(desdev,0,sizeof(desdev));
- if (readlink(str,desdev,sizeof(desdev) - 1) == -1)
- {
- sprintf(str,"/sys/class/sound/controlC%d/device",i);
- memset(desdev,0,sizeof(desdev));
- if (readlink(str,desdev,sizeof(desdev) - 1) == -1) continue;
- }
- cp = strrchr(desdev,'/');
- if (cp) *cp = 0; else continue;
- cp = strrchr(desdev,'/');
- if (!cp) continue;
- cp++;
- if (!strcasecmp(cp,devstr)) break;
- }
- if (i >= 32) return -1;
- return i;
- }
- static int usb_list_check(char *devstr)
- {
- char *s = usb_device_list;
- if (!s) return(0);
- while(*s)
- {
- if (!strcasecmp(s,devstr)) return(1);
- s += strlen(s) + 1;
- }
- return(0);
- }
- static int hidhdwconfig(struct chan_usbradio_pvt *o)
- {
- if(o->hdwtype==1)
- {
- o->hid_gpio_ctl = 0x08;
- o->hid_gpio_ctl_loc = 2;
- o->hid_io_cor = 4;
- o->hid_io_cor_loc = 1;
- o->hid_io_ctcss = 2;
- o->hid_io_ctcss_loc = 1;
- o->hid_io_ptt = 8;
- o->hid_gpio_loc = 1;
- }
- else if(o->hdwtype==0)
- {
- o->hid_gpio_ctl = 0x0c;
- o->hid_gpio_ctl_loc = 2;
- o->hid_io_cor = 2;
- o->hid_io_cor_loc = 0;
- o->hid_io_ctcss = 2;
- o->hid_io_ctcss_loc = 1;
- o->hid_io_ptt = 4;
- o->hid_gpio_loc = 1;
- }
- else if(o->hdwtype==3)
- {
- o->hid_gpio_ctl = 0x0c;
- o->hid_gpio_ctl_loc = 2;
- o->hid_io_cor = 2;
- o->hid_io_cor_loc = 0;
- o->hid_io_ctcss = 2;
- o->hid_io_ctcss_loc = 1;
- o->hid_io_ptt = 4;
- o->hid_gpio_loc = 1;
- }
- return 0;
- }
- static void kickptt(struct chan_usbradio_pvt *o)
- {
- char c = 0;
-
- if (!o) return;
- if (!o->pttkick) return;
- if (write(o->pttkick[1],&c,1) < 0) {
- ast_log(LOG_ERROR, "write() failed: %s\n", strerror(errno));
- }
- }
- static void *hidthread(void *arg)
- {
- unsigned char buf[4],bufsave[4],keyed;
- char lastrx, txtmp;
- int res;
- struct usb_device *usb_dev;
- struct usb_dev_handle *usb_handle;
- struct chan_usbradio_pvt *o = (struct chan_usbradio_pvt *) arg;
- struct pollfd pfd = { .events = POLLIN };
- usb_dev = hid_device_init(o->devstr);
- if (usb_dev == NULL) {
- ast_log(LOG_ERROR,"USB HID device not found\n");
- pthread_exit(NULL);
- }
- usb_handle = usb_open(usb_dev);
- if (usb_handle == NULL) {
- ast_log(LOG_ERROR,"Not able to open USB device\n");
- pthread_exit(NULL);
- }
- if (usb_claim_interface(usb_handle,C108_HID_INTERFACE) < 0)
- {
- if (usb_detach_kernel_driver_np(usb_handle,C108_HID_INTERFACE) < 0) {
- ast_log(LOG_ERROR,"Not able to detach the USB device\n");
- pthread_exit(NULL);
- }
- if (usb_claim_interface(usb_handle,C108_HID_INTERFACE) < 0) {
- ast_log(LOG_ERROR,"Not able to claim the USB device\n");
- pthread_exit(NULL);
- }
- }
- memset(buf,0,sizeof(buf));
- buf[2] = o->hid_gpio_ctl;
- buf[1] = 0;
- hid_set_outputs(usb_handle,buf);
- memcpy(bufsave,buf,sizeof(buf));
- if (pipe(o->pttkick) == -1)
- {
- ast_log(LOG_ERROR,"Not able to create pipe\n");
- pthread_exit(NULL);
- }
- traceusb1(("hidthread: Starting normally on %s!!\n",o->name));
- lastrx = 0;
-
- while (!o->stophid) {
- pfd.fd = o->pttkick[0];
- pfd.revents = 0;
- res = ast_poll(&pfd, 1, 50);
- if (res < 0) {
- ast_log(LOG_WARNING, "poll() failed: %s\n", strerror(errno));
- usleep(10000);
- continue;
- }
- if (pfd.revents & POLLIN) {
- char c;
- if (read(o->pttkick[0], &c, 1) < 0) {
- ast_log(LOG_ERROR, "read() failed: %s\n", strerror(errno));
- }
- }
- if (o->wanteeprom) {
- ast_mutex_lock(&o->eepromlock);
- if (o->eepromctl == 1) {
-
- if (!get_eeprom(usb_handle, o->eeprom)) {
- if (o->eeprom[EEPROM_MAGIC_ADDR] != EEPROM_MAGIC) {
- ast_log(LOG_NOTICE, "UNSUCCESSFUL: EEPROM MAGIC NUMBER BAD on channel %s\n", o->name);
- } else {
- o->rxmixerset = o->eeprom[EEPROM_RXMIXERSET];
- o->txmixaset = o->eeprom[EEPROM_TXMIXASET];
- o->txmixbset = o->eeprom[EEPROM_TXMIXBSET];
- memcpy(&o->rxvoiceadj, &o->eeprom[EEPROM_RXVOICEADJ], sizeof(float));
- memcpy(&o->rxctcssadj, &o->eeprom[EEPROM_RXCTCSSADJ], sizeof(float));
- o->txctcssadj = o->eeprom[EEPROM_TXCTCSSADJ];
- o->rxsquelchadj = o->eeprom[EEPROM_RXSQUELCHADJ];
- ast_log(LOG_NOTICE,"EEPROM Loaded on channel %s\n",o->name);
- }
- } else {
- ast_log(LOG_NOTICE, "USB Adapter has no EEPROM installed or Checksum BAD on channel %s\n", o->name);
- }
- hid_set_outputs(usb_handle,bufsave);
- }
- if (o->eepromctl == 2) {
- put_eeprom(usb_handle,o->eeprom);
- hid_set_outputs(usb_handle,bufsave);
- ast_log(LOG_NOTICE, "USB Parameters written to EEPROM on %s\n", o->name);
- }
- o->eepromctl = 0;
- ast_mutex_unlock(&o->eepromlock);
- }
- buf[o->hid_gpio_ctl_loc] = o->hid_gpio_ctl;
- hid_get_inputs(usb_handle,buf);
- keyed = !(buf[o->hid_io_cor_loc] & o->hid_io_cor);
- if (keyed != o->rxhidsq) {
- if (o->debuglevel) {
- printf("chan_usbradio() hidthread: update rxhidsq = %d\n", keyed);
- }
- o->rxhidsq=keyed;
- }
-
- txtmp = o->pmrChan->txPttOut;
- if (o->lasttx != txtmp) {
- o->pmrChan->txPttHid = o->lasttx = txtmp;
- if (o->debuglevel) {
- ast_debug(0, "hidthread: tx set to %d\n", txtmp);
- }
- buf[o->hid_gpio_loc] = 0;
- if (!o->invertptt) {
- if (txtmp) {
- buf[o->hid_gpio_loc] = o->hid_io_ptt;
- }
- } else {
- if (!txtmp) {
- buf[o->hid_gpio_loc] = o->hid_io_ptt;
- }
- }
- buf[o->hid_gpio_ctl_loc] = o->hid_gpio_ctl;
- memcpy(bufsave, buf, sizeof(buf));
- hid_set_outputs(usb_handle, buf);
- }
- time(&o->lasthidtime);
- }
- buf[o->hid_gpio_loc] = 0;
- if (o->invertptt) {
- buf[o->hid_gpio_loc] = o->hid_io_ptt;
- }
- buf[o->hid_gpio_ctl_loc] = o->hid_gpio_ctl;
- hid_set_outputs(usb_handle, buf);
- pthread_exit(0);
- }
- static struct chan_usbradio_pvt *find_desc(char *dev)
- {
- struct chan_usbradio_pvt *o = NULL;
- if (!dev)
- ast_log(LOG_WARNING, "null dev\n");
- for (o = usbradio_default.next; o && o->name && dev && strcmp(o->name, dev) != 0; o = o->next);
- if (!o)
- {
- ast_log(LOG_WARNING, "could not find <%s>\n", dev ? dev : "--no-device--");
- }
- return o;
- }
- static struct chan_usbradio_pvt *find_desc_usb(char *devstr)
- {
- struct chan_usbradio_pvt *o = NULL;
- if (!devstr)
- ast_log(LOG_WARNING, "null dev\n");
- for (o = usbradio_default.next; o && devstr && strcmp(o->devstr, devstr) != 0; o = o->next);
- return o;
- }
- #if 0
- static char *ast_ext_ctx(const char *src, char **ext, char **ctx)
- {
- struct chan_usbradio_pvt *o = find_desc(usbradio_active);
- if (ext == NULL || ctx == NULL)
- return NULL;
- *ext = *ctx = NULL;
- if (src && *src != '\0')
- *ext = ast_strdup(src);
- if (*ext == NULL)
- return NULL;
- if (!o->overridecontext) {
-
- *ctx = strrchr(*ext, '@');
- if (*ctx)
- *(*ctx)++ = '\0';
- }
- return *ext;
- }
- #endif
- static int used_blocks(struct chan_usbradio_pvt *o)
- {
- struct audio_buf_info info;
- if (ioctl(o->sounddev, SNDCTL_DSP_GETOSPACE, &info)) {
- if (!(o->warned & WARN_used_blocks)) {
- ast_log(LOG_WARNING, "Error reading output space\n");
- o->warned |= WARN_used_blocks;
- }
- return 1;
- }
- if (o->total_blocks == 0) {
- if (0)
- ast_log(LOG_WARNING, "fragtotal %d size %d avail %d\n", info.fragstotal, info.fragsize, info.fragments);
- o->total_blocks = info.fragments;
- }
- return o->total_blocks - info.fragments;
- }
- static int soundcard_writeframe(struct chan_usbradio_pvt *o, short *data)
- {
- int res;
- if (o->sounddev < 0)
- setformat(o, O_RDWR);
- if (o->sounddev < 0)
- return 0;
-
-
-
- if(!o->pmrChan->txPttIn && !o->pmrChan->txPttOut)
- {
-
- }
-
- res = used_blocks(o);
- if (res > o->queuesize) {
-
- if (o->w_errors++ == 0 && (usbradio_debug & 0x4))
- ast_log(LOG_WARNING, "write: used %d blocks (%d)\n", res, o->w_errors);
- return 0;
- }
- o->w_errors = 0;
- return write(o->sounddev, ((void *) data), FRAME_SIZE * 2 * 12);
- }
- #ifndef NEW_ASTERISK
- static void send_sound(struct chan_usbradio_pvt *o)
- {
- short myframe[FRAME_SIZE];
- int ofs, l, start;
- int l_sampsent = o->sampsent;
- struct sound *s;
- if (o->cursound < 0)
- return;
- s = &sounds[o->cursound];
- for (ofs = 0; ofs < FRAME_SIZE; ofs += l) {
- l = s->samplen - l_sampsent;
- if (l > 0) {
- start = l_sampsent % s->datalen;
- if (l > FRAME_SIZE - ofs)
- l = FRAME_SIZE - ofs;
- if (l > s->datalen - start)
- l = s->datalen - start;
- memmove(myframe + ofs, s->data + start, l * 2);
- if (0)
- ast_log(LOG_WARNING, "send_sound sound %d/%d of %d into %d\n", l_sampsent, l, s->samplen, ofs);
- l_sampsent += l;
- } else {
- static const short silence[FRAME_SIZE] = { 0, };
- l += s->silencelen;
- if (l > 0) {
- if (l > FRAME_SIZE - ofs)
- l = FRAME_SIZE - ofs;
- memmove(myframe + ofs, silence, l * 2);
- l_sampsent += l;
- } else {
- if (s->repeat == 0) {
- o->cursound = -1;
- o->nosound = 0;
- if (ofs < FRAME_SIZE)
- memmove(myframe + ofs, silence, (FRAME_SIZE - ofs) * 2);
- }
- l_sampsent = 0;
- }
- }
- }
- l = soundcard_writeframe(o, myframe);
- if (l > 0)
- o->sampsent = l_sampsent;
- }
- static void *sound_thread(void *arg)
- {
- char ign[4096];
- struct chan_usbradio_pvt *o = (struct chan_usbradio_pvt *) arg;
-
- read(o->sounddev, ign, sizeof(ign));
- for (;;) {
- struct pollfd pfd[2] = { { .fd = o->sndcmd[0], .events = POLLIN }, { .fd = o->sounddev } };
- int res;
- if (o->cursound > -1 && o->sounddev < 0) {
- setformat(o, O_RDWR);
- } else if (o->cursound == -1 && o->owner == NULL) {
- setformat(o, O_CLOSE);
- }
- if (o->sounddev > -1) {
- if (!o->owner) {
- pfd[1].events = POLLIN;
- }
- if (o->cursound > -1) {
- pfd[1].events |= POLLOUT;
- }
- }
- res = ast_poll(pfd, o->sounddev > -1 ? 2 : 1, -1);
- if (res < 1) {
- ast_log(LOG_WARNING, "poll failed: %s\n", strerror(errno));
- sleep(1);
- continue;
- }
- if (pfd[0].revents & POLLIN) {
-
- int i, what = -1;
- read(o->sndcmd[0], &what, sizeof(what));
- for (i = 0; sounds[i].ind != -1; i++) {
- if (sounds[i].ind == what) {
- o->cursound = i;
- o->sampsent = 0;
- o->nosound = 1;
- break;
- }
- }
- if (sounds[i].ind == -1) {
- ast_log(LOG_WARNING, "invalid sound index: %d\n", what);
- }
- }
- if (o->sounddev > -1) {
- if (pfd[1].revents & POLLIN) {
- read(o->sounddev, ign, sizeof(ign));
- }
- if (pfd[1].revents & POLLOUT) {
- send_sound(o);
- }
- }
- }
- return NULL;
- }
- #endif
- static int setformat(struct chan_usbradio_pvt *o, int mode)
- {
- int fmt, desired, res, fd;
- char device[100];
- if (o->sounddev >= 0) {
- ioctl(o->sounddev, SNDCTL_DSP_RESET, 0);
- close(o->sounddev);
- o->duplex = M_UNSET;
- o->sounddev = -1;
- }
- if (mode == O_CLOSE)
- return 0;
- o->lastopen = ast_tvnow();
- strcpy(device,"/dev/dsp");
- if (o->devicenum)
- sprintf(device,"/dev/dsp%d",o->devicenum);
- fd = o->sounddev = open(device, mode | O_NONBLOCK);
- if (fd < 0) {
- ast_log(LOG_WARNING, "Unable to re-open DSP device %d: %s\n", o->devicenum, strerror(errno));
- return -1;
- }
- if (o->owner)
- o->owner->fds[0] = fd;
- #if __BYTE_ORDER == __LITTLE_ENDIAN
- fmt = AFMT_S16_LE;
- #else
- fmt = AFMT_S16_BE;
- #endif
- res = ioctl(fd, SNDCTL_DSP_SETFMT, &fmt);
- if (res < 0) {
- ast_log(LOG_WARNING, "Unable to set format to 16-bit signed\n");
- return -1;
- }
- switch (mode) {
- case O_RDWR:
- res = ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
-
- res = ioctl(fd, SNDCTL_DSP_GETCAPS, &fmt);
- if (res == 0 && (fmt & DSP_CAP_DUPLEX)) {
- if (option_verbose > 1)
- ast_verbose(VERBOSE_PREFIX_2 "Console is full duplex\n");
- o->duplex = M_FULL;
- };
- break;
- case O_WRONLY:
- o->duplex = M_WRITE;
- break;
- case O_RDONLY:
- o->duplex = M_READ;
- break;
- }
- fmt = 1;
- res = ioctl(fd, SNDCTL_DSP_STEREO, &fmt);
- if (res < 0) {
- ast_log(LOG_WARNING, "Failed to set audio device to mono\n");
- return -1;
- }
- fmt = desired = 48000;
- res = ioctl(fd, SNDCTL_DSP_SPEED, &fmt);
- if (res < 0) {
- ast_log(LOG_WARNING, "Failed to set audio device to mono\n");
- return -1;
- }
- if (fmt != desired) {
- if (!(o->warned & WARN_speed)) {
- ast_log(LOG_WARNING,
- "Requested %d Hz, got %d Hz -- sound may be choppy\n",
- desired, fmt);
- o->warned |= WARN_speed;
- }
- }
-
- if (o->frags) {
- fmt = o->frags;
- res = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &fmt);
- if (res < 0) {
- if (!(o->warned & WARN_frag)) {
- ast_log(LOG_WARNING,
- "Unable to set fragment size -- sound may be choppy\n");
- o->warned |= WARN_frag;
- }
- }
- }
-
- res = PCM_ENABLE_INPUT | PCM_ENABLE_OUTPUT;
- res = ioctl(fd, SNDCTL_DSP_SETTRIGGER, &res);
-
- return 0;
- }
- static int usbradio_digit_begin(struct ast_channel *c, char digit)
- {
- return 0;
- }
- static int usbradio_digit_end(struct ast_channel *c, char digit, unsigned int duration)
- {
-
- ast_verbose(" << Console Received digit %c of duration %u ms >> \n",
- digit, duration);
- return 0;
- }
- static int usbradio_text(struct ast_channel *c, const char *text)
- {
- struct chan_usbradio_pvt *o = find_desc(usbradio_active);
- double tx,rx;
- char cnt,rxs[16],txs[16],txpl[16],rxpl[16];
- char pwr,*cmd;
- cmd = alloca(strlen(text) + 10);
-
- if(o->debuglevel)ast_verbose(" << Console Received usbradio text %s >> \n", text);
- cnt = sscanf(text, "%300s %15s %15s %15s %15s %1c", cmd, rxs, txs, rxpl, txpl, &pwr);
- if (strcmp(cmd,"SETCHAN")==0)
- {
- u8 chan;
- chan=strtod(rxs,NULL);
- ppbinout(chan);
- if(o->debuglevel)ast_log(LOG_NOTICE,"parse usbradio SETCHAN cmd: %s chan: %i\n",text,chan);
- return 0;
- }
-
- if (cnt < 6)
- {
- ast_log(LOG_ERROR,"Cannot parse usbradio text: %s\n",text);
- return 0;
- }
- else
- {
- if(o->debuglevel)ast_verbose(" << %s %s %s %s %s %c >> \n", cmd,rxs,txs,rxpl,txpl,pwr);
- }
-
- if (strcmp(cmd,"SETFREQ")==0)
- {
- if(o->debuglevel)ast_log(LOG_NOTICE,"parse usbradio SETFREQ cmd: %s\n",text);
- tx=strtod(txs,NULL);
- rx=strtod(rxs,NULL);
- o->set_txfreq = round(tx * (double)1000000);
- o->set_rxfreq = round(rx * (double)1000000);
- o->pmrChan->txpower = (pwr == 'H');
- strcpy(o->set_rxctcssfreqs,rxpl);
- strcpy(o->set_txctcssfreqs,txpl);
-
- o->b.remoted=1;
- xpmr_config(o);
- return 0;
- }
- ast_log(LOG_ERROR,"Cannot parse usbradio cmd: %s\n",text);
- return 0;
- }
- static void ring(struct chan_usbradio_pvt *o, int x)
- {
- #ifndef NEW_ASTERISK
- write(o->sndcmd[1], &x, sizeof(x));
- #endif
- }
- static int usbradio_call(struct ast_channel *c, char *dest, int timeout)
- {
- struct chan_usbradio_pvt *o = c->tech_pvt;
- o->stophid = 0;
- time(&o->lasthidtime);
- ast_pthread_create_background(&o->hidthread, NULL, hidthread, o);
- ast_setstate(c, AST_STATE_UP);
- return 0;
- }
- static int usbradio_answer(struct ast_channel *c)
- {
- #ifndef NEW_ASTERISK
- struct chan_usbradio_pvt *o = c->tech_pvt;
- #endif
- ast_setstate(c, AST_STATE_UP);
- #ifndef NEW_ASTERISK
- o->cursound = -1;
- o->nosound = 0;
- #endif
- return 0;
- }
- static int usbradio_hangup(struct ast_channel *c)
- {
- struct chan_usbradio_pvt *o = c->tech_pvt;
-
- #ifndef NEW_ASTERISK
- o->cursound = -1;
- o->nosound = 0;
- #endif
- c->tech_pvt = NULL;
- o->owner = NULL;
- ast_module_unref(ast_module_info->self);
- if (o->hookstate) {
- if (o->autoanswer || o->autohangup) {
-
- o->hookstate = 0;
- setformat(o, O_CLOSE);
- } else {
-
- ring(o, AST_CONTROL_CONGESTION);
- }
- }
- o->stophid = 1;
- pthread_join(o->hidthread,NULL);
- return 0;
- }
- static int usbradio_write(struct ast_channel *c, struct ast_frame *f)
- {
- struct chan_usbradio_pvt *o = c->tech_pvt;
- traceusb2(("usbradio_write() o->nosound= %i\n",o->nosound));
- #ifndef NEW_ASTERISK
-
- if (o->nosound)
- return 0;
-
- o->cursound = -1;
- #endif
-
- #if DEBUG_CAPTURES == 1
- if (ftxcapraw && o->b.txcapraw)
- {
- i16 i, tbuff[f->datalen];
- for(i=0;i<f->datalen;i+=2)
- {
- tbuff[i]= ((i16*)(f->data.ptr))[i/2];
- tbuff[i+1]= o->txkeyed*M_Q13;
- }
- if (fwrite(tbuff,2,f->datalen,ftxcapraw) != f->datalen) {
- ast_log(LOG_ERROR, "write() failed: %s\n", strerror(errno));
- }
-
- }
- #endif
-
- PmrTx(o->pmrChan,(i16*)f->data.ptr);
-
- return 0;
- }
- static struct ast_frame *usbradio_read(struct ast_channel *c)
- {
- int res, src, datalen, oldpttout;
- int cd,sd;
- struct chan_usbradio_pvt *o = c->tech_pvt;
- struct ast_frame *f = &o->read_f,*f1;
- struct ast_frame wf = { AST_FRAME_CONTROL };
- time_t now;
- traceusb2(("usbradio_read()\n"));
- if (o->lasthidtime)
- {
- time(&now);
- if ((now - o->lasthidtime) > 3)
- {
- ast_log(LOG_ERROR,"HID process has died or something!!\n");
- return NULL;
- }
- }
-
-
- memset(f, '\0', sizeof(struct ast_frame));
- f->frametype = AST_FRAME_NULL;
- f->src = usbradio_tech.type;
- res = read(o->sounddev, o->usbradio_read_buf + o->readpos,
- sizeof(o->usbradio_read_buf) - o->readpos);
- if (res < 0)
- {
- if (errno != EAGAIN) return NULL;
- if (o->readerrs++ > READERR_THRESHOLD)
- {
- ast_log(LOG_ERROR,"Stuck USB read channel [%s], un-sticking it!\n",o->name);
- o->readerrs = 0;
- return NULL;
- }
- if (o->readerrs == 1)
- ast_log(LOG_WARNING,"Possibly stuck USB read channel. [%s]\n",o->name);
- return f;
- }
- if (o->readerrs) ast_log(LOG_WARNING,"Nope, USB read channel [%s] wasn't stuck after all.\n",o->name);
- o->readerrs = 0;
- o->readpos += res;
- if (o->readpos < sizeof(o->usbradio_read_buf))
- return f;
- if (o->mute)
- return f;
- #if DEBUG_CAPTURES == 1
- if ((o->b.rxcapraw && frxcapraw) && (fwrite((o->usbradio_read_buf + AST_FRIENDLY_OFFSET),1,FRAME_SIZE * 2 * 2 * 6,frxcapraw) != FRAME_SIZE * 2 * 2 * 6)) {
- ast_log(LOG_ERROR, "fwrite() failed: %s\n", strerror(errno));
- }
- #endif
- #if 1
- if(o->txkeyed||o->txtestkey)
- {
- if(!o->pmrChan->txPttIn)
- {
- o->pmrChan->txPttIn=1;
- if(o->debuglevel) ast_log(LOG_NOTICE,"txPttIn = %i, chan %s\n",o->pmrChan->txPttIn,o->owner->name);
- }
- }
- else if(o->pmrChan->txPttIn)
- {
- o->pmrChan->txPttIn=0;
- if(o->debuglevel) ast_log(LOG_NOTICE,"txPttIn = %i, chan %s\n",o->pmrChan->txPttIn,o->owner->name);
- }
- oldpttout = o->pmrChan->txPttOut;
- PmrRx( o->pmrChan,
- (i16 *)(o->usbradio_read_buf + AST_FRIENDLY_OFFSET),
- (i16 *)(o->usbradio_read_buf_8k + AST_FRIENDLY_OFFSET),
- (i16 *)(o->usbradio_write_buf_1));
- if (oldpttout != o->pmrChan->txPttOut)
- {
- if(o->debuglevel) ast_log(LOG_NOTICE,"txPttOut = %i, chan %s\n",o->pmrChan->txPttOut,o->owner->name);
- kickptt(o);
- }
- #if 0
- if (!ftxoutraw) ftxoutraw = fopen(TX_CAP_OUT_FILE,"w");
- if (ftxoutraw) fwrite(o->usbradio_write_buf_1,1,FRAME_SIZE * 2 * 6,ftxoutraw);
- #endif
- #if DEBUG_CAPTURES == 1 && XPMR_DEBUG0 == 1
- if ((o->b.txcap2 && ftxcaptrace) && (fwrite((o->pmrChan->ptxDebug),1,FRAME_SIZE * 2 * 16,ftxcaptrace) != FRAME_SIZE * 2 * 16)) {
- ast_log(LOG_ERROR, "fwrite() failed: %s\n", strerror(errno));
- }
- #endif
-
-
- datalen = FRAME_SIZE * 24;
- src = 0;
- while (src < datalen)
- {
-
- int l = sizeof(o->usbradio_write_buf) - o->usbradio_write_dst;
- if (datalen - src >= l)
- {
-
- memcpy(o->usbradio_write_buf + o->usbradio_write_dst, o->usbradio_write_buf_1 + src, l);
- soundcard_writeframe(o, (short *) o->usbradio_write_buf);
- src += l;
- o->usbradio_write_dst = 0;
- }
- else
- {
-
- l = datalen - src;
- memcpy(o->usbradio_write_buf + o->usbradio_write_dst, o->usbradio_write_buf_1 + src, l);
- src += l;
- o->usbradio_write_dst += l;
- }
- }
- #else
- static FILE *hInput;
- i16 iBuff[FRAME_SIZE*2*6];
- o->pmrChan->b.rxCapture=1;
- if(!hInput)
- {
- hInput = fopen("/usr/src/xpmr/testdata/rx_in.pcm","r");
- if(!hInput)
- {
- printf(" Input Data File Not Found.\n");
- return 0;
- }
- }
- if(0==fread((void *)iBuff,2,FRAME_SIZE*2*6,hInput))exit;
- PmrRx( o->pmrChan,
- (i16 *)iBuff,
- (i16 *)(o->usbradio_read_buf_8k + AST_FRIENDLY_OFFSET));
- #endif
- #if 0
- if (!frxoutraw) frxoutraw = fopen(RX_CAP_OUT_FILE,"w");
- if (frxoutraw) fwrite((o->usbradio_read_buf_8k + AST_FRIENDLY_OFFSET),1,FRAME_SIZE * 2,frxoutraw);
- #endif
- #if DEBUG_CAPTURES == 1 && XPMR_DEBUG0 == 1
- if ((frxcaptrace && o->b.rxcap2 && o->pmrChan->b.radioactive) && (fwrite((o->pmrChan->prxDebug),1,FRAME_SIZE * 2 * 16,frxcaptrace) != FRAME_SIZE * 2 * 16 )) {
- ast_log(LOG_ERROR, "fwrite() failed: %s\n", strerror(errno));
- }
- #endif
- cd = 0;
- if(o->rxcdtype==CD_HID && (o->pmrChan->rxExtCarrierDetect!=o->rxhidsq))
- o->pmrChan->rxExtCarrierDetect=o->rxhidsq;
-
- if(o->rxcdtype==CD_HID_INVERT && (o->pmrChan->rxExtCarrierDetect==o->rxhidsq))
- o->pmrChan->rxExtCarrierDetect=!o->rxhidsq;
-
- if( (o->rxcdtype==CD_HID && o->rxhidsq) ||
- (o->rxcdtype==CD_HID_INVERT && !o->rxhidsq) ||
- (o->rxcdtype==CD_XPMR_NOISE && o->pmrChan->rxCarrierDetect) ||
- (o->rxcdtype==CD_XPMR_VOX && o->pmrChan->rxCarrierDetect)
- )
- {
- if (!o->pmrChan->txPttOut || o->radioduplex)cd=1;
- }
- else
- {
- cd=0;
- }
- if(cd!=o->rxcarrierdetect)
- {
- o->rxcarrierdetect=cd;
- if(o->debuglevel) ast_log(LOG_NOTICE,"rxcarrierdetect = %i, chan %s\n",cd,o->owner->name);
-
- }
- if(o->pmrChan->b.ctcssRxEnable && o->pmrChan->rxCtcss->decode!=o->rxctcssdecode)
- {
- if(o->debuglevel)ast_log(LOG_NOTICE,"rxctcssdecode = %i, chan %s\n",o->pmrChan->rxCtcss->decode,o->owner->name);
-
- o->rxctcssdecode=o->pmrChan->rxCtcss->decode;
- strcpy(o->rxctcssfreq, o->pmrChan->rxctcssfreq);
- }
- #ifndef HAVE_XPMRX
- if( !o->pmrChan->b.ctcssRxEnable ||
- ( o->pmrChan->b.ctcssRxEnable &&
- o->pmrChan->rxCtcss->decode>CTCSS_NULL &&
- o->pmrChan->smode==SMODE_CTCSS )
- )
- {
- sd=1;
- }
- else
- {
- sd=0;
- }
- #else
- if( (!o->pmrChan->b.ctcssRxEnable && !o->pmrChan->b.dcsRxEnable && !o->pmrChan->b.lmrRxEnable) ||
- ( o->pmrChan->b.ctcssRxEnable &&
- o->pmrChan->rxCtcss->decode>CTCSS_NULL &&
- o->pmrChan->smode==SMODE_CTCSS ) ||
- ( o->pmrChan->b.dcsRxEnable &&
- o->pmrChan->decDcs->decode > 0 &&
- o->pmrChan->smode==SMODE_DCS )
- )
- {
- sd=1;
- }
- else
- {
- sd=0;
- }
- if(o->pmrChan->decDcs->decode!=o->rxdcsdecode)
- {
- if(o->debuglevel)ast_log(LOG_NOTICE,"rxdcsdecode = %s, chan %s\n",o->pmrChan->rxctcssfreq,o->owner->name);
-
- o->rxdcsdecode=o->pmrChan->decDcs->decode;
- strcpy(o->rxctcssfreq, o->pmrChan->rxctcssfreq);
- }
- if(o->pmrChan->rptnum && (o->pmrChan->pLsdCtl->cs[o->pmrChan->rptnum].b.rxkeyed != o->rxlsddecode))
- {
- if(o->debuglevel)ast_log(LOG_NOTICE,"rxLSDecode = %s, chan %s\n",o->pmrChan->rxctcssfreq,o->owner->name);
- o->rxlsddecode=o->pmrChan->pLsdCtl->cs[o->pmrChan->rptnum].b.rxkeyed;
- strcpy(o->rxctcssfreq, o->pmrChan->rxctcssfreq);
- }
- if( (o->pmrChan->rptnum>0 && o->pmrChan->smode==SMODE_LSD && o->pmrChan->pLsdCtl->cs[o->pmrChan->rptnum].b.rxkeyed)||
- (o->pmrChan->smode==SMODE_DCS && o->pmrChan->decDcs->decode>0) )
- {
- sd=1;
- }
- #endif
- if ( cd && sd )
- {
-
- if(!o->rxkeyed && o->debuglevel)ast_log(LOG_NOTICE,"o->rxkeyed = 1, chan %s\n", o->owner->name);
- o->rxkeyed = 1;
- }
- else
- {
-
- if(o->rxkeyed && o->debuglevel)ast_log(LOG_NOTICE,"o->rxkeyed = 0, chan %s\n",o->owner->name);
- o->rxkeyed = 0;
- }
-
- if (o->lastrx && (!o->rxkeyed))
- {
- o->lastrx = 0;
-
- wf.subclass.integer = AST_CONTROL_RADIO_UNKEY;
- ast_queue_frame(o->owner, &wf);
- }
- else if ((!o->lastrx) && (o->rxkeyed))
- {
- o->lastrx = 1;
-
- wf.subclass.integer = AST_CONTROL_RADIO_KEY;
- if(o->rxctcssdecode)
- {
- wf.data.ptr = o->rxctcssfreq;
- wf.datalen = strlen(o->rxctcssfreq) + 1;
- TRACEO(1,("AST_CONTROL_RADIO_KEY text=%s\n",o->rxctcssfreq));
- }
- ast_queue_frame(o->owner, &wf);
- }
- o->readpos = AST_FRIENDLY_OFFSET;
- if (c->_state != AST_STATE_UP)
- return f;
-
- f->frametype = AST_FRAME_VOICE;
- f->subclass.codec = AST_FORMAT_SLINEAR;
- f->samples = FRAME_SIZE;
- f->datalen = FRAME_SIZE * 2;
- f->data.ptr = o->usbradio_read_buf_8k + AST_FRIENDLY_OFFSET;
- if (o->boost != BOOST_SCALE) {
- int i, x;
- int16_t *p = (int16_t *) f->data.ptr;
- for (i = 0; i < f->samples; i++) {
- x = (p[i] * o->boost) / BOOST_SCALE;
- if (x > 32767)
- x = 32767;
- else if (x < -32768)
- x = -32768;
- p[i] = x;
- }
- }
- f->offset = AST_FRIENDLY_OFFSET;
- if (o->dsp)
- {
- f1 = ast_dsp_process(c,o->dsp,f);
- if ((f1->frametype == AST_FRAME_DTMF_END) ||
- (f1->frametype == AST_FRAME_DTMF_BEGIN))
- {
- if ((f1->subclass.integer == 'm') || (f1->subclass.integer == 'u'))
- {
- f1->frametype = AST_FRAME_NULL;
- f1->subclass.integer = 0;
- return(f1);
- }
- if (f1->frametype == AST_FRAME_DTMF_END)
- ast_log(LOG_NOTICE, "Got DTMF char %c\n", f1->subclass.integer);
- return(f1);
- }
- }
- return f;
- }
- static int usbradio_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
- {
- struct chan_usbradio_pvt *o = newchan->tech_pvt;
- ast_log(LOG_WARNING,"usbradio_fixup()\n");
- o->owner = newchan;
- return 0;
- }
- static int usbradio_indicate(struct ast_channel *c, int cond, const void *data, size_t datalen)
- {
- struct chan_usbradio_pvt *o = c->tech_pvt;
- int res = -1;
- switch (cond) {
- case AST_CONTROL_BUSY:
- case AST_CONTROL_CONGESTION:
- case AST_CONTROL_RINGING:
- res = cond;
- break;
- case AST_CONTROL_INCOMPLETE:
- res = AST_CONTROL_CONGESTION;
- break;
- case -1:
- #ifndef NEW_ASTERISK
- o->cursound = -1;
- o->nosound = 0;
- #endif
- return 0;
- case AST_CONTROL_VIDUPDATE:
- res = -1;
- break;
- case AST_CONTROL_HOLD:
- ast_verbose(" << Console Has Been Placed on Hold >> \n");
- ast_moh_start(c, data, o->mohinterpret);
- break;
- case AST_CONTROL_UNHOLD:
- ast_verbose(" << Console Has Been Retrieved from Hold >> \n");
- ast_moh_stop(c);
- break;
- case AST_CONTROL_PROCEEDING:
- ast_verbose(" << Call Proceeding... >> \n");
- ast_moh_stop(c);
- break;
- case AST_CONTROL_PROGRESS:
- ast_verbose(" << Call Progress... >> \n");
- ast_moh_stop(c);
- break;
- case AST_CONTROL_RADIO_KEY:
- o->txkeyed = 1;
- if(o->debuglevel)ast_verbose(" << AST_CONTROL_RADIO_KEY Radio Transmit On. >> \n");
- break;
- case AST_CONTROL_RADIO_UNKEY:
- o->txkeyed = 0;
- if(o->debuglevel)ast_verbose(" << AST_CONTROL_RADIO_UNKEY Radio Transmit Off. >> \n");
- break;
- default:
- ast_log(LOG_WARNING, "Don't know how to display condition %d on %s\n", cond, c->name);
- return -1;
- }
- if (res > -1)
- ring(o, res);
- return 0;
- }
- static struct ast_channel *usbradio_new(struct chan_usbradio_pvt *o, char *ext, char *ctx, int state, const char *linkedid)
- {
- struct ast_channel *c;
- c = ast_channel_alloc(1, state, o->cid_num, o->cid_name, "", ext, ctx, linkedid, 0, "Radio/%s", o->name);
- if (c == NULL)
- return NULL;
- c->tech = &usbradio_tech;
- if (o->sounddev < 0)
- setformat(o, O_RDWR);
- c->fds[0] = o->sounddev;
- c->nativeformats = AST_FORMAT_SLINEAR;
- c->readformat = AST_FORMAT_SLINEAR;
- c->writeformat = AST_FORMAT_SLINEAR;
- c->tech_pvt = o;
- if (!ast_strlen_zero(o->language))
- ast_string_field_set(c, language, o->language);
-
- if (!ast_strlen_zero(o->cid_num)) {
- c->caller.ani.number.valid = 1;
- c->caller.ani.number.str = ast_strdup(o->cid_num);
- }
- if (!ast_strlen_zero(ext)) {
- c->dialed.number.str = ast_strdup(ext);
- }
- o->owner = c;
- ast_module_ref(ast_module_info->self);
- ast_jb_configure(c, &global_jbconf);
- if (state != AST_STATE_DOWN) {
- if (ast_pbx_start(c)) {
- ast_log(LOG_WARNING, "Unable to start PBX on %s\n", c->name);
- ast_hangup(c);
- o->owner = c = NULL;
-
-
- }
- }
- return c;
- }
- static struct ast_channel *usbradio_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause)
- {
- struct ast_channel *c;
- struct chan_usbradio_pvt *o = find_desc(data);
- TRACEO(1,("usbradio_request()\n"));
- if (0)
- {
- ast_log(LOG_WARNING, "usbradio_request type <%s> data 0x%p <%s>\n", type, data, (char *) data);
- }
- if (o == NULL) {
- ast_log(LOG_NOTICE, "Device %s not found\n", (char *) data);
-
- return NULL;
- }
- if ((format & AST_FORMAT_SLINEAR) == 0) {
- ast_log(LOG_NOTICE, "Format 0x%" PRIx64 " unsupported\n", format);
- return NULL;
- }
- if (o->owner) {
- ast_log(LOG_NOTICE, "Already have a call (chan %p) on the usb channel\n", o->owner);
- *cause = AST_CAUSE_BUSY;
- return NULL;
- }
- c = usbradio_new(o, NULL, NULL, AST_STATE_DOWN, requestor ? requestor->linkedid : NULL);
- if (c == NULL) {
- ast_log(LOG_WARNING, "Unable to create new usb channel\n");
- return NULL;
- }
-
- o->b.remoted=0;
- xpmr_config(o);
- return c;
- }
- static int console_key(int fd, int argc, char *argv[])
- {
- struct chan_usbradio_pvt *o = find_desc(usbradio_active);
- if (argc != 2)
- return RESULT_SHOWUSAGE;
- o->txtestkey = 1;
- return RESULT_SUCCESS;
- }
- static int console_unkey(int fd, int argc, char *argv[])
- {
- struct chan_usbradio_pvt *o = find_desc(usbradio_active);
- if (argc != 2)
- return RESULT_SHOWUSAGE;
- o->txtestkey = 0;
- return RESULT_SUCCESS;
- }
- static int radio_tune(int fd, int argc, char *argv[])
- {
- struct chan_usbradio_pvt *o = find_desc(usbradio_active);
- int i=0;
- if ((argc < 2) || (argc > 4))
- return RESULT_SHOWUSAGE;
- if (argc == 2)
- {
- ast_cli(fd,"Active radio interface is [%s]\n",usbradio_active);
- ast_cli(fd,"Output A is currently set to ");
- if(o->txmixa==TX_OUT_COMPOSITE)ast_cli(fd,"composite.\n");
- else if (o->txmixa==TX_OUT_VOICE)ast_cli(fd,"voice.\n");
- else if (o->txmixa==TX_OUT_LSD)ast_cli(fd,"tone.\n");
- else if (o->txmixa==TX_OUT_AUX)ast_cli(fd,"auxvoice.\n");
- else ast_cli(fd,"off.\n");
- ast_cli(fd,"Output B is currently set to ");
- if(o->txmixb==TX_OUT_COMPOSITE)ast_cli(fd,"composite.\n");
- else if (o->txmixb==TX_OUT_VOICE)ast_cli(fd,"voice.\n");
- else if (o->txmixb==TX_OUT_LSD)ast_cli(fd,"tone.\n");
- else if (o->txmixb==TX_OUT_AUX)ast_cli(fd,"auxvoice.\n");
- else ast_cli(fd,"off.\n");
- ast_cli(fd,"Tx Voice Level currently set to %d\n",o->txmixaset);
- ast_cli(fd,"Tx Tone Level currently set to %d\n",o->txctcssadj);
- ast_cli(fd,"Rx Squelch currently set to %d\n",o->rxsquelchadj);
- ast_cli(fd,"Device String is %s\n",o->devstr);
- return RESULT_SHOWUSAGE;
- }
- o->pmrChan->b.tuning=1;
- if (!strcasecmp(argv[2],"rxnoise")) tune_rxinput(fd,o);
- else if (!strcasecmp(argv[2],"rxvoice")) tune_rxvoice(fd,o);
- else if (!strcasecmp(argv[2],"rxtone")) tune_rxctcss(fd,o);
- else if (!strcasecmp(argv[2],"rxsquelch"))
- {
- if (argc == 3)
- {
- ast_cli(fd,"Current Signal Strength is %d\n",((32767-o->pmrChan->rxRssi)*1000/32767));
- ast_cli(fd,"Current Squelch setting is %d\n",o->rxsquelchadj);
-
-
- } else {
- i = atoi(argv[3]);
- if ((i < 0) || (i > 999)) return RESULT_SHOWUSAGE;
- ast_cli(fd,"Changed Squelch setting to %d\n",i);
- o->rxsquelchadj = i;
- *(o->pmrChan->prxSquelchAdjust)= ((999 - i) * 32767) / 1000;
- }
- }
- else if (!strcasecmp(argv[2],"txvoice")) {
- i = 0;
- if( (o->txmixa!=TX_OUT_VOICE) && (o->txmixb!=TX_OUT_VOICE) &&
- (o->txmixa!=TX_OUT_COMPOSITE) && (o->txmixb!=TX_OUT_COMPOSITE)
- )
- {
- ast_log(LOG_ERROR,"No txvoice output configured.\n");
- }
- else if (argc == 3)
- {
- if((o->txmixa==TX_OUT_VOICE)||(o->txmixa==TX_OUT_COMPOSITE))
- ast_cli(fd,"Current txvoice setting on Channel A is %d\n",o->txmixaset);
- else
- ast_cli(fd,"Current txvoice setting on Channel B is %d\n",o->txmixbset);
- }
- else
- {
- i = atoi(argv[3]);
- if ((i < 0) || (i > 999)) return RESULT_SHOWUSAGE;
- if((o->txmixa==TX_OUT_VOICE)||(o->txmixa==TX_OUT_COMPOSITE))
- {
- o->txmixaset=i;
- ast_cli(fd,"Changed txvoice setting on Channel A to %d\n",o->txmixaset);
- }
- else
- {
- o->txmixbset=i;
- ast_cli(fd,"Changed txvoice setting on Channel B to %d\n",o->txmixbset);
- }
- mixer_write(o);
- mult_set(o);
- ast_cli(fd,"Changed Tx Voice Output setting to %d\n",i);
- }
- o->pmrChan->b.txCtcssInhibit=1;
- tune_txoutput(o,i,fd);
- o->pmrChan->b.txCtcssInhibit=0;
- }
- else if (!strcasecmp(argv[2],"txall")) {
- i = 0;
- if( (o->txmixa!=TX_OUT_VOICE) && (o->txmixb!=TX_OUT_VOICE) &&
- (o->txmixa!=TX_OUT_COMPOSITE) && (o->txmixb!=TX_OUT_COMPOSITE)
- )
- {
- ast_log(LOG_ERROR,"No txvoice output configured.\n");
- }
- else if (argc == 3)
- {
- if((o->txmixa==TX_OUT_VOICE)||(o->txmixa==TX_OUT_COMPOSITE))
- ast_cli(fd,"Current txvoice setting on Channel A is %d\n",o->txmixaset);
- else
- ast_cli(fd,"Current txvoice setting on Channel B is %d\n",o->txmixbset);
- }
- else
- {
- i = atoi(argv[3]);
- if ((i < 0) || (i > 999)) return RESULT_SHOWUSAGE;
- if((o->txmixa==TX_OUT_VOICE)||(o->txmixa==TX_OUT_COMPOSITE))
- {
- o->txmixaset=i;
- ast_cli(fd,"Changed txvoice setting on Channel A to %d\n",o->txmixaset);
- }
- else
- {
- o->txmixbset=i;
- ast_cli(fd,"Changed txvoice setting on Channel B to %d\n",o->txmixbset);
- }
- mixer_write(o);
- mult_set(o);
- ast_cli(fd,"Changed Tx Voice Output setting to %d\n",i);
- }
- tune_txoutput(o,i,fd);
- }
- else if (!strcasecmp(argv[2],"auxvoice")) {
- i = 0;
- if( (o->txmixa!=TX_OUT_AUX) && (o->txmixb!=TX_OUT_AUX))
- {
- ast_log(LOG_WARNING,"No auxvoice output configured.\n");
- }
- else if (argc == 3)
- {
- if(o->txmixa==TX_OUT_AUX)
- ast_cli(fd,"Current auxvoice setting on Channel A is %d\n",o->txmixaset);
- else
- ast_cli(fd,"Current auxvoice setting on Channel B is %d\n",o->txmixbset);
- }
- else
- {
- i = atoi(argv[3]);
- if ((i < 0) || (i > 999)) return RESULT_SHOWUSAGE;
- if(o->txmixa==TX_OUT_AUX)
- {
- o->txmixbset=i;
- ast_cli(fd,"Changed auxvoice setting on Channel A to %d\n",o->txmixaset);
- }
- else
- {
- o->txmixbset=i;
- ast_cli(fd,"Changed auxvoice setting on Channel B to %d\n",o->txmixbset);
- }
- mixer_write(o);
- mult_set(o);
- }
-
- }
- else if (!strcasecmp(argv[2],"txtone"))
- {
- if (argc == 3)
- ast_cli(fd,"Current Tx CTCSS modulation setting = %d\n",o->txctcssadj);
- else
- {
- i = atoi(argv[3]);
- if ((i < 0) || (i > 999)) return RESULT_SHOWUSAGE;
- o->txctcssadj = i;
- set_txctcss_level(o);
- ast_cli(fd,"Changed Tx CTCSS modulation setting to %i\n",i);
- }
- o->txtestkey=1;
- usleep(5000000);
- o->txtestkey=0;
- }
- else if (!strcasecmp(argv[2],"dump")) pmrdump(o);
- else if (!strcasecmp(argv[2],"nocap"))
- {
- ast_cli(fd,"File capture (trace) was rx=%d tx=%d and now off.\n",o->b.rxcap2,o->b.txcap2);
- ast_cli(fd,"File capture (raw) was rx=%d tx=%d and now off.\n",o->b.rxcapraw,o->b.txcapraw);
- o->b.rxcapraw=o->b.txcapraw=o->b.rxcap2=o->b.txcap2=o->pmrChan->b.rxCapture=o->pmrChan->b.txCapture=0;
- if (frxcapraw) { fclose(frxcapraw); frxcapraw = NULL; }
- if (frxcaptrace) { fclose(frxcaptrace); frxcaptrace = NULL; }
- if (frxoutraw) { fclose(frxoutraw); frxoutraw = NULL; }
- if (ftxcapraw) { fclose(ftxcapraw); ftxcapraw = NULL; }
- if (ftxcaptrace) { fclose(ftxcaptrace); ftxcaptrace = NULL; }
- if (ftxoutraw) { fclose(ftxoutraw); ftxoutraw = NULL; }
- }
- else if (!strcasecmp(argv[2],"rxtracecap"))
- {
- if (!frxcaptrace) frxcaptrace= fopen(RX_CAP_TRACE_FILE,"w");
- ast_cli(fd,"Trace rx on.\n");
- o->b.rxcap2=o->pmrChan->b.rxCapture=1;
- }
- else if (!strcasecmp(argv[2],"txtracecap"))
- {
- if (!ftxcaptrace) ftxcaptrace= fopen(TX_CAP_TRACE_FILE,"w");
- ast_cli(fd,"Trace tx on.\n");
- o->b.txcap2=o->pmrChan->b.txCapture=1;
- }
- else if (!strcasecmp(argv[2],"rxcap"))
- {
- if (!frxcapraw) frxcapraw = fopen(RX_CAP_RAW_FILE,"w");
- ast_cli(fd,"cap rx raw on.\n");
- o->b.rxcapraw=1;
- }
- else if (!strcasecmp(argv[2],"txcap"))
- {
- if (!ftxcapraw) ftxcapraw = fopen(TX_CAP_RAW_FILE,"w");
- ast_cli(fd,"cap tx raw on.\n");
- o->b.txcapraw=1;
- }
- else if (!strcasecmp(argv[2],"save"))
- {
- tune_write(o);
- ast_cli(fd,"Saved radio tuning settings to usbradio_tune_%s.conf\n",o->name);
- }
- else if (!strcasecmp(argv[2],"load"))
- {
- ast_mutex_lock(&o->eepromlock);
- while(o->eepromctl)
- {
- ast_mutex_unlock(&o->eepromlock);
- usleep(10000);
- ast_mutex_lock(&o->eepromlock);
- }
- o->eepromctl = 1;
- ast_mutex_unlock(&o->eepromlock);
- ast_cli(fd,"Requesting loading of tuning settings from EEPROM for channel %s\n",o->name);
- }
- else
- {
- o->pmrChan->b.tuning=0;
- return RESULT_SHOWUSAGE;
- }
- o->pmrChan->b.tuning=0;
- return RESULT_SUCCESS;
- }
- static int set_txctcss_level(struct chan_usbradio_pvt *o)
- {
- if (o->txmixa == TX_OUT_LSD)
- {
- o->txmixaset=o->txctcssadj;
- mixer_write(o);
- mult_set(o);
- }
- else if (o->txmixb == TX_OUT_LSD)
- {
- o->txmixbset=o->txctcssadj;
- mixer_write(o);
- mult_set(o);
- }
- else
- {
- *o->pmrChan->ptxCtcssAdjust=(o->txctcssadj * M_Q8) / 1000;
- }
- return 0;
- }
- static int radio_set_debug(int fd, int argc, char *argv[])
- {
- struct chan_usbradio_pvt *o = find_desc(usbradio_active);
- o->debuglevel=1;
- ast_cli(fd,"usbradio debug on.\n");
- return RESULT_SUCCESS;
- }
- static int radio_set_debug_off(int fd, int argc, char *argv[])
- {
- struct chan_usbradio_pvt *o = find_desc(usbradio_active);
- o->debuglevel=0;
- ast_cli(fd,"usbradio debug off.\n");
- return RESULT_SUCCESS;
- }
- static int radio_active(int fd, int argc, char *argv[])
- {
- if (argc == 2)
- ast_cli(fd, "active (command) USB Radio device is [%s]\n", usbradio_active);
- else if (argc != 3)
- return RESULT_SHOWUSAGE;
- else {
- struct chan_usbradio_pvt *o;
- if (strcmp(argv[2], "show") == 0) {
- for (o = usbradio_default.next; o; o = o->next)
- ast_cli(fd, "device [%s] exists\n", o->name);
- return RESULT_SUCCESS;
- }
- o = find_desc(argv[2]);
- if (o == NULL)
- ast_cli(fd, "No device [%s] exists\n", argv[2]);
- else
- {
- struct chan_usbradio_pvt *ao;
- for (ao = usbradio_default.next; ao && ao->name ; ao = ao->next)ao->pmrChan->b.radioactive=0;
- usbradio_active = o->name;
- o->pmrChan->b.radioactive=1;
- }
- }
- return RESULT_SUCCESS;
- }
- static int radio_set_xpmr_debug(int fd, int argc, char *argv[])
- {
- struct chan_usbradio_pvt *o = find_desc(usbradio_active);
- if (argc == 4)
- {
- int i;
- i = atoi(argv[3]);
- if ((i >= 0) && (i <= 100))
- {
- o->pmrChan->tracelevel=i;
- }
- }
-
- ast_cli(fd,"usbradio xdebug on tracelevel %i\n",o->pmrChan->tracelevel);
- return RESULT_SUCCESS;
- }
- static char key_usage[] =
- "Usage: radio key\n"
- " Simulates COR active.\n";
- static char unkey_usage[] =
- "Usage: radio unkey\n"
- " Simulates COR un-active.\n";
- static char active_usage[] =
- "Usage: radio active [device-name]\n"
- " If used without a parameter, displays which device is the current\n"
- "one being commanded. If a device is specified, the commanded radio device is changed\n"
- "to the device specified.\n";
- static char radio_tune_usage[] =
- "Usage: radio tune <function>\n"
- " rxnoise\n"
- " rxvoice\n"
- " rxtone\n"
- " rxsquelch [newsetting]\n"
- " txvoice [newsetting]\n"
- " txtone [newsetting]\n"
- " auxvoice [newsetting]\n"
- " save (settings to tuning file)\n"
- " load (tuning settings from EEPROM)\n"
- "\n All [newsetting]'s are values 0-999\n\n";
-
- #ifndef NEW_ASTERISK
- static struct ast_cli_entry cli_usbradio[] = {
- { { "radio", "key", NULL },
- console_key, "Simulate Rx Signal Present",
- key_usage, NULL, NULL},
- { { "radio", "unkey", NULL },
- console_unkey, "Simulate Rx Signal Lusb",
- unkey_usage, NULL, NULL },
- { { "radio", "tune", NULL },
- radio_tune, "Radio Tune",
- radio_tune_usage, NULL, NULL },
- { { "radio", "set", "debug", NULL },
- radio_set_debug, "Radio Debug",
- radio_tune_usage, NULL, NULL },
- { { "radio", "set", "debug", "off", NULL },
- radio_set_debug_off, "Radio Debug",
- radio_tune_usage, NULL, NULL },
- { { "radio", "active", NULL },
- radio_active, "Change commanded device",
- active_usage, NULL, NULL },
- { { "radio", "set", "xdebug", NULL },
- radio_set_xpmr_debug, "Radio set xpmr debug level",
- active_usage, NULL, NULL },
- };
- #endif
- #if 0
- static void store_callerid(struct chan_usbradio_pvt *o, char *s)
- {
- ast_callerid_split(s, o->cid_name, sizeof(o->cid_name), o->cid_num, sizeof(o->cid_num));
- }
- #endif
- static void store_rxdemod(struct chan_usbradio_pvt *o, const char *s)
- {
- if (!strcasecmp(s,"no")){
- o->rxdemod = RX_AUDIO_NONE;
- }
- else if (!strcasecmp(s,"speaker")){
- o->rxdemod = RX_AUDIO_SPEAKER;
- }
- else if (!strcasecmp(s,"flat")){
- o->rxdemod = RX_AUDIO_FLAT;
- }
- else {
- ast_log(LOG_WARNING,"Unrecognized rxdemod parameter: %s\n",s);
- }
-
- }
-
- static void store_txmixa(struct chan_usbradio_pvt *o, const char *s)
- {
- if (!strcasecmp(s,"no")){
- o->txmixa = TX_OUT_OFF;
- }
- else if (!strcasecmp(s,"voice")){
- o->txmixa = TX_OUT_VOICE;
- }
- else if (!strcasecmp(s,"tone")){
- o->txmixa = TX_OUT_LSD;
- }
- else if (!strcasecmp(s,"composite")){
- o->txmixa = TX_OUT_COMPOSITE;
- }
- else if (!strcasecmp(s,"auxvoice")){
- o->txmixa = TX_OUT_AUX;
- }
- else {
- ast_log(LOG_WARNING,"Unrecognized txmixa parameter: %s\n",s);
- }
-
- }
- static void store_txmixb(struct chan_usbradio_pvt *o, const char *s)
- {
- if (!strcasecmp(s,"no")){
- o->txmixb = TX_OUT_OFF;
- }
- else if (!strcasecmp(s,"voice")){
- o->txmixb = TX_OUT_VOICE;
- }
- else if (!strcasecmp(s,"tone")){
- o->txmixb = TX_OUT_LSD;
- }
- else if (!strcasecmp(s,"composite")){
- o->txmixb = TX_OUT_COMPOSITE;
- }
- else if (!strcasecmp(s,"auxvoice")){
- o->txmixb = TX_OUT_AUX;
- }
- else {
- ast_log(LOG_WARNING,"Unrecognized txmixb parameter: %s\n",s);
- }
-
- }
- static void store_rxcdtype(struct chan_usbradio_pvt *o, const char *s)
- {
- if (!strcasecmp(s,"no")){
- o->rxcdtype = CD_IGNORE;
- }
- else if (!strcasecmp(s,"usb")){
- o->rxcdtype = CD_HID;
- }
- else if (!strcasecmp(s,"dsp")){
- o->rxcdtype = CD_XPMR_NOISE;
- }
- else if (!strcasecmp(s,"vox")){
- o->rxcdtype = CD_XPMR_VOX;
- }
- else if (!strcasecmp(s,"usbinvert")){
- o->rxcdtype = CD_HID_INVERT;
- }
- else {
- ast_log(LOG_WARNING,"Unrecognized rxcdtype parameter: %s\n",s);
- }
-
- }
- static void store_rxsdtype(struct chan_usbradio_pvt *o, const char *s)
- {
- if (!strcasecmp(s,"no") || !strcasecmp(s,"SD_IGNORE")){
- o->rxsdtype = SD_IGNORE;
- }
- else if (!strcasecmp(s,"usb") || !strcasecmp(s,"SD_HID")){
- o->rxsdtype = SD_HID;
- }
- else if (!strcasecmp(s,"usbinvert") || !strcasecmp(s,"SD_HID_INVERT")){
- o->rxsdtype = SD_HID_INVERT;
- }
- else if (!strcasecmp(s,"software") || !strcasecmp(s,"SD_XPMR")){
- o->rxsdtype = SD_XPMR;
- }
- else {
- ast_log(LOG_WARNING,"Unrecognized rxsdtype parameter: %s\n",s);
- }
-
- }
- static void store_rxgain(struct chan_usbradio_pvt *o, const char *s)
- {
- float f;
- sscanf(s, "%30f", &f);
- o->rxgain = f;
-
- }
- static void store_rxvoiceadj(struct chan_usbradio_pvt *o, const char *s)
- {
- float f;
- sscanf(s, "%30f", &f);
- o->rxvoiceadj = f;
-
- }
- static void store_rxctcssadj(struct chan_usbradio_pvt *o, const char *s)
- {
- float f;
- sscanf(s, "%30f", &f);
- o->rxctcssadj = f;
-
- }
- static void store_txtoctype(struct chan_usbradio_pvt *o, const char *s)
- {
- if (!strcasecmp(s,"no") || !strcasecmp(s,"TOC_NONE")){
- o->txtoctype = TOC_NONE;
- }
- else if (!strcasecmp(s,"phase") || !strcasecmp(s,"TOC_PHASE")){
- o->txtoctype = TOC_PHASE;
- }
- else if (!strcasecmp(s,"notone") || !strcasecmp(s,"TOC_NOTONE")){
- o->txtoctype = TOC_NOTONE;
- }
- else {
- ast_log(LOG_WARNING,"Unrecognized txtoctype parameter: %s\n",s);
- }
- }
- static void tune_txoutput(struct chan_usbradio_pvt *o, int value, int fd)
- {
- o->txtestkey=1;
- o->pmrChan->txPttIn=1;
- TxTestTone(o->pmrChan, 1);
- if (fd > 0) ast_cli(fd,"Tone output starting on channel %s...\n",o->name);
- usleep(5000000);
- TxTestTone(o->pmrChan, 0);
- if (fd > 0) ast_cli(fd,"Tone output ending on channel %s...\n",o->name);
- o->pmrChan->txPttIn=0;
- o->txtestkey=0;
- }
- static void tune_rxinput(int fd, struct chan_usbradio_pvt *o)
- {
- const int target=23000;
- const int tolerance=2000;
- const int settingmin=1;
- const int settingstart=2;
- const int maxtries=12;
- float settingmax;
-
- int setting=0, tries=0, tmpdiscfactor, meas;
- int tunetype=0;
- settingmax = o->micmax;
- if(o->pmrChan->rxDemod)tunetype=1;
- o->pmrChan->b.tuning=1;
- setting = settingstart;
- ast_cli(fd,"tune rxnoise maxtries=%i, target=%i, tolerance=%i\n",maxtries,target,tolerance);
- while(tries<maxtries)
- {
- setamixer(o->devicenum,MIXER_PARAM_MIC_CAPTURE_VOL,setting,0);
- setamixer(o->devicenum,MIXER_PARAM_MIC_BOOST,o->rxboostset,0);
-
- usleep(100000);
- if(o->rxcdtype!=CD_XPMR_NOISE || o->rxdemod==RX_AUDIO_SPEAKER)
- {
-
- o->pmrChan->spsMeasure->source = o->pmrChan->spsRx->source;
- o->pmrChan->spsMeasure->discfactor=2000;
- o->pmrChan->spsMeasure->enabled=1;
- o->pmrChan->spsMeasure->amax = o->pmrChan->spsMeasure->amin = 0;
- usleep(400000);
- meas=o->pmrChan->spsMeasure->apeak;
- o->pmrChan->spsMeasure->enabled=0;
- }
- else
- {
-
- tmpdiscfactor=o->pmrChan->spsRx->discfactor;
- o->pmrChan->spsRx->discfactor=(i16)2000;
- o->pmrChan->spsRx->discounteru=o->pmrChan->spsRx->discounterl=0;
- o->pmrChan->spsRx->amax=o->pmrChan->spsRx->amin=0;
- usleep(200000);
- meas=o->pmrChan->rxRssi;
- o->pmrChan->spsRx->discfactor=tmpdiscfactor;
- o->pmrChan->spsRx->discounteru=o->pmrChan->spsRx->discounterl=0;
- o->pmrChan->spsRx->amax=o->pmrChan->spsRx->amin=0;
- }
- if(!meas)meas++;
- ast_cli(fd,"tries=%i, setting=%i, meas=%i\n",tries,setting,meas);
- if( meas<(target-tolerance) || meas>(target+tolerance) || tries<3){
- setting=setting*target/meas;
- }
- else if(tries>4 && meas>(target-tolerance) && meas<(target+tolerance) )
- {
- break;
- }
- if(setting<settingmin)setting=settingmin;
- else if(setting>settingmax)setting=settingmax;
- tries++;
- }
- ast_cli(fd,"DONE tries=%i, setting=%i, meas=%i\n",tries,
- (setting * 1000) / o->micmax,meas);
- if( meas<(target-tolerance) || meas>(target+tolerance) ){
- ast_cli(fd,"ERROR: RX INPUT ADJUST FAILED.\n");
- }else{
- ast_cli(fd,"INFO: RX INPUT ADJUST SUCCESS.\n");
- o->rxmixerset=(setting * 1000) / o->micmax;
- }
- o->pmrChan->b.tuning=0;
- }
- static void tune_rxvoice(int fd, struct chan_usbradio_pvt *o)
- {
- const int target=7200;
- const int tolerance=360;
- const float settingmin=0.1;
- const float settingmax=4;
- const float settingstart=1;
- const int maxtries=12;
- float setting;
- int tries=0, meas;
- ast_cli(fd,"INFO: RX VOICE ADJUST START.\n");
- ast_cli(fd,"target=%i tolerance=%i \n",target,tolerance);
- o->pmrChan->b.tuning=1;
- if(!o->pmrChan->spsMeasure)
- ast_cli(fd,"ERROR: NO MEASURE BLOCK.\n");
- if(!o->pmrChan->spsMeasure->source || !o->pmrChan->prxVoiceAdjust )
- ast_cli(fd,"ERROR: NO SOURCE OR MEASURE SETTING.\n");
- o->pmrChan->spsMeasure->source=o->pmrChan->spsRxOut->sink;
- o->pmrChan->spsMeasure->enabled=1;
- o->pmrChan->spsMeasure->discfactor=1000;
-
- setting=settingstart;
-
- while(tries<maxtries)
- {
- *(o->pmrChan->prxVoiceAdjust)=setting*M_Q8;
- usleep(10000);
- o->pmrChan->spsMeasure->amax = o->pmrChan->spsMeasure->amin = 0;
- usleep(1000000);
- meas = o->pmrChan->spsMeasure->apeak;
- ast_cli(fd,"tries=%i, setting=%f, meas=%i\n",tries,setting,meas);
- if( meas<(target-tolerance) || meas>(target+tolerance) || tries<3){
- setting=setting*target/meas;
- }
- else if(tries>4 && meas>(target-tolerance) && meas<(target+tolerance) )
- {
- break;
- }
- if(setting<settingmin)setting=settingmin;
- else if(setting>settingmax)setting=settingmax;
- tries++;
- }
- o->pmrChan->spsMeasure->enabled=0;
- ast_cli(fd,"DONE tries=%i, setting=%f, meas=%f\n",tries,setting,(float)meas);
- if( meas<(target-tolerance) || meas>(target+tolerance) ){
- ast_cli(fd,"ERROR: RX VOICE GAIN ADJUST FAILED.\n");
- }else{
- ast_cli(fd,"INFO: RX VOICE GAIN ADJUST SUCCESS.\n");
- o->rxvoiceadj=setting;
- }
- o->pmrChan->b.tuning=0;
- }
- static void tune_rxctcss(int fd, struct chan_usbradio_pvt *o)
- {
- const int target=2400;
- const int tolerance=100;
- const float settingmin=0.1;
- const float settingmax=8;
- const float settingstart=1;
- const int maxtries=12;
- float setting;
- int tries=0, meas;
- ast_cli(fd,"INFO: RX CTCSS ADJUST START.\n");
- ast_cli(fd,"target=%i tolerance=%i \n",target,tolerance);
- o->pmrChan->b.tuning=1;
- o->pmrChan->spsMeasure->source=o->pmrChan->prxCtcssMeasure;
- o->pmrChan->spsMeasure->discfactor=400;
- o->pmrChan->spsMeasure->enabled=1;
- setting=settingstart;
- while(tries<maxtries)
- {
- *(o->pmrChan->prxCtcssAdjust)=setting*M_Q8;
- usleep(10000);
- o->pmrChan->spsMeasure->amax = o->pmrChan->spsMeasure->amin = 0;
- usleep(500000);
- meas = o->pmrChan->spsMeasure->apeak;
- ast_cli(fd,"tries=%i, setting=%f, meas=%i\n",tries,setting,meas);
- if( meas<(target-tolerance) || meas>(target+tolerance) || tries<3){
- setting=setting*target/meas;
- }
- else if(tries>4 && meas>(target-tolerance) && meas<(target+tolerance) )
- {
- break;
- }
- if(setting<settingmin)setting=settingmin;
- else if(setting>settingmax)setting=settingmax;
- tries++;
- }
- o->pmrChan->spsMeasure->enabled=0;
- ast_cli(fd,"DONE tries=%i, setting=%f, meas=%f\n",tries,setting,(float)meas);
- if( meas<(target-tolerance) || meas>(target+tolerance) ){
- ast_cli(fd,"ERROR: RX CTCSS GAIN ADJUST FAILED.\n");
- }else{
- ast_cli(fd,"INFO: RX CTCSS GAIN ADJUST SUCCESS.\n");
- o->rxctcssadj=setting;
- }
- o->pmrChan->b.tuning=0;
- }
- static void tune_write(struct chan_usbradio_pvt *o)
- {
- FILE *fp;
- char fname[200];
- snprintf(fname,sizeof(fname) - 1,"/etc/asterisk/usbradio_tune_%s.conf",o->name);
- fp = fopen(fname,"w");
- fprintf(fp,"[%s]\n",o->name);
- fprintf(fp,"; name=%s\n",o->name);
- fprintf(fp,"; devicenum=%i\n",o->devicenum);
- fprintf(fp,"devstr=%s\n",o->devstr);
- fprintf(fp,"rxmixerset=%i\n",o->rxmixerset);
- fprintf(fp,"txmixaset=%i\n",o->txmixaset);
- fprintf(fp,"txmixbset=%i\n",o->txmixbset);
- fprintf(fp,"rxvoiceadj=%f\n",o->rxvoiceadj);
- fprintf(fp,"rxctcssadj=%f\n",o->rxctcssadj);
- fprintf(fp,"txctcssadj=%i\n",o->txctcssadj);
- fprintf(fp,"rxsquelchadj=%i\n",o->rxsquelchadj);
- fclose(fp);
- if(o->wanteeprom)
- {
- ast_mutex_lock(&o->eepromlock);
- while(o->eepromctl)
- {
- ast_mutex_unlock(&o->eepromlock);
- usleep(10000);
- ast_mutex_lock(&o->eepromlock);
- }
- o->eeprom[EEPROM_RXMIXERSET] = o->rxmixerset;
- o->eeprom[EEPROM_TXMIXASET] = o->txmixaset;
- o->eeprom[EEPROM_TXMIXBSET] = o->txmixbset;
- memcpy(&o->eeprom[EEPROM_RXVOICEADJ],&o->rxvoiceadj,sizeof(float));
- memcpy(&o->eeprom[EEPROM_RXCTCSSADJ],&o->rxctcssadj,sizeof(float));
- o->eeprom[EEPROM_TXCTCSSADJ] = o->txctcssadj;
- o->eeprom[EEPROM_RXSQUELCHADJ] = o->rxsquelchadj;
- o->eepromctl = 2;
- ast_mutex_unlock(&o->eepromlock);
- }
- }
- static void mixer_write(struct chan_usbradio_pvt *o)
- {
- setamixer(o->devicenum,MIXER_PARAM_MIC_PLAYBACK_SW,0,0);
- setamixer(o->devicenum,MIXER_PARAM_MIC_PLAYBACK_VOL,0,0);
- setamixer(o->devicenum,MIXER_PARAM_SPKR_PLAYBACK_SW,1,0);
- setamixer(o->devicenum,MIXER_PARAM_SPKR_PLAYBACK_VOL,
- o->txmixaset * o->spkrmax / 1000,
- o->txmixbset * o->spkrmax / 1000);
- setamixer(o->devicenum,MIXER_PARAM_MIC_CAPTURE_VOL,
- o->rxmixerset * o->micmax / 1000,0);
- setamixer(o->devicenum,MIXER_PARAM_MIC_BOOST,o->rxboostset,0);
- setamixer(o->devicenum,MIXER_PARAM_MIC_CAPTURE_SW,1,0);
- }
- static void mult_set(struct chan_usbradio_pvt *o)
- {
- if(o->pmrChan->spsTxOutA) {
- o->pmrChan->spsTxOutA->outputGain =
- mult_calc((o->txmixaset * 152) / 1000);
- }
- if(o->pmrChan->spsTxOutB){
- o->pmrChan->spsTxOutB->outputGain =
- mult_calc((o->txmixbset * 152) / 1000);
- }
- }
- static int mult_calc(int value)
- {
- const int multx=M_Q8;
- int pot,mult;
- pot=((int)(value/4)*4)+2;
- mult = multx-( ( multx * (3-(value%4)) ) / (pot+2) );
- return(mult);
- }
- #define pd(x) {printf(#x" = %d\n",x);}
- #define pp(x) {printf(#x" = %p\n",x);}
- #define ps(x) {printf(#x" = %s\n",x);}
- #define pf(x) {printf(#x" = %f\n",x);}
- #if 0
- static int usbhider(struct chan_usbradio_pvt *o, int opt)
- {
- unsigned char buf[4];
- char lastrx, txtmp;
- if(opt)
- {
- struct usb_device *usb_dev;
- usb_dev = hid_device_init(o->devstr);
- if (usb_dev == NULL) {
- ast_log(LOG_ERROR,"USB HID device not found\n");
- return -1;
- }
- o->usb_handle = usb_open(usb_dev);
- if (o->usb_handle == NULL) {
- ast_log(LOG_ERROR,"Not able to open USB device\n");
- return -1;
- }
- if (usb_claim_interface(o->usb_handle,C108_HID_INTERFACE) < 0)
- {
- if (usb_detach_kernel_driver_np(o->usb_handle,C108_HID_INTERFACE) < 0) {
- ast_log(LOG_ERROR,"Not able to detach the USB device\n");
- return -1;
- }
- if (usb_claim_interface(o->usb_handle,C108_HID_INTERFACE) < 0) {
- ast_log(LOG_ERROR,"Not able to claim the USB device\n");
- return -1;
- }
- }
-
- memset(buf,0,sizeof(buf));
- buf[2] = o->hid_gpio_ctl;
- buf[1] = 0;
- hid_set_outputs(o->usb_handle,buf);
- memcpy(bufsave,buf,sizeof(buf));
-
- buf[o->hid_gpio_ctl_loc] = o->hid_gpio_ctl;
- o->lasttx=0;
- }
-
- txtmp=o->pmrChan->txPttOut;
-
- if (o->lasttx != txtmp)
- {
- o->pmrChan->txPttHid=o->lasttx = txtmp;
- if(o->debuglevel)printf("usbhid: tx set to %d\n",txtmp);
- buf[o->hid_gpio_loc] = 0;
- if (!o->invertptt)
- {
- if (txtmp) buf[o->hid_gpio_loc] = o->hid_io_ptt;
- }
- else
- {
- if (!txtmp) buf[o->hid_gpio_loc] = o->hid_io_ptt;
- }
- buf[o->hid_gpio_ctl_loc] = o->hid_gpio_ctl;
- hid_set_outputs(o->usb_handle,buf);
- }
- return(0);
- }
- #endif
- static void pmrdump(struct chan_usbradio_pvt *o)
- {
- t_pmr_chan *p;
- int i;
- p=o->pmrChan;
- printf("\nodump()\n");
- pd(o->devicenum);
- ps(o->devstr);
- pd(o->micmax);
- pd(o->spkrmax);
- pd(o->rxdemod);
- pd(o->rxcdtype);
- pd(o->rxsdtype);
- pd(o->txtoctype);
- pd(o->rxmixerset);
- pd(o->rxboostset);
- pf(o->rxvoiceadj);
- pf(o->rxctcssadj);
- pd(o->rxsquelchadj);
- ps(o->txctcssdefault);
- ps(o->txctcssfreq);
- pd(o->numrxctcssfreqs);
- if(o->numrxctcssfreqs>0)
- {
- for(i=0;i<o->numrxctcssfreqs;i++)
- {
- printf(" %i = %s %s\n",i,o->rxctcss[i],o->txctcss[i]);
- }
- }
- pd(o->b.rxpolarity);
- pd(o->b.txpolarity);
- pd(o->txprelim);
- pd(o->txmixa);
- pd(o->txmixb);
-
- pd(o->txmixaset);
- pd(o->txmixbset);
-
- printf("\npmrdump()\n");
-
- pd(p->devicenum);
- printf("prxSquelchAdjust=%i\n",*(o->pmrChan->prxSquelchAdjust));
- pd(p->rxCarrierPoint);
- pd(p->rxCarrierHyst);
- pd(*p->prxVoiceAdjust);
- pd(*p->prxCtcssAdjust);
- pd(p->rxfreq);
- pd(p->txfreq);
- pd(p->rxCtcss->relax);
-
- pd(p->numrxcodes);
- if(o->pmrChan->numrxcodes>0)
- {
- for(i=0;i<o->pmrChan->numrxcodes;i++)
- {
- printf(" %i = %s\n",i,o->pmrChan->pRxCode[i]);
- }
- }
- pd(p->txTocType);
- ps(p->pTxCodeDefault);
- pd(p->txcodedefaultsmode);
- pd(p->numtxcodes);
- if(o->pmrChan->numtxcodes>0)
- {
- for(i=0;i<o->pmrChan->numtxcodes;i++)
- {
- printf(" %i = %s\n",i,o->pmrChan->pTxCode[i]);
- }
- }
- pd(p->b.rxpolarity);
- pd(p->b.txpolarity);
- pd(p->b.dcsrxpolarity);
- pd(p->b.dcstxpolarity);
- pd(p->b.lsdrxpolarity);
- pd(p->b.lsdtxpolarity);
- pd(p->txMixA);
- pd(p->txMixB);
-
- pd(p->rxDeEmpEnable);
- pd(p->rxCenterSlicerEnable);
- pd(p->rxCtcssDecodeEnable);
- pd(p->rxDcsDecodeEnable);
- pd(p->b.ctcssRxEnable);
- pd(p->b.dcsRxEnable);
- pd(p->b.lmrRxEnable);
- pd(p->b.dstRxEnable);
- pd(p->smode);
- pd(p->txHpfEnable);
- pd(p->txLimiterEnable);
- pd(p->txPreEmpEnable);
- pd(p->txLpfEnable);
- if(p->spsTxOutA)pd(p->spsTxOutA->outputGain);
- if(p->spsTxOutB)pd(p->spsTxOutB->outputGain);
- pd(p->txPttIn);
- pd(p->txPttOut);
- pd(p->tracetype);
- return;
- }
- static int xpmr_config(struct chan_usbradio_pvt *o)
- {
-
- TRACEO(1,("xpmr_config()\n"));
- if(o->pmrChan==NULL)
- {
- ast_log(LOG_ERROR,"pmr channel structure NULL\n");
- return 1;
- }
- o->pmrChan->rxCtcss->relax = o->rxctcssrelax;
- o->pmrChan->txpower=0;
- if(o->b.remoted)
- {
- o->pmrChan->pTxCodeDefault = o->set_txctcssdefault;
- o->pmrChan->pRxCodeSrc=o->set_rxctcssfreqs;
- o->pmrChan->pTxCodeSrc=o->set_txctcssfreqs;
- o->pmrChan->rxfreq=o->set_rxfreq;
- o->pmrChan->txfreq=o->set_txfreq;
-
- }
- else
- {
-
- o->pmrChan->pTxCodeDefault = o->txctcssdefault;
- o->pmrChan->pRxCodeSrc = o->rxctcssfreqs;
- o->pmrChan->pTxCodeSrc = o->txctcssfreqs;
-
- o->pmrChan->rxfreq = o->rxfreq;
- o->pmrChan->txfreq = o->txfreq;
- }
-
- code_string_parse(o->pmrChan);
- if(o->pmrChan->rxfreq) o->pmrChan->b.reprog=1;
- return 0;
- }
- static struct chan_usbradio_pvt *store_config(struct ast_config *cfg, char *ctg)
- {
- struct ast_variable *v;
- struct chan_usbradio_pvt *o;
- struct ast_config *cfg1;
- int i;
- char fname[200];
- #ifdef NEW_ASTERISK
- struct ast_flags zeroflag = {0};
- #endif
- if (ctg == NULL) {
- traceusb1((" store_config() ctg == NULL\n"));
- o = &usbradio_default;
- ctg = "general";
- } else {
-
- if (strcmp(ctg, "general") == 0) {
- o = &usbradio_default;
- } else {
-
- if (!(o = ast_calloc(1, sizeof(*o))))
- return NULL;
- *o = usbradio_default;
- o->name = ast_strdup(ctg);
- if (!usbradio_active)
- usbradio_active = o->name;
- }
- }
- ast_mutex_init(&o->eepromlock);
- strcpy(o->mohinterpret, "default");
-
- for (v = ast_variable_browse(cfg, ctg); v; v = v->next) {
- M_START((char *)v->name, (char *)v->value);
-
- if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
- continue;
- #if 0
- M_BOOL("autoanswer", o->autoanswer)
- M_BOOL("autohangup", o->autohangup)
- M_BOOL("overridecontext", o->overridecontext)
- M_STR("context", o->ctx)
- M_STR("language", o->language)
- M_STR("mohinterpret", o->mohinterpret)
- M_STR("extension", o->ext)
- M_F("callerid", store_callerid(o, v->value))
- #endif
- M_UINT("frags", o->frags)
- M_UINT("queuesize",o->queuesize)
- #if 0
- M_UINT("devicenum",o->devicenum)
- #endif
- M_UINT("debug", usbradio_debug)
- M_BOOL("rxcpusaver",o->rxcpusaver)
- M_BOOL("txcpusaver",o->txcpusaver)
- M_BOOL("invertptt",o->invertptt)
- M_F("rxdemod",store_rxdemod(o,(char *)v->value))
- M_BOOL("txprelim",o->txprelim);
- M_F("txmixa",store_txmixa(o,(char *)v->value))
- M_F("txmixb",store_txmixb(o,(char *)v->value))
- M_F("carrierfrom",store_rxcdtype(o,(char *)v->value))
- M_F("rxsdtype",store_rxsdtype(o,(char *)v->value))
- M_UINT("rxsqvox",o->rxsqvoxadj)
- M_STR("txctcssdefault",o->txctcssdefault)
- M_STR("rxctcssfreqs",o->rxctcssfreqs)
- M_STR("txctcssfreqs",o->txctcssfreqs)
- M_UINT("rxfreq",o->rxfreq)
- M_UINT("txfreq",o->txfreq)
- M_F("rxgain",store_rxgain(o,(char *)v->value))
- M_BOOL("rxboost",o->rxboostset)
- M_UINT("rxctcssrelax",o->rxctcssrelax)
- M_F("txtoctype",store_txtoctype(o,(char *)v->value))
- M_UINT("hdwtype",o->hdwtype)
- M_UINT("eeprom",o->wanteeprom)
- M_UINT("duplex",o->radioduplex)
- M_UINT("txsettletime",o->txsettletime)
- M_BOOL("rxpolarity",o->b.rxpolarity)
- M_BOOL("txpolarity",o->b.txpolarity)
- M_BOOL("dcsrxpolarity",o->b.dcsrxpolarity)
- M_BOOL("dcstxpolarity",o->b.dcstxpolarity)
- M_BOOL("lsdrxpolarity",o->b.lsdrxpolarity)
- M_BOOL("lsdtxpolarity",o->b.lsdtxpolarity)
- M_BOOL("loopback",o->b.loopback)
- M_BOOL("radioactive",o->b.radioactive)
- M_UINT("rptnum",o->rptnum)
- M_UINT("idleinterval",o->idleinterval)
- M_UINT("turnoffs",o->turnoffs)
- M_UINT("tracetype",o->tracetype)
- M_UINT("tracelevel",o->tracelevel)
- M_UINT("area",o->area)
- M_STR("ukey",o->ukey)
- M_END(;
- );
- }
- o->debuglevel=0;
- if (o == &usbradio_default)
- return NULL;
- snprintf(fname,sizeof(fname) - 1,config1,o->name);
- #ifdef NEW_ASTERISK
- cfg1 = ast_config_load(fname,zeroflag);
- #else
- cfg1 = ast_config_load(fname);
- #endif
- o->rxmixerset = 500;
- o->txmixaset = 500;
- o->txmixbset = 500;
- o->rxvoiceadj = 0.5;
- o->rxctcssadj = 0.5;
- o->txctcssadj = 200;
- o->rxsquelchadj = 500;
- o->devstr[0] = 0;
- if (cfg1 && cfg1 != CONFIG_STATUS_FILEINVALID) {
- for (v = ast_variable_browse(cfg1, o->name); v; v = v->next) {
-
- M_START((char *)v->name, (char *)v->value);
- M_UINT("rxmixerset", o->rxmixerset)
- M_UINT("txmixaset", o->txmixaset)
- M_UINT("txmixbset", o->txmixbset)
- M_F("rxvoiceadj",store_rxvoiceadj(o,(char *)v->value))
- M_F("rxctcssadj",store_rxctcssadj(o,(char *)v->value))
- M_UINT("txctcssadj",o->txctcssadj);
- M_UINT("rxsquelchadj", o->rxsquelchadj)
- M_STR("devstr", o->devstr)
- M_END(;
- );
- }
- ast_config_destroy(cfg1);
- } else ast_log(LOG_WARNING,"File %s not found, using default parameters.\n",fname);
- if(o->wanteeprom)
- {
- ast_mutex_lock(&o->eepromlock);
- while(o->eepromctl)
- {
- ast_mutex_unlock(&o->eepromlock);
- usleep(10000);
- ast_mutex_lock(&o->eepromlock);
- }
- o->eepromctl = 1;
- ast_mutex_unlock(&o->eepromlock);
- }
-
- if ((!usb_list_check(o->devstr)) || find_desc_usb(o->devstr))
- {
- char *s;
- for(s = usb_device_list; *s; s += strlen(s) + 1)
- {
- if (!find_desc_usb(s)) break;
- }
- if (!*s)
- {
- ast_log(LOG_WARNING,"Unable to assign USB device for channel %s\n",o->name);
- goto error;
- }
- ast_log(LOG_NOTICE,"Assigned USB device %s to usbradio channel %s\n",s,o->name);
- strcpy(o->devstr,s);
- }
- i = usb_get_usbdev(o->devstr);
- if (i < 0)
- {
- ast_log(LOG_ERROR,"Not able to find alsa USB device\n");
- goto error;
- }
- o->devicenum = i;
- o->micmax = amixer_max(o->devicenum,MIXER_PARAM_MIC_CAPTURE_VOL);
- o->spkrmax = amixer_max(o->devicenum,MIXER_PARAM_SPKR_PLAYBACK_VOL);
- o->lastopen = ast_tvnow();
- o->dsp = ast_dsp_new();
- if (o->dsp)
- {
- #ifdef NEW_ASTERISK
- ast_dsp_set_features(o->dsp,DSP_FEATURE_DIGIT_DETECT);
- ast_dsp_set_digitmode(o->dsp,DSP_DIGITMODE_DTMF | DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_RELAXDTMF);
- #else
- ast_dsp_set_features(o->dsp,DSP_FEATURE_DTMF_DETECT);
- ast_dsp_digitmode(o->dsp,DSP_DIGITMODE_DTMF | DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_RELAXDTMF);
- #endif
- }
- if(o->pmrChan==NULL)
- {
- t_pmr_chan tChan;
-
- memset(&tChan,0,sizeof(t_pmr_chan));
- tChan.pTxCodeDefault = o->txctcssdefault;
- tChan.pRxCodeSrc = o->rxctcssfreqs;
- tChan.pTxCodeSrc = o->txctcssfreqs;
- tChan.rxDemod=o->rxdemod;
- tChan.rxCdType=o->rxcdtype;
- tChan.rxSqVoxAdj=o->rxsqvoxadj;
- if (o->txprelim)
- tChan.txMod = 2;
- tChan.txMixA = o->txmixa;
- tChan.txMixB = o->txmixb;
- tChan.rxCpuSaver=o->rxcpusaver;
- tChan.txCpuSaver=o->txcpusaver;
- tChan.b.rxpolarity=o->b.rxpolarity;
- tChan.b.txpolarity=o->b.txpolarity;
- tChan.b.dcsrxpolarity=o->b.dcsrxpolarity;
- tChan.b.dcstxpolarity=o->b.dcstxpolarity;
- tChan.b.lsdrxpolarity=o->b.lsdrxpolarity;
- tChan.b.lsdtxpolarity=o->b.lsdtxpolarity;
- tChan.tracetype=o->tracetype;
- tChan.tracelevel=o->tracelevel;
- tChan.rptnum=o->rptnum;
- tChan.idleinterval=o->idleinterval;
- tChan.turnoffs=o->turnoffs;
- tChan.area=o->area;
- tChan.ukey=o->ukey;
- tChan.name=o->name;
- o->pmrChan=createPmrChannel(&tChan,FRAME_SIZE);
-
- o->pmrChan->radioDuplex=o->radioduplex;
- o->pmrChan->b.loopback=0;
- o->pmrChan->txsettletime=o->txsettletime;
- o->pmrChan->rxCpuSaver=o->rxcpusaver;
- o->pmrChan->txCpuSaver=o->txcpusaver;
- *(o->pmrChan->prxSquelchAdjust) =
- ((999 - o->rxsquelchadj) * 32767) / 1000;
- *(o->pmrChan->prxVoiceAdjust)=o->rxvoiceadj*M_Q8;
- *(o->pmrChan->prxCtcssAdjust)=o->rxctcssadj*M_Q8;
- o->pmrChan->rxCtcss->relax=o->rxctcssrelax;
- o->pmrChan->txTocType = o->txtoctype;
- if ( (o->txmixa == TX_OUT_LSD) ||
- (o->txmixa == TX_OUT_COMPOSITE) ||
- (o->txmixb == TX_OUT_LSD) ||
- (o->txmixb == TX_OUT_COMPOSITE))
- {
- set_txctcss_level(o);
- }
- if( (o->txmixa!=TX_OUT_VOICE) && (o->txmixb!=TX_OUT_VOICE) &&
- (o->txmixa!=TX_OUT_COMPOSITE) && (o->txmixb!=TX_OUT_COMPOSITE)
- )
- {
- ast_log(LOG_ERROR,"No txvoice output configured.\n");
- }
-
- if( o->txctcssfreq[0] &&
- o->txmixa!=TX_OUT_LSD && o->txmixa!=TX_OUT_COMPOSITE &&
- o->txmixb!=TX_OUT_LSD && o->txmixb!=TX_OUT_COMPOSITE
- )
- {
- ast_log(LOG_ERROR,"No txtone output configured.\n");
- }
-
- if(o->b.radioactive)
- {
-
-
-
-
-
- usbradio_active = o->name;
-
-
-
- ast_log(LOG_NOTICE,"radio active set to [%s]\n",o->name);
- }
- }
- xpmr_config(o);
- TRACEO(1,("store_config() 120\n"));
- mixer_write(o);
- TRACEO(1,("store_config() 130\n"));
- mult_set(o);
- TRACEO(1,("store_config() 140\n"));
- hidhdwconfig(o);
- TRACEO(1,("store_config() 200\n"));
- #ifndef NEW_ASTERISK
- if (pipe(o->sndcmd) != 0) {
- ast_log(LOG_ERROR, "Unable to create pipe\n");
- goto error;
- }
- ast_pthread_create_background(&o->sthread, NULL, sound_thread, o);
- #endif
-
- if (o != &usbradio_default) {
- o->next = usbradio_default.next;
- usbradio_default.next = o;
- }
- TRACEO(1,("store_config() complete\n"));
- return o;
-
- error:
- if (o != &usbradio_default)
- free(o);
- return NULL;
- }
- #if DEBUG_FILETEST == 1
- int RxTestIt(struct chan_usbradio_pvt *o)
- {
- const int numSamples = SAMPLES_PER_BLOCK;
- const int numChannels = 16;
- i16 sample,i,ii;
-
- i32 txHangTime;
- i16 txEnable;
- t_pmr_chan tChan;
- t_pmr_chan *pChan;
- FILE *hInput=NULL, *hOutput=NULL, *hOutputTx=NULL;
-
- i16 iBuff[numSamples*2*6], oBuff[numSamples];
-
- printf("RxTestIt()\n");
- pChan=o->pmrChan;
- pChan->b.txCapture=1;
- pChan->b.rxCapture=1;
- txEnable = 0;
- hInput = fopen("/usr/src/xpmr/testdata/rx_in.pcm","r");
- if(!hInput){
- printf(" RxTestIt() File Not Found.\n");
- return 0;
- }
- hOutput = fopen("/usr/src/xpmr/testdata/rx_debug.pcm","w");
- printf(" RxTestIt() Working...\n");
-
- while(!feof(hInput))
- {
- fread((void *)iBuff,2,numSamples*2*6,hInput);
-
- if(txHangTime)txHangTime-=numSamples;
- if(txHangTime<0)txHangTime=0;
-
- if(pChan->rxCtcss->decode)txHangTime=(8000/1000*2000);
- if(pChan->rxCtcss->decode && !txEnable)
- {
- txEnable=1;
-
- }
- else if(!pChan->rxCtcss->decode && txEnable)
- {
- txEnable=0;
- }
- PmrRx(pChan,iBuff,oBuff);
- if (fwrite((void *)pChan->prxDebug,2,numSamples*numChannels,hOutput) != numSamples * numChannels) {
- ast_log(LOG_ERROR, "fwrite() failed: %s\n", strerror(errno));
- }
- }
- pChan->b.txCapture=0;
- pChan->b.rxCapture=0;
- if(hInput)fclose(hInput);
- if(hOutput)fclose(hOutput);
- printf(" RxTestIt() Complete.\n");
- return 0;
- }
- #endif
- #ifdef NEW_ASTERISK
- static char *res2cli(int r)
- {
- switch (r)
- {
- case RESULT_SUCCESS:
- return(CLI_SUCCESS);
- case RESULT_SHOWUSAGE:
- return(CLI_SHOWUSAGE);
- default:
- return(CLI_FAILURE);
- }
- }
- static char *handle_console_key(struct ast_cli_entry *e,
- int cmd, struct ast_cli_args *a)
- {
- char *argv[] = { "radio", "key", NULL };
- switch (cmd) {
- case CLI_INIT:
- e->command = "radio key";
- e->usage = key_usage;
- return NULL;
- case CLI_GENERATE:
- return NULL;
- }
- return res2cli(console_key(a->fd, 2, argv));
- }
- static char *handle_console_unkey(struct ast_cli_entry *e,
- int cmd, struct ast_cli_args *a)
- {
- char *argv[] = { "radio", "unkey", NULL };
- switch (cmd) {
- case CLI_INIT:
- e->command = "radio unkey";
- e->usage = unkey_usage;
- return NULL;
- case CLI_GENERATE:
- return NULL;
- }
- return res2cli(console_unkey(a->fd, 2, argv));
- }
- static char *handle_radio_tune(struct ast_cli_entry *e,
- int cmd, struct ast_cli_args *a)
- {
- char *argv[5] = { "radio", "tune", a->argc > 2 ? (char *) a->argv[2] : NULL, a->argc > 3 ? (char *) a->argv[3] : NULL };
- switch (cmd) {
- case CLI_INIT:
- e->command = "radio tune";
- e->usage = radio_tune_usage;
- return NULL;
- case CLI_GENERATE:
- return NULL;
- }
- return res2cli(radio_tune(a->fd, a->argc, argv));
- }
- static char *handle_radio_debug(struct ast_cli_entry *e,
- int cmd, struct ast_cli_args *a)
- {
- switch (cmd) {
- case CLI_INIT:
- e->command = "radio debug";
- e->usage = radio_tune_usage;
- return NULL;
- case CLI_GENERATE:
- return NULL;
- }
- return res2cli(radio_set_debug(a->fd, a->argc, NULL ));
- }
- static char *handle_radio_debug_off(struct ast_cli_entry *e,
- int cmd, struct ast_cli_args *a)
- {
- switch (cmd) {
- case CLI_INIT:
- e->command = "radio debug off";
- e->usage = radio_tune_usage;
- return NULL;
- case CLI_GENERATE:
- return NULL;
- }
- return res2cli(radio_set_debug_off(a->fd, a->argc, NULL ));
- }
- static char *handle_radio_active(struct ast_cli_entry *e,
- int cmd, struct ast_cli_args *a)
- {
- char *argv[4] = { "radio", "active", a->argc > 2 ? (char *) a->argv[2] : NULL, };
- switch (cmd) {
- case CLI_INIT:
- e->command = "radio active";
- e->usage = active_usage;
- return NULL;
- case CLI_GENERATE:
- return NULL;
- }
- return res2cli(radio_active(a->fd, a->argc, argv));
- }
- static char *handle_set_xdebug(struct ast_cli_entry *e,
- int cmd, struct ast_cli_args *a)
- {
- char *argv[5] = { "radio", "set", "xdebug", a->argc == 4 ? (char *) a->argv[3] : NULL, };
- switch (cmd) {
- case CLI_INIT:
- e->command = "radio set xdebug";
- e->usage = active_usage;
- return NULL;
- case CLI_GENERATE:
- return NULL;
- }
- return res2cli(radio_set_xpmr_debug(a->fd, a->argc, argv));
- }
- static struct ast_cli_entry cli_usbradio[] = {
- AST_CLI_DEFINE(handle_console_key,"Simulate Rx Signal Present"),
- AST_CLI_DEFINE(handle_console_unkey,"Simulate Rx Signal Loss"),
- AST_CLI_DEFINE(handle_radio_tune,"Radio Tune"),
- AST_CLI_DEFINE(handle_radio_debug,"Radio Debug On"),
- AST_CLI_DEFINE(handle_radio_debug_off,"Radio Debug Off"),
- AST_CLI_DEFINE(handle_radio_active,"Change commanded device"),
- AST_CLI_DEFINE(handle_set_xdebug,"Radio set xpmr debug level")
- };
- #endif
- #include "./xpmr/xpmr.c"
- #ifdef HAVE_XPMRX
- #include "./xpmrx/xpmrx.c"
- #endif
- static int load_module(void)
- {
- struct ast_config *cfg = NULL;
- char *ctg = NULL;
- #ifdef NEW_ASTERISK
- struct ast_flags zeroflag = {0};
- #endif
- if (hid_device_mklist()) {
- ast_log(LOG_NOTICE, "Unable to make hid list\n");
- return AST_MODULE_LOAD_DECLINE;
- }
- usb_list_check("");
- usbradio_active = NULL;
-
- memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
-
- #ifdef NEW_ASTERISK
- if (!(cfg = ast_config_load(config,zeroflag)) || cfg == CONFIG_STATUS_FILEINVALID) {
- #else
- if (!(cfg = ast_config_load(config))) || cfg == CONFIG_STATUS_FILEINVALID {
- #endif
- ast_log(LOG_NOTICE, "Unable to load config %s\n", config);
- return AST_MODULE_LOAD_DECLINE;
- }
- do {
- store_config(cfg, ctg);
- } while ( (ctg = ast_category_browse(cfg, ctg)) != NULL);
- ast_config_destroy(cfg);
- if (find_desc(usbradio_active) == NULL) {
- ast_log(LOG_NOTICE, "radio active device %s not found\n", usbradio_active);
-
-
- return AST_MODULE_LOAD_DECLINE;
- }
- if (ast_channel_register(&usbradio_tech)) {
- ast_log(LOG_ERROR, "Unable to register channel type 'usb'\n");
- return AST_MODULE_LOAD_DECLINE;
- }
- ast_cli_register_multiple(cli_usbradio, ARRAY_LEN(cli_usbradio));
- return AST_MODULE_LOAD_SUCCESS;
- }
- static int unload_module(void)
- {
- struct chan_usbradio_pvt *o;
- ast_log(LOG_WARNING, "unload_module() called\n");
- ast_channel_unregister(&usbradio_tech);
- ast_cli_unregister_multiple(cli_usbradio, ARRAY_LEN(cli_usbradio));
- for (o = usbradio_default.next; o; o = o->next) {
- ast_log(LOG_WARNING, "destroyPmrChannel() called\n");
- if(o->pmrChan)destroyPmrChannel(o->pmrChan);
-
- #if DEBUG_CAPTURES == 1
- if (frxcapraw) { fclose(frxcapraw); frxcapraw = NULL; }
- if (frxcaptrace) { fclose(frxcaptrace); frxcaptrace = NULL; }
- if (frxoutraw) { fclose(frxoutraw); frxoutraw = NULL; }
- if (ftxcapraw) { fclose(ftxcapraw); ftxcapraw = NULL; }
- if (ftxcaptrace) { fclose(ftxcaptrace); ftxcaptrace = NULL; }
- if (ftxoutraw) { fclose(ftxoutraw); ftxoutraw = NULL; }
- #endif
- close(o->sounddev);
- #ifndef NEW_ASTERISK
- if (o->sndcmd[0] > 0) {
- close(o->sndcmd[0]);
- close(o->sndcmd[1]);
- }
- #endif
- if (o->dsp) ast_dsp_free(o->dsp);
- if (o->owner)
- ast_softhangup(o->owner, AST_SOFTHANGUP_APPUNLOAD);
- if (o->owner)
- return -1;
-
-
- }
- return 0;
- }
- AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "usb Console Channel Driver");
|