// We start with two helper functions
unsigned char net_sms_checkcaller(char *caller)
{
    char *p = par_get(PARAM_REGPHONE);
    if (*p && strncmp(p,caller,strlen(p)) == 0)
        return 1;
    else
    {
        if ((sys_features[FEATURE_CARBITS]&FEATURE_CB_SAD_SMS) == 0)
            net_send_sms_rom(caller,NET_MSG_DENIED);
        return 0;
    }
}
unsigned char net_sms_checkpassarg(char *caller, char *arguments)
{
    char *p = par_get(PARAM_MODULEPASS);

    if ((arguments != NULL)&&(*p && strncmp(p,arguments,strlen(p))==0))
        return 1;
    else
    {
        if ((sys_features[FEATURE_CARBITS]&FEATURE_CB_SAD_SMS) == 0)
            net_send_sms_rom(caller,NET_MSG_DENIED);
        return 0;
    }
}
void net_sms_in(char *caller, char *buf, unsigned char pos)
  {
  // The buf contains an SMS command
  // and caller contains the caller telephone number
  char *p;

  // Convert SMS command (first word) to upper-case
  for (p=buf; ((*p!=0)&&(*p!=' ')); p++)
  	if ((*p > 0x60) && (*p < 0x7b)) *p=*p-0x20;

  // Command parsing...
  if (memcmppgm2ram(buf, (char const rom far*)"REGISTER ", 9) == 0)
    { // Register phone
    p = par_get(PARAM_REGPASS);
    if (strncmp(p,buf+9,strlen(p))==0)
      {
      par_set(PARAM_REGPHONE, caller);
      net_send_sms_rom(caller,NET_MSG_REGISTERED);
      }
    else
      {
#ifndef OVMS_SUPPRESS_ACCESSDENIED_SMS
        net_send_sms_rom(caller,NET_MSG_DENIED);
#endif
      }
    }
  else if (memcmppgm2ram(buf, (char const rom far*)"PASS ", 5) == 0)
    {
    p = par_get(PARAM_REGPHONE);
    if (strncmp(p,caller,strlen(p)) == 0)
      {
      par_set(PARAM_REGPASS, buf+5);
      net_send_sms_rom(caller,NET_MSG_PASSWORD);
      }
    else
      {
#ifndef OVMS_SUPPRESS_ACCESSDENIED_SMS
      net_send_sms_rom(caller,NET_MSG_DENIED);
#endif
      }
    }
  else if (memcmppgm2ram(buf, (char const rom far*)"GPS ", 4) == 0)
    {
    p = par_get(PARAM_REGPASS);
    if (strncmp(p,buf+4,strlen(p))==0)
      net_sms_gps(caller);
    else
      {
#ifndef OVMS_SUPPRESS_ACCESSDENIED_SMS
      net_send_sms_rom(caller,NET_MSG_DENIED);
#endif
      }
    }
  else if (memcmppgm2ram(buf, (char const rom far*)"GPS", 3) == 0)
    {
    p = par_get(PARAM_REGPHONE);
    if (strncmp(p,caller,strlen(p)) == 0)
      net_sms_gps(caller);
    else
      {
#ifndef OVMS_SUPPRESS_ACCESSDENIED_SMS
      net_send_sms_rom(caller,NET_MSG_DENIED);
#endif
      }
    }
  else if (memcmppgm2ram(buf, (char const rom far*)"STAT ", 5) == 0)
    {
    p = par_get(PARAM_REGPASS);
    if (strncmp(p,buf+5,strlen(p))==0)
      net_sms_stat(caller);
    else
      {
#ifndef OVMS_SUPPRESS_ACCESSDENIED_SMS
      net_send_sms_rom(caller,NET_MSG_DENIED);
#endif
      }
    }
  else if (memcmppgm2ram(buf, (char const rom far*)"STAT", 4) == 0)
    {
    p = par_get(PARAM_REGPHONE);
    if (strncmp(p,caller,strlen(p)) == 0)
      net_sms_stat(caller);
    else
      {
#ifndef OVMS_SUPPRESS_ACCESSDENIED_SMS
      net_send_sms_rom(caller,NET_MSG_DENIED);
#endif
      }
    }
  else if (memcmppgm2ram(buf, (char const rom far*)"PARAMS?", 7) == 0)
    {
    p = par_get(PARAM_REGPHONE);
    if (strncmp(p,caller,strlen(p)) == 0)
      net_sms_params(caller);
    else
      {
#ifndef OVMS_SUPPRESS_ACCESSDENIED_SMS
      net_send_sms_rom(caller,NET_MSG_DENIED);
#endif
      }
    }
  else if (memcmppgm2ram(buf, (char const rom far*)"PARAMS ", 7) == 0)
    {
    p = par_get(PARAM_REGPHONE);
    if (strncmp(p,caller,strlen(p)) == 0)
      {
      unsigned char d = PARAM_MILESKM;
      unsigned char x = 7;
      unsigned char y = x;
      while ((y<=(pos+1))&&(d < PARAM_MAX))
        {
        if ((buf[y] == ' ')||(buf[y] == '\0'))
          {
          buf[y] = '\0';
          if ((buf[x]=='-')&&(buf[x+1]=='\0'))
            buf[x] = '\0'; // Special case '-' is empty value
          par_set(d++, buf+x);
          x=++y;
          }
        else
          y++;
        }
      net_send_sms_rom(caller,NET_MSG_PARAMS);
      net_state_enter(NET_STATE_SOFTRESET);
      }
    else
      {
#ifndef OVMS_SUPPRESS_ACCESSDENIED_SMS
      net_send_sms_rom(caller,NET_MSG_DENIED);
#endif
      }
    }
  else if (memcmppgm2ram(buf, (char const rom far*)"FEATURE ", 8) == 0)
    {
    p = par_get(PARAM_REGPHONE);
    if (strncmp(p,caller,strlen(p)) == 0)
      {
      unsigned char y = 8;
      unsigned int f;
      while (y<=(pos+1))
        {
        if ((buf[y] == ' ')||(buf[y] == '\0'))
          {
          buf[y] = '\0';
          f = atoi(buf+8);
          if ((f>=0)&&(f<FEATURES_MAX))
            sys_features[f] = atoi(buf+y+1);
          break; // Exit the while loop, as we are done
          }
        else
          y++;
        }
      }
    else
      {
#ifndef OVMS_SUPPRESS_ACCESSDENIED_SMS
      net_send_sms_rom(caller,NET_MSG_DENIED);
#endif
      }
    }
  else if (memcmppgm2ram(buf, (char const rom far*)"RESET", 5) == 0)
    {
    p = par_get(PARAM_REGPHONE);
    if (strncmp(p,caller,strlen(p)) == 0)
      {
      net_state_enter(NET_STATE_HARDRESET);
      }
    else
      {
#ifndef OVMS_SUPPRESS_ACCESSDENIED_SMS
      net_send_sms_rom(caller,NET_MSG_DENIED);
#endif
      }
    }
  else // SMS didn't match any command pattern, forward to user via net msg
    {
    net_msg_forward_sms(caller, buf);
    }
  }
