network_ra.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (C) 2011-2017 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include <stdint.h>
  32. #include <stdlib.h>
  33. #include <stdio.h>
  34. #include "network_ra.h"
  35. #include "service_provider.h"
  36. // Used to send requests to the service provider sample. It
  37. // simulates network communication between the ISV app and the
  38. // ISV service provider. This would be modified in a real
  39. // product to use the proper IP communication.
  40. //
  41. // @param server_url String name of the server URL
  42. // @param p_req Pointer to the message to be sent.
  43. // @param p_resp Pointer to a pointer of the response message.
  44. // @return int
  45. int ra_network_send_receive(const char *server_url,
  46. const ra_samp_request_header_t *p_req,
  47. ra_samp_response_header_t **p_resp)
  48. {
  49. int ret = 0;
  50. ra_samp_response_header_t* p_resp_msg;
  51. if((NULL == server_url) ||
  52. (NULL == p_req) ||
  53. (NULL == p_resp))
  54. {
  55. return -1;
  56. }
  57. switch(p_req->type)
  58. {
  59. case TYPE_RA_MSG0:
  60. ret = sp_ra_proc_msg0_req((const sample_ra_msg0_t*)((uint8_t*)p_req
  61. + sizeof(ra_samp_request_header_t)),
  62. p_req->size);
  63. if (0 != ret)
  64. {
  65. fprintf(stderr, "\nError, call sp_ra_proc_msg1_req fail [%s].",
  66. __FUNCTION__);
  67. }
  68. break;
  69. case TYPE_RA_MSG1:
  70. ret = sp_ra_proc_msg1_req((const sample_ra_msg1_t*)((uint8_t*)p_req
  71. + sizeof(ra_samp_request_header_t)),
  72. p_req->size,
  73. &p_resp_msg);
  74. if(0 != ret)
  75. {
  76. fprintf(stderr, "\nError, call sp_ra_proc_msg1_req fail [%s].",
  77. __FUNCTION__);
  78. }
  79. else
  80. {
  81. *p_resp = p_resp_msg;
  82. }
  83. break;
  84. case TYPE_RA_MSG3:
  85. ret =sp_ra_proc_msg3_req((const sample_ra_msg3_t*)((uint8_t*)p_req +
  86. sizeof(ra_samp_request_header_t)),
  87. p_req->size,
  88. &p_resp_msg);
  89. if(0 != ret)
  90. {
  91. fprintf(stderr, "\nError, call sp_ra_proc_msg3_req fail [%s].",
  92. __FUNCTION__);
  93. }
  94. else
  95. {
  96. *p_resp = p_resp_msg;
  97. }
  98. break;
  99. default:
  100. ret = -1;
  101. fprintf(stderr, "\nError, unknown ra message type. Type = %d [%s].",
  102. p_req->type, __FUNCTION__);
  103. break;
  104. }
  105. return ret;
  106. }
  107. // Used to free the response messages. In the sample code, the
  108. // response messages are allocated by the SP code.
  109. //
  110. //
  111. // @param resp Pointer to the response buffer to be freed.
  112. void ra_free_network_response_buffer(ra_samp_response_header_t *resp)
  113. {
  114. if(resp!=NULL)
  115. {
  116. free(resp);
  117. }
  118. }