1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 
5 #ifndef KV_ADAPT_H
6 #define KV_ADAPT_H
7 
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11 
12 /**
13  * @brief Read KV flash partition
14  *
15  * @param[in]   offset the offset of the partition
16  * @param[out]  buf    the store buffer for read
17  * @param[in]   nbytes the length to read
18  *
19  * @return 0 on success, otherwise will be failed
20  */
21 int32_t kv_flash_read(uint32_t offset, void *buf, uint32_t nbytes);
22 
23 /**
24  * @brief Write KV flash partition
25  *
26  * @param[in]   offset the offset of the partition
27  * @param[in]   buf    the store buffer for write
28  * @param[in]   nbytes the length to write
29  *
30  * @return 0 on success, otherwise will be failed
31  */
32 int32_t kv_flash_write(uint32_t offset, void *buf, uint32_t nbytes);
33 
34 /**
35  * @brief Erase KV flash partition
36  *
37  * @param[in]  offset the offset of the partition
38  * @param[in]  size   the length to erase
39  *
40  * @return 0 on success, otherwise will be failed
41  */
42 int32_t kv_flash_erase(uint32_t offset, uint32_t size);
43 
44 /**
45  * @brief Create OS lock
46  *
47  * @return lock pointer for success, NULL is failed
48  */
49 void *kv_lock_create(void);
50 
51 /**
52  * @breif Free OS lock
53  *
54  * @param[in]  lock pointer to the os lock
55  *
56  * @return 0 on success, otherwise is failed
57  *
58  */
59 int32_t kv_lock_free(void *lock);
60 
61 /**
62  * @brief Lock the lock
63  *
64  * @param[in]  lock   pointer to the os lock
65  *
66  * @return 0 on success, otherwise is failed
67  *
68  */
69 int32_t kv_lock(void *lock);
70 
71 /**
72  * @brief Unlock the lock
73  *
74  * @param[in]  lock pointer to the os lock
75  *
76  * @return 0 on success, otherwise is failed
77  *
78  */
79 int32_t kv_unlock(void *lock);
80 
81 /**
82  * @brief Create OS semaphore
83  *
84  * @return semaphore pointer for success, NULL is failed
85  *
86  */
87 void *kv_sem_create(void);
88 
89 /**
90  * @brief Delete OS semaphore
91  *
92  * @param[in]  sem pointer to the os semaphore
93  *
94  * @return 0 on success, otherwise is failed
95  *
96  */
97 int32_t kv_sem_free(void *sem);
98 
99 /**
100  * @brief Take a semaphore
101  *
102  * @param[in]  sem     pointer to the semaphore
103  *
104  * @return 0 on success, otherwise is failed
105  *
106  */
107 int32_t kv_sem_wait(void *sem);
108 
109 /**
110  * @brief Give a semaphore and wakeup all waiting task
111  *
112  * @param[in]  sem pointer to the semaphore
113  *
114  * @return 0 on success, otherwise is failed
115  *
116  */
117 int32_t kv_sem_post_all(void *sem);
118 
119 /**
120  * @brief Create a task
121  *
122  * @param[in] name name of the task
123  * @param[in] fn   the entry function of task
124  * @param[in] arg  the parameter of task entry function
125  * @param[in] stack the size of task stack
126  *
127  * @return 0 on success, otherwise is failed
128  */
129 int32_t kv_start_task(const char *name, void (*fn)(void *), void *arg,
130                       uint32_t stack);
131 
132 /**
133  * @brief Delete a task
134  *
135  * @return none
136  */
137 void kv_delete_task(void);
138 
139 /**
140  * @brief wrapper of MM allocation
141  *
142  * @param[in]  size size of the mem to alloc
143  *
144  * @return NULL is error, other is memory address
145  *
146  */
147 void *kv_malloc(uint32_t size);
148 
149 /**
150  * @brief wrapper of MM free
151  *
152  * @param[in] ptr address point of the mem
153  *
154  */
155 void kv_free(void *ptr);
156 
157 /**
158  * @brief Encrypt data
159  *
160  * @param[in]  input     the pointer to the input data
161  * @param[out] output    the pointer to the encryption result
162  * @param[in]  input_len the length of the input data
163  *
164  * @return 0 on success, otherwise is failed
165  */
166 int32_t kv_secure_encrypt(uint8_t *input, uint8_t *output, uint32_t input_len);
167 
168 /**
169  * @brief Decrypt data
170  *
171  * @param[in]  input     the pointer to the input data
172  * @param[out] output    the pointer to the decryption result
173  * @param[in]  input_len the length of the input data
174  *
175  * @return 0 on success, otherwise is failed
176  */
177 int32_t kv_secure_decrypt(uint8_t *input, uint8_t *output, uint32_t input_len);
178 
179 /**
180  * @brief Get the key for encrypt/decrypt algorithm
181  *
182  * @param[in] key_len the length of the key
183  *
184  * @return key buffer pointer, NULL is error
185  */
186 uint8_t *kv_secure_get_key(uint32_t key_len);
187 
188 /**
189  * @brief Get the iv for encrypt/decrypt algorithm
190  *
191  * @param[in] iv_len the length of the iv
192  *
193  * @return iv buffer pointer, NULL is error
194  */
195 uint8_t *kv_secure_get_iv(uint32_t iv_len);
196 
197 #ifdef __cplusplus
198 }
199 #endif
200 
201 #endif  /* KV_ADAPT_H */
202 
203