void net_sms_in(char *caller, char *buf, unsigned char pos)
  {
  // The buf contains an SMS command
  // and caller contains the caller telephone number
  char *p;

  // Convert SMS command (first word) to upper-case
  for (p=buf; ((*p!=0)&&(*p!=' ')); p++)
  	if ((*p > 0x60) && (*p < 0x7b)) *p=*p-0x20;

  // Command parsing...
  if (memcmppgm2ram(buf, (char const rom far*)"REGISTER ", 9) == 0)
    { // Register phone
    p = par_get(PARAM_REGPASS);
    if (strncmp(p,buf+9,strlen(p))==0)
      {
      par_set(PARAM_REGPHONE, caller);
      net_send_sms_rom(caller,NET_MSG_REGISTERED);
      }
    else
      net_send_sms_rom(caller,NET_MSG_DENIED);
    }
  else if (memcmppgm2ram(buf, (char const rom far*)"PASS ", 5) == 0)
    {
    p = par_get(PARAM_REGPHONE);
    if (strncmp(p,caller,strlen(p)) == 0)
      {
      par_set(PARAM_REGPASS, buf+5);
      net_send_sms_rom(caller,NET_MSG_PASSWORD);
      }
    else
      net_send_sms_rom(caller,NET_MSG_DENIED);
    }
  else if (memcmppgm2ram(buf, (char const rom far*)"GPS ", 4) == 0)
    {
    p = par_get(PARAM_REGPASS);
    if (strncmp(p,buf+4,strlen(p))==0)
      net_sms_gps(caller);
    else
      net_send_sms_rom(caller,NET_MSG_DENIED);
    }
  else if (memcmppgm2ram(buf, (char const rom far*)"GPS", 3) == 0)
    {
    p = par_get(PARAM_REGPHONE);
    if (strncmp(p,caller,strlen(p)) == 0)
      net_sms_gps(caller);
    else
      net_send_sms_rom(caller,NET_MSG_DENIED);
    }
  else if (memcmppgm2ram(buf, (char const rom far*)"STAT ", 5) == 0)
    {
    p = par_get(PARAM_REGPASS);
    if (strncmp(p,buf+5,strlen(p))==0)
      net_sms_stat(caller);
    else
      net_send_sms_rom(caller,NET_MSG_DENIED);
    }
  else if (memcmppgm2ram(buf, (char const rom far*)"STAT", 4) == 0)
    {
    p = par_get(PARAM_REGPHONE);
    if (strncmp(p,caller,strlen(p)) == 0)
      net_sms_stat(caller);
    else
      net_send_sms_rom(caller,NET_MSG_DENIED);
    }
  else if (memcmppgm2ram(buf, (char const rom far*)"PARAMS?", 7) == 0)
    {
    p = par_get(PARAM_REGPHONE);
    if (strncmp(p,caller,strlen(p)) == 0)
      net_sms_params(caller);
    else
      net_send_sms_rom(caller,NET_MSG_DENIED);
    }
  else if (memcmppgm2ram(buf, (char const rom far*)"PARAMS ", 7) == 0)
    {
    p = par_get(PARAM_REGPHONE);
    if (strncmp(p,caller,strlen(p)) == 0)
      {
      unsigned char d = PARAM_MILESKM;
      unsigned char x = 7;
      unsigned char y = x;
      while ((y<=(pos+1))&&(d < PARAM_MAX))
        {
        if ((buf[y] == ' ')||(buf[y] == '\0'))
          {
          buf[y] = '\0';
          if ((buf[x]=='-')&&(buf[x+1]=='\0'))
            buf[x] = '\0'; // Special case '-' is empty value
          par_set(d++, buf+x);
          x=++y;
          }
        else
          y++;
        }
      net_send_sms_rom(caller,NET_MSG_PARAMS);
      net_state_enter(NET_STATE_SOFTRESET);
      }
    else
      net_send_sms_rom(caller,NET_MSG_DENIED);
    }
  }