blob: d0ccb3fd0cabe5b39e2df5c320ffcc962cd63d87 [file] [log] [blame]
Pengfei Liub4734232020-01-17 18:25:10 +08001#include <stdio.h>
2#include <unistd.h>
3#include <sys/types.h>
4#include <sys/stat.h>
5#include <fcntl.h>
6#include <string.h>
7#include <errno.h>
8#include <dvr_types.h>
9#include <dvr_utils.h>
Gong Ke2a0ebbe2021-05-25 15:22:50 +080010
11#ifdef __ANDROID_API__
hualing chena3f427d2021-03-24 15:30:00 +080012#include <cutils/properties.h>
Gong Ke2a0ebbe2021-05-25 15:22:50 +080013#endif
Pengfei Liub4734232020-01-17 18:25:10 +080014
hualing chena540a7e2020-03-27 16:44:05 +080015/****************************************************************************
16 * Macro definitions
17 ***************************************************************************/
18
19/****************************************************************************
20 * Static functions
21 ***************************************************************************/
22int (*Write_Sysfs_ptr)(const char *path, char *value);
23int (*ReadNum_Sysfs_ptr)(const char *path, char *value, int size);
24int (*Read_Sysfs_ptr)(const char *path, char *value);
25
26typedef struct dvr_rw_sysfs_cb_s
Pengfei Liub4734232020-01-17 18:25:10 +080027{
hualing chena540a7e2020-03-27 16:44:05 +080028 DVR_Read_Sysfs_Cb readSysfsCb;
29 DVR_Write_Sysfs_Cb writeSysfsCb;
30}dvr_rw_sysfs_cb_t;
Pengfei Liub4734232020-01-17 18:25:10 +080031
hualing chena540a7e2020-03-27 16:44:05 +080032dvr_rw_sysfs_cb_t rwSysfsCb = {.readSysfsCb = NULL, .writeSysfsCb = NULL};
Pengfei Liub4734232020-01-17 18:25:10 +080033
hualing chena540a7e2020-03-27 16:44:05 +080034typedef struct dvr_rw_prop_cb_s
35{
36 DVR_Read_Prop_Cb readPropCb;
37 DVR_Write_Prop_Cb writePropCb;
38}dvr_rw_prop_cb_t;
Pengfei Liub4734232020-01-17 18:25:10 +080039
hualing chena540a7e2020-03-27 16:44:05 +080040dvr_rw_prop_cb_t rwPropCb = {.readPropCb = NULL, .writePropCb = NULL};
41
hualing chena540a7e2020-03-27 16:44:05 +080042/****************************************************************************
43 * API functions
44 ***************************************************************************/
45
46/**\brief regist rw sysfs cb
47 * \param[in] fun callback
48 * \return
49 * - DVR_SUCCESS
50 * - error
51 */
52int dvr_register_rw_sys(DVR_Read_Sysfs_Cb RCb, DVR_Write_Sysfs_Cb WCb)
53{
54
55 if (RCb == NULL || WCb == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +080056 DVR_INFO("dvr_register_rw_sys error param is NULL !!");
hualing chena540a7e2020-03-27 16:44:05 +080057 return DVR_FAILURE;
58 }
59 if (!rwSysfsCb.readSysfsCb)
60 rwSysfsCb.readSysfsCb = RCb;
61 if (!rwSysfsCb.writeSysfsCb)
62 rwSysfsCb.writeSysfsCb = WCb;
Wentao MA96f68962022-06-15 19:45:35 +080063 DVR_INFO("dvr_register_rw_sys success !!");
hualing chena540a7e2020-03-27 16:44:05 +080064 return DVR_SUCCESS;
Pengfei Liub4734232020-01-17 18:25:10 +080065}
hualing chena540a7e2020-03-27 16:44:05 +080066
67/**\brief unregist rw sys cb
68 */
69int dvr_unregister_rw_sys()
70{
71 if (rwSysfsCb.readSysfsCb)
72 rwSysfsCb.readSysfsCb = NULL;
73 if (rwSysfsCb.writeSysfsCb)
74 rwSysfsCb.writeSysfsCb = NULL;
75 return DVR_SUCCESS;
76}
77
78/**\brief regist rw prop cb
79 * \param[in] fun callback
80 * \return
81 * - DVR_SUCCESS
82 * - error
83 */
84
85int dvr_rgister_rw_prop(DVR_Read_Prop_Cb RCb, DVR_Write_Prop_Cb WCb)
86{
87 if (RCb == NULL || WCb == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +080088 DVR_INFO("dvr_rgister_rw_prop error param is NULL !!");
hualing chena540a7e2020-03-27 16:44:05 +080089 return DVR_FAILURE;
90 }
91
92 if (!rwPropCb.readPropCb)
93 rwPropCb.readPropCb = RCb;
94 if (!rwPropCb.writePropCb)
95 rwPropCb.writePropCb = WCb;
96
Wentao MA96f68962022-06-15 19:45:35 +080097 DVR_INFO("dvr_rgister_rw_prop !!");
hualing chena540a7e2020-03-27 16:44:05 +080098 return DVR_SUCCESS;
99}
100
101/**\brief unregist rw prop cb */
102int dvr_unregister_rw_prop()
103{
104 if (rwPropCb.readPropCb)
105 rwPropCb.readPropCb = NULL;
106 if (rwPropCb.writePropCb)
107 rwPropCb.writePropCb = NULL;
108 return DVR_SUCCESS;
109}
110
111/**\brief Write a string cmd to a file
112 * \param[in] name, File name
113 * \param[in] cmd, String command
114 * \return DVR_SUCCESS On success
115 * \return Error code On failure
116 */
117
118int dvr_file_echo(const char *name, const char *cmd)
119{
120 int fd, len, ret;
121 if (name == NULL || cmd == NULL) {
122 return DVR_FAILURE;
123 }
124
125 if (rwSysfsCb.writeSysfsCb)
126 {
127 rwSysfsCb.writeSysfsCb(name, cmd);
128 return DVR_SUCCESS;
129 }
130
131 fd = open(name, O_WRONLY);
132 if (fd == -1)
133 {
Wentao MA96f68962022-06-15 19:45:35 +0800134 DVR_INFO("cannot open file \"%s\"", name);
hualing chena540a7e2020-03-27 16:44:05 +0800135 return DVR_FAILURE;
136 }
137
138 len = strlen(cmd);
139
140 ret = write(fd, cmd, len);
141 if (ret != len)
142 {
Wentao MA96f68962022-06-15 19:45:35 +0800143 DVR_INFO("write failed file:\"%s\" cmd:\"%s\" error:\"%s\"", name, cmd, strerror(errno));
hualing chena540a7e2020-03-27 16:44:05 +0800144 close(fd);
145 return DVR_FAILURE;
146 }
147
148 close(fd);
149 return DVR_SUCCESS;
150}
151
152/**\brief read sysfs file
153 * \param[in] name, File name
154 * \param[out] buf, store sysfs node value
155 * \return DVR_SUCCESS On success
156 * \return Error code On failure
157 */
158
159int dvr_file_read(const char *name, char *buf, int len)
160{
161 FILE *fp;
162 char *ret;
163
164 if (name == NULL || buf == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800165 DVR_INFO("dvr_file_read error param is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800166 return DVR_FAILURE;
167 }
168
169 if (rwSysfsCb.readSysfsCb)
170 {
171 rwSysfsCb.readSysfsCb(name, buf, len);
172 return DVR_SUCCESS;
173 }
174
175
176 fp = fopen(name, "r");
177 if (!fp)
178 {
Wentao MA96f68962022-06-15 19:45:35 +0800179 DVR_INFO("cannot open file \"%s\"", name);
hualing chena540a7e2020-03-27 16:44:05 +0800180 return DVR_FAILURE;
181 }
182
183 ret = fgets(buf, len, fp);
184 if (!ret)
185 {
Wentao MA96f68962022-06-15 19:45:35 +0800186 DVR_INFO("read the file:\"%s\" error:\"%s\" failed", name, strerror(errno));
hualing chena540a7e2020-03-27 16:44:05 +0800187 }
188
189 fclose(fp);
190 return ret ? DVR_SUCCESS : DVR_FAILURE;
191}
192
193
194/**\brief Write a string cmd to a prop
195 * \param[in] name, prop name
196 * \param[in] cmd, String command
197 * \return DVR_SUCCESS On success
198 * \return Error code On failure
199 */
200
201int dvr_prop_echo(const char *name, const char *cmd)
202{
203 if (name == NULL || cmd == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800204 DVR_ERROR("dvr_prop_echo: error param is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800205 return DVR_FAILURE;
206 }
207
208 if (rwPropCb.writePropCb)
209 {
210 rwPropCb.writePropCb(name, cmd);
211 return DVR_SUCCESS;
212 }
Gong Ke2a0ebbe2021-05-25 15:22:50 +0800213
214#ifdef __ANDROID_API__
hualing chena3f427d2021-03-24 15:30:00 +0800215 property_set(name, cmd);
Gong Ke2a0ebbe2021-05-25 15:22:50 +0800216#endif
Wentao MA96f68962022-06-15 19:45:35 +0800217 DVR_DEBUG("dvr_prop_echo: error writePropCb is NULL, used property_set");
hualing chena540a7e2020-03-27 16:44:05 +0800218 return DVR_FAILURE;
219}
220
221/**\brief read prop value
222 * \param[in] name, prop name
223 * \param[out] buf, store prop node value
224 * \return DVR_SUCCESS On success
225 * \return Error code On failure
226 */
227
228int dvr_prop_read(const char *name, char *buf, int len)
229{
230 if (name == NULL || buf == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800231 DVR_INFO("dvr_prop_read: error param is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800232 return DVR_FAILURE;
233 }
234
235 if (rwPropCb.readPropCb)
236 {
237 rwPropCb.readPropCb(name, buf, len);
238 return DVR_SUCCESS;
239 }
Gong Ke2a0ebbe2021-05-25 15:22:50 +0800240
241#ifdef __ANDROID_API__
hualing chena3f427d2021-03-24 15:30:00 +0800242 property_get(name, buf, "");
Gong Ke2a0ebbe2021-05-25 15:22:50 +0800243#endif
Wentao MA96f68962022-06-15 19:45:35 +0800244 DVR_INFO("dvr_prop_read: error readPropCb is NULL, used property_get");
hualing chena540a7e2020-03-27 16:44:05 +0800245 return DVR_FAILURE;
246}
247
Wentao MA907b6432022-08-01 06:23:08 +0000248#define NSEC_PER_SEC 1000000000L
249void clock_timespec_subtract(struct timespec *ts1, struct timespec *ts2, struct timespec *ts3)
250{
251 time_t sec;
252 long nsec;
253 sec = ts1->tv_sec - ts2->tv_sec;
254 nsec = ts1->tv_nsec - ts2->tv_nsec;
255 if (ts1->tv_sec >= 0 && ts1->tv_nsec >=0) {
256 if ((sec < 0 && nsec > 0) || (sec > 0 && nsec >= NSEC_PER_SEC)) {
257 nsec -= NSEC_PER_SEC;
258 sec++;
259 }
260 if (sec > 0 && nsec < 0) {
261 nsec += NSEC_PER_SEC;
262 sec--;
263 }
264 } else {
265 if (nsec <= -NSEC_PER_SEC || nsec >= NSEC_PER_SEC) {
266 nsec += NSEC_PER_SEC;
267 sec--;
268 }
269 if ((sec < 0 && nsec > 0)) {
270 nsec -= NSEC_PER_SEC;
271 sec++;
272 }
273 }
274 ts3->tv_sec = sec;
275 ts3->tv_nsec = nsec;
276}
277