libcm is a C development framework with an emphasis on audio signal processing applications.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

cmDevCfg.c 40KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  1. #include "cmGlobal.h"
  2. #include "cmFloatTypes.h"
  3. #include "cmRpt.h"
  4. #include "cmErr.h"
  5. #include "cmCtx.h"
  6. #include "cmMem.h"
  7. #include "cmMallocDebug.h"
  8. #include "cmLinkedHeap.h"
  9. #include "cmFileSys.h"
  10. #include "cmJson.h"
  11. #include "cmText.h"
  12. #include "cmThread.h"
  13. #include "cmTime.h"
  14. #include "cmMidi.h"
  15. #include "cmMidiPort.h"
  16. #include "cmAudioPort.h"
  17. #include "cmUdpPort.h"
  18. #include "cmRtSysMsg.h"
  19. #include "cmRtNet.h"
  20. #include "cmRtSys.h"
  21. #include "cmDevCfg.h"
  22. cmDevCfgH_t cmDevCfgNullHandle = cmSTATIC_NULL_HANDLE;
  23. typedef struct cmDcmCfg_str
  24. {
  25. cmChar_t* dcLabelStr; // the cfg label
  26. unsigned cfgId; // unique among all cfg's assigned to a loc
  27. cmTypeDcmId_t typeId; // the cfg type id (e.g. midi, audio, net, ...)
  28. cmChar_t* descStr; // summary description string
  29. // NOTE: any fields added to this structure, type generic (above)
  30. // or type specific (below), must be explicitely duplicated in cmDevCfgLocStore().
  31. union
  32. {
  33. cmDcmMidi_t m;
  34. cmDcmAudio_t a;
  35. //cmDcmNet_t n;
  36. } u;
  37. struct cmDcmCfg_str* next;
  38. struct cmDcmCfg_str* prev;
  39. } cmDcmCfg_t;
  40. typedef struct
  41. {
  42. cmTypeDcmId_t tid; // Type Id for this map or tInvalidDcmTId if the record is not active.
  43. unsigned cfgId; // cfgId of the cfg recd assoc'd with this map
  44. cmDcmCfg_t* cfg; // pointer to the cfg recd assoc'd with this map (cfg->cfgId == cmDcmMap_t.cfgId)
  45. } cmDcmMap_t;
  46. typedef struct
  47. {
  48. bool activeFl;
  49. cmDcmMap_t* map;
  50. unsigned mapCnt;
  51. } cmDcmApp_t;
  52. typedef struct cmDcmLoc_str
  53. {
  54. cmChar_t* labelStr;
  55. cmDcmApp_t* app;
  56. unsigned appCnt;
  57. cmDcmCfg_t* cfg;
  58. struct cmDcmLoc_str* next;
  59. struct cmDcmLoc_str* prev;
  60. } cmDcmLoc_t;
  61. typedef struct
  62. {
  63. cmErr_t err;
  64. cmCtx_t* ctx;
  65. cmDcmLoc_t* loc;
  66. cmDcmLoc_t* clp;
  67. cmChar_t* fn;
  68. } cmDcm_t;
  69. cmDcm_t* _cmDcmHandleToPtr( cmDevCfgH_t h )
  70. {
  71. cmDcm_t* p = (cmDcm_t*)h.h;
  72. assert( p!=NULL );
  73. return p;
  74. }
  75. const cmChar_t* _cmDcmTrimLabel( cmDcm_t*p, const cmChar_t* s, const cmChar_t* title )
  76. {
  77. if( s == NULL )
  78. {
  79. cmErrMsg(&p->err,kBlankLabelDcRC,"A blank '%s' label was encountered.",title);
  80. return NULL;
  81. }
  82. while( *s && isspace(*s) )
  83. ++s;
  84. if( strlen(s) == 0 )
  85. {
  86. cmErrMsg(&p->err,kBlankLabelDcRC,"An empty '%s' label was encountered.",title);
  87. return NULL;
  88. }
  89. return s;
  90. }
  91. void _cmDcmFreeMidi( cmDcmMidi_t* r )
  92. {
  93. cmMemFree(r->devLabelStr);
  94. cmMemFree(r->portLabelStr);
  95. }
  96. void _cmDcmDuplMidi( cmDcmMidi_t* d, const cmDcmMidi_t* s )
  97. {
  98. d->devLabelStr = cmMemAllocStr(s->devLabelStr);
  99. d->portLabelStr = cmMemAllocStr(s->portLabelStr);
  100. d->inputFl = s->inputFl;
  101. d->devIdx = s->devIdx;
  102. d->portIdx = s->portIdx;
  103. }
  104. void _cmDcmFreeAudio( cmDcmAudio_t* r )
  105. {
  106. cmMemFree(r->inDevLabelStr);
  107. cmMemFree(r->outDevLabelStr);
  108. cmMemFree((cmChar_t*)r->netNodeLabel);
  109. cmMemFree((cmChar_t*)r->bcastAddr);
  110. cmMemFree((cmChar_t*)r->ipAddr);
  111. }
  112. void _cmDcmDuplAudio( cmDcmAudio_t* d, const cmDcmAudio_t* s )
  113. {
  114. d->inDevLabelStr = cmMemAllocStr(s->inDevLabelStr);
  115. d->outDevLabelStr = cmMemAllocStr(s->outDevLabelStr);
  116. d->rtSysArgs = s->rtSysArgs;
  117. }
  118. /*
  119. void _cmDcmFreeNet( cmDcmNet_t* r )
  120. {
  121. cmMemFree(r->sockAddr);
  122. }
  123. void _cmDcmDuplNet( cmDcmNet_t* d, const cmDcmNet_t* s )
  124. {
  125. d->sockAddr = cmMemAllocStr(s->sockAddr);
  126. d->portNumber = s->portNumber;
  127. d->activeFl = s->activeFl;
  128. }
  129. */
  130. void _cmDcmFreeCfg( cmDcm_t* p, cmDcmLoc_t* lp, cmDcmCfg_t* cp )
  131. {
  132. // unlink the cfg recd
  133. if( cp->prev == NULL )
  134. lp->cfg = cp->next;
  135. else
  136. cp->prev->next = cp->next;
  137. if( cp->next != NULL )
  138. cp->next->prev = cp->prev;
  139. cmMemFree(cp->dcLabelStr);
  140. cmMemFree(cp->descStr);
  141. switch( cp->typeId )
  142. {
  143. case kMidiDcmTId: _cmDcmFreeMidi(&cp->u.m); break;
  144. case kAudioDcmTId: _cmDcmFreeAudio(&cp->u.a); break;
  145. //case kNetDcmTId: _cmDcmFreeNet(&cp->u.n); break;
  146. default:
  147. assert(0);
  148. break;
  149. }
  150. cmMemFree(cp);
  151. }
  152. void _cmDcmFreeLoc( cmDcm_t* p, cmDcmLoc_t* lp )
  153. {
  154. unsigned i;
  155. // unlink the loc recd
  156. if( lp->prev == NULL )
  157. p->loc = lp->next;
  158. else
  159. lp->prev->next = lp->next;
  160. if( lp->next != NULL )
  161. lp->next->prev = lp->prev;
  162. // free each app
  163. for(i=0; i<lp->appCnt; ++i)
  164. cmMemFree(lp->app[i].map);
  165. cmMemFree(lp->app);
  166. cmMemFree(lp->labelStr);
  167. while( lp->cfg != NULL )
  168. _cmDcmFreeCfg(p,lp,lp->cfg);
  169. cmMemFree(lp);
  170. }
  171. void _cmDcmFreeAllLocs( cmDcm_t* p )
  172. {
  173. while( p->loc != NULL )
  174. _cmDcmFreeLoc(p,p->loc);
  175. p->clp = NULL;
  176. }
  177. cmDcRC_t _cmDcmFree( cmDcm_t* p )
  178. {
  179. cmDcRC_t rc = kOkDcRC;
  180. _cmDcmFreeAllLocs(p);
  181. cmMemFree(p->fn);
  182. cmMemFree(p);
  183. return rc;
  184. }
  185. cmDcmLoc_t* _cmDcmFindLoc( cmDcm_t* p, const cmChar_t* labelStr )
  186. {
  187. cmDcmLoc_t* lp = p->loc;
  188. for(; lp!=NULL; lp=lp->next)
  189. if(strcmp(lp->labelStr,labelStr) == 0)
  190. return lp;
  191. return NULL;
  192. }
  193. cmDcRC_t _cmDcmNewLoc( cmDcm_t* p, const cmChar_t* labelStr, cmDcmLoc_t** locRef)
  194. {
  195. cmDcmLoc_t* lp;
  196. assert(locRef != NULL);
  197. *locRef = NULL;
  198. // verify and condition the label
  199. if((labelStr = _cmDcmTrimLabel(p,labelStr,"location")) == NULL )
  200. return cmErrLastRC(&p->err);
  201. // verify that the label is not already in use
  202. if((lp = _cmDcmFindLoc(p,labelStr)) != NULL )
  203. return cmErrMsg(&p->err,kDuplLabelDcRC,"The location label '%s' is already in use.",labelStr);
  204. // create a new loc recd
  205. lp = cmMemAllocZ(cmDcmLoc_t,1);
  206. lp->labelStr = cmMemAllocStr(labelStr);
  207. // if the cur loc is not current set - then prepend loc
  208. if( p->clp == NULL )
  209. {
  210. lp->prev = NULL;
  211. lp->next = p->loc;
  212. if( p->loc == NULL )
  213. p->loc = lp;
  214. else
  215. p->loc->prev = lp;
  216. }
  217. else // otherwise insert loc after the cur loc
  218. {
  219. lp->prev = p->clp;
  220. lp->next = p->clp->next;
  221. if( p->clp->next != NULL )
  222. p->clp->next->prev = lp;
  223. p->clp->next = lp;
  224. }
  225. // make the new loc the current loc
  226. p->clp = lp;
  227. *locRef = lp;
  228. return kOkDcRC;
  229. }
  230. cmDcRC_t _cmDevCfgRead( cmDcm_t* p, cmJsonH_t jsH, const cmJsonNode_t* rootObjPtr );
  231. cmDcRC_t _cmDevCfgReadFile( cmDcm_t* p, const cmChar_t* fn )
  232. {
  233. cmDcRC_t rc = kOkDcRC;
  234. cmJsonH_t jsH = cmJsonNullHandle;
  235. // initialize the JSON tree from the preferences file
  236. if( cmJsonInitializeFromFile( &jsH, fn, p->ctx ) != kOkJsRC )
  237. {
  238. rc = cmErrMsg(&p->err,kJsonFailDcRC,"JSON initialization from '%s' failed.",cmStringNullGuard(fn));
  239. goto errLabel;
  240. }
  241. if((rc = _cmDevCfgRead(p, jsH, cmJsonRoot(jsH))) != kOkJsRC )
  242. goto errLabel;
  243. errLabel:
  244. if( cmJsonFinalize(&jsH) != kOkJsRC )
  245. cmErrMsg(&p->err,kJsonFailDcRC,"JSON finalization failed following dev cfg read.");
  246. return rc;
  247. }
  248. cmDcRC_t cmDevCfgAlloc( cmCtx_t* c, cmDevCfgH_t* hp, const cmChar_t* fn )
  249. {
  250. cmDcRC_t rc;
  251. cmDcmLoc_t* lp;
  252. if((rc = cmDevCfgFree(hp)) != kOkDcRC )
  253. return rc;
  254. cmDcm_t* p = cmMemAllocZ(cmDcm_t,1);
  255. cmErrSetup(&p->err,&c->rpt,"cmDevCfg");
  256. p->ctx = c;
  257. if( fn != NULL )
  258. {
  259. p->fn = cmMemAllocStr(fn);
  260. if( cmFsIsFile(fn) )
  261. {
  262. // if the file read fails then reset and go with the default empty setup.
  263. if(_cmDevCfgReadFile(p,fn) != kOkDcRC )
  264. _cmDcmFreeAllLocs(p);
  265. }
  266. }
  267. // if the location array is empty then create a default location
  268. if( p->loc == NULL )
  269. {
  270. if((rc = _cmDcmNewLoc(p,"Default", &lp)) != kOkDcRC )
  271. goto errLabel;
  272. }
  273. hp->h = p;
  274. errLabel:
  275. if( rc != kOkDcRC )
  276. _cmDcmFree(p);
  277. return rc;
  278. }
  279. cmDcRC_t cmDevCfgFree( cmDevCfgH_t* hp )
  280. {
  281. cmDcRC_t rc = kOkDcRC;
  282. if( hp == NULL || cmDevCfgIsValid(*hp) == false )
  283. return rc;
  284. cmDcm_t* p = _cmDcmHandleToPtr(*hp);
  285. if((rc = _cmDcmFree(p)) != kOkDcRC )
  286. return rc;
  287. hp->h = NULL;
  288. return rc;
  289. }
  290. bool cmDevCfgIsValid( cmDevCfgH_t h )
  291. { return h.h != NULL; }
  292. unsigned cmDevCfgCount( cmDevCfgH_t h, cmTypeDcmId_t typeId )
  293. {
  294. unsigned n = 0;
  295. cmDcm_t* p = _cmDcmHandleToPtr(h);
  296. assert( p->clp != NULL);
  297. const cmDcmCfg_t* cp = p->clp->cfg;
  298. for(; cp!= NULL; cp=cp->next)
  299. if( cp->typeId == typeId )
  300. ++n;
  301. return n;
  302. }
  303. cmDcmCfg_t* _cmDevCfgIndexToPtr( cmDcm_t* p, cmTypeDcmId_t typeId, unsigned index, bool errFl )
  304. {
  305. unsigned n = 0;
  306. assert( p->clp != NULL);
  307. cmDcmCfg_t* cp = p->clp->cfg;
  308. for(; cp!= NULL; cp=cp->next)
  309. if( cp->typeId == typeId )
  310. {
  311. if( n == index )
  312. return cp;
  313. ++n;
  314. }
  315. if( errFl )
  316. cmErrMsg(&p->err,kInvalidCfgIdxDcRC,"The cfg index %i is not valid in location '%s'.",cmStringNullGuard(p->clp->labelStr));
  317. return NULL;
  318. }
  319. const cmChar_t* cmDevCfgLabel( cmDevCfgH_t h, cmTypeDcmId_t typeId, unsigned index )
  320. {
  321. cmDcm_t* p = _cmDcmHandleToPtr(h);
  322. cmDcmCfg_t* cp;
  323. if((cp = _cmDevCfgIndexToPtr(p,typeId,index,false)) == NULL )
  324. return NULL;
  325. return cp->dcLabelStr;
  326. }
  327. const cmChar_t* cmDevCfgDesc( cmDevCfgH_t h, cmTypeDcmId_t typeId, unsigned index )
  328. {
  329. cmDcm_t* p = _cmDcmHandleToPtr(h);
  330. cmDcmCfg_t* cp;
  331. if((cp = _cmDevCfgIndexToPtr(p,typeId,index,false)) == NULL )
  332. return NULL;
  333. return cp->descStr;
  334. }
  335. cmDcmCfg_t* _cmDcmCfgLabelToPtr( cmDcm_t* p, cmTypeDcmId_t typeId, const cmChar_t* label, bool errFl )
  336. {
  337. assert( p->clp != NULL);
  338. cmDcmCfg_t* cp = p->clp->cfg;
  339. for(; cp!= NULL; cp=cp->next)
  340. if( cp->typeId == typeId )
  341. {
  342. if( strcmp(cp->dcLabelStr,label) == 0 )
  343. return cp;
  344. }
  345. if( errFl )
  346. cmErrMsg(&p->err,kLabelNotFoundDcRC, "The cfg label '%s' was not found.",cmStringNullGuard(label));
  347. return NULL;
  348. }
  349. unsigned cmDevCfgLabelToIndex( cmDevCfgH_t h, cmTypeDcmId_t typeId, const cmChar_t* label )
  350. {
  351. unsigned n = 0;
  352. cmDcm_t* p = _cmDcmHandleToPtr(h);
  353. assert( p->clp != NULL);
  354. const cmDcmCfg_t* cp = p->clp->cfg;
  355. for(; cp!= NULL; cp=cp->next)
  356. if( cp->typeId == typeId )
  357. {
  358. if( strcmp(cp->dcLabelStr,label) == 0 )
  359. return n;
  360. ++n;
  361. }
  362. return cmInvalidIdx;
  363. }
  364. cmDcmCfg_t* _cmDcmFindOrCreateCfg( cmDcm_t* p, cmTypeDcmId_t typeId, const cmChar_t* dcLabelStr )
  365. {
  366. assert( p->clp != NULL );
  367. cmDcmCfg_t* cp;
  368. // validate the label
  369. if( (dcLabelStr=_cmDcmTrimLabel(p,dcLabelStr,"cfg")) == NULL )
  370. return NULL;
  371. // if the cfg recd already exists then return it
  372. if((cp = _cmDcmCfgLabelToPtr(p, typeId, dcLabelStr, false )) != NULL )
  373. return cp;
  374. unsigned newCfgId = 0;
  375. // use ep to track the end of the list
  376. cmDcmCfg_t* ep = NULL;
  377. // find the max cfgId used by this loc
  378. cp = p->clp->cfg;
  379. for(; cp!=NULL; cp=cp->next)
  380. {
  381. if( cp->cfgId > newCfgId )
  382. newCfgId = cp->cfgId;
  383. ep = cp;
  384. }
  385. // add one to the max cfgId to create a unique cfgId
  386. newCfgId += 1;
  387. // allocate a new cfg recd
  388. cp = cmMemAllocZ(cmDcmCfg_t,1);
  389. cp->dcLabelStr = cmMemAllocStr(dcLabelStr);
  390. cp->cfgId = newCfgId;
  391. cp->typeId = typeId;
  392. // link to the end of the loc's cfg list
  393. if( ep == NULL )
  394. p->clp->cfg = cp;
  395. else
  396. {
  397. ep->next = cp;
  398. cp->prev = ep;
  399. }
  400. return cp;
  401. }
  402. void _cmDcmDisconnectMap( cmDcmMap_t* mp )
  403. {
  404. mp->tid = kInvalidDcmTId;
  405. mp->cfgId = cmInvalidId;
  406. mp->cfg = NULL;
  407. }
  408. cmDcRC_t cmDevCfgDeleteCfg( cmDevCfgH_t h, cmTypeDcmId_t typeId, const cmChar_t* dcLabelStr )
  409. {
  410. cmDcmCfg_t* cp;
  411. cmDcm_t* p = _cmDcmHandleToPtr(h);
  412. unsigned i,j;
  413. // locate the cfg to delete
  414. if((cp = _cmDcmCfgLabelToPtr(p,typeId,dcLabelStr,true)) == NULL )
  415. return cmErrLastRC(&p->err);
  416. // for every app in the current location ...
  417. for(i=0; i<p->clp->appCnt; ++i)
  418. {
  419. cmDcmApp_t* ap = p->clp->app + i;
  420. // ... disconnect any maps to this cfg
  421. if( ap->activeFl && ap->map != NULL )
  422. for(j=0; j<ap->mapCnt; ++j)
  423. if( ap->map[j].cfgId == cp->cfgId )
  424. _cmDcmDisconnectMap(ap->map + j );
  425. }
  426. // unlink and release the cfg recd
  427. _cmDcmFreeCfg(p,p->clp,cp);
  428. return kOkDcRC;
  429. }
  430. cmDcRC_t cmDevCfgCreateMap( cmDevCfgH_t h, cmTypeDcmId_t typeId, const cmChar_t* dcLabelStr, unsigned usrAppId, unsigned usrMapId )
  431. {
  432. cmDcRC_t rc = kOkDcRC;
  433. cmDcm_t* p = _cmDcmHandleToPtr(h);
  434. cmDcmCfg_t* cp;
  435. // locate the cfg to map to
  436. if((cp = _cmDcmCfgLabelToPtr(p,typeId,dcLabelStr,true)) == NULL )
  437. return cmErrLastRC(&p->err);
  438. // if usrAppId references a new app
  439. if( usrAppId >= p->clp->appCnt )
  440. {
  441. p->clp->appCnt = usrAppId + 1;
  442. p->clp->app = cmMemResizePZ(cmDcmApp_t,p->clp->app,p->clp->appCnt);
  443. }
  444. cmDcmApp_t* ap = p->clp->app + usrAppId;
  445. // if usrMapId ref's a new map
  446. if( usrMapId >= ap->mapCnt )
  447. {
  448. ap->mapCnt = usrMapId+1;
  449. ap->map = cmMemResizePZ(cmDcmMap_t,ap->map,ap->mapCnt);
  450. }
  451. cmDcmMap_t* mp = ap->map + usrMapId;
  452. mp->tid = typeId;
  453. mp->cfgId = cp->cfgId;
  454. mp->cfg = cp;
  455. return rc;
  456. }
  457. cmDcmMap_t* _cmDcmFindMap( cmDcm_t* p, cmTypeDcmId_t typeId, unsigned usrAppId, unsigned usrMapId, bool activeFl )
  458. {
  459. if( usrAppId >= p->clp->appCnt )
  460. {
  461. cmErrMsg(&p->err,kInvalidUserAppIdRC,"The user app. id %i is out of range %i in location '%s'.",usrAppId,p->clp->appCnt,cmStringNullGuard(p->clp->labelStr));
  462. goto errLabel;
  463. }
  464. cmDcmApp_t* ap = p->clp->app + usrAppId;
  465. if( usrMapId >= ap->mapCnt )
  466. {
  467. cmErrMsg(&p->err,kInvalidUserMapIdRC,"The user map id %i is out of range %i in location '%s'.",usrMapId,ap->mapCnt,cmStringNullGuard(p->clp->labelStr));
  468. goto errLabel;
  469. }
  470. if( activeFl && (ap->map[usrMapId].tid == kInvalidDcmTId || ap->map[usrMapId].cfg == NULL))
  471. {
  472. cmErrMsg(&p->err,kInvalidUserMapIdRC,"The user map id %i inactive in location '%s'.",usrMapId,cmStringNullGuard(p->clp->labelStr));
  473. goto errLabel;
  474. }
  475. return ap->map + usrMapId;
  476. errLabel:
  477. return NULL;
  478. }
  479. cmDcRC_t cmDevCfgDeleteMap( cmDevCfgH_t h, cmTypeDcmId_t typeId, unsigned usrAppId, unsigned usrMapId )
  480. {
  481. cmDcm_t* p = _cmDcmHandleToPtr(h);
  482. cmDcmMap_t* mp;
  483. if((mp = _cmDcmFindMap(p,typeId,usrAppId,usrMapId,false)) == NULL )
  484. return cmErrLastRC(&p->err);
  485. _cmDcmDisconnectMap( mp );
  486. return kOkDcRC;
  487. }
  488. cmDcRC_t cmDevCfgNameMidiPort(
  489. cmDevCfgH_t h,
  490. const cmChar_t* dcLabelStr,
  491. const cmChar_t* devLabelStr,
  492. const cmChar_t* portLabelStr,
  493. bool inputFl )
  494. {
  495. cmDcm_t* p = _cmDcmHandleToPtr(h);
  496. cmDcmCfg_t* cp;
  497. unsigned midiDevIdx;
  498. unsigned midiPortIdx;
  499. // validate the label
  500. if((dcLabelStr = _cmDcmTrimLabel(p,dcLabelStr,"MIDI cfg")) == NULL)
  501. return cmErrLastRC(&p->err);
  502. // verify that the device label is valid
  503. if((midiDevIdx = cmMpDeviceNameToIndex( devLabelStr )) == cmInvalidIdx )
  504. return cmErrMsg(&p->err, kInvalidArgDcRC,"The MIDI device name '%s' is not valid.",devLabelStr);
  505. // verify that the port label is valid
  506. if((midiPortIdx = cmMpDevicePortNameToIndex( midiDevIdx, inputFl ? kInMpFl : kOutMpFl, portLabelStr )) == cmInvalidIdx )
  507. return cmErrMsg(&p->err, kInvalidArgDcRC,"The MIDI port name '%s' is not valid on the device '%s'.",portLabelStr,devLabelStr);
  508. // if dcLabelStr is already in use for this location and type then update
  509. // the assoc'd recd otherwise create a new one.
  510. if((cp = _cmDcmFindOrCreateCfg(p,kMidiDcmTId, dcLabelStr)) == NULL )
  511. return cmErrLastRC(&p->err);
  512. cp->u.m.label = cp->dcLabelStr;
  513. cp->u.m.devLabelStr = cmMemResizeStr(cp->u.m.devLabelStr,devLabelStr);
  514. cp->u.m.portLabelStr = cmMemResizeStr(cp->u.m.portLabelStr,portLabelStr);
  515. cp->u.m.inputFl = inputFl;
  516. cp->u.m.devIdx = midiDevIdx;
  517. cp->u.m.portIdx = midiPortIdx;
  518. cp->descStr = cmTsPrintfP(cp->descStr,"%s %s %s",inputFl?"Input":"Output",devLabelStr,portLabelStr);
  519. return kOkDcRC;
  520. }
  521. const cmDcmMidi_t* cmDevCfgMidiCfg( cmDevCfgH_t h, unsigned cfgIdx )
  522. {
  523. cmDcm_t* p = _cmDcmHandleToPtr(h);
  524. cmDcmCfg_t* cp;
  525. if((cp = _cmDevCfgIndexToPtr(p, kMidiDcmTId, cfgIdx, true )) == NULL )
  526. return NULL;
  527. return &cp->u.m;
  528. }
  529. const cmDcmMidi_t* cmDevCfgMidiDevMap( cmDevCfgH_t h, unsigned usrAppId, unsigned usrMapId )
  530. {
  531. cmDcm_t* p = _cmDcmHandleToPtr(h);
  532. cmDcmMap_t* mp;
  533. if((mp =_cmDcmFindMap(p,kMidiDcmTId, usrAppId, usrMapId, true )) == NULL )
  534. return NULL;
  535. return &mp->cfg->u.m;
  536. }
  537. const cmDcmMidi_t* cmDevCfgMidiCfgFromLabel( cmDevCfgH_t h, const cmChar_t* cfgLabel )
  538. {
  539. cmDcm_t* p = _cmDcmHandleToPtr(h);
  540. const cmDcmMidi_t* c;
  541. unsigned idx;
  542. if((idx = cmDevCfgLabelToIndex(h, kMidiDcmTId, cfgLabel )) == cmInvalidIdx )
  543. {
  544. cmErrMsg(&p->err,kLabelNotFoundDcRC,"The MIDI cfg. '%s' was not found.",cmStringNullGuard(cfgLabel));
  545. return NULL;
  546. }
  547. if((c = cmDevCfgMidiCfg(h,idx)) == NULL )
  548. {
  549. cmErrMsg(&p->err,kInvalidCfgIdxDcRC,"The MIDI cfg. index %i is invalid.",idx);
  550. return NULL;
  551. }
  552. return c;
  553. }
  554. cmDcRC_t cmDevCfgNameAudioPort(
  555. cmDevCfgH_t h,
  556. const cmChar_t* dcLabelStr,
  557. const cmChar_t* inDevNameStr,
  558. const cmChar_t* outDevNameStr,
  559. bool syncInputFl,
  560. unsigned msgQueueByteCnt,
  561. unsigned devFramesPerCycle,
  562. unsigned dspFramesPerCycle,
  563. unsigned audioBufCnt,
  564. double srate,
  565. const cmChar_t* netNodeLabel,
  566. const cmChar_t* bcastAddr,
  567. const cmChar_t* ipAddr,
  568. cmUdpPort_t ipPort,
  569. bool activeFl )
  570. {
  571. cmDcm_t* p = _cmDcmHandleToPtr(h);
  572. cmDcmCfg_t* cp;
  573. unsigned inDevIdx = cmInvalidIdx;
  574. unsigned outDevIdx = cmInvalidIdx;
  575. // validate the label
  576. if((dcLabelStr = _cmDcmTrimLabel(p,dcLabelStr,"Audio cfg")) == NULL)
  577. return cmErrLastRC(&p->err);
  578. // validate the input device
  579. if( inDevNameStr != NULL )
  580. if(( inDevIdx = cmApDeviceLabelToIndex(inDevNameStr)) == cmInvalidIdx )
  581. return cmErrMsg(&p->err, kInvalidArgDcRC,"The input audio device name '%s' is not valid.",cmStringNullGuard(inDevNameStr));
  582. // validate the output device
  583. if( outDevNameStr != NULL )
  584. if(( outDevIdx = cmApDeviceLabelToIndex(outDevNameStr)) == cmInvalidIdx )
  585. return cmErrMsg(&p->err, kInvalidArgDcRC,"The output audio device name '%s' is not valid.",cmStringNullGuard(outDevNameStr));
  586. // validate the msg byte cnt
  587. if( msgQueueByteCnt == 0 )
  588. return cmErrMsg(&p->err, kInvalidArgDcRC,"The 'message queue size' must be greater than zero.");
  589. // validate the dev. frames per cycle
  590. if( devFramesPerCycle == 0 )
  591. return cmErrMsg(&p->err, kInvalidArgDcRC,"The 'device frames per cycle' must be greater than zero.");
  592. // validate the dsp frames per cycle
  593. if( dspFramesPerCycle == 0 || ((devFramesPerCycle/dspFramesPerCycle) * dspFramesPerCycle != devFramesPerCycle) )
  594. return cmErrMsg(&p->err, kInvalidArgDcRC,"The 'DSP frames per cycle' must be greater than zero and an integer factor of 'Device frames per cycle.'.");
  595. // validate the sample rate
  596. if( srate == 0 )
  597. return cmErrMsg(&p->err, kInvalidArgDcRC,"The audio sample rate must be greater than zero.");
  598. // if dcLabelStr is already in use for this location and type then update
  599. // the assoc'd recd otherwise create a new one.
  600. if((cp = _cmDcmFindOrCreateCfg(p,kAudioDcmTId, dcLabelStr)) == NULL )
  601. return cmErrLastRC(&p->err);
  602. unsigned inChCnt = cmApDeviceChannelCount( inDevIdx, true );
  603. unsigned outChCnt = cmApDeviceChannelCount( outDevIdx, false );
  604. cp->u.a.label = cp->dcLabelStr;
  605. cp->u.a.inDevLabelStr = cmMemAllocStr(inDevNameStr==NULL?"":inDevNameStr);
  606. cp->u.a.outDevLabelStr = cmMemAllocStr(outDevNameStr==NULL?"":outDevNameStr);
  607. cp->u.a.activeFl = activeFl;
  608. cp->u.a.rtSysArgs.rpt = p->err.rpt;
  609. cp->u.a.rtSysArgs.inDevIdx = inDevIdx;
  610. cp->u.a.rtSysArgs.outDevIdx = outDevIdx;
  611. cp->u.a.rtSysArgs.syncInputFl = syncInputFl;
  612. cp->u.a.rtSysArgs.msgQueueByteCnt = msgQueueByteCnt;
  613. cp->u.a.rtSysArgs.devFramesPerCycle = devFramesPerCycle;
  614. cp->u.a.rtSysArgs.dspFramesPerCycle = dspFramesPerCycle;
  615. cp->u.a.rtSysArgs.audioBufCnt = audioBufCnt;
  616. cp->u.a.rtSysArgs.srate = srate;
  617. cp->u.a.netNodeLabel = netNodeLabel==NULL ? NULL : cmMemAllocStr(netNodeLabel);
  618. cp->u.a.bcastAddr = bcastAddr ==NULL ? NULL : cmMemAllocStr(bcastAddr);
  619. cp->u.a.ipAddr = ipAddr ==NULL ? NULL : cmMemAllocStr(ipAddr);
  620. cp->u.a.ipPort = ipPort;
  621. cp->descStr = cmTsPrintfP(cp->descStr,"%sIn: Chs:%i %s\nOut: Chs:%i %s",activeFl?"":"INACTIVE ",inChCnt,cp->u.a.inDevLabelStr,outChCnt,cp->u.a.outDevLabelStr);
  622. return kOkDcRC;
  623. }
  624. bool cmDevCfgAudioIsDeviceActive( cmDevCfgH_t h, const cmChar_t* devNameStr, bool inputFl )
  625. {
  626. cmDcm_t* p = _cmDcmHandleToPtr(h);
  627. assert( p->clp != NULL );
  628. cmDcmCfg_t* cp = p->clp->cfg;
  629. unsigned i;
  630. for(i=0; cp!=NULL; cp=cp->next)
  631. if( cp->typeId == kAudioDcmTId )
  632. {
  633. bool fl;
  634. if( inputFl )
  635. fl = strcmp(devNameStr,cp->u.a.inDevLabelStr)==0;
  636. else
  637. fl = strcmp(devNameStr,cp->u.a.outDevLabelStr)==0;
  638. if( fl )
  639. return cp->u.a.activeFl;
  640. ++i;
  641. }
  642. return false;
  643. }
  644. unsigned cmDevCfgAudioActiveCount( cmDevCfgH_t h )
  645. {
  646. cmDcm_t* p = _cmDcmHandleToPtr(h);
  647. assert( p->clp != NULL );
  648. cmDcmCfg_t* cp = p->clp->cfg;
  649. unsigned n;
  650. for(n=0; cp!=NULL; cp=cp->next)
  651. if( cp->typeId == kAudioDcmTId && cp->u.a.activeFl )
  652. ++n;
  653. return n;
  654. }
  655. const cmChar_t* cmDevCfgAudioActiveLabel( cmDevCfgH_t h, unsigned idx )
  656. {
  657. cmDcm_t* p = _cmDcmHandleToPtr(h);
  658. assert( p->clp != NULL );
  659. cmDcmCfg_t* cp = p->clp->cfg;
  660. unsigned i;
  661. for(i=0; cp!=NULL; cp=cp->next)
  662. if( cp->typeId == kAudioDcmTId && cp->u.a.activeFl )
  663. {
  664. if( i == idx )
  665. return cp->dcLabelStr;
  666. ++i;
  667. }
  668. assert(0);
  669. return NULL;
  670. }
  671. const cmDcmAudio_t* cmDevCfgAudioActiveCfg( cmDevCfgH_t h, unsigned idx )
  672. {
  673. cmDcm_t* p = _cmDcmHandleToPtr(h);
  674. assert( p->clp != NULL );
  675. cmDcmCfg_t* cp = p->clp->cfg;
  676. unsigned i;
  677. for(i=0; cp!=NULL; cp=cp->next)
  678. if( cp->typeId == kAudioDcmTId && cp->u.a.activeFl )
  679. {
  680. if( i == idx )
  681. return &cp->u.a;
  682. ++i;
  683. }
  684. assert(0);
  685. return NULL;
  686. }
  687. unsigned cmDevCfgAudioActiveIndex( cmDevCfgH_t h, const cmChar_t* cfgLabel )
  688. {
  689. cmDcm_t* p = _cmDcmHandleToPtr(h);
  690. if( cfgLabel == NULL )
  691. return cmInvalidIdx;
  692. assert( p->clp != NULL );
  693. cmDcmCfg_t* cp = p->clp->cfg;
  694. unsigned i;
  695. for(i=0; cp!=NULL; cp=cp->next)
  696. if( cp->typeId == kAudioDcmTId && cp->u.a.activeFl)
  697. {
  698. if( cp->dcLabelStr!=NULL && strcmp(cp->dcLabelStr,cfgLabel) == 0 )
  699. return i;
  700. ++i;
  701. }
  702. return cmInvalidIdx;
  703. }
  704. const cmDcmAudio_t* cmDevCfgAudioCfg( cmDevCfgH_t h, unsigned cfgIdx )
  705. {
  706. cmDcm_t* p = _cmDcmHandleToPtr(h);
  707. cmDcmCfg_t* cp;
  708. if((cp = _cmDevCfgIndexToPtr(p, kAudioDcmTId, cfgIdx, true )) == NULL )
  709. return NULL;
  710. return &cp->u.a;
  711. }
  712. const cmDcmAudio_t* cmDevCfgAudioDevMap( cmDevCfgH_t h, unsigned usrAppId, unsigned usrMapId )
  713. {
  714. cmDcm_t* p = _cmDcmHandleToPtr(h);
  715. cmDcmMap_t* mp;
  716. if((mp =_cmDcmFindMap(p,kAudioDcmTId, usrAppId, usrMapId, true )) == NULL )
  717. return NULL;
  718. return &mp->cfg->u.a;
  719. }
  720. /*
  721. cmDcRC_t cmDevCfgNameNetPort(
  722. cmDevCfgH_t h,
  723. const cmChar_t* dcLabelStr,
  724. const cmChar_t* sockAddr,
  725. unsigned portNumber,
  726. bool activeFl)
  727. {
  728. cmDcm_t* p = _cmDcmHandleToPtr(h);
  729. cmDcmCfg_t* cp;
  730. if( portNumber > 0xffff )
  731. return cmErrMsg(&p->err,kInvalidArgDcRC,"The network port number %i is invalid. The valid IP port number range is:0-0xffff.");
  732. // validate the label
  733. if((dcLabelStr = _cmDcmTrimLabel(p,dcLabelStr,"Net cfg")) == NULL)
  734. return cmErrLastRC(&p->err);
  735. // if dcLabelStr is already in use for this location and type then update
  736. // the assoc'd recd otherwise create a new one.
  737. if((cp = _cmDcmFindOrCreateCfg(p,kNetDcmTId, dcLabelStr)) == NULL )
  738. return cmErrLastRC(&p->err);
  739. cp->u.n.label = cp->dcLabelStr;
  740. cp->u.n.sockAddr = cmMemAllocStr(sockAddr);
  741. cp->u.n.portNumber = portNumber;
  742. cp->u.n.activeFl = activeFl;
  743. cp->descStr = cmTsPrintfP(cp->descStr,"%s %s:%i",activeFl?"":"INACTIVE",sockAddr,portNumber);
  744. return kOkDcRC;
  745. }
  746. unsigned cmDevCfgNetActiveCount( cmDevCfgH_t h )
  747. {
  748. cmDcm_t* p = _cmDcmHandleToPtr(h);
  749. assert( p->clp != NULL );
  750. cmDcmCfg_t* cp = p->clp->cfg;
  751. unsigned n;
  752. for(n=0; cp!=NULL; cp=cp->next)
  753. if( cp->typeId == kNetDcmTId && cp->u.n.activeFl )
  754. ++n;
  755. return n;
  756. }
  757. const cmDcmNet_t* cmDevCfgNetActiveCfg( cmDevCfgH_t h, unsigned idx )
  758. {
  759. cmDcm_t* p = _cmDcmHandleToPtr(h);
  760. assert( p->clp != NULL );
  761. cmDcmCfg_t* cp = p->clp->cfg;
  762. unsigned i;
  763. for(i=0; cp!=NULL; cp=cp->next)
  764. if( cp->typeId == kNetDcmTId && cp->u.n.activeFl )
  765. {
  766. if( i == idx )
  767. return &cp->u.n;
  768. ++i;
  769. }
  770. assert(0);
  771. return NULL;
  772. }
  773. const cmDcmNet_t* cmDevCfgNetCfg( cmDevCfgH_t h, unsigned cfgIdx )
  774. {
  775. cmDcm_t* p = _cmDcmHandleToPtr(h);
  776. cmDcmCfg_t* cp;
  777. if((cp = _cmDevCfgIndexToPtr(p, kNetDcmTId, cfgIdx, true )) == NULL )
  778. return NULL;
  779. return &cp->u.n;
  780. }
  781. const cmDcmNet_t* cmDevCfgNetDevMap( cmDevCfgH_t h, unsigned usrAppId, unsigned usrMapId )
  782. {
  783. cmDcm_t* p = _cmDcmHandleToPtr(h);
  784. cmDcmMap_t* mp;
  785. if((mp =_cmDcmFindMap(p,kNetDcmTId, usrAppId, usrMapId, true )) == NULL )
  786. return NULL;
  787. return &mp->cfg->u.n;
  788. }
  789. */
  790. unsigned cmDevCfgLocCount( cmDevCfgH_t h )
  791. {
  792. unsigned n = 0;
  793. cmDcm_t* p = _cmDcmHandleToPtr(h);
  794. const cmDcmLoc_t* lp = p->loc;
  795. for(; lp!=NULL; lp=lp->next)
  796. ++n;
  797. return n;
  798. }
  799. const cmChar_t* cmDevCfgLocLabel( cmDevCfgH_t h, unsigned locIdx )
  800. {
  801. cmDcm_t* p = _cmDcmHandleToPtr(h);
  802. const cmDcmLoc_t* lp = p->loc;
  803. unsigned i;
  804. for(i=0; lp!=NULL; lp=lp->next,++i)
  805. if( i == locIdx )
  806. return lp->labelStr;
  807. assert(0);
  808. return NULL;
  809. }
  810. cmDcmLoc_t* _cmDcmLocLabelToPtr( cmDcm_t* p, const cmChar_t* locLabelStr, bool errFl)
  811. {
  812. if((locLabelStr = _cmDcmTrimLabel(p,locLabelStr,"location")) == NULL )
  813. return NULL;
  814. cmDcmLoc_t* lp = p->loc;
  815. for(; lp!=NULL; lp=lp->next)
  816. if( strcmp(lp->labelStr,locLabelStr) == 0 )
  817. return lp;
  818. if( errFl )
  819. cmErrMsg(&p->err,kLabelNotFoundDcRC,"The location label '%s' was not found.",locLabelStr);
  820. return NULL;
  821. }
  822. cmDcRC_t cmDevCfgLocStore( cmDevCfgH_t h, const cmChar_t* locLabelStr )
  823. {
  824. cmDcRC_t rc = kOkDcRC;
  825. cmDcm_t* p = _cmDcmHandleToPtr(h);
  826. unsigned i,j;
  827. // if this location label is already in use then it has already been stored.
  828. if( _cmDcmLocLabelToPtr(p,locLabelStr,false) != NULL )
  829. return kOkDcRC;
  830. // store the current loc ptr
  831. cmDcmLoc_t* olp = p->clp;
  832. cmDcmLoc_t* nlp;
  833. // create a new location recd and make it current
  834. if((rc = _cmDcmNewLoc(p,locLabelStr,&nlp)) != kOkDcRC )
  835. return kOkDcRC;
  836. // duplicate the cfg's
  837. cmDcmCfg_t* ocp = olp->cfg;
  838. for(; ocp!=NULL; ocp=ocp->next)
  839. {
  840. cmDcmCfg_t* ncp;
  841. // this will always create (never find) a new cfg recd
  842. if((ncp = _cmDcmFindOrCreateCfg(p,ocp->typeId, ocp->dcLabelStr)) == NULL )
  843. {
  844. rc = cmErrLastRC(&p->err);
  845. goto errLabel;
  846. }
  847. // duplicate the desc. string
  848. ncp->descStr = cmMemAllocStr(ocp->descStr);
  849. switch( ncp->typeId )
  850. {
  851. case kMidiDcmTId: _cmDcmDuplMidi(&ncp->u.m,&ocp->u.m); break;
  852. case kAudioDcmTId: _cmDcmDuplAudio(&ncp->u.a,&ocp->u.a); break;
  853. //case kNetDcmTId: _cmDcmDuplNet( &ncp->u.n,&ocp->u.n); break;
  854. default:
  855. assert(0);
  856. break;
  857. }
  858. }
  859. // duplicate the app array
  860. nlp->appCnt = olp->appCnt;
  861. nlp->app = cmMemAllocZ(cmDcmApp_t,nlp->appCnt);
  862. for(i=0; i<nlp->appCnt; ++i)
  863. {
  864. cmDcmApp_t* nap = nlp->app + i;
  865. cmDcmApp_t* oap = olp->app + i;
  866. //nap->usrAppId = oap->usrAppId;
  867. nap->activeFl = oap->activeFl;
  868. nap->mapCnt = oap->mapCnt;
  869. nap->map = cmMemAllocZ(cmDcmMap_t,nap->mapCnt);
  870. for(j=0; j<nap->mapCnt; ++j)
  871. {
  872. cmDcmMap_t* nmp = nap->map + j;
  873. cmDcmMap_t* omp = oap->map + j;
  874. nmp->tid = omp->tid;
  875. nmp->cfgId = omp->cfgId;
  876. if( omp->tid != kInvalidDcmTId && omp->cfg->dcLabelStr != NULL )
  877. nmp->cfg = _cmDcmCfgLabelToPtr(p,nmp->tid,omp->cfg->dcLabelStr,true);
  878. }
  879. }
  880. errLabel:
  881. if( rc != kOkDcRC )
  882. _cmDcmFreeLoc(p,nlp);
  883. return rc;
  884. }
  885. cmDcRC_t cmDevCfgLocRecall( cmDevCfgH_t h, const cmChar_t* locLabelStr )
  886. {
  887. cmDcm_t* p = _cmDcmHandleToPtr(h);
  888. cmDcmLoc_t* lp;
  889. if((lp = _cmDcmLocLabelToPtr(p,locLabelStr,true)) == NULL)
  890. return cmErrLastRC(&p->err);
  891. p->clp = lp;
  892. return kOkDcRC;
  893. }
  894. cmDcRC_t cmDevCfgLocDelete( cmDevCfgH_t h, const cmChar_t* locLabelStr )
  895. {
  896. cmDcm_t* p = _cmDcmHandleToPtr(h);
  897. cmDcmLoc_t* lp;
  898. if((lp = _cmDcmLocLabelToPtr(p,locLabelStr,true)) == NULL )
  899. return cmErrLastRC(&p->err);
  900. _cmDcmFreeLoc(p,lp);
  901. return kOkDcRC;
  902. }
  903. unsigned cmDevCfgLocCurIndex( cmDevCfgH_t h )
  904. {
  905. unsigned i;
  906. cmDcm_t* p = _cmDcmHandleToPtr(h);
  907. cmDcmLoc_t* lp = p->loc;
  908. for(i=0; lp!=NULL; lp=lp->next,++i)
  909. if( lp == p->clp )
  910. return i;
  911. assert(0);
  912. return cmInvalidIdx;
  913. }
  914. cmDcRC_t _cmDcmJsonNotFound( cmDcm_t* p, const cmChar_t* tagStr )
  915. { return cmErrMsg(&p->err,kJsonFailDcRC,"JSON element '%s' not found.",tagStr); }
  916. cmDcRC_t _cmDcmJsonSyntaxErr( cmDcm_t* p, const cmChar_t* tagStr )
  917. { return cmErrMsg(&p->err,kJsonFailDcRC,"JSON syntax error '%s' not found.",tagStr); }
  918. cmDcRC_t _cmDevCfgRead( cmDcm_t* p, cmJsonH_t jsH, const cmJsonNode_t* rootObjPtr )
  919. {
  920. cmDcRC_t rc = kOkDcRC;
  921. const cmChar_t* errLabelPtr = NULL;
  922. cmJsonNode_t* cfgNp, *locArrNp;
  923. unsigned i,j;
  924. cmDevCfgH_t h;
  925. h.h = p;
  926. // clear the all locations
  927. _cmDcmFreeAllLocs(p);
  928. if((cfgNp = cmJsonFindValue(jsH, "cfg", rootObjPtr, kObjectTId )) == NULL )
  929. return _cmDcmJsonNotFound(p,"cfg");
  930. // get loc array
  931. if((locArrNp = cmJsonFindValue(jsH,"loc",cfgNp, kArrayTId )) == NULL )
  932. return _cmDcmJsonNotFound(p,"loc");
  933. // for each loc object
  934. for(i=0; i<cmJsonChildCount(locArrNp); ++i)
  935. {
  936. cmJsonNode_t* locObjNp, *cfgArrNp;
  937. const cmChar_t* locLabelStr = NULL;
  938. // get the loc object
  939. if((locObjNp = cmJsonArrayElement(locArrNp,i)) == NULL || cmJsonIsObject(locObjNp)==false )
  940. return _cmDcmJsonSyntaxErr(p,"loc object");
  941. // read the loc object fields
  942. if( cmJsonMemberValues(locObjNp, &errLabelPtr,
  943. "label", kStringTId, &locLabelStr,
  944. "cfg", kArrayTId, &cfgArrNp,
  945. NULL ) != kOkJsRC )
  946. { return _cmDcmJsonNotFound(p,errLabelPtr); }
  947. // create a new location recd
  948. cmDcmLoc_t* locPtr = NULL;
  949. if((rc = _cmDcmNewLoc(p,locLabelStr,&locPtr)) != kOkDcRC )
  950. return cmErrMsg(&p->err,kJsonFailDcRC,"Location '%s' create failed.",cmStringNullGuard(locLabelStr));
  951. /*
  952. // read each app object
  953. for(j=0; j<cmJsonChildCount(appArrNp); ++j)
  954. {
  955. cmJsonNode_t* appObjNp;
  956. // get the app object
  957. if((appObjNp = cmJsonArrayElement(appArrNp,j)) == NULL || cmJsonIsObject(appObjNp)==false )
  958. return _cmDcmJsonSyntaxErr(p,"loc object");
  959. // locate the map array
  960. cmJsonNode_t* mapArrNp;
  961. if((mapArrNp = cmJsonFindValue(jsH,"map",appObjNp,kArrayTId)) == NULL )
  962. return _cmDcmJsonNotFound(p,"map");
  963. // for each map array
  964. for(k=0; k<cmJsonChildCount(mapArrNp); ++k)
  965. {
  966. unsigned tid,cfgId;
  967. cmJsonNode_t* mapObjNp;
  968. if((mapObjNp = cmJsonArrayElement(mapArrNp,j)) == NULL || cmJsonIsObject(mapObjNp)==false)
  969. return _cmDcmJsonSyntaxErr(p,"cfg object");
  970. if( cmJsonMemberValues( mapObjNp, &errLabelPtr,
  971. "tid", kIntTId, &tid,
  972. "cfgId", kIntTId, &cfgId,
  973. NULL ) != kOkDcRC )
  974. { return _cmDcmJsonSyntaxErr(p,errLabelPtr); }
  975. }
  976. }
  977. */
  978. // read each cfg object
  979. for(j=0; j<cmJsonChildCount(cfgArrNp); ++j)
  980. {
  981. cmJsonNode_t* cfgObjNp;
  982. const cmChar_t* dcLabelStr;
  983. const cmChar_t* descStr;
  984. unsigned cfgId, typeId;
  985. if((cfgObjNp = cmJsonArrayElement(cfgArrNp,j)) == NULL || cmJsonIsObject(cfgObjNp)==false)
  986. return _cmDcmJsonSyntaxErr(p,"cfg object");
  987. if( cmJsonMemberValues( cfgObjNp, &errLabelPtr,
  988. "label", kStringTId, &dcLabelStr,
  989. "cfgId", kIntTId, &cfgId,
  990. "typeId", kIntTId, &typeId,
  991. "desc", kStringTId, &descStr,
  992. NULL ) != kOkJsRC )
  993. { _cmDcmJsonSyntaxErr(p,errLabelPtr); }
  994. cmDcmMidi_t m;
  995. cmDcmAudio_t a;
  996. //cmDcmNet_t n;
  997. switch( typeId )
  998. {
  999. case kMidiDcmTId:
  1000. if( cmJsonMemberValues( cfgObjNp, &errLabelPtr,
  1001. "devLabelStr", kStringTId, &m.devLabelStr,
  1002. "portLabelStr",kStringTId, &m.portLabelStr,
  1003. "inputFl", kBoolTId, &m.inputFl,
  1004. NULL) != kOkJsRC )
  1005. {
  1006. rc = _cmDcmJsonSyntaxErr(p,errLabelPtr);
  1007. goto errLabel;
  1008. }
  1009. if((rc = cmDevCfgNameMidiPort(h,dcLabelStr,m.devLabelStr,m.portLabelStr,m.inputFl)) != kOkDcRC )
  1010. goto errLabel;
  1011. break;
  1012. case kAudioDcmTId:
  1013. if( cmJsonMemberValues( cfgObjNp, &errLabelPtr,
  1014. "inDevLabelStr", kStringTId, &a.inDevLabelStr,
  1015. "outDevLabelStr", kStringTId, &a.outDevLabelStr,
  1016. "syncInputFl", kBoolTId, &a.rtSysArgs.syncInputFl,
  1017. "msgQueueByteCnt", kIntTId, &a.rtSysArgs.msgQueueByteCnt,
  1018. "devFramesPerCycle", kIntTId, &a.rtSysArgs.devFramesPerCycle,
  1019. "dspFramesPerCycle", kIntTId, &a.rtSysArgs.dspFramesPerCycle,
  1020. "audioBufCnt", kIntTId, &a.rtSysArgs.audioBufCnt,
  1021. "srate", kRealTId, &a.rtSysArgs.srate,
  1022. "netNodeLabel", kStringTId, &a.netNodeLabel,
  1023. "bcastAddr", kStringTId, &a.bcastAddr,
  1024. "ipAddr", kStringTId, &a.ipAddr,
  1025. "ipPort", kIntTId, &a.ipPort,
  1026. "active", kBoolTId, &a.activeFl,
  1027. NULL ) != kOkJsRC )
  1028. {
  1029. rc = _cmDcmJsonSyntaxErr(p,errLabelPtr);
  1030. goto errLabel;
  1031. }
  1032. if((rc = cmDevCfgNameAudioPort(h,dcLabelStr,a.inDevLabelStr,a.outDevLabelStr,
  1033. a.rtSysArgs.syncInputFl,
  1034. a.rtSysArgs.msgQueueByteCnt,
  1035. a.rtSysArgs.devFramesPerCycle,
  1036. a.rtSysArgs.dspFramesPerCycle,
  1037. a.rtSysArgs.audioBufCnt,
  1038. a.rtSysArgs.srate,
  1039. a.netNodeLabel,
  1040. a.bcastAddr,
  1041. a.ipAddr,
  1042. a.ipPort,
  1043. a.activeFl)) != kOkDcRC )
  1044. {
  1045. goto errLabel;
  1046. }
  1047. break;
  1048. /*
  1049. case kNetDcmTId:
  1050. if( cmJsonMemberValues( cfgObjNp, &errLabelPtr,
  1051. "sockAddr", kStringTId, &n.sockAddr,
  1052. "portNumber", kIntTId, &n.portNumber,
  1053. "activeFl", kBoolTId, &n.activeFl,
  1054. NULL ) != kOkJsRC )
  1055. {
  1056. rc = _cmDcmJsonSyntaxErr(p,errLabelPtr);
  1057. goto errLabel;
  1058. }
  1059. if((rc = cmDevCfgNameNetPort(h,dcLabelStr,n.sockAddr,n.portNumber,n.activeFl)) != kOkDcRC )
  1060. goto errLabel;
  1061. break;
  1062. */
  1063. default:
  1064. assert(0);
  1065. break;
  1066. }
  1067. }
  1068. }
  1069. errLabel:
  1070. return kOkDcRC;
  1071. }
  1072. cmDcRC_t _cmDevCfgWrite( cmDcm_t* p, cmJsonH_t jsH, cmJsonNode_t* rootObjPtr )
  1073. {
  1074. cmDcRC_t rc = kOkDcRC;
  1075. const cmDcmLoc_t* lp = p->loc;
  1076. cmJsonNode_t* cfgNp = cmJsonInsertPairObject(jsH, rootObjPtr, "cfg" );
  1077. // create the loc array
  1078. cmJsonNode_t* locArrNp = cmJsonInsertPairArray( jsH, cfgNp, "loc" );
  1079. for(; lp!=NULL; lp=lp->next)
  1080. {
  1081. // create the 'loc' object
  1082. cmJsonNode_t* locObjNp = cmJsonCreateObject(jsH,locArrNp);
  1083. // set the loc label
  1084. cmJsonInsertPairString(jsH, locObjNp, "label", lp->labelStr );
  1085. /*
  1086. // create the 'loc.app[]' array
  1087. cmJsonNode_t* appArrNp = cmJsonInsertPairArray(jsH,locObjNp,"app");
  1088. // for each app recd
  1089. for(i=0; i<lp->appCnt; ++i)
  1090. if( lp->app[i].activeFl )
  1091. {
  1092. // create the app recd
  1093. cmJsonNode_t* appNp = cmJsonCreateObject(jsH,appArrNp );
  1094. // create the map array
  1095. cmJsonNode_t* mapArrNp = cmJsonInsertPairArray(jsH, appNp, "map" );
  1096. // for each map recd
  1097. for(j=0; j<lp->app[i].mapCnt; ++j)
  1098. {
  1099. cmJsonNode_t* mapNp = cmJsonCreateObject(jsH,mapArrNp);
  1100. cmJsonInsertPairs(jsH, mapNp,
  1101. "tid", kIntTId, lp->app[i].map[j].tid,
  1102. "cfgId", kIntTId, lp->app[i].map[j].cfgId,
  1103. NULL );
  1104. }
  1105. }
  1106. */
  1107. // create the 'loc.cfg[]' array
  1108. cmJsonNode_t* cfgArrNp = cmJsonInsertPairArray(jsH,locObjNp,"cfg");
  1109. // for each cfg recd
  1110. cmDcmCfg_t* cp = lp->cfg;
  1111. for(; cp!=NULL; cp=cp->next)
  1112. {
  1113. // create the cfg recd
  1114. cmJsonNode_t* cfgObjNp = cmJsonCreateObject(jsH,cfgArrNp);
  1115. // fill the cfg recd
  1116. cmJsonInsertPairs(jsH, cfgObjNp,
  1117. "label", kStringTId, cp->dcLabelStr,
  1118. "cfgId", kIntTId, cp->cfgId,
  1119. "typeId", kIntTId, cp->typeId,
  1120. "desc", kStringTId, cp->descStr,
  1121. NULL );
  1122. switch( cp->typeId )
  1123. {
  1124. case kMidiDcmTId:
  1125. cmJsonInsertPairs(jsH, cfgObjNp,
  1126. "devLabelStr", kStringTId, cp->u.m.devLabelStr,
  1127. "portLabelStr",kStringTId, cp->u.m.portLabelStr,
  1128. "inputFl", kBoolTId, cp->u.m.inputFl,
  1129. NULL );
  1130. break;
  1131. case kAudioDcmTId:
  1132. cmJsonInsertPairs(jsH, cfgObjNp,
  1133. "inDevLabelStr", kStringTId, cp->u.a.inDevLabelStr,
  1134. "outDevLabelStr", kStringTId, cp->u.a.outDevLabelStr,
  1135. "syncInputFl", kBoolTId, cp->u.a.rtSysArgs.syncInputFl,
  1136. "msgQueueByteCnt", kIntTId, cp->u.a.rtSysArgs.msgQueueByteCnt,
  1137. "devFramesPerCycle", kIntTId, cp->u.a.rtSysArgs.devFramesPerCycle,
  1138. "dspFramesPerCycle", kIntTId, cp->u.a.rtSysArgs.dspFramesPerCycle,
  1139. "audioBufCnt", kIntTId, cp->u.a.rtSysArgs.audioBufCnt,
  1140. "srate", kRealTId, cp->u.a.rtSysArgs.srate,
  1141. "netNodeLabel", kStringTId, cp->u.a.netNodeLabel,
  1142. "bcastAddr", kStringTId, cp->u.a.bcastAddr,
  1143. "ipAddr", kStringTId, cp->u.a.ipAddr,
  1144. "ipPort", kIntTId, cp->u.a.ipPort,
  1145. "active", kBoolTId, cp->u.a.activeFl,
  1146. NULL );
  1147. break;
  1148. /*
  1149. case kNetDcmTId:
  1150. cmJsonInsertPairs(jsH, cfgObjNp,
  1151. "sockAddr", kStringTId, cp->u.n.sockAddr,
  1152. "portNumber",kIntTId, cp->u.n.portNumber,
  1153. "activeFl", kBoolTId, cp->u.n.activeFl,
  1154. NULL );
  1155. break;
  1156. */
  1157. default:
  1158. assert(0);
  1159. break;
  1160. }
  1161. }
  1162. }
  1163. return rc;
  1164. }
  1165. cmDcRC_t cmDevCfgWrite( cmDevCfgH_t h, const cmChar_t* fn )
  1166. {
  1167. cmDcRC_t rc = kOkDcRC;
  1168. cmDcm_t* p = _cmDcmHandleToPtr(h);
  1169. cmJsonH_t jsH = cmJsonNullHandle;
  1170. if( fn == NULL )
  1171. fn = p->fn;
  1172. // validate the filename
  1173. if( fn == NULL || strlen(fn)==0 )
  1174. return cmErrMsg(&p->err,kInvalidFnDcRC,"No output file name was provided.");
  1175. // create a json object
  1176. if( cmJsonInitialize( &jsH, p->ctx ) != kOkJsRC )
  1177. {
  1178. rc = cmErrMsg(&p->err,kJsonFailDcRC,"An empty JSON tree could not be created.");
  1179. goto errLabel;
  1180. }
  1181. // insert a wrapper object as the root
  1182. if( cmJsonCreateObject( jsH, NULL ) == NULL )
  1183. {
  1184. rc = cmErrMsg(&p->err,kJsonFailDcRC,"The JSON root object could not be created.");
  1185. goto errLabel;
  1186. }
  1187. // fill the JSON tree
  1188. if((rc = _cmDevCfgWrite(p,jsH,cmJsonRoot(jsH))) != kOkDcRC )
  1189. goto errLabel;
  1190. // write the output file
  1191. if( cmJsonWrite(jsH, cmJsonRoot(jsH), fn ) != kOkJsRC )
  1192. {
  1193. rc = cmErrMsg(&p->err,kJsonFailDcRC,"The JSON file write failed on '%s'.",cmStringNullGuard(fn));
  1194. goto errLabel;
  1195. }
  1196. errLabel:
  1197. if( cmJsonFinalize(&jsH) != kOkJsRC )
  1198. cmErrMsg(&p->err,kJsonFailDcRC,"JSON tree finalization failed.");
  1199. return rc;
  1200. }