restbitvar.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "restvar.h"
  2. #define GET_BOOL_VAL(p, m) (!!(*p & m))
  3. #define SET_BIT(p, m) (*p |= m)
  4. #define CLR_BIT(p, m) (*p &= ~m)
  5. #define STORE_BIT(p, m, b) (b) ? SET_BIT(p, m) : CLR_BIT(p, m)
  6. /////////////////////////////////////////////////////////////////////////////
  7. CRestBitVariable::CRestBitVariable(void *pData, size_t nOffset, int nBitNr, HSHM hShm, const char *pszName, CRest *pParent)
  8. : m_name(pszName), m_pszPath(NULL), m_hShm(hShm), m_pParent(pParent), m_nCbVarpath(0)
  9. {
  10. if(!pData || !hShm || (nBitNr > 7))
  11. {
  12. ASSERT(false);
  13. return;
  14. }
  15. m_mask = (1 << nBitNr);
  16. m_pShmByte = (uint8_t*)pData + nOffset;
  17. }
  18. CRestBitVariable::~CRestBitVariable(void)
  19. {
  20. }
  21. /////////////////////////////////////////////////////////////////////////////
  22. void CRestBitVariable::CreateMembersTable(CRestVarTable &vt)
  23. {
  24. vt.AddVar(static_cast<CRest*>(this));
  25. }
  26. void CRestBitVariable::InitPath(CRest *pParent, const char *pszMemberName, int nIndex)
  27. {
  28. CRest::CreatePath(pParent, pszMemberName, nIndex, m_path);
  29. m_pszPath = m_path.c_str();
  30. m_nCbVarpath = m_path.length();
  31. }
  32. void CRestBitVariable::GetValue(int nReqIndex, json_t *pjtMap)
  33. {
  34. json_array_append_new(pjtMap, GetValue(nReqIndex));
  35. }
  36. json_t* CRestBitVariable::GetValue(int nReqIndex)
  37. {
  38. json_t *pjtVal = NULL;
  39. Lock();
  40. bool bVal = GET_BOOL_VAL(m_pShmByte, m_mask);
  41. Unlock();
  42. if((pjtVal = json_boolean(bVal)))
  43. return CreateValueObject(nReqIndex, m_pszPath, -1, json_typeof(pjtVal), m_name.c_str(), pjtVal);
  44. else
  45. return CreateStatusObject(nReqIndex, -1, "Unexpected Error!", m_pszPath);
  46. }
  47. json_t* CRestBitVariable::SetValue(int nReqIndex, json_t *pjtVal)
  48. {
  49. if(!pjtVal)
  50. return CreateStatusObject(nReqIndex, -1, "Unexpected error!", GetPath());
  51. json_int_t jInt;
  52. int nCode = 0;
  53. const char *pszMsg = NULL;
  54. int nType = json_typeof(pjtVal);
  55. switch(nType)
  56. {
  57. case JSON_INTEGER:
  58. jInt = json_integer_value(pjtVal);
  59. STORE_BIT(m_pShmByte, m_mask, !!jInt);
  60. break;
  61. case JSON_TRUE:
  62. SET_BIT(m_pShmByte, m_mask);
  63. break;
  64. case JSON_FALSE:
  65. CLR_BIT(m_pShmByte, m_mask);
  66. break;
  67. default:
  68. nCode = 4;
  69. pszMsg = "Cannot assign incompatible JSON type!";
  70. break;
  71. }
  72. return CreateStatusObject(nReqIndex, nCode, pszMsg, GetPath());
  73. }
  74. /////////////////////////////////////////////////////////////////////////////
  75. void CRestBitVariable::Lock(void)
  76. {
  77. ::GfaIpcLockSHM(m_hShm);
  78. }
  79. void CRestBitVariable::Unlock(void)
  80. {
  81. ::GfaIpcUnlockSHM(m_hShm);
  82. }