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