main.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <stdio.h>
  2. #include <string.h> // memset
  3. #include <gfa/gfaserial.h>
  4. #include <gfa/gfamininetmst.h>
  5. #include "../src/dbghlp.h"
  6. ////////////////////////////////////////////////////////////////////////////////////
  7. #define _DEVICE_NAME "/dev/ttyO4"
  8. #define _NODE_ADDR 0x11
  9. #define _BAUD_RATE 19200
  10. #define _DATA_BITS 8
  11. #define _STOP_BITS 1
  12. #define _PARITY 'N'
  13. ////////////////////////////////////////////////////////////////////////////////////
  14. static int _BootloaderGetExecutionContext(HGFAMINEMST hMinem, uint8_t nNodeAddr)
  15. {
  16. if(hMinem && !NODE_IS_MULTICAST(nNodeAddr))
  17. {
  18. uint8_t nIndex;
  19. ssize_t nRet, nLen;
  20. char txb[32], rxb[256];
  21. nLen = GfaMininetMasterBuildFrame(hMinem, nNodeAddr, 0, "BU", 2, txb, sizeof(txb));
  22. if((nRet = GfaMininetMasterTransmitFrame(hMinem, txb, nLen)) != nLen)
  23. return -1;
  24. if((nLen = GfaMininetMasterReceiveFrame(hMinem, rxb, sizeof(rxb), true)) <= 0)
  25. return -1;
  26. nRet = GfaMininetMasterEvaluateSlaveResponse(hMinem, nNodeAddr, rxb, nLen, true, &nIndex);
  27. if(nRet == MINET_SLAVE_RESPONSE_SUCCESS)
  28. return -1; // Slave is neither a Bootloader nor an App
  29. if(nRet == MINET_SLAVE_RESPONSE_ACK)
  30. return 0; // Bootloader responding
  31. else if(nRet == MINET_SLAVE_RESPONSE_INDEX_IS_STATUS_CODE)
  32. return 1; // Application responding
  33. return -1;
  34. }
  35. return -1;
  36. }
  37. ////////////////////////////////////////////////////////////////////////////////////
  38. ////////////////////////////////////////////////////////////////////////////////////
  39. int main(void)
  40. {
  41. int nRet = 0;
  42. ////////////////////////////////////////////////////////////////////////////////
  43. GFA_MININET_MST_CFG_PARAMS mmcp;
  44. memset(&mmcp, 0, sizeof(mmcp));
  45. if(GfaSerialGetDeviceInterface(&mmcp.devcfg.itf))
  46. {
  47. HGFAMINEMST hMinem;
  48. GFA_SER_CFG_PARAMS scp;
  49. memset(&scp, 0, sizeof(scp));
  50. ////////////////////////////////////////////////////////////////////////////
  51. // serial interface configuration
  52. scp.baud = _BAUD_RATE;
  53. scp.data = _DATA_BITS;
  54. scp.stop = _STOP_BITS;
  55. scp.parity = _PARITY;
  56. scp.bHandleTxEcho = true;
  57. ////////////////////////////////////////////////////////////////////////////
  58. // mininet master configuration
  59. mmcp.devcfg.pszDeviceName = _DEVICE_NAME;
  60. mmcp.devcfg.pDevParams = &scp;
  61. mmcp.devcfg.nSizeDevParams = sizeof(scp);
  62. ////////////////////////////////////////////////////////////////////////////
  63. //
  64. if((hMinem = GfaMininetMasterOpen(&mmcp)))
  65. {
  66. do
  67. {
  68. if((nRet = GfaMininetMasterSetVerbosity(hMinem, 4)) < 0) // enable output of frames
  69. break;
  70. if((nRet = GfaMininetMasterResetSlaveIndex(hMinem, _NODE_ADDR)) < 0)
  71. break;
  72. if((nRet = GfaMininetMasterPingSlave(hMinem, _NODE_ADDR)) < 0)
  73. break;
  74. nRet = _BootloaderGetExecutionContext(hMinem, _NODE_ADDR); // demonstrates the implementation of commands
  75. }
  76. while(0);
  77. GfaMininetMasterClose(hMinem);
  78. }
  79. }
  80. return nRet;
  81. }