1 /*
2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License. * Description: KV NOTIFY MODULE -- UTILS.
14 */
15
16 #include "nv_notify.h"
17 #include "nv_storage.h"
18 #include "nv_porting.h"
19
20 #if (CONFIG_NV_SUPPORT_CHANGE_NOTIFY == NV_YES)
21
nv_change_notify_segment_is_valid(nv_direct_ctrl_t * nv_ctrl,uint16_t min_key,uint16_t max_key)22 bool nv_change_notify_segment_is_valid(nv_direct_ctrl_t *nv_ctrl, uint16_t min_key, uint16_t max_key)
23 {
24 nv_changed_proc_t *notify_list = nv_ctrl->nv_change_notify_list;
25 for (uint8_t i = 0; i < nv_ctrl->notify_registered_nums; i++) {
26 if ((min_key < notify_list[i].min_key && max_key < notify_list[i].min_key) ||
27 (min_key > notify_list[i].max_key && max_key > notify_list[i].max_key)) {
28 continue;
29 }
30 return false;
31 }
32 return true;
33 }
34
nv_direct_notify_list_init(void)35 errcode_t nv_direct_notify_list_init(void)
36 {
37 nv_direct_ctrl_t *nv_ctrl = nv_direct_get_nv_ctrl();
38 nv_ctrl->notify_regitser_max_nums = MCORE_REGISTER_NV_NOTIFY_MAX_NUM;
39 nv_ctrl->notify_registered_nums = 0;
40 uint8_t notify_func_nums = nv_ctrl->notify_regitser_max_nums;
41 nv_changed_proc_t **notify_list = &nv_ctrl->nv_change_notify_list;
42 if (*notify_list != NULL) {
43 kv_free(*notify_list);
44 *notify_list = NULL;
45 }
46
47 *notify_list = (nv_changed_proc_t *)kv_malloc(notify_func_nums * sizeof(nv_changed_proc_t));
48 if (*notify_list == NULL) {
49 return ERRCODE_MALLOC;
50 }
51 return ERRCODE_SUCC;
52 }
53
54
nv_change_notify(uint16_t key)55 void nv_change_notify(uint16_t key)
56 {
57 nv_direct_ctrl_t *nv_ctrl = nv_direct_get_nv_ctrl();
58 nv_changed_proc_t *notify_list = nv_ctrl->nv_change_notify_list;
59 if (notify_list == NULL) {
60 return;
61 }
62
63 for (uint8_t index = 0; index < nv_ctrl->notify_registered_nums; index++) {
64 if ((key < notify_list[index].min_key) || (key > notify_list[index].max_key)) {
65 continue;
66 }
67
68 /* There may have the same range, this would cause multiple calls to the functions. */
69 if (notify_list[index].func != NULL) {
70 notify_list[index].func(key);
71 }
72 }
73 }
74 #endif /* #if (CONFIG_NV_SUPPORT_CHANGE_NOTIFY == NV_YES) */
75