libcm is a C development framework with an emphasis on audio signal processing applications.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

cmDevCfg.c 40KB

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