contentLister/inc/system.h File Reference

Go to the source code of this file.

Defines

#define PINCODE_MIN_LENGTH   4
#define PINCODE_MAX_LENGTH   9
#define POWERMGMTIFACE   "/dev/battery"
#define BATTERY_IOCTL_BASE   'b'
#define BATTERY_IOCTL_READ_CURRENT   _IOR( BATTERY_IOCTL_BASE,16,unsigned int)

Functions

int shell_exec (const char *cmd)
int fork_exec (const int argc, char *const argv[])
int scGet_Reconnect (gboolean *connect_after_reboot)
gboolean is_battery_charging (void)
gint sysset_read_pincode_onoff (gboolean *enable)
gint sysset_read_pincode_string (gchar *pincode)
gboolean sysset_is_pincode_empty ()
const char * get_sysset_macAddress (void)
gboolean delete_fsitem (const gchar *path)


Define Documentation

#define BATTERY_IOCTL_BASE   'b'

Definition at line 36 of file system.h.

#define BATTERY_IOCTL_READ_CURRENT   _IOR( BATTERY_IOCTL_BASE,16,unsigned int)

Definition at line 37 of file system.h.

#define PINCODE_MAX_LENGTH   9

Definition at line 32 of file system.h.

#define PINCODE_MIN_LENGTH   4

Definition at line 31 of file system.h.

#define POWERMGMTIFACE   "/dev/battery"

Definition at line 34 of file system.h.


Function Documentation

gboolean delete_fsitem ( const gchar *  path  ) 

Definition at line 602 of file system.c.

00603 {
00604     int    rc;
00605     char*  cp;
00606     int    argc;
00607     char*  argv[10];
00608     gboolean retVal = FALSE;  // return value
00609 
00610     g_assert(path);
00611 
00612     rc = unlink(path);
00613     if (rc == 0)
00614     {
00615         retVal = TRUE;
00616     }
00617     else
00618     {
00619         // command = rm -rf <path>
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 }

Here is the call graph for this function:

int fork_exec ( const int  argc,
char *const   argv[] 
)

Definition at line 127 of file system.c.

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     // spawn child process
00137     switch (pid = fork())
00138     {
00139         case 0:
00140             // child process: execute command
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             // error: fork failed
00147             CL_ERRORPRINTF("fork returns [%d] errno [%d] - %s", pid, errno, strerror(errno));
00148             g_assert_not_reached();
00149             break;
00150 
00151         default:
00152             // parent process: wait for child and return its exit value
00153             waitpid(pid, &status, 0);
00154             if (WIFEXITED(status))
00155             {
00156                 rc = WEXITSTATUS(status);
00157             }
00158     }
00159 
00160     return rc;
00161 }

const char* get_sysset_macAddress ( void   ) 

Definition at line 289 of file system.c.

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                 // child process has vanished,
00323                 // probably because our on_sigchld() catched the signal
00324                 ok = TRUE;
00325             }
00326         }
00327         else if ( WIFEXITED(nProcessRet)  &&  WEXITSTATUS(nProcessRet) == 0 )
00328         {
00329             // process has exited with status ok
00330             ok = TRUE;
00331         }
00332         if (ok)
00333         {
00334             // The string as retrieved from sysset is 15 (MAX_HEX_DECORATION+MAC_LENGTH+1) characters long.
00335             //   e.g 0x00167c000000\n
00336             // The first two (MAX_HEX_DECORATION) characters need to be removed
00337             // and dashes inserted to separate byte values
00338             //   resulting in: 00-16-7c-00-00-00
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                     // m.s. nibble
00353                     *cp1 = toupper(*cp2);
00354                     cp1++;
00355                     cp2++;
00356 
00357                     // l.s. nibble
00358                     *cp1 = toupper(*cp2);
00359                     cp1++;
00360                     cp2++;
00361 
00362                     // skip byte separator
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 }

gboolean is_battery_charging ( void   ) 

Definition at line 248 of file system.c.

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)  // in milliamps, positive or negative
00262         {
00263             CL_ERRORPRINTF("ioctl read current failed - %s", strerror(errno));
00264         }
00265         else
00266         {
00267             current = battery_current & 0xFFFF; // convert from unsigned int to short
00268             if (current > 0)
00269             {
00270                 is_charging = TRUE;
00271             }
00272         }
00273 
00274         close(pwr_fd);
00275     }
00276 
00277     return is_charging;
00278 }

int scGet_Reconnect ( gboolean *  connect_after_reboot  ) 

Definition at line 164 of file system.c.

