libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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