network_ra.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (C) 2011-2016 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_MSG1:
  60. ret = sp_ra_proc_msg1_req((const sample_ra_msg1_t*)((uint8_t*)p_req
  61. + sizeof(ra_samp_request_header_t)),
  62. p_req->size,
  63. &p_resp_msg);
  64. if(0 != ret)
  65. {
  66. fprintf(stderr, "\nError, call sp_ra_proc_msg1_req fail [%s].",
  67. __FUNCTION__);
  68. }
  69. else
  70. {
  71. *p_resp = p_resp_msg;
  72. }
  73. break;
  74. case TYPE_RA_MSG3:
  75. ret =sp_ra_proc_msg3_req((const sample_ra_msg3_t*)((uint8_t*)p_req +
  76. sizeof(ra_samp_request_header_t)),
  77. p_req->size,
  78. &p_resp_msg);
  79. if(0 != ret)
  80. {
  81. fprintf(stderr, "\nError, call sp_ra_proc_msg3_req fail [%s].",
  82. __FUNCTION__);
  83. }
  84. else
  85. {
  86. *p_resp = p_resp_msg;
  87. }
  88. break;
  89. default:
  90. ret = -1;
  91. fprintf(stderr, "\nError, unknown ra message type. Type = %d [%s].",
  92. p_req->type, __FUNCTION__);
  93. break;
  94. }
  95. return ret;
  96. }
  97. // Used to free the response messages. In the sample code, the
  98. // response messages are allocated by the SP code.
  99. //
  100. //
  101. // @param resp Pointer to the response buffer to be freed.
  102. void ra_free_network_response_buffer(ra_samp_response_header_t *resp)
  103. {
  104. if(resp!=NULL)
  105. {
  106. free(resp);
  107. }
  108. }