00165 {
00166     FILE     *POUT = 0;
00167     int      nProcessRet = 0;
00168 
00169     int      nRet = -1;              // 0 = ok, -1 = error
00170     *connect_after_reboot = FALSE;   // safe value
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                 // child process has vanished,
00199                 // probably because our on_sigchld() catched the signal
00200                 ok = TRUE;
00201             }
00202         }
00203         else if ( WIFEXITED(nProcessRet)  &&  WEXITSTATUS(nProcessRet) == 0 )
00204         {
00205             // process has exited with status ok
00206             ok = TRUE;
00207         }
00208         if (ok)
00209         {
00210             // The string as retrieved by sysset is 1 + 1 (newline) character long.
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;       // ok
00227                 }
00228                 else if (strcmp(szBuffer, "1") == 0)
00229                 {
00230                     *connect_after_reboot = TRUE;
00231                     nRet = 0;       // ok
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 }

int shell_exec ( const char *  cmd  ) 

Definition at line 49 of file system.c.

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 }

gboolean sysset_is_pincode_empty (  ) 

Definition at line 589 of file system.c.

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 }

Here is the call graph for this function:

gint sysset_read_pincode_onoff ( gboolean *  enable  ) 

Definition at line 384 of file system.c.

00385 {
00386     FILE *POUT = 0;
00387     int nProcessRet = 0;
00388     int nRet = -1;              // 0 = ok, -1 = error
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                 // child process has vanished,
00420                 // probably because our on_sigchld() catched the signal
00421                 ok = TRUE;
00422             }
00423         }
00424         else if ( WIFEXITED(nProcessRet)  &&  WEXITSTATUS(nProcessRet) == 0 )
00425         {
00426             // process has exited with status ok
00427             ok = TRUE;
00428         }
00429         if (ok)
00430         {
00431             int nLen;
00432 
00433             // The string as retrieved by sysset is 1 + 1 (newline) character long.
00434             CL_LOGPRINTF("pclose returned %d", WEXITSTATUS(nProcessRet));
00435             nLen = strlen(szBuffer);
00436             // it's old sysset version, treat binary 0 as "0"
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                 // remove '\n'
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;       // ok
00461                     CL_LOGPRINTF("Read PINCODE onoff: %d", *enable);
00462                 }
00463                 else if (strcmp(szBuffer, "1") == 0)
00464                 {
00465                     *enable = TRUE;
00466                     nRet = 0;       // ok
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 }

gint sysset_read_pincode_string ( gchar *  pincode  ) 

Definition at line 490 of file system.c.

00491 {
00492     FILE *POUT = 0;
00493     int nProcessRet = 0;
00494     char string[PINCODE_MAX_LENGTH + 1] = "";
00495     int nRet = -1;              // 0 = ok, -1 = error
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                 // child process has vanished,
00525                 // probably because our on_sigchld() catched the signal
00526                 ok = TRUE;
00527             }
00528         }
00529         else if ( WIFEXITED(nProcessRet)  &&  WEXITSTATUS(nProcessRet) == 0 )
00530         {
00531             // process has exited with status ok
00532             ok = TRUE;
00533         }
00534         if (ok)
00535         {
00536             // The string as retrieved from sysset is 11(PINCODE_HEX_DECORATION+PINCODE_LENGTH+1) characters long.
00537             //   e.g 0x85F5E0FF\n
00538             // "99999999"<--99999999<--"0x05F5E0FF"<--"0x85F5E0FF\n"
00539             // firstly remove the length of pincode in '0x85F5E0FF\n'
00540             // then, convert hex string to integer, convert the integer to string 
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                 // remove '\n'
00554                 if (szBuffer[nLen - 1] == '\n')
00555                 {
00556                     nLen = nLen - 1;
00557                     szBuffer[nLen] = '\0';
00558                 }
00559                 // get the length of pincode
00560                 ch[0] = szBuffer[strlen("0x8") - 1];        // ch='8'
00561                 ch[1] = '\0';
00562                 len = strtoul(ch, NULL, 16);
00563                 if (len > PINCODE_MAX_LENGTH)
00564                     len = PINCODE_MAX_LENGTH;
00565                 // remove the lenght of pincode
00566                 szBuffer[strlen("0x8") - 1] = '0';
00567                 // convert hex string to integer
00568                 integer = strtoul(szBuffer, NULL, 16);
00569                 // convert integer to string
00570                 sprintf(string, "%0*d", len, integer);
00571                 // deal with some special cases, treat 0x00000000 as 0x40000000
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 }


Generated on Sun Dec 14 17:13:01 2008 by  doxygen 1.5.6