Remove unused directories and files in video_in_hal
[staging/basesystem.git] / hal / nv_hal / src / nv_hal.cpp
1 /*
2  * @copyright Copyright(c) 2018-2020 TOYOTA MOTOR CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "nv_hal.h"
17 #include <errno.h>
18 #include <fcntl.h>
19 #include <aglpath.h>
20 #include <sys/syscall.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25
26 #include "nv_hal_nvhallog.h"
27
28 /**
29  * Media type difine.
30  */
31 const char *kMediaType[NVHALMEDIA_MAX] = {
32   "/tmp/bkup/",
33   "/ramd/BS/ns/backup_manager/rwdata/",
34   "/nv/BS/ns/backup_manager/rwdata/",
35 };
36
37 const int kMaxPath = 127;    // Max length of path
38
39 /**
40  *  Initialize Nv hal
41  */
42 EFrameworkunifiedStatus InitNv(void) {
43   return eFrameworkunifiedStatusOK;
44 }
45
46 /**
47  * Get data size.
48  */
49 EFrameworkunifiedStatus GetSizeNv(enum NvHalMedia media, const char *filename, uint32_t *size) {
50   if ((media < NVHALMEDIA_CACHEDRAM) || (media > NVHALMEDIA_NAND)) {  // Argument range checking
51     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "invalid media type:%d\n", media);
52     return eFrameworkunifiedStatusInvldParam;
53   }
54   if (NULL == filename) {  // NULL checking of arguments
55     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "filename is NULL.\n");
56     return eFrameworkunifiedStatusInvldParam;
57   }
58   if (NULL == size) {  // NULL checking of arguments
59     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "size is NULL.\n");
60     return eFrameworkunifiedStatusInvldParam;
61   }
62   char path[kMaxPath];
63   memset(path, '\0', kMaxPath);
64   snprintf(path, kMaxPath, "%s%s", kMediaType[media], filename);
65   // Geting the file size
66   struct stat file_stat;
67   if (0 > lstat(path, &file_stat)) {
68     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "lstat() error, errno=%d\n", errno);
69     return eFrameworkunifiedStatusFileLoadError;
70   }
71   *size = file_stat.st_size;
72   return eFrameworkunifiedStatusOK;
73 }
74
75
76
77 /**
78  * Reading data from memory device.
79  */
80 EFrameworkunifiedStatus ReadNv(enum NvHalMedia media, const char *filename, uint8_t *buffer, uint32_t size) {
81   if ((media < NVHALMEDIA_CACHEDRAM) || (media > NVHALMEDIA_NAND)) {
82     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "invalid media type:%d\n", media);
83     return eFrameworkunifiedStatusInvldParam;
84   }
85   if (NULL == filename) {
86     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "filename is NULL.\n");
87     return eFrameworkunifiedStatusInvldParam;
88   }
89   if (NULL == buffer) {
90     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "buffer is NULL.\n");
91     return eFrameworkunifiedStatusInvldParam;
92   }
93   if (0 == size) {
94     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "invalid size:%d\n", size);
95     return eFrameworkunifiedStatusInvldParam;
96   }
97   char path[kMaxPath];
98   memset(path, '\0', kMaxPath);
99   snprintf(path, kMaxPath, "%s%s", kMediaType[media], filename);
100   struct stat file_stat;
101   if (0 > lstat(path, &file_stat)) {
102     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "lstat() error, errno=%d\n", errno);
103     return eFrameworkunifiedStatusFail;
104   }
105   if (file_stat.st_size != size) {
106     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "size error:%ld != %d\n", file_stat.st_size, size);
107     return eFrameworkunifiedStatusFail;
108   }
109   int fd = open(path, O_RDONLY);
110   if (-1 == fd) {
111     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "open(%s) error, errno=%d\n", path, errno);
112     return eFrameworkunifiedStatusFail;
113   } else {
114     size_t tot_read;
115     // Read data by support EINTR
116     for (tot_read = 0; tot_read < static_cast<size_t>(size);) {
117       ssize_t read_size = pread(fd, &buffer[tot_read], static_cast<size_t>(size) - tot_read, tot_read);
118       if (-1 == read_size) {
119         if (errno == EINTR) {
120           continue;
121         } else {
122           FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "pread() error, errno=%d\n", errno);
123           if (0 != close(fd)) {
124             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "close() error, errno=%d\n", errno);
125           }
126           return eFrameworkunifiedStatusFail;
127         }
128       }
129       tot_read += read_size;
130     }
131     if (0 != close(fd)) {
132       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "close() error, errno=%d\n", errno);
133       return eFrameworkunifiedStatusFail;
134     }
135   }
136   return eFrameworkunifiedStatusOK;
137 }
138
139 /**
140  * Writing data to memory device.
141  */
142 EFrameworkunifiedStatus WriteNv(enum NvHalMedia media, const char *filename, uint8_t *buffer, uint32_t size) {
143   if ((media < NVHALMEDIA_CACHEDRAM) || (media > NVHALMEDIA_NAND)) {
144     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "invalid media type:%d\n", media);
145     return eFrameworkunifiedStatusInvldParam;
146   }
147   if (NULL == filename) {
148     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "filename is NULL.\n");
149     return eFrameworkunifiedStatusInvldParam;
150   }
151   if (NULL == buffer) {
152     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "buffer is NULL.\n");
153     return eFrameworkunifiedStatusInvldParam;
154   }
155   if (0 == size) {
156     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "invalid size:%d\n", size);
157     return eFrameworkunifiedStatusInvldParam;
158   }
159   char path[kMaxPath];
160   memset(path, '\0', kMaxPath);
161   snprintf(path, kMaxPath, "%s%s", kMediaType[media], filename);
162   struct stat file_stat;
163   // Check file exists or not, mkdir first if file no exists.
164   if (0 > lstat(path, &file_stat)) {
165     char *dir_point = path;
166     char dir_buff[kMaxPath];
167     dir_point++;
168     while ((dir_point = strchr(dir_point, '/'))) {
169       memset(dir_buff, '\0', kMaxPath);
170       memcpy(dir_buff, path, dir_point - path);
171       if (0 > mkdir(dir_buff, 0770)) {
172         if (EEXIST != errno) {
173           FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "mkdir() error, errno=%d\n", errno);
174           return eFrameworkunifiedStatusFail;
175         } else {
176           dir_point++;
177           continue;
178         }
179       }
180       dir_point++;
181     }
182   }
183   int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0660);
184   if (-1 == fd) {
185     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "open(%s) error, errno=%d\n", path, errno);
186     return eFrameworkunifiedStatusFail;
187   } else {
188     size_t tot_written;
189     // Write data by support EINTR
190     for (tot_written = 0; tot_written < static_cast<size_t>(size);) {
191       ssize_t write_size = pwrite(fd, &buffer[tot_written], static_cast<size_t>(size) - tot_written, tot_written);
192       if (0 >= write_size) {
193         if (-1 == write_size && errno == EINTR) {
194           continue;
195         } else {
196           FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "pwrite() error, errno=%d\n", errno);
197           if (0 != close(fd)) {
198             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "close() error, errno=%d\n", errno);
199           }
200           return eFrameworkunifiedStatusFail;
201         }
202       }
203       tot_written += write_size;
204     }
205     if (0 != close(fd)) {
206       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "close() error, errno=%d\n", errno);
207       return eFrameworkunifiedStatusFail;
208     }
209   }
210   return eFrameworkunifiedStatusOK;
211 }
212
213 /**
214  * Delete data.
215  */
216 EFrameworkunifiedStatus DeleteNv(enum NvHalMedia media, const char *filename) {
217   if ((media < NVHALMEDIA_CACHEDRAM) || (media > NVHALMEDIA_NAND)) {
218     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "invalid media type:%d\n", media);
219     return eFrameworkunifiedStatusInvldParam;
220   }
221   if (NULL == filename) {
222     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "filename is NULL.\n");
223     return eFrameworkunifiedStatusInvldParam;
224   }
225
226   char path[kMaxPath];
227   memset(path, '\0', kMaxPath);
228   snprintf(path, kMaxPath, "%s%s", kMediaType[media], filename);
229   struct stat file_stat;
230
231   if (0 > lstat(path, &file_stat)) {
232     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "lstat() error, errno=%d\n", errno);
233     return eFrameworkunifiedStatusFail;
234   }
235
236   if (0 != unlink(path)) {
237     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "unlink() error, errno=%d\n", errno);
238     return eFrameworkunifiedStatusFail;
239   }
240
241   return eFrameworkunifiedStatusOK;
242 }