libcm is a C development framework with an emphasis on audio signal processing applications.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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