yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame^] | 1 | #include "sd_httpshare.h" |
| 2 | #include "sqlite3.h" |
| 3 | |
| 4 | extern char usb_lun_path[]; |
| 5 | |
| 6 | int zte_del_file(char *path) |
| 7 | { |
| 8 | char cmd[MAX_CMD_LEN] = {0}; |
| 9 | |
| 10 | if (!path) { |
| 11 | slog(SDCARD_PRINT, SLOG_ERR, "[sdcard-hotplug]zte_del_file null\n"); |
| 12 | return 0; |
| 13 | } |
| 14 | |
| 15 | sprintf(cmd, "/bin/rm -rf '%s'", path); |
| 16 | zxic_system(cmd); |
| 17 | |
| 18 | if (access(path, F_OK) == 0) { |
| 19 | slog(SDCARD_PRINT, SLOG_ERR, "[sdcard-hotplug]delfile %s fail!\n", path); |
| 20 | } |
| 21 | return 1; |
| 22 | } |
| 23 | |
| 24 | int zte_umount_dev() |
| 25 | { |
| 26 | char cmd[MAX_CMD_LEN] = {0}; |
| 27 | char sd_path[20] = {0}; |
| 28 | |
| 29 | cfg_get_item ("cur_tcard_blk", sd_path, sizeof(sd_path)); |
| 30 | if (strlen(sd_path) != 0) { |
| 31 | sprintf(cmd, "/bin/umount %s", sd_path); |
| 32 | } |
| 33 | else { |
| 34 | sprintf(cmd, "/bin/umount %s", USB_DEV_SDCARD_PATH); |
| 35 | } |
| 36 | zxic_system(cmd); |
| 37 | |
| 38 | memset(cmd, 0, sizeof(cmd)); |
| 39 | sprintf(cmd, "/bin/umount %s", USB_DEV_SDCARD_PATH_BACK); |
| 40 | zxic_system(cmd); |
| 41 | |
| 42 | memset(cmd, 0, sizeof(cmd)); |
| 43 | sprintf(cmd, "/bin/umount %s", SD_CARD_PATH); |
| 44 | zxic_system(cmd); |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | int zte_check_sdcard_exist() |
| 49 | { |
| 50 | char cmd[MAX_CMD_LEN] = {0}; |
| 51 | int ret = 0; |
| 52 | FILE *fd = NULL; |
| 53 | |
| 54 | char size_sd[ZTE_HTTPSHARE_LEN_12] = {0}; |
| 55 | |
| 56 | if(access("/sys/kernel/debug/mmc1/present", R_OK) != 0) |
| 57 | { |
| 58 | slog(SDCARD_PRINT, SLOG_ERR, "[sdcard-hotplug] zte_check_sdcard_exist file not exist!\n"); |
| 59 | return ret; |
| 60 | } |
| 61 | |
| 62 | sprintf(cmd, "echo %s>%s", USB_DEV_SDCARD_PATH_BACK, TCARD_SIZE_FILE); |
| 63 | soft_system(cmd); |
| 64 | |
| 65 | //fd = popen("cat /proc/proc_sd/size","r"); |
| 66 | fd = popen("cat /sys/kernel/debug/mmc1/present", "r"); |
| 67 | |
| 68 | if (fd == NULL) { |
| 69 | slog(SDCARD_PRINT, SLOG_ERR, "[sdcard-hotplug]zte_check_sdcard_exist popen file = NULL\n"); |
| 70 | return ret; |
| 71 | } |
| 72 | if (!feof(fd) && fgets(size_sd, sizeof(size_sd), fd) != NULL) { |
| 73 | ret = atoi(size_sd); |
| 74 | } |
| 75 | |
| 76 | pclose(fd); |
| 77 | |
| 78 | slog(SDCARD_PRINT, SLOG_DEBUG, "[sdcard-hotplug]zte_check_sdcard_exist :get size data = %d\n", ret); |
| 79 | |
| 80 | return ret; |
| 81 | } |
| 82 | |
| 83 | // Get current mode from nv |
| 84 | zte_httpshare_current_mode_type zte_httpshare_get_current_mode() |
| 85 | { |
| 86 | char nv_item[ZTE_HTTPSHARE_DEFAULT_LEN] = {0}; |
| 87 | cfg_get_item("sdcard_mode_option", nv_item, sizeof(nv_item)); |
| 88 | slog(SDCARD_PRINT, SLOG_DEBUG, "[sdcard-hotplug]sdcard_mode_option:%s\n", nv_item); |
| 89 | |
| 90 | if (0 == strcmp("1", nv_item)) { |
| 91 | return ZTE_HTTPSHARE_HTTPSHARE; |
| 92 | } else { |
| 93 | return ZTE_HTTPSHARE_USB; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | int zte_httpshare_mount_sd() |
| 98 | { |
| 99 | char cmd[MAX_CMD_LEN] = {0}; |
| 100 | char sd_path[20] = {0}; |
| 101 | int i = 0; |
| 102 | |
| 103 | zte_umount_dev(); |
| 104 | |
| 105 | //ÂÖѯ·ÖÇø1-10 |
| 106 | for (i = 1; i <= 10; i++) { |
| 107 | memset(sd_path, 0, sizeof(sd_path)); |
| 108 | snprintf(sd_path, sizeof(sd_path), "/dev/mmcblk0p%d", i); |
| 109 | |
| 110 | memset(cmd, 0, sizeof(cmd)); |
| 111 | sprintf(cmd, "/bin/mount -t vfat %s %s", sd_path, SD_CARD_PATH); |
| 112 | if (0 == zxic_system(cmd)) { |
| 113 | cfg_set("cur_tcard_blk", sd_path); |
| 114 | sleep(2); |
| 115 | return 1; |
| 116 | } |
| 117 | slog(MISC_PRINT, SLOG_ERR,"[sdcard-hotplug]zte_httpshare_mount_sd %s error!\n", sd_path); |
| 118 | } |
| 119 | |
| 120 | memset(cmd, 0, sizeof(cmd)); |
| 121 | sprintf(cmd, "/bin/mount -t vfat %s %s", USB_DEV_SDCARD_PATH_BACK, SD_CARD_PATH); |
| 122 | if (0 != zxic_system(cmd)) { |
| 123 | slog(MISC_PRINT, SLOG_ERR,"[sdcard-hotplug]zte_httpshare_mount_sd %s error!\n", USB_DEV_SDCARD_PATH_BACK); |
| 124 | return 0; |
| 125 | } |
| 126 | sleep(2); |
| 127 | return 1; |
| 128 | } |
| 129 | |
| 130 | int zte_mount_httpshare() |
| 131 | { |
| 132 | char cmd[MAX_CMD_LEN] = {0}; |
| 133 | |
| 134 | sprintf(cmd, "echo NULL > %s", usb_lun_path); |
| 135 | //zte_httpshare_call_system("echo NULL > /sys/devices/platform/zx29_hsotg.0/gadget/lun1/file"); |
| 136 | soft_system(cmd); |
| 137 | if (-1 == access(SD_CARD_PATH, F_OK)) { |
| 138 | slog(SDCARD_PRINT, SLOG_ERR, "[sdcard-hotplug]zte_mount_httpshare mmc2 no exist!\n"); |
| 139 | |
| 140 | if(mkdir(SD_CARD_PATH, 0777) < 0) // cov M CHECKED_RETURN |
| 141 | { |
| 142 | slog(SDCARD_PRINT, SLOG_ERR, "mkdir fail, path:%s!\n", SD_CARD_PATH); |
| 143 | } |
| 144 | return zte_httpshare_mount_sd(); |
| 145 | } else { |
| 146 | //system("umount /mnt/jffs2/mmc2"); |
| 147 | slog(SDCARD_PRINT, SLOG_DEBUG, "[sdcard-hotplug]zte_mount_httpshare %s exist\n", SD_CARD_PATH); |
| 148 | return zte_httpshare_mount_sd(); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | void zte_mount_usb() |
| 153 | { |
| 154 | char cmd[MAX_CMD_LEN] = {0}; |
| 155 | |
| 156 | sprintf(cmd, "echo %s > %s", USB_DEV_SDCARD_PATH_BACK, usb_lun_path); |
| 157 | soft_system(cmd); |
| 158 | //zte_httpshare_call_system("echo dev/mmcblk0 > /sys/devices/platform/zx29_hsotg.0/gadget/lun1/file"); |
| 159 | zte_umount_dev(); |
| 160 | } |
| 161 | |
| 162 | int zte_httpshare_change_current_mode(zte_httpshare_current_mode_type mode) |
| 163 | { |
| 164 | char cmd[ZTE_HTTPSHARE_DEFAULT_LEN] = {0}; |
| 165 | zte_umount_dev(); |
| 166 | if (ZTE_HTTPSHARE_HTTPSHARE == mode) { |
| 167 | if (!zte_mount_httpshare()) { /*¹ÒÔØsd¿¨*/ |
| 168 | /*¹ÒÔØsdcardʧ°Ü*/ |
| 169 | slog(SDCARD_PRINT, SLOG_ERR, "[sdcard-hotplug]change to %d mode faile\n", mode); |
| 170 | cfg_set("sdcard_mode_option", "0"); |
| 171 | |
| 172 | sprintf(cmd, "echo %s > %s", USB_DEV_SDCARD_PATH_BACK, usb_lun_path); |
| 173 | soft_system(cmd); |
| 174 | //zte_httpshare_call_system("echo dev/mmcblk0 > /sys/devices/platform/zx29_hsotg.0/gadget/lun1/file"); |
| 175 | return ZTE_CHANGE_MODE_ERROR; |
| 176 | } |
| 177 | slog(SDCARD_PRINT, SLOG_NORMAL, "[sdcard-hotplug]change to %d mode suc.\n", mode); |
| 178 | cfg_set("sdcard_mode_option", "1"); |
| 179 | } else { |
| 180 | zte_mount_usb(); |
| 181 | cfg_set("sdcard_mode_option", "0"); |
| 182 | } |
| 183 | return ZTE_CHANGE_MODE_OK; |
| 184 | |
| 185 | } |
| 186 | |
| 187 | int zte_init_sdcard_path() |
| 188 | { |
| 189 | if (-1 == access(SD_CARD_PATH_PR, F_OK)) { |
| 190 | slog(SDCARD_PRINT, SLOG_ERR, "zte_init_sdcard_path:%s does not exist, create it.\n", SD_CARD_PATH_PR); |
| 191 | |
| 192 | if (-1 == mkdir(SD_CARD_PATH_PR, 0777)) { /*lint !e1055*/ |
| 193 | slog(SDCARD_PRINT, SLOG_ERR, "zte_init_sdcard_path:failed to create %s.\n", SD_CARD_PATH_PR); |
| 194 | return ZTE_HTTPSHARE_FAILURE; |
| 195 | } |
| 196 | } |
| 197 | return ZTE_HTTPSHARE_SUCCESS; |
| 198 | } |
| 199 | |
| 200 | int zte_init_sdcard_mode() |
| 201 | { |
| 202 | if (zte_check_sdcard_exist() > 0) { |
| 203 | slog(SDCARD_PRINT, SLOG_DEBUG, "[sdcard-hotplug]sd ok!\n"); |
| 204 | |
| 205 | cfg_set("sd_card_state", "1"); |
| 206 | |
| 207 | if (ZTE_HTTPSHARE_USB == zte_httpshare_get_current_mode()) { |
| 208 | return zte_httpshare_change_current_mode(ZTE_HTTPSHARE_USB); |
| 209 | } else { |
| 210 | return zte_httpshare_change_current_mode(ZTE_HTTPSHARE_HTTPSHARE); |
| 211 | } |
| 212 | } else { |
| 213 | slog(SDCARD_PRINT, SLOG_DEBUG, "[sdcard-hotplug]no sd!\n"); |
| 214 | cfg_set("sd_card_state", "0"); |
| 215 | zte_del_file(SD_CARD_PATH); |
| 216 | return ZTE_CHANGE_MODE_ERROR; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | zte_httpshare_return_e_type zte_httpshare_check_and_creat_dir(char *path) |
| 221 | { |
| 222 | if (!path) { |
| 223 | slog(SDCARD_PRINT, SLOG_ERR, "[sdcard-hotplug]zte_httpshare_check_and_creat_dir: check dir path null.\n"); |
| 224 | return ZTE_HTTPSHARE_FAILURE; |
| 225 | } |
| 226 | |
| 227 | if (-1 == access(path, F_OK)) { |
| 228 | slog(SDCARD_PRINT, SLOG_ERR, "[sdcard-hotplug]zte_httpshare_check_and_creat_dir:%s does not exist,socreate it.\n", ZTE_HTTPSHARE_DB_DIR); |
| 229 | |
| 230 | if (-1 == mkdir(path, 0777)) { /*lint !e1055*/ |
| 231 | slog(SDCARD_PRINT, SLOG_ERR, "[sdcard-hotplug]zte_httpshare_check_and_creat_dir:failed to create db dir.\n"); |
| 232 | return ZTE_HTTPSHARE_FAILURE; |
| 233 | } |
| 234 | } |
| 235 | return ZTE_HTTPSHARE_SUCCESS; |
| 236 | } |
| 237 | |
| 238 | zte_httpshare_db_result_e_type zte_httpshare_db_open(sqlite3**db) |
| 239 | { |
| 240 | sqlite3* tmp_db; |
| 241 | int rc = 0; |
| 242 | |
| 243 | if (NULL == db) { |
| 244 | slog(SDCARD_PRINT, SLOG_ERR, "[sdcard-hotplug]zte_httpshare_db_open:invalide inputs.\n"); |
| 245 | return ZTE_HTTPSHARE_DB_ERROR_INVAILD_PTR; |
| 246 | } |
| 247 | |
| 248 | rc = sqlite3_open(ZTE_HTTPSHARE_DB_PATH, &tmp_db); |
| 249 | if (rc) { |
| 250 | slog(SDCARD_PRINT, SLOG_ERR, "[sdcard-hotplug]zte_httpshare_db_open:can not open db,sqlite3_errmsg:%s\n", sqlite3_errmsg(tmp_db)); |
| 251 | (void)sqlite3_close(tmp_db); |
| 252 | return ZTE_HTTPSHARE_DB_ERROR_NOT_OPEN_DB; |
| 253 | } |
| 254 | *db = tmp_db; |
| 255 | return ZTE_HTTPSHARE_DB_OK; |
| 256 | } |
| 257 | |
| 258 | zte_httpshare_db_result_e_type zte_httpshare_db_close(sqlite3*db) |
| 259 | { |
| 260 | int rc = 0; |
| 261 | |
| 262 | if (NULL == db) { |
| 263 | slog(SDCARD_PRINT, SLOG_ERR, "[sdcard-hotplug]zte_httpshare_db_close:invalide inputs.\n"); |
| 264 | return ZTE_HTTPSHARE_DB_ERROR_INVAILD_PTR; |
| 265 | } |
| 266 | rc = sqlite3_close(db); |
| 267 | if (rc) { |
| 268 | slog(SDCARD_PRINT, SLOG_ERR, "[sdcard-hotplug]zte_httpshare_db_close:can not close db.\n"); |
| 269 | return ZTE_HTTPSHARE_DB_ERROR; |
| 270 | } |
| 271 | return ZTE_HTTPSHARE_DB_OK; |
| 272 | } |
| 273 | |
| 274 | zte_httpshare_db_result_e_type zte_httpshare_db_exec_sql(const char *sql, sqlite3_callback callback, void *fvarg) |
| 275 | { |
| 276 | sqlite3* db; |
| 277 | int rc = 0; |
| 278 | |
| 279 | if (NULL == sql) { |
| 280 | slog(SDCARD_PRINT, SLOG_ERR, "[sdcard-hotplug]zte_httpshare_db_exec_sql:invalide inputs.\n"); |
| 281 | return ZTE_HTTPSHARE_DB_ERROR_INVAILD_PTR; |
| 282 | } |
| 283 | if (0 != zte_httpshare_db_open(&db)) { |
| 284 | slog(SDCARD_PRINT, SLOG_ERR, "[sdcard-hotplug]zte_httpshare_db_exec_sql:open httpshare.db failed.\n"); |
| 285 | return ZTE_HTTPSHARE_DB_ERROR_NOT_OPEN_DB; |
| 286 | } |
| 287 | rc = sqlite3_exec(db, sql, callback, fvarg, NULL); |
| 288 | |
| 289 | slog(SDCARD_PRINT, SLOG_ERR, "[sdcard-hotplug]zte_httpshare_db_exec_sql:%s rc=%d\n", sql, rc); |
| 290 | if (rc) { |
| 291 | slog(SDCARD_PRINT, SLOG_ERR, "[sdcard-hotplug]zte_httpshare_db_exec_sql:can not exec sql,sqlite3_errmsg:%s.\n", sqlite3_errmsg(db)); |
| 292 | (void)sqlite3_close(db); |
| 293 | return ZTE_HTTPSHARE_DB_ERROR; |
| 294 | } |
| 295 | (void)zte_httpshare_db_close(db); |
| 296 | |
| 297 | return ZTE_HTTPSHARE_DB_OK; |
| 298 | } |
| 299 | |
| 300 | zte_httpshare_db_result_e_type zte_httpshare_create_table() |
| 301 | { |
| 302 | zte_httpshare_db_result_e_type result = ZTE_HTTPSHARE_DB_OK; |
| 303 | |
| 304 | //create httpshare table |
| 305 | result = zte_httpshare_db_exec_sql(ZTE_CREATE_TABLE_HTTPSHARE_SQL, NULL, NULL); |
| 306 | |
| 307 | if (ZTE_HTTPSHARE_DB_OK != result) { |
| 308 | slog(SDCARD_PRINT, SLOG_ERR, "[sdcard-hotplug]zte_httpshare_create_table:create httpshare table result is %d\n", result); |
| 309 | return result; |
| 310 | } |
| 311 | return result; |
| 312 | } |
| 313 | |
| 314 | |
| 315 | void zte_httpshare_init() |
| 316 | { |
| 317 | (void)zte_del_file(ZTE_HTTPSHARE_DB_PATH); |
| 318 | |
| 319 | /*ºËʵÐèÒªµÄ·¾¶Ä¿Â¼ÊÇ·ñ´æÔÚ£¬²»´æÔÚÔò´´½¨*/ |
| 320 | if (ZTE_HTTPSHARE_SUCCESS != zte_init_sdcard_path()) { |
| 321 | slog(SDCARD_PRINT, SLOG_ERR, "zte_httpshare_init:%s does not exist.\n", SD_CARD_PATH_PR); |
| 322 | return; |
| 323 | } |
| 324 | |
| 325 | /*´´½¨httpshareµÄdb·¾¶*/ |
| 326 | if (ZTE_HTTPSHARE_SUCCESS != zte_httpshare_check_and_creat_dir(ZTE_HTTPSHARE_DB_DIR)) { |
| 327 | slog(SDCARD_PRINT, SLOG_ERR, "[sdcard-hotplug]zte_httpshare_init:zte_httpshare_check_and_creat_dir ZTE_HTTPSHARE_DB_DIR fail!\n"); |
| 328 | return; |
| 329 | } |
| 330 | /*´´½¨httpshareµÄ±íÏî*/ |
| 331 | if (ZTE_HTTPSHARE_DB_OK != zte_httpshare_create_table()) { |
| 332 | slog(SDCARD_PRINT, SLOG_ERR, "[sdcard-hotplug]zte_httpshare_init:zte_httpshare_create_table fail!\n"); |
| 333 | return; |
| 334 | } |
| 335 | |
| 336 | zte_init_sdcard_mode(); |
| 337 | } |
| 338 | |
| 339 | |