blob: 88423c3fd6f58b290442cdbd503f1ca1ae0c12b4 [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>
10
hualing chena540a7e2020-03-27 16:44:05 +080011/****************************************************************************
12 * Macro definitions
13 ***************************************************************************/
14
15/****************************************************************************
16 * Static functions
17 ***************************************************************************/
18int (*Write_Sysfs_ptr)(const char *path, char *value);
19int (*ReadNum_Sysfs_ptr)(const char *path, char *value, int size);
20int (*Read_Sysfs_ptr)(const char *path, char *value);
21
22typedef struct dvr_rw_sysfs_cb_s
Pengfei Liub4734232020-01-17 18:25:10 +080023{
hualing chena540a7e2020-03-27 16:44:05 +080024 DVR_Read_Sysfs_Cb readSysfsCb;
25 DVR_Write_Sysfs_Cb writeSysfsCb;
26}dvr_rw_sysfs_cb_t;
Pengfei Liub4734232020-01-17 18:25:10 +080027
hualing chena540a7e2020-03-27 16:44:05 +080028dvr_rw_sysfs_cb_t rwSysfsCb = {.readSysfsCb = NULL, .writeSysfsCb = NULL};
Pengfei Liub4734232020-01-17 18:25:10 +080029
hualing chena540a7e2020-03-27 16:44:05 +080030typedef struct dvr_rw_prop_cb_s
31{
32 DVR_Read_Prop_Cb readPropCb;
33 DVR_Write_Prop_Cb writePropCb;
34}dvr_rw_prop_cb_t;
Pengfei Liub4734232020-01-17 18:25:10 +080035
hualing chena540a7e2020-03-27 16:44:05 +080036dvr_rw_prop_cb_t rwPropCb = {.readPropCb = NULL, .writePropCb = NULL};
37
38
39/****************************************************************************
40 * API functions
41 ***************************************************************************/
42
43/**\brief regist rw sysfs cb
44 * \param[in] fun callback
45 * \return
46 * - DVR_SUCCESS
47 * - error
48 */
49int dvr_register_rw_sys(DVR_Read_Sysfs_Cb RCb, DVR_Write_Sysfs_Cb WCb)
50{
51
52 if (RCb == NULL || WCb == NULL) {
53 DVR_DEBUG(1, "dvr_register_rw_sys error param is NULL !!");
54 return DVR_FAILURE;
55 }
56 if (!rwSysfsCb.readSysfsCb)
57 rwSysfsCb.readSysfsCb = RCb;
58 if (!rwSysfsCb.writeSysfsCb)
59 rwSysfsCb.writeSysfsCb = WCb;
60 DVR_DEBUG(1, "dvr_register_rw_sys success !!");
61 return DVR_SUCCESS;
Pengfei Liub4734232020-01-17 18:25:10 +080062}
hualing chena540a7e2020-03-27 16:44:05 +080063
64/**\brief unregist rw sys cb
65 */
66int dvr_unregister_rw_sys()
67{
68 if (rwSysfsCb.readSysfsCb)
69 rwSysfsCb.readSysfsCb = NULL;
70 if (rwSysfsCb.writeSysfsCb)
71 rwSysfsCb.writeSysfsCb = NULL;
72 return DVR_SUCCESS;
73}
74
75/**\brief regist rw prop cb
76 * \param[in] fun callback
77 * \return
78 * - DVR_SUCCESS
79 * - error
80 */
81
82int dvr_rgister_rw_prop(DVR_Read_Prop_Cb RCb, DVR_Write_Prop_Cb WCb)
83{
84 if (RCb == NULL || WCb == NULL) {
85 DVR_DEBUG(1, "dvr_rgister_rw_prop error param is NULL !!");
86 return DVR_FAILURE;
87 }
88
89 if (!rwPropCb.readPropCb)
90 rwPropCb.readPropCb = RCb;
91 if (!rwPropCb.writePropCb)
92 rwPropCb.writePropCb = WCb;
93
94 DVR_DEBUG(1, "dvr_rgister_rw_prop !!");
95 return DVR_SUCCESS;
96}
97
98/**\brief unregist rw prop cb */
99int dvr_unregister_rw_prop()
100{
101 if (rwPropCb.readPropCb)
102 rwPropCb.readPropCb = NULL;
103 if (rwPropCb.writePropCb)
104 rwPropCb.writePropCb = NULL;
105 return DVR_SUCCESS;
106}
107
108/**\brief Write a string cmd to a file
109 * \param[in] name, File name
110 * \param[in] cmd, String command
111 * \return DVR_SUCCESS On success
112 * \return Error code On failure
113 */
114
115int dvr_file_echo(const char *name, const char *cmd)
116{
117 int fd, len, ret;
118 if (name == NULL || cmd == NULL) {
119 return DVR_FAILURE;
120 }
121
122 if (rwSysfsCb.writeSysfsCb)
123 {
124 rwSysfsCb.writeSysfsCb(name, cmd);
125 return DVR_SUCCESS;
126 }
127
128 fd = open(name, O_WRONLY);
129 if (fd == -1)
130 {
131 DVR_DEBUG(1, "cannot open file \"%s\"", name);
132 return DVR_FAILURE;
133 }
134
135 len = strlen(cmd);
136
137 ret = write(fd, cmd, len);
138 if (ret != len)
139 {
140 DVR_DEBUG(1, "write failed file:\"%s\" cmd:\"%s\" error:\"%s\"", name, cmd, strerror(errno));
141 close(fd);
142 return DVR_FAILURE;
143 }
144
145 close(fd);
146 return DVR_SUCCESS;
147}
148
149/**\brief read sysfs file
150 * \param[in] name, File name
151 * \param[out] buf, store sysfs node value
152 * \return DVR_SUCCESS On success
153 * \return Error code On failure
154 */
155
156int dvr_file_read(const char *name, char *buf, int len)
157{
158 FILE *fp;
159 char *ret;
160
161 if (name == NULL || buf == NULL) {
162 DVR_DEBUG(1, "dvr_file_read error param is NULL");
163 return DVR_FAILURE;
164 }
165
166 if (rwSysfsCb.readSysfsCb)
167 {
168 rwSysfsCb.readSysfsCb(name, buf, len);
169 return DVR_SUCCESS;
170 }
171
172
173 fp = fopen(name, "r");
174 if (!fp)
175 {
176 DVR_DEBUG(1, "cannot open file \"%s\"", name);
177 return DVR_FAILURE;
178 }
179
180 ret = fgets(buf, len, fp);
181 if (!ret)
182 {
183 DVR_DEBUG(1, "read the file:\"%s\" error:\"%s\" failed", name, strerror(errno));
184 }
185
186 fclose(fp);
187 return ret ? DVR_SUCCESS : DVR_FAILURE;
188}
189
190
191/**\brief Write a string cmd to a prop
192 * \param[in] name, prop name
193 * \param[in] cmd, String command
194 * \return DVR_SUCCESS On success
195 * \return Error code On failure
196 */
197
198int dvr_prop_echo(const char *name, const char *cmd)
199{
200 if (name == NULL || cmd == NULL) {
201 DVR_DEBUG(1, "dvr_prop_echo: error param is NULL");
202 return DVR_FAILURE;
203 }
204
205 if (rwPropCb.writePropCb)
206 {
207 rwPropCb.writePropCb(name, cmd);
208 return DVR_SUCCESS;
209 }
210 DVR_DEBUG(1, "dvr_prop_echo: error writePropCb is NULL");
211 return DVR_FAILURE;
212}
213
214/**\brief read prop value
215 * \param[in] name, prop name
216 * \param[out] buf, store prop node value
217 * \return DVR_SUCCESS On success
218 * \return Error code On failure
219 */
220
221int dvr_prop_read(const char *name, char *buf, int len)
222{
223 if (name == NULL || buf == NULL) {
224 DVR_DEBUG(1, "dvr_prop_read: error param is NULL");
225 return DVR_FAILURE;
226 }
227
228 if (rwPropCb.readPropCb)
229 {
230 rwPropCb.readPropCb(name, buf, len);
231 return DVR_SUCCESS;
232 }
233 DVR_DEBUG(1, "dvr_prop_read: error readPropCb is NULL");
234 return DVR_FAILURE;
235}
236