scpi_response_helper.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #define SCPI_CORE_PRIVATE // access to 'scpi_core_private.h'
  2. #include "app/scpi/scpi_core.h"
  3. #include "app/scpi/scpi_core_private.h"
  4. #include "app/scpi/scpi_parser.h"
  5. #define SCPI_CORE_STATE__PROCESSDATA_GUARD
  6. #include "app/scpi/CommandParserStates/process_data.h"
  7. // @scpi_response_helper
  8. // Helper function. Helps to implement correct data output from the QUERY.
  9. // Copies the query data and updates the @sRead.nDataLength and @common_ctx->status
  10. // to the appropriate values, also returns actual query data offset.
  11. // @common_ctx - common context pointer passed to the handler;
  12. // @idx - current data offset in the handler;
  13. // @source - the data source pointer;
  14. // @isdone - optional, if passed, specifies wheither to set DONE status or not;
  15. // Returns: actual data offset for the handler after the data pushed into the output buffer
  16. size_t scpi_response_helper( sProcessProgramDataCommonContext_t * common_ctx, size_t idx, void * source, size_t source_max, bool * isdone )
  17. {
  18. sScpiParserContext_t * global_ctx = common_ctx->global_ctx;
  19. // @src - response string iterator
  20. const char * src = ((const char *)source) + idx;
  21. // @length - length of data to be copied
  22. size_t length = strnlen( (const char *)src, source_max );
  23. // @length - number of bytes actually written
  24. size_t written = scpi_WriteChunkOutput( src, length );
  25. // Check for end-condition:
  26. if( NULL != isdone )
  27. {
  28. // external controlled DONE status
  29. if( *isdone )
  30. {
  31. common_ctx->status = eProgramDataDone;
  32. }
  33. else
  34. {
  35. common_ctx->status = eProgramDataNeedRead;
  36. }
  37. }
  38. else
  39. {
  40. // internal controlled DONE status
  41. if( written == length )
  42. {
  43. common_ctx->status = eProgramDataDone;
  44. }
  45. else
  46. {
  47. common_ctx->status = eProgramDataNeedRead;
  48. }
  49. }
  50. return written;
  51. }
  52. // @scpi_response_done_helper
  53. // Helper function. Helps to implement setting of the status
  54. // @isdone - DONE status for handler
  55. void scpi_response_done_helper( sProcessProgramDataCommonContext_t * common_ctx, bool isdone )
  56. {
  57. // external controlled DONE status
  58. if( isdone )
  59. {
  60. common_ctx->status = eProgramDataDone;
  61. }
  62. else
  63. {
  64. common_ctx->status = eProgramDataNeedRead;
  65. }
  66. }