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.

cmMsgProtocol.c 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include "cmPrefix.h"
  2. #include "cmGlobal.h"
  3. #include "cmFloatTypes.h"
  4. #include "cmRpt.h"
  5. #include "cmErr.h"
  6. #include "cmCtx.h"
  7. #include "cmJson.h"
  8. #include "dsp/cmDspValue.h"
  9. #include "cmMsgProtocol.h"
  10. // buffer layout is:
  11. // [ cmDspUiHdr_t <data> ]
  12. // The format of the <data> is determiend by hdr.value.
  13. // Since hdr.value is the last field in the cmDspUiHdr_t record
  14. // the data follows this value.
  15. cmMsgRC_t cmMsgSend(
  16. cmErr_t* err,
  17. unsigned asSubIdx,
  18. unsigned msgTypeId,
  19. unsigned selId,
  20. unsigned flags,
  21. unsigned instId,
  22. unsigned instVarId,
  23. const cmDspValue_t* valPtr,
  24. cmMsgSendFuncPtr_t sendFunc,
  25. void* cbDataPtr )
  26. {
  27. unsigned bufByteCnt = sizeof(cmDspUiHdr_t);
  28. unsigned dataByteCnt = 0;
  29. if( valPtr != NULL )
  30. dataByteCnt = cmDsvSerialDataByteCount(valPtr);
  31. bufByteCnt += dataByteCnt;
  32. char buf[ bufByteCnt ];
  33. cmDspUiHdr_t* hdr = (cmDspUiHdr_t*)buf;
  34. hdr->asSubIdx = asSubIdx;
  35. hdr->uiId = msgTypeId; // see kXXXSelAsId identifiers in cmAudioSys.h
  36. hdr->selId = selId; // if msgTypeId==kUiSelAsId then see kXXXDuId in cmDspUi.h
  37. hdr->flags = flags;
  38. hdr->instId = instId;
  39. hdr->instVarId = instVarId;
  40. if( valPtr == NULL )
  41. cmDsvSetNull(&hdr->value);
  42. else
  43. {
  44. // this function relies on the 'hdr.value' field being the last field in the 'hdr'.
  45. if( cmDsvSerialize( valPtr, &hdr->value, sizeof(cmDspValue_t) + dataByteCnt) != kOkDsvRC )
  46. return cmErrMsg(err,kSerializeFailMsgRC,"An attempt to serialize a UI msg failed.");
  47. }
  48. const void* vp = buf;
  49. if( sendFunc(cbDataPtr,bufByteCnt,vp) != cmOkRC )
  50. return cmErrMsg(err,kSendFailMsgRC,"An attempt to transmit a msg to the host failed.");
  51. return kOkMsgRC;
  52. }
  53. // Return the unsigned value at the specified byte offset into the msg buffer.
  54. cmMsgRC_t _cmMsgPeekAtUInt( const void* msgArray[], unsigned msgByteCntArray[], unsigned segCnt, unsigned offset, unsigned* retValPtr )
  55. {
  56. unsigned i,j,k;
  57. for(k=0,i=0; i<segCnt; ++i)
  58. for(j=0; j<msgByteCntArray[i]; ++j,++k)
  59. if( k == offset )
  60. break;
  61. if( i == segCnt )
  62. return kDecodeFailMsgRC;
  63. *retValPtr = *((unsigned*)((char*)(msgArray[i]) + j));
  64. return kOkMsgRC;
  65. }
  66. cmMsgRC_t cmMsgPeekAsSubIndex( const void* msgArray[], unsigned msgByteCntArray[], unsigned segCnt, unsigned* retValPtr )
  67. {
  68. cmDspUiHdr_t h;
  69. unsigned offset = ((char*)(&h.asSubIdx)) - ((char*)&h);
  70. return _cmMsgPeekAtUInt(msgArray,msgByteCntArray,segCnt,offset,retValPtr);
  71. }
  72. cmMsgRC_t cmMsgPeekMsgTypeId( const void* msgArray[], unsigned msgByteCntArray[], unsigned segCnt, unsigned* retValPtr )
  73. {
  74. cmDspUiHdr_t h;
  75. unsigned offset = ((char*)(&h.uiId)) - ((char*)&h);
  76. return _cmMsgPeekAtUInt(msgArray,msgByteCntArray,segCnt,offset,retValPtr);
  77. }
  78. cmMsgRC_t cmMsgPeekSelId( const void* msgArray[], unsigned msgByteCntArray[], unsigned segCnt, unsigned* retValPtr )
  79. {
  80. cmDspUiHdr_t h;
  81. unsigned offset = ((char*)(&h.selId)) - ((char*)&h);
  82. return _cmMsgPeekAtUInt(msgArray,msgByteCntArray,segCnt,offset,retValPtr);
  83. }
  84. cmMsgRC_t cmMsgPeekFlags( const void* msgArray[], unsigned msgByteCntArray[], unsigned segCnt, unsigned* retValPtr )
  85. {
  86. cmDspUiHdr_t h;
  87. unsigned offset = ((char*)(&h.flags)) - ((char*)&h);
  88. return _cmMsgPeekAtUInt(msgArray,msgByteCntArray,segCnt,offset,retValPtr);
  89. }
  90. cmMsgRC_t cmMsgPeekInstId( const void* msgArray[], unsigned msgByteCntArray[], unsigned segCnt, unsigned* retValPtr )
  91. {
  92. cmDspUiHdr_t h;
  93. unsigned offset = ((char*)(&h.instId)) - ((char*)&h);
  94. return _cmMsgPeekAtUInt(msgArray,msgByteCntArray,segCnt,offset,retValPtr);
  95. }
  96. cmMsgRC_t cmMsgPeekInstVarId( const void* msgArray[], unsigned msgByteCntArray[], unsigned segCnt, unsigned* retValPtr )
  97. {
  98. cmDspUiHdr_t h;
  99. unsigned offset = ((char*)(&h.instVarId)) - ((char*)&h);
  100. return _cmMsgPeekAtUInt(msgArray,msgByteCntArray,segCnt,offset,retValPtr);
  101. }