⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mbchecker.c

📁 手机短消息的服务器端和客户端的源代码 是应用于LINUX/UNIX开发环境的
💻 C
📖 第 1 页 / 共 2 页
字号:
	if (get_gsm_answer (fd, scratch, BIGBUFF, 20)) {#ifdef INCL_DEBUG_CODE          fprintf (stderr, "%s\n", scratch);#endif	  /* check for "OK" */	  if (strstr (scratch, "OK") == NULL) {	    mdm_unlock (mdmopendevice);	    hangup (fd);	    free (scratch);            syslog ((FACILITY | LOG_ERR), "can't send PUK to GSM or wrong PUK.");	    mdmperror ("sms_serv: can't send PUK to GSM or wrong PUK");	    return (-1);	  }	}	else {	  mdm_unlock (mdmopendevice);	  hangup (fd);	  free (scratch);          syslog ((FACILITY | LOG_ERR), "GSM module not responding.");	  mdmperror ("sms_serv: GSM not responding");	  return (-1);	}	/* store new PIN in conf. file (GSMDEVFILE) */	/* log it */	syslog ((FACILITY | LOG_WARNING), "new PIN for </dev/%s> set to [%04d].", gsm->device, newpin);      }      else {                                     /* ERROR */	mdm_unlock (mdmopendevice);	hangup (fd);	free (scratch);        syslog ((FACILITY | LOG_ERR), "unable to get SIM status info.");	mdmperror ("sms_serv: unable to get SIM status info");	return (-1);      }                 /* if "PIN" elseif "PUK" else ... */      /* query the status again */      sprintf (scratch, "at+cpin?\r");      tell_gsm (fd, scratch);      memset (scratch, 0, (BIGBUFF + 1));      if (!get_gsm_answer (fd, scratch, BIGBUFF, 1)) {	mdm_unlock (mdmopendevice);	hangup (fd);	free (scratch);        syslog ((FACILITY | LOG_ERR), "GSM module not responding.");	mdmperror ("sms_serv: GSM not responding");	return (-1);      }#ifdef INCL_DEBUG_CODE      fprintf (stderr, "%s\n", scratch);#endif    }                                 /* while (!"READY") */  }  else {    mdm_unlock (mdmopendevice);    hangup (fd);    free (scratch);    syslog ((FACILITY | LOG_ERR), "GSM module not responding.");    mdmperror ("sms_serv: GSM not responding");    return (-1);  }    /*------------set "no notify for incoming SMS messages" */  sprintf (scratch, "at+cnmi=0,0,0,0,0\r");  tell_gsm (fd, scratch);  memset (scratch, 0, (BIGBUFF + 1));  if (get_gsm_answer (fd, scratch, BIGBUFF, 1)) {#ifdef INCL_DEBUG_CODE    fprintf (stderr, "%s\n", scratch);#endif    /* check for "OK" */    if (strstr (scratch, "OK") == NULL) {      mdm_unlock (mdmopendevice);      hangup (fd);      free (scratch);      syslog ((FACILITY | LOG_ERR), "error after sending AT+CNMI command.");      mdmperror ("sms_serv: error after sending AT+CNMI command");      return (-1);    }  }  else {    mdm_unlock (mdmopendevice);    hangup (fd);    free (scratch);    syslog ((FACILITY | LOG_ERR), "GSM module not responding.");    mdmperror ("sms_serv: GSM not responding");    return (-1);  }    /*----------------Check stored SCA against default SMSC */  sprintf (scratch, "at+csca?\r");  tell_gsm (fd, scratch);  memset (scratch, 0, (BIGBUFF + 1));  if (get_gsm_answer (fd, scratch, BIGBUFF, 1)) {#ifdef INCL_DEBUG_CODE    fprintf (stderr, "%s\n", scratch);#endif    /* compare with default SMSC */    if (strstr (scratch, gsm->defsca) == NULL) {      /* let's set it then */      sprintf (scratch, "at+csca=\"%s\"\r", gsm->defsca);      tell_gsm (fd, scratch);      memset (scratch, 0, (BIGBUFF + 1));      if (get_gsm_answer (fd, scratch, BIGBUFF, 2)) {#ifdef INCL_DEBUG_CODE        fprintf (stderr, "%s\n", scratch);#endif	/* check for "OK" */	if (strstr (scratch, "OK") == NULL) {	  mdm_unlock (mdmopendevice);	  hangup (fd);	  free (scratch);          syslog ((FACILITY | LOG_ERR), "error after sending AT+CSCA= command.");	  mdmperror ("sms_serv: error after sending AT+CSCA= command");	  return (-1);	}      }      else {	mdm_unlock (mdmopendevice);	hangup (fd);	free (scratch);        syslog ((FACILITY | LOG_ERR), "GSM module not responding.");	mdmperror ("sms_serv: GSM not responding");	return (-1);      }    }  }  else {    mdm_unlock (mdmopendevice);    hangup (fd);    free (scratch);    syslog ((FACILITY | LOG_ERR), "GSM module not responding.");    mdmperror ("sms_serv: GSM not responding");    return (-1);  }    /*---------------------Am I registered on the network ? */  sprintf (scratch, "at+creg?\r");  tell_gsm (fd, scratch);  memset (scratch, 0, (BIGBUFF + 1));  if (get_gsm_answer (fd, scratch, BIGBUFF, 1)) {#ifdef INCL_DEBUG_CODE    fprintf (stderr, "%s\n", scratch);#endif    /* check for "0,1" status */    if (strstr (scratch, "+CREG: 0,1") == NULL) {      mdm_unlock (mdmopendevice);      hangup (fd);      free (scratch);      syslog ((FACILITY | LOG_ERR), "not registered on the network - can't check mailbox.");      mdmperror ("sms_serv: not registered on the network - can't check mailbox");      return (-1);    }  }  else {    mdm_unlock (mdmopendevice);    hangup (fd);    free (scratch);    syslog ((FACILITY | LOG_ERR), "GSM module not responding.");    mdmperror ("sms_serv: GSM not responding");    return (-1);  }    /*-----------------------Now loop and read incoming SMS */  sprintf (scratch, "at+cmgl=0\r");  tell_gsm (fd, scratch);  memset (scratch, 0, (BIGBUFF + 1));  if (get_gsm_answer (fd, scratch, BIGBUFF, 1)) {#ifdef INCL_DEBUG_CODE    fprintf (stderr, "%s\n", scratch);#endif    /*....................look for "+CMGL:" in GSM answer */    p1 = scratch;    while (p1 && ((p1 = strstr (p1, "+CMGL:")) != NULL)) {      /* got new message - isolate it from the batch */      p2 = (p1 + 6);      if ((p2 = strstr (p2, "+CMGL:")) != NULL) {        /* there's another one behind */	p1[((p2 - p1) - 1)] = '\0';      }      if ((message = mbparse (p1)) == NULL) {	mdm_unlock (mdmopendevice);	hangup (fd);	free (scratch);	syslog ((FACILITY | LOG_ERR), "mbparse() internal error.");	mdmperror ("sms_serv: mbparse() internal error");	return (-1);      }      mbox_list_insert (&mailbox, message);      nmsgin++;      p1 = p2;    }                              /* while ((msgstart... */    if (nmsgin) {      /*............dump all messages to the mailbox file */      if (dumptofile (&mailbox, gsm->device) != nmsgin) {	syslog ((FACILITY | LOG_ERR), "failed to save messages to inbox file.");	mdmperror ("sms_serv: failed to save messages to inbox file");      }      /*.................remove the messages from the SIM */      message = mailbox.head;      while (message != NULL) {	sprintf (scratch, "at+cmgd=%d\r", message->msgid);	tell_gsm (fd, scratch);	memset (scratch, 0, (BIGBUFF + 1));	if (get_gsm_answer (fd, scratch, BIGBUFF, 1)) {#ifdef INCL_DEBUG_CODE	  fprintf (stderr, "%s\n", scratch);#endif	  /* check for "OK" */	  if (strstr (scratch, "OK") == NULL) {	    mdm_unlock (mdmopendevice);	    hangup (fd);	    free (scratch);	    syslog ((FACILITY | LOG_ERR), "error after sending AT+CMGD command.");	    mdmperror ("sms_serv: error after sending AT+CMGD command");	    return (-1);	  }	}	else {	  mdm_unlock (mdmopendevice);	  hangup (fd);	  free (scratch);	  syslog ((FACILITY | LOG_ERR), "GSM module not responding.");	  mdmperror ("sms_serv: GSM not responding");	  return (-1);	}        message = message->next;      }                        /* while (message != NULL) */      /*...............clean the mailbox list from memory */      free_mbox_list (&mailbox);    }                                      /* if (nmsgin) */  }  else {    mdm_unlock (mdmopendevice);    hangup (fd);    free (scratch);    syslog ((FACILITY | LOG_ERR), "GSM module not responding.");    mdmperror ("sms_serv: GSM not responding");    return (-1);  }    /*----------------------------------Close communication */  mdm_unlock (mdmopendevice);  hangup (fd);  free (scratch);  return (nmsgin);}                                           /* mbcheck () *//*========================================================*/void mbcheck_wrapper (){  extern int errno;  extern int global_sem;  extern int shmem_sem;  extern struct gsms_def *gsmdevices;  extern int ngsmdevs;     /* num. configured GSM devices */  int nwaitsecs = 0;  int last_errno;  int retval;  char device[MAXDEVLEN];  struct gsms_def gsm;  int msgin;  int i;  int cpfd;                     /* checkpoint file descr. */  int gsm_done[ngsmdevs];  int maxiterat;  int iteration;  char *missedg;  struct sigaction sa_c;  /*--------------------------------------Initializations */  for (i = 0; i < ngsmdevs; i++) {    gsm_done[i] = FALSE;  }  maxiterat = (2 * ngsmdevs);  iteration = 0;    /*--------------------------------------------Main loop */  while ((!all_done (gsm_done, ngsmdevs)) && (iteration < maxiterat)) {    syslog ((FACILITY | LOG_INFO), "MBC waits for a free GSM instance (pass %d)...",           (iteration + 1));      while (((retval = sem_decreq (global_sem)) == -1) &&          (errno == EAGAIN) &&	  (nwaitsecs < M_TIMEOUT)) {      sleep (W_STEP);      nwaitsecs += W_STEP;    }                                      /* while (...) */    last_errno = errno;      if (retval == -1) {          /* failed to get a modem */      switch (last_errno) {        case EAGAIN: {          syslog ((FACILITY | LOG_WARNING), "MBC timeout expired (all GSMs busy).");          break;        }        default: {          syserr ("sms_serv(MBC): can't decrease global semaphore");	  break;        }      }                            /* switch (last_errno) */    }    else {               /* ---- Begin Crit. Sect. #1 --- */      /* at least 1 GSM module is free - find it */      if (sem_wait (shmem_sem) == -1)        syserr ("sms_serv(MBC): failed to wait on shared mem. semaphore");      /* ---- Begin Crit. Sect. #2 ---- */      /* first catch SIGTERM to handle child being killed while GSM's locked */      sa_c.sa_handler = MBC_unlock_gsm;      if (sigfillset (&sa_c.sa_mask) == -1)        syserr ("sms_serv(MBC): can't fill signal set");      sa_c.sa_flags = 0;      if (sigaction (SIGTERM, &sa_c, NULL) == -1)        syserr ("sms_serv(MBC): can't catch SIGTERM");      /* search for MBC_instance both free and not yet processed */      MBC_instance = 0;      while (MBC_instance < ngsmdevs) {	if (gsmdevices[MBC_instance].free && !gsm_done[MBC_instance]) {	  /* let's process this one */	  break;	}        MBC_instance++;      }                     /* while (MBC_instance < ngsmdevs) */      if (MBC_instance < ngsmdevs) {        /* got it - now lock it (device lockfile handled by libmodem) */        MBC_instance_is_set = TRUE;        gsmdevices[MBC_instance].free = FALSE;        gsmdevices[MBC_instance].owner = getpid ();              /* copy to "local" space, to avoid the shmem balagan */        if (gsmdevcpy (&gsm, &gsmdevices[MBC_instance]) == -1)          syserr ("sms_serv(MBC): error copying GSM MBC_instance");              /* leave crit. sect. */        if (sem_signal (shmem_sem) == -1)          syserr ("sms_serv(MBC): can't signal shared mem. semaphore");        /* ---- End Crit. Sect. #2 ---- */        /* log it */        syslog ((FACILITY | LOG_NOTICE), "MBC now polling device </dev/%s>.", gsm.device);            /* ---->>> Actual GSM Dialogue <<<---- */        if ((msgin = mbcheck (&gsm)) != -1) {          /* log it */          syslog ((FACILITY | LOG_NOTICE), "MBC check ok, got %d msg from </dev/%s>.", msgin, gsm.device);        }        else { /* some error occured */          syslog ((FACILITY | LOG_WARNING), "MBC ERROR: failed to poll </dev/%s>.", gsm.device);        }	/* mark this GSM as "done" */	gsm_done[MBC_instance] = TRUE;            /* now free the modem again */        if (sem_wait (shmem_sem) == -1)          syserr ("sms_serv(MBC): failed to wait on shared mem. semaphore");        /* ---- Begin Crit. Sect. #3 ---- */        gsmdevices[MBC_instance].free = TRUE;        gsmdevices[MBC_instance].owner = 0;        MBC_instance_is_set = FALSE;        /* leave crit. sect. */        if (sem_signal (shmem_sem) == -1)          syserr ("sms_serv(MBC): can't signal shared mem. semaphore");        /* ---- End Crit. Sect. #3 ---- */        /* leave main crit. sect. */        if (sem_signal (global_sem) == -1)          syserr ("sms_serv(MBC): can't signal global semaphore");	/* ---- End Crit. Sect. #1 ---- */      }      else {        /* leave crit. sect. */        if (sem_signal (shmem_sem) == -1)          syserr ("sms_serv(MBC): can't signal shared mem. semaphore");        /* ---- End Crit. Sect. #2 ---- */        /* leave main crit. sect. - release unused resource */        if (sem_signal (global_sem) == -1)          syserr ("sms_serv(MBC): can't signal global semaphore");	/* ---- End Crit. Sect. #1 ---- */        syslog ((FACILITY | LOG_WARNING), "MBC all free GSM's already processed");      }                   /* if (MBC_instance < ngsmdevs) */    }                                /* if (retval == -1) */    iteration++;  }                               /* while ((!all_done... */  /*-------------------------------------------Conclusion */  /* update timestamp on checkpoint file */  if ((cpfd = open (CHECKPOINTF, O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1) {    perror ("sms_serv(MBC): can't update checkpoint file");    syslog ((FACILITY | LOG_WARNING), "MBC can't update checkpoint file.");  }  close (cpfd);    /* Based on gsm_done, report on missed MBC_instances */  if (all_done (gsm_done, ngsmdevs)) {    /* all done */    syslog ((FACILITY | LOG_NOTICE), "MBC all devices processed.");  }  else {    /* missed some */    missedg = (char *) malloc ((MINIBUFF + 1) * sizeof (char));    missedg[0] = '\0';    for (i = 0; i < ngsmdevs; i++) {      if (!gsm_done[i]) {        strcat (missedg, gsmdevices[i].device);	strcat (missedg, ", ");      }    }    /* remove trailing ", " sequence */    missedg[strlen (missedg) - 2] = '\0';    syslog ((FACILITY | LOG_WARNING), "MBC poll missed {%s}.", missedg);    free (missedg);  }}                                   /* mbcheck_wrapper () *//*========================================================*//*========================================================== * EOF : mbchecker.c *===================*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -