btstack_tlv_rtthread.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (C) 2017 BlueKitchen GmbH
  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. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
  21. * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. */
  31. #define BTSTACK_FILE__ "btstack_tlv_rtthread.c"
  32. #include "btstack_tlv.h"
  33. #include "btstack_tlv_rtthread.h"
  34. #include "btstack_debug.h"
  35. #include "btstack_util.h"
  36. #include "btstack_debug.h"
  37. #include <rtthread.h>
  38. #include "easyflash.h"
  39. #define EASYFLASH_KEY_SIZE 15
  40. void tag2key(uint32_t tag, char *key)
  41. {
  42. rt_snprintf(key, EASYFLASH_KEY_SIZE, "0x%08x", tag);
  43. }
  44. /**
  45. * Get Value for Tag
  46. * @param context
  47. * @param tag
  48. * @param buffer
  49. * @param buffer_size
  50. * @returns size of value
  51. */
  52. static int btstack_tlv_rtthread_get_tag(void * context, uint32_t tag, uint8_t * buffer, uint32_t buffer_size)
  53. {
  54. UNUSED(context);
  55. // tag: 0x12345678 - key: "0x12345678"
  56. char key[EASYFLASH_KEY_SIZE];
  57. tag2key(tag, key);
  58. uint32_t bytes_to_read = 0;
  59. ef_get_env_blob(key, NULL, 0, &bytes_to_read);
  60. if (bytes_to_read == 0) {
  61. log_error("get tag %s fail", key);
  62. return 0;
  63. }
  64. // return len if buffer = NULL
  65. if (!buffer)
  66. return bytes_to_read;
  67. // otherwise copy data into buffer
  68. bytes_to_read = btstack_min(bytes_to_read, buffer_size);
  69. ef_get_env_blob(key, buffer, bytes_to_read, NULL);
  70. log_info("get tag %s success, value: ", key);
  71. log_info_hexdump(buffer, bytes_to_read);
  72. return bytes_to_read;
  73. }
  74. /**
  75. * Store Tag
  76. * @param context
  77. * @param tag
  78. * @param data
  79. * @param data_size
  80. * @returns 0 on success
  81. */
  82. static int btstack_tlv_rtthread_store_tag(void * context, uint32_t tag, const uint8_t * data, uint32_t data_size)
  83. {
  84. UNUSED(context);
  85. // tag: 0x12345678 - key: "0x12345678"
  86. char key[EASYFLASH_KEY_SIZE];
  87. tag2key(tag, key);
  88. EfErrCode err = ef_set_env_blob(key, data, data_size);
  89. if (err != EF_NO_ERR) {
  90. log_error("store tag %s fail, err (%d)", key, err);
  91. return -1;
  92. }
  93. log_info("store tag %s success, value: ", key);
  94. log_info_hexdump(data, data_size);
  95. return 0;
  96. }
  97. /**
  98. * Delete Tag
  99. * @note it is not expected that delete operation fails, please use at least log_error in case of errors
  100. * @param context
  101. * @param tag
  102. */
  103. static void btstack_tlv_rtthread_delete_tag(void * context, uint32_t tag)
  104. {
  105. UNUSED(context);
  106. // tag: 0x12345678 - key: "0x12345678"
  107. char key[EASYFLASH_KEY_SIZE];
  108. tag2key(tag, key);
  109. EfErrCode err = ef_del_env(key);
  110. if (err != EF_NO_ERR) {
  111. log_error("delete tag %s fail, err (%d)", key, err);
  112. }
  113. log_info("delete tag %s success", key);
  114. }
  115. static const btstack_tlv_t btstack_tlv_rtthread = {
  116. /* int (*get_tag)(..); */ &btstack_tlv_rtthread_get_tag,
  117. /* int (*store_tag)(..); */ &btstack_tlv_rtthread_store_tag,
  118. /* void (*delete_tag)(v..); */ &btstack_tlv_rtthread_delete_tag,
  119. };
  120. /**
  121. * Init Tag Length Value Store
  122. */
  123. const btstack_tlv_t * btstack_tlv_rtthread_init_instance(btstack_tlv_rtthread_t * self){
  124. UNUSED(self);
  125. return &btstack_tlv_rtthread;
  126. }