property_manager.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright (c) 2019 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file property manager interface
  8. */
  9. #include <os_common_api.h>
  10. #include <string.h>
  11. #include <stdio.h>
  12. #include <errno.h>
  13. #include <stdlib.h>
  14. #include <sys/util.h>
  15. #include <property_inner.h>
  16. #define SYS_LOG_DOMAIN "property"
  17. #ifndef SYS_LOG_LEVEL
  18. #define SYS_LOG_LEVEL CONFIG_SYS_LOG_DEFAULT_LEVEL
  19. #endif
  20. int property_get(const char *key, char *value, int value_len)
  21. {
  22. int ret = 0;
  23. #ifdef CONFIG_PROPERTY_CACHE
  24. ret = property_cache_get(key, value, value_len);
  25. #else
  26. #ifdef CONFIG_NVRAM_CONFIG
  27. ret = nvram_config_get(key, value, value_len);
  28. if (ret < 0) {
  29. return -ENOENT;
  30. }
  31. #endif
  32. #endif
  33. return ret;
  34. }
  35. int property_set(const char *key, char *value, int value_len)
  36. {
  37. int ret = -ENOENT;
  38. #ifdef CONFIG_PROPERTY_CACHE
  39. ret = property_cache_set(key, value, value_len);
  40. #else
  41. #ifdef CONFIG_NVRAM_CONFIG
  42. ret = nvram_config_set(key, value, value_len);
  43. #endif
  44. #endif
  45. return ret;
  46. }
  47. int property_set_factory(const char *key, char *value, int value_len)
  48. {
  49. int ret = -ENOENT;
  50. #ifdef CONFIG_NVRAM_CONFIG
  51. ret = nvram_config_set_factory(key, value, value_len);
  52. #endif
  53. return ret;
  54. }
  55. int property_get_int(const char *key, int default_value)
  56. {
  57. uint32_t property = 0;
  58. char temp_data[16] = {0};
  59. if (property_get(key, temp_data, sizeof(temp_data)) > 0) {
  60. property = atoi(temp_data);
  61. } else {
  62. property = default_value;
  63. }
  64. return property;
  65. }
  66. int property_set_int(const char *key, int value)
  67. {
  68. int ret = 0;
  69. char temp_data[16] = {0};
  70. snprintf(temp_data, sizeof(temp_data), "%d", value);
  71. if (!property_set(key, temp_data, strlen(temp_data))) {
  72. SYS_LOG_ERR("key %s value %d\n", key, value);
  73. ret = -EACCES;
  74. }
  75. return ret;
  76. }
  77. int property_get_byte_array(const char *key, char *value, int value_len, const char *default_value)
  78. {
  79. char temp_data[16] = {0};
  80. int len = property_get(key, temp_data, sizeof(temp_data));
  81. if (len > 0) {
  82. if (len > (value_len*2)) {
  83. len = value_len*2;
  84. }
  85. hex2bin(temp_data, len, value, value_len);
  86. } else {
  87. memcpy(value, default_value, value_len);
  88. }
  89. return 0;
  90. }
  91. int property_set_byte_array(const char *key, char *value, int value_len)
  92. {
  93. int ret = 0;
  94. char temp_data[16] = {0};
  95. int off = 0;
  96. for (int i = 0 ; i < value_len; i++) {
  97. if (!off) {
  98. off += snprintf(&temp_data[off], sizeof(temp_data) - off, ",");
  99. }
  100. off += snprintf(&temp_data[off], sizeof(temp_data) - off, "%d", value[i]);
  101. }
  102. if (!property_set(key, temp_data, sizeof(temp_data))) {
  103. SYS_LOG_ERR("key %s value %s\n", key, value);
  104. ret = -EACCES;
  105. }
  106. return ret;
  107. }
  108. int property_get_int_array(const char *key, int *value, int value_len, const short *default_value)
  109. {
  110. char temp_data[16] = {0};
  111. char *str_begin = NULL;
  112. char *str_end = NULL;
  113. int index = 0;
  114. if (property_get(key, temp_data, sizeof(temp_data)) > 0) {
  115. str_begin = temp_data;
  116. while (!str_begin) {
  117. str_end = strstr(str_begin, ",");
  118. if (str_end) {
  119. *str_end = 0;
  120. }
  121. value[index++] = atoi(str_begin);
  122. str_begin = str_end + 1;
  123. }
  124. } else {
  125. memcpy(value, default_value, value_len);
  126. }
  127. return 0;
  128. }
  129. int property_set_int_array(const char *key, int *value, int value_len)
  130. {
  131. int ret = 0;
  132. char temp_data[16] = {0};
  133. int off = 0;
  134. for (int i = 0 ; i < value_len; i++) {
  135. if (!off) {
  136. off += snprintf(&temp_data[off], sizeof(temp_data) - off, ",");
  137. }
  138. off += snprintf(&temp_data[off], sizeof(temp_data) - off, "%d", value[i]);
  139. }
  140. if (!property_set(key, temp_data, sizeof(temp_data))) {
  141. SYS_LOG_ERR("key %s error\n", key);
  142. ret = -EACCES;
  143. }
  144. return ret;
  145. }
  146. int property_flush(const char *key)
  147. {
  148. #ifdef CONFIG_PROPERTY_CACHE
  149. property_cache_flush(key);
  150. #endif
  151. return 0;
  152. }
  153. int property_flush_req(const char *key)
  154. {
  155. #ifdef CONFIG_PROPERTY_CACHE
  156. property_cache_flush_req(key);
  157. #endif
  158. return 0;
  159. }
  160. int property_flush_req_deal(void)
  161. {
  162. #ifdef CONFIG_PROPERTY_CACHE
  163. property_cache_flush_req_deal();
  164. #endif
  165. return 0;
  166. }
  167. int property_manager_init(void)
  168. {
  169. #ifdef CONFIG_PROPERTY_CACHE
  170. property_cache_init();
  171. #endif
  172. return 0;
  173. }