00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026 #include <strings.h>
00027 #include <gtk/gtk.h>
00028 #include <libermanifest/ermanifest.h>
00029 #include <liberregxml/erregapi.h>
00030
00031 #include "erConnect.h"
00032 #include "system.h"
00033 #include "gtkPincodeScreen.h"
00034 #include "control.h"
00035 #include "erMSDisk.h"
00036 #include "contentListerLog.h"
00037
00038
00039 static expMemType_e g_export_memory_type = expMain;
00040 static expMemType_e g_memory_type_used = expUndefined;
00041
00042 static int erMSDiskDisconnectFlash(void);
00043 static int erMSDiskReconnectFlash(void);
00044 static gboolean is_device_mounted(const char* dev);
00045
00046 static const char* dev[expUndefined] =
00047 {
00048 "/dev/tffsa6",
00049 "/dev/mmc1",
00050 "/dev/sda1",
00051 "/dev/hda1"
00052 };
00053
00054 expMemType_e get_exported_memory_type(void)
00055 {
00056 return g_export_memory_type;
00057 }
00058
00059 expMemType_e get_memory_type_used(void)
00060 {
00061 return g_memory_type_used;
00062 }
00063
00064 expMemType_e read_exported_memory_type(void)
00065 {
00066 regExportMemType_t* theExportMemType = NULL;
00067 expMemType_e theExpMemType = expMain;
00068
00069
00070 theExportMemType = erRegGetExportMemType();
00071 if (theExportMemType)
00072 {
00073
00074 if (strcmp(theExportMemType->location, MAIN_CATEGORY) == 0)
00075 {
00076 theExpMemType = expMain;
00077 }
00078 else if (strcmp(theExportMemType->location, SD_CATEGORY) == 0)
00079 {
00080 theExpMemType = expMMC;
00081 }
00082 else if (strcmp(theExportMemType->location, USB_CATEGORY) == 0)
00083 {
00084 theExpMemType = expUSB;
00085 }
00086 else if (strcmp(theExportMemType->location, CF_CATEGORY) == 0)
00087 {
00088 theExpMemType = expCF;
00089 }
00090 else
00091 {
00092 CL_ERRORPRINTF("Invalid exporting memory type: %s", theExportMemType->location);
00093 }
00094
00095 erRegFreeExportMemType(theExportMemType);
00096 }
00097 else
00098 {
00099 CL_ERRORPRINTF("erRegGetExportMemType returns NULL");
00100 }
00101
00102 CL_LOGPRINTF("leave, return [%d]", theExpMemType);
00103 return theExpMemType;
00104 }
00105
00106 void erMSDiskInit()
00107 {
00108 g_export_memory_type = read_exported_memory_type();
00109 }
00110
00111
00112 static gboolean is_device_mounted(const char* dev)
00113 {
00114 FILE* fp = fopen(PROCFS_MOUNT_FILE, "r");
00115 if (NULL == fp)
00116 {
00117 printf("Can NOT open %s\n", PROCFS_MOUNT_FILE);
00118 return -1;
00119 }
00120
00121 char buf[128];
00122 int len = strlen(dev);
00123 gboolean bFound = FALSE;
00124 while (!feof(fp))
00125 {
00126 fgets(buf, 128, fp);
00127 if (strncmp(buf, dev, len) == 0)
00128 {
00129
00130 bFound = TRUE;
00131 break;
00132 }
00133 }
00134
00135 fclose(fp);
00136 return bFound;
00137 }
00138
00139 void erStartMSDiskApp()
00140 {
00141 char cmd[CMD_LEN];
00142 expMemType_e memory_type_new = expMain;
00143
00144 CL_WARNPRINTF("entry: g_export_memory_type [%d] g_memory_type_used [%d]", g_export_memory_type, g_memory_type_used);
00145
00146
00147 if ( is_device_mounted(dev[g_export_memory_type]) )
00148 {
00149 memory_type_new = g_export_memory_type;
00150 }
00151
00152 if (g_memory_type_used == memory_type_new)
00153 {
00154 ;
00155 }
00156 else
00157 {
00158
00159 if (g_memory_type_used != expUndefined)
00160 {
00161 erStopMSDiskApp();
00162 }
00163
00164
00165 g_memory_type_used = memory_type_new;
00166 snprintf(cmd, sizeof(cmd), "sync ; msdisk %s &", dev[g_memory_type_used]);
00167 CL_WARNPRINTF("-- starting msdisk [%s]", cmd);
00168 system(cmd);
00169 }
00170 }
00171
00172 void erStopMSDiskApp()
00173 {
00174
00175 CL_WARNPRINTF("-- stopping msdisk");
00176 system("killall msdisk; sync; sync;");
00177
00178 g_memory_type_used = expUndefined;
00179 }
00180
00181 int erMSDiskSetConnected(int connected)
00182 {
00183 CL_WARNPRINTF("connected = %d", connected);
00184
00185 if (connected == 1)
00186 {
00187 listerShowUSBScreen();
00188 setListerState(STATE_USB_CONNECTED);
00189
00190 #ifdef ENABLE_CONFIG_EXPORT_IMPORT
00191 ctrl_export_config();
00192 #endif
00193
00194
00195
00196 if (g_export_memory_type != expUSB)
00197 {
00198 erMSDiskDisconnectFlash();
00199 }
00200
00201 return 0;
00202 }
00203 else if (connected == 0)
00204 {
00205
00206 if (g_export_memory_type != expUSB)
00207 {
00208 erMSDiskReconnectFlash();
00209 }
00210
00211 listerHideUSBScreen();
00212 setListerState(STATE_NORMAL);
00213
00214 #ifdef ENABLE_CONFIG_EXPORT_IMPORT
00215 ctrl_handle_import_config();
00216 #endif
00217
00218 mdsRecreateArchives(st_StorageTypeMain);
00219 mdsRecreateArchives(st_StorageTypeCF);
00220 mdsRecreateArchives(st_StorageTypeSD);
00221 mdsRecreateArchives(st_StorageTypeUSB);
00222
00223 return 0;
00224 }
00225 else
00226 {
00227 return -1;
00228 }
00229 }
00230
00231 static int erMSDiskDisconnectFlash()
00232 {
00233
00234 ctrl_disconnect_wait();
00235
00236
00237 system("sync; sync; sync");
00238
00239 char cmd[CMD_LEN];
00240 snprintf(cmd, CMD_LEN, "umount %s", dev[g_memory_type_used]);
00241
00242 int ret = system(cmd);
00243 if (ret == 0)
00244 {
00245 return 0;
00246 }
00247 else
00248 {
00249 CL_ERRORPRINTF("Mount process returned error %d", ret);
00250 return -1;
00251 }
00252 }
00253
00254 static int erMSDiskReconnectFlash()
00255 {
00256 int rc;
00257 int ret = -1;
00258 char cmd[CMD_LEN];
00259
00260
00261
00262 snprintf(cmd, CMD_LEN, "mount %s", dev[g_memory_type_used]);
00263
00264
00265 erStopMSDiskApp();
00266
00267
00268 CL_WARNPRINTF("remounting memory, command [%s]", cmd);
00269 rc = system(cmd);
00270 if (rc == 0)
00271 {
00272 rc = 0;
00273 }
00274 else
00275 {
00276 CL_ERRORPRINTF("Mount process returned error %d", rc);
00277 }
00278
00279
00280 erStartMSDiskApp();
00281
00282 return ret;
00283 }
00284
00285 void erMSDisk_storage_changed(st_ContentType_e storageType, gboolean storagePresent)
00286 {
00287 CL_LOGPRINTF("storageType = %d, storagePresent = %d", storageType, storagePresent);
00288
00289
00290 expMemType_e storage_type = expUndefined;
00291 switch (storageType)
00292 {
00293 case st_StorageTypeCF:
00294 storage_type = expCF;
00295 break;
00296 case st_StorageTypeSD:
00297 storage_type = expMMC;
00298 break;
00299 case st_StorageTypeUSB:
00300 storage_type = expUSB;
00301 break;
00302 default:
00303 break;
00304 }
00305
00306 if (getListerState() == STATE_USB_CONNECTED)
00307 {
00308
00309 if (storagePresent == FALSE)
00310 {
00311
00312 if (storage_type == g_memory_type_used)
00313 {
00314
00315 CL_WARNPRINTF("WARNING! Card is plugged out when connected to PC.");
00316
00317
00318 erStopMSDiskApp();
00319 listerHideUSBScreen();
00320 setListerState(STATE_NORMAL);
00321 }
00322 else
00323 {
00324
00325 }
00326 }
00327 }
00328 else if (storage_type == g_export_memory_type)
00329 {
00330 erStartMSDiskApp();
00331 }
00332 }
00333
00334 void erMSDisk_handle_reread_settings()
00335 {
00336
00337 g_export_memory_type = read_exported_memory_type();
00338 }
00339