1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 #include <fcntl.h>
5 #include <sys/types.h>
6 #include <unistd.h>
7 #include <stdio.h>
8 #include <sys/ioctl.h>
9 #include "k_api.h"
10 #include "kv_conf.h"
11 #include "kv_adapt.h"
12 #include "aos/hal/flash.h"
13 #include "vfsdev/flash_dev.h"
14 #if (CONFIG_U_FLASH_CORE > 0)
15 #include <aos/mtd.h>
16 #endif
17 
18 static int32_t g_kv_flash_fd = -1;
19 
kv_flash_fd_open(void)20 static int32_t kv_flash_fd_open(void)
21 {
22     int fd;
23     char dev_str[16] = {0};
24     if(g_kv_flash_fd >= 0){
25         return g_kv_flash_fd;
26     }
27 
28 #if (CONFIG_U_FLASH_CORE > 0)
29     snprintf(dev_str,16-1,"/dev/mtdblock%d",KV_PARTITION);
30 #else
31     snprintf(dev_str,16-1,"/dev/flash%d",KV_PARTITION);
32 #endif
33 
34     fd = open(dev_str, 0);
35     if(fd < 0){
36         return fd;
37     }
38     g_kv_flash_fd = fd;
39 
40     return g_kv_flash_fd;
41 }
42 
kv_flash_read(uint32_t offset,void * buf,uint32_t nbytes)43 int32_t kv_flash_read(uint32_t offset, void *buf, uint32_t nbytes)
44 {
45     int32_t ret;
46     int fd;
47     if(kv_flash_fd_open() < 0){
48         return -1;
49     }
50     fd = g_kv_flash_fd;
51     ret = lseek(fd, offset, SEEK_SET);
52     if(ret < 0){
53         return ret;
54     }
55     ret = read(fd, buf, nbytes);
56     if(ret > 0){
57        return 0;
58     }else{
59         return -1;
60     }
61 }
62 
kv_flash_write(uint32_t offset,void * buf,uint32_t nbytes)63 int32_t kv_flash_write(uint32_t offset, void *buf, uint32_t nbytes)
64 {
65     int32_t ret;
66     int fd;
67     if(kv_flash_fd_open() < 0){
68         return -1;
69     }
70     fd = g_kv_flash_fd;
71     ret = lseek(fd, offset, SEEK_SET);
72     if(ret < 0){
73         return ret;
74     }
75     ret = write(fd, buf, nbytes);
76     if(ret > 0){
77        return 0;
78     }else{
79         return -1;
80     }
81 }
82 
kv_flash_erase(uint32_t offset,uint32_t size)83 int32_t kv_flash_erase(uint32_t offset, uint32_t size)
84 {
85     int32_t ret;
86     int fd;
87 #if (CONFIG_U_FLASH_CORE > 0)
88     struct mtd_erase_info erase_arg;
89 #endif
90 
91     if(kv_flash_fd_open() < 0){
92         return -1;
93     }
94     fd = g_kv_flash_fd;
95     ret = lseek(fd, offset, SEEK_SET);
96     if(ret < 0){
97         return ret;
98     }
99 
100 #if (CONFIG_U_FLASH_CORE > 0)
101     erase_arg.offset = offset;
102     erase_arg.length = size;
103     ret = ioctl(fd, IOC_MTD_ERASE, &erase_arg);
104 #else
105     ret = ioctl(fd, IOC_FLASH_ERASE_FLASH, size);
106 #endif
107 
108     return ret;
109 }
110 
kv_lock_create(void)111 void *kv_lock_create(void)
112 {
113     int32_t   ret;
114     kmutex_t *m;
115 
116     m = krhino_mm_alloc(sizeof(kmutex_t));
117     if (m == NULL) {
118         return NULL;
119     }
120 
121     ret = krhino_mutex_create(m, "KV");
122     if (ret != RHINO_SUCCESS) {
123         krhino_mm_free(m);
124         return NULL;
125     }
126 
127     return (void *)m;
128 }
129 
kv_lock_free(void * lock)130 int32_t kv_lock_free(void *lock)
131 {
132     int32_t ret;
133 
134     kmutex_t *m = (kmutex_t *)lock;
135 
136     if (m == NULL) {
137         return -1;
138     }
139 
140     ret = krhino_mutex_del(m);
141     if (ret != RHINO_SUCCESS) {
142         return ret;
143     }
144 
145     krhino_mm_free(m);
146 
147     return ret;
148 }
149 
kv_lock(void * lock)150 int32_t kv_lock(void *lock)
151 {
152     return krhino_mutex_lock((kmutex_t *)lock, RHINO_WAIT_FOREVER);
153 }
154 
kv_unlock(void * lock)155 int32_t kv_unlock(void *lock)
156 {
157     return krhino_mutex_unlock((kmutex_t *)lock);
158 }
159 
kv_sem_create(void)160 void *kv_sem_create(void)
161 {
162     int32_t ret;
163     ksem_t *s;
164 
165     s = krhino_mm_alloc(sizeof(ksem_t));
166     if (s == NULL) {
167         return NULL;
168     }
169 
170     ret = krhino_sem_create(s, "KV", 0);
171     if (ret != RHINO_SUCCESS) {
172         krhino_mm_free(s);
173         return NULL;
174     }
175 
176     return (void *)s;
177 }
178 
kv_sem_free(void * sem)179 int32_t kv_sem_free(void *sem)
180 {
181     int32_t ret;
182 
183     ksem_t *m = (ksem_t *)sem;
184 
185     if (m == NULL) {
186         return -1;
187     }
188 
189     ret = krhino_sem_del(m);
190     if (ret != RHINO_SUCCESS) {
191         return ret;
192     }
193 
194     krhino_mm_free(m);
195 
196     return ret;
197 }
198 
kv_sem_wait(void * sem)199 int32_t kv_sem_wait(void *sem)
200 {
201     return krhino_sem_take((ksem_t *)sem, RHINO_WAIT_FOREVER);
202 }
203 
kv_sem_post_all(void * sem)204 int32_t kv_sem_post_all(void *sem)
205 {
206     return krhino_sem_give_all((ksem_t *)sem);
207 }
208 
kv_start_task(const char * name,void (* fn)(void *),void * arg,uint32_t stack)209 int32_t kv_start_task(const char *name, void (*fn)(void *), void *arg,
210                       uint32_t stack)
211 {
212     ktask_t *task_handle = NULL;
213     return krhino_task_dyn_create(&task_handle, name, arg, KV_TASK_PRIORITY, 0,
214                                   stack / sizeof(cpu_stack_t), fn, 1u);
215 }
216 
kv_delete_task(void)217 void kv_delete_task(void)
218 {
219     krhino_task_dyn_del(NULL);
220 }
221 
kv_malloc(uint32_t size)222 void *kv_malloc(uint32_t size)
223 {
224     return krhino_mm_alloc(size);
225 }
226 
kv_free(void * ptr)227 void kv_free(void *ptr)
228 {
229     krhino_mm_free(ptr);
230 }
231