00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00024 #include <config.h>
00025
00026 #include <alloca.h>
00027 #include <ctype.h>
00028 #include <errno.h>
00029 #include <fcntl.h>
00030 #include <stdlib.h>
00031 #include <stdio.h>
00032 #include <string.h>
00033 #include <sys/ioctl.h>
00034 #include <sys/stat.h>
00035 #include <sys/types.h>
00036 #include <sys/wait.h>
00037 #include <unistd.h>
00038
00039 #include <glib.h>
00040 #include <gdk/gdk.h>
00041
00042 #include "contentListerLog.h"
00043 #include "system.h"
00044
00045
00046 #define MAX_ARGS 255
00047
00048
00049 int shell_exec(const char *cmd)
00050 {
00051 const char *p = cmd;
00052 char *buf = alloca(strlen(cmd) + 1), *bufp = buf;
00053 char *argv[MAX_ARGS + 1];
00054 int nargs = 0;
00055 int escape = 0, squote = 0, dquote = 0;
00056 int rc;
00057 int i;
00058
00059 if (cmd[0] == 0)
00060 {
00061 errno = ENOENT;
00062 return -1;
00063 }
00064
00065 while (*p)
00066 {
00067 if (escape)
00068 {
00069 *bufp++ = *p;
00070 escape = 0;
00071 }
00072 else
00073 {
00074 switch (*p)
00075 {
00076 case '\\':
00077 escape = 1;
00078 break;
00079 case '"':
00080 if (squote)
00081 *bufp++ = *p;
00082 else
00083 dquote = !dquote;
00084 break;
00085 case '\'':
00086 if (dquote)
00087 *bufp++ = *p;
00088 else
00089 squote = !squote;
00090 break;
00091 case ' ':
00092 if (!squote && !dquote)
00093 {
00094 *bufp = 0;
00095 if (nargs < MAX_ARGS)
00096 argv[nargs++] = strdup(buf);
00097 bufp = buf;
00098 while (*(p+1) == ' ')
00099 p++;
00100 break;
00101 }
00102 default:
00103 *bufp++ = *p;
00104 break;
00105 }
00106 }
00107 p++;
00108 }
00109
00110 if (bufp != buf)
00111 {
00112 *bufp = 0;
00113 if (nargs < MAX_ARGS)
00114 argv[nargs++] = strdup(buf);
00115 }
00116
00117 argv[nargs] = NULL;
00118 rc = execvp(argv[0], argv);
00119
00120 for (i = 0; i < nargs; i++)
00121 free(argv[i]);
00122
00123 return rc;
00124 }
00125
00126
00127 int fork_exec(const int argc, char *const argv[])
00128 {
00129 int rc = -1;
00130 int pid;
00131 int status;
00132
00133 CL_LOGPRINTF("entry: [%s]", argv[0]);
00134 g_assert(argv[argc] == NULL);
00135
00136
00137 switch (pid = fork())
00138 {
00139 case 0:
00140
00141 rc = execvp(argv[0], argv);
00142 CL_ERRORPRINTF("execvp [%s] returns [%d] errno [%d] - %s", argv[0], rc, errno, strerror(errno));
00143 exit(1);
00144
00145 case -1:
00146
00147 CL_ERRORPRINTF("fork returns [%d] errno [%d] - %s", pid, errno, strerror(errno));
00148 g_assert_not_reached();
00149 break;
00150
00151 default:
00152
00153 waitpid(pid, &status, 0);
00154 if (WIFEXITED(status))
00155 {
00156 rc = WEXITSTATUS(status);
00157 }
00158 }
00159
00160 return rc;
00161 }
00162
00163
00164 int scGet_Reconnect(gboolean* connect_after_reboot)
00165 {
00166 FILE *POUT = 0;
00167 int nProcessRet = 0;
00168
00169 int nRet = -1;
00170 *connect_after_reboot = FALSE;
00171
00172
00173 POUT = popen("sysset -r -a 153 -l 1", "r");
00174 if (POUT == NULL)
00175 {
00176 CL_ERRORPRINTF("Could not read Connect_After_Reboot");
00177 }
00178 else
00179 {
00180 int nChar;
00181 int index = 0;
00182 char szBuffer[100];
00183
00184 while (((nChar = getc(POUT)) != EOF) && (index < sizeof(szBuffer) - 1))
00185 {
00186 szBuffer[index] = (char)nChar;
00187 index++;
00188 }
00189 szBuffer[index] = '\0';
00190 CL_LOGPRINTF("echoed %s", szBuffer);
00191
00192 nProcessRet = pclose(POUT);
00193 gboolean ok = FALSE;
00194 if (nProcessRet == -1)
00195 {
00196 if (errno == ECHILD)
00197 {
00198
00199
00200 ok = TRUE;
00201 }
00202 }
00203 else if ( WIFEXITED(nProcessRet) && WEXITSTATUS(nProcessRet) == 0 )
00204 {
00205
00206 ok = TRUE;
00207 }
00208 if (ok)
00209 {
00210
00211 int nLen = strlen(szBuffer);
00212 if (szBuffer[nLen - 1] == '\n')
00213 {
00214 nLen = nLen - 1;
00215 szBuffer[nLen] = '\0';
00216 }
00217 if (nLen != 1)
00218 {
00219 CL_ERRORPRINTF("Retrieved Reconnect flag [%s] wrong length [%d]", szBuffer, nLen);
00220 }
00221 else
00222 {
00223 if (strcmp(szBuffer, "0") == 0)
00224 {
00225 *connect_after_reboot = FALSE;
00226 nRet = 0;
00227 }
00228 else if (strcmp(szBuffer, "1") == 0)
00229 {
00230 *connect_after_reboot = TRUE;
00231 nRet = 0;
00232 }
00233 else
00234 {
00235 CL_ERRORPRINTF("Retrieved Reconnect flag has illegal value: [%s]", szBuffer);
00236 }
00237 }
00238 }
00239 else
00240 {
00241 CL_ERRORPRINTF("pclose did not exit normally");
00242 }
00243 }
00244
00245 return nRet;
00246 }
00247
00248 gboolean is_battery_charging(void)
00249 {
00250 gboolean is_charging = FALSE;
00251 unsigned int battery_current;
00252 short current;
00253
00254 int pwr_fd = open(POWERMGMTIFACE, O_RDWR);
00255 if (pwr_fd < 0)
00256 {
00257 CL_ERRORPRINTF("Error opening /dev/battery - %s", strerror(errno));
00258 }
00259 else
00260 {
00261 if (ioctl(pwr_fd, BATTERY_IOCTL_READ_CURRENT, &battery_current) == -1)
00262 {
00263 CL_ERRORPRINTF("ioctl read current failed - %s", strerror(errno));
00264 }
00265 else
00266 {
00267 current = battery_current & 0xFFFF;
00268 if (current > 0)
00269 {
00270 is_charging = TRUE;
00271 }
00272 }
00273
00274 close(pwr_fd);
00275 }
00276
00277 return is_charging;
00278 }
00279
00280
00281 #define PINCODE_LENGTH (4*2)
00282 #define PINCODE_HEX_DECORATION 2
00283
00284 #define MAC_LENGTH 12
00285 #define MAC_HEX_DECORATION 2
00286
00287 #define PROCESS_BUFFER_MAX (2*1024)
00288
00289 const char* get_sysset_macAddress(void)
00290 {
00291 FILE *POUT = 0;
00292 int nProcessRet = 0;
00293
00294 static char mac_address[MAC_LENGTH+6] = " - - - - - ";
00295
00296
00297 POUT = popen("sysset -r -b -a 262 -l 6", "r");
00298 if (POUT == NULL)
00299 {
00300 CL_ERRORPRINTF("Could not read EUI64");
00301 }
00302 else
00303 {
00304 int nChar;
00305 int index = 0;
00306 char szBuffer[PROCESS_BUFFER_MAX + 1];
00307
00308 while (((nChar = getc(POUT)) != EOF) && (index < PROCESS_BUFFER_MAX))
00309 {
00310 szBuffer[index] = (char) nChar;
00311 index++;
00312 }
00313 szBuffer[index] = '\0';
00314 CL_LOGPRINTF("echoed %s", szBuffer);
00315
00316 nProcessRet = pclose(POUT);
00317 gboolean ok = FALSE;
00318 if (nProcessRet == -1)
00319 {
00320 if (errno == ECHILD)
00321 {
00322
00323
00324 ok = TRUE;
00325 }
00326 }
00327 else if ( WIFEXITED(nProcessRet) && WEXITSTATUS(nProcessRet) == 0 )
00328 {
00329
00330 ok = TRUE;
00331 }
00332 if (ok)
00333 {
00334
00335
00336
00337
00338
00339
00340 CL_LOGPRINTF("pclose returned %d", WEXITSTATUS(nProcessRet));
00341 int nLen = strlen(szBuffer);
00342 if (nLen != MAC_LENGTH + 2 + 1)
00343 {
00344 CL_ERRORPRINTF("Wrong length [%d] of MAC address [%s]", nLen, szBuffer);
00345 }
00346 else
00347 {
00348 char* cp1 = mac_address;
00349 char* cp2 = szBuffer + MAC_HEX_DECORATION;
00350 for (index = 0; index < MAC_LENGTH; index += 2)
00351 {
00352
00353 *cp1 = toupper(*cp2);
00354 cp1++;
00355 cp2++;
00356
00357
00358 *cp1 = toupper(*cp2);
00359 cp1++;
00360 cp2++;
00361
00362
00363 cp1++;
00364 }
00365 }
00366 }
00367 else
00368 {
00369 CL_ERRORPRINTF("pclose did not exit normally");
00370 }
00371 }
00372
00373 CL_LOGPRINTF("return [%s]", mac_address);
00374 return mac_address;
00375 }
00376
00377
00379
00380
00381
00382
00383
00384 gint sysset_read_pincode_onoff(gboolean * enable)
00385 {
00386 FILE *POUT = 0;
00387 int nProcessRet = 0;
00388 int nRet = -1;
00389
00390 if (NULL == enable)
00391 return nRet;
00392 *enable = FALSE;
00393
00394 POUT = popen("sysset -r -a 58 -l 1", "r");
00395 if (POUT == NULL)
00396 {
00397 CL_ERRORPRINTF("Could not read PINCODE on/off");
00398 }
00399 else
00400 {
00401 int nChar;
00402 int index = 0;
00403 char szBuffer[100];
00404
00405 while (((nChar = getc(POUT)) != EOF) && (index < sizeof(szBuffer) - 1))
00406 {
00407 szBuffer[index] = (char)nChar;
00408 index++;
00409 }
00410 szBuffer[index] = '\0';
00411 CL_LOGPRINTF("Read PINCODE onoff- echoed %s", szBuffer);
00412
00413 nProcessRet = pclose(POUT);
00414 gboolean ok = FALSE;
00415 if (nProcessRet == -1)
00416 {
00417 if (errno == ECHILD)
00418 {
00419
00420
00421 ok = TRUE;
00422 }
00423 }
00424 else if ( WIFEXITED(nProcessRet) && WEXITSTATUS(nProcessRet) == 0 )
00425 {
00426
00427 ok = TRUE;
00428 }
00429 if (ok)
00430 {
00431 int nLen;
00432
00433
00434 CL_LOGPRINTF("pclose returned %d", WEXITSTATUS(nProcessRet));
00435 nLen = strlen(szBuffer);
00436
00437 if (0 == nLen)
00438 {
00439 szBuffer[0] = '0';
00440 szBuffer[1] = '\n';
00441 szBuffer[2] = '\0';
00442 nLen = 2;
00443 }
00444 if (nLen != 2)
00445 {
00446 CL_ERRORPRINTF("Retrieved pincode on/off [%s] wrong length [%d]", szBuffer, nLen);
00447 }
00448 else
00449 {
00450
00451 if (szBuffer[nLen - 1] == '\n')
00452 {
00453 nLen = nLen - 1;
00454 szBuffer[nLen] = '\0';
00455 }
00456
00457 if (strcmp(szBuffer, "0") == 0)
00458 {
00459 *enable = FALSE;
00460 nRet = 0;
00461 CL_LOGPRINTF("Read PINCODE onoff: %d", *enable);
00462 }
00463 else if (strcmp(szBuffer, "1") == 0)
00464 {
00465 *enable = TRUE;
00466 nRet = 0;
00467 CL_LOGPRINTF("Read PINCODE onoff: %d", *enable);
00468 }
00469 else
00470 {
00471 CL_ERRORPRINTF("Retrieved pincode on/off has illegal value: [%s]", szBuffer);
00472 }
00473 }
00474 }
00475 else
00476 {
00477 CL_ERRORPRINTF("Read PINCODE onoff- pclose did not exit normally");
00478 }
00479 }
00480
00481 CL_LOGPRINTF("Read PINCODE onoff returned %d", nRet);
00482 return nRet;
00483 }
00484
00485
00486
00487
00488
00489
00490 gint sysset_read_pincode_string(gchar * pincode)
00491 {
00492 FILE *POUT = 0;
00493 int nProcessRet = 0;
00494 char string[PINCODE_MAX_LENGTH + 1] = "";
00495 int nRet = -1;
00496
00497 if (NULL == pincode)
00498 return nRet;
00499
00500 POUT = popen("sysset -r -b -a 59 -l 4", "r");
00501 if (POUT == NULL)
00502 {
00503 CL_ERRORPRINTF("Sysset could not read PINCODE");
00504 }
00505 else
00506 {
00507 int nChar;
00508 int index = 0;
00509 char szBuffer[PROCESS_BUFFER_MAX + 1];
00510
00511 while (((nChar = getc(POUT)) != EOF) && (index < PROCESS_BUFFER_MAX))
00512 {
00513 szBuffer[index] = (char)nChar;
00514 index++;
00515 }
00516 szBuffer[index] = '\0';
00517 CL_LOGPRINTF("Read PINCODE echoed %s", szBuffer);
00518 nProcessRet = pclose(POUT);
00519 gboolean ok = FALSE;
00520 if (nProcessRet == -1)
00521 {
00522 if (errno == ECHILD)
00523 {
00524
00525
00526 ok = TRUE;
00527 }
00528 }
00529 else if ( WIFEXITED(nProcessRet) && WEXITSTATUS(nProcessRet) == 0 )
00530 {
00531
00532 ok = TRUE;
00533 }
00534 if (ok)
00535 {
00536
00537
00538
00539
00540
00541
00542 CL_LOGPRINTF("Read PINCODE-pclose returned %d", WEXITSTATUS(nProcessRet));
00543 int nLen = strlen(szBuffer);
00544 if (nLen != (PINCODE_HEX_DECORATION + PINCODE_LENGTH + 1))
00545 {
00546 CL_ERRORPRINTF("Wrong length [%d] of PINCODE address [%s]", nLen, szBuffer);
00547 }
00548 else
00549 {
00550 char ch[2];
00551 int len;
00552 int integer = 0;
00553
00554 if (szBuffer[nLen - 1] == '\n')
00555 {
00556 nLen = nLen - 1;
00557 szBuffer[nLen] = '\0';
00558 }
00559
00560 ch[0] = szBuffer[strlen("0x8") - 1];
00561 ch[1] = '\0';
00562 len = strtoul(ch, NULL, 16);
00563 if (len > PINCODE_MAX_LENGTH)
00564 len = PINCODE_MAX_LENGTH;
00565
00566 szBuffer[strlen("0x8") - 1] = '0';
00567
00568 integer = strtoul(szBuffer, NULL, 16);
00569
00570 sprintf(string, "%0*d", len, integer);
00571
00572 if (0 == len && 0 == integer)
00573 {
00574 strcpy(string, "0000");
00575 }
00576 strcpy(pincode, string);
00577 CL_LOGPRINTF("PINCODE read returned [%s]", pincode);
00578 nRet = 0;
00579 }
00580 }
00581 else
00582 {
00583 CL_ERRORPRINTF("Read PINCODE pclose did not exit normally");
00584 }
00585 }
00586 return nRet;
00587 }
00588
00589 gboolean sysset_is_pincode_empty()
00590 {
00591 gchar pincode[PINCODE_MAX_LENGTH + 1];
00592 int nLen;
00593 gboolean empty = FALSE;
00594
00595 sysset_read_pincode_string(pincode);
00596 nLen = strlen(pincode);
00597 if (0 == nLen)
00598 empty = TRUE;
00599 return empty;
00600 }
00601
00602 gboolean delete_fsitem(const gchar* path)
00603 {
00604 int rc;
00605 char* cp;
00606 int argc;
00607 char* argv[10];
00608 gboolean retVal = FALSE;
00609
00610 g_assert(path);
00611
00612 rc = unlink(path);
00613 if (rc == 0)
00614 {
00615 retVal = TRUE;
00616 }
00617 else
00618 {
00619
00620 argc = 0;
00621 argv[argc++] = "rm";
00622 argv[argc++] = "-rf";
00623
00624 cp = alloca( strlen(path) + 1 );
00625 g_assert(cp != NULL);
00626 strcpy(cp, path);
00627 argv[argc++] = cp;
00628
00629 argv[argc] = NULL;
00630 g_assert( argc < (sizeof(argv)/sizeof(argv[0])) );
00631 rc = fork_exec(argc, argv);
00632 if (rc == 0)
00633 {
00634 retVal = TRUE;
00635 }
00636 else
00637 {
00638 CL_ERRORPRINTF( "Cannot remove path [%s] - error [%d] [%s]",
00639 path, rc, strerror(rc) );
00640 }
00641 }
00642
00643 return retVal;
00644 }
00645