Ejemplo n.º 1
0
GSM_Error GSM_WaitForOnce(GSM_StateMachine *s, unsigned const char *buffer,
			  int length, int type, int timeout)
{
	GSM_Phone_Data *Phone = &s->Phone.Data;
	GSM_Protocol_Message sentmsg;
	int i = 0;

	do {
		if (length != 0) {
			sentmsg.Length 	= length;
			sentmsg.Type	= type;
			sentmsg.Buffer 	= (unsigned char *)malloc(length);
			memcpy(sentmsg.Buffer,buffer,length);
			Phone->SentMsg  = &sentmsg;
		}

		/* Some data received. Reset timer */
		if (GSM_ReadDevice(s, TRUE) > 0) {
			i = 0;
		} else {
			if (s->Abort) {
				return ERR_ABORTED;
			}
			usleep(10000);
		}

		if (length != 0) {
			free(sentmsg.Buffer);
			sentmsg.Buffer = NULL;
			Phone->SentMsg = NULL;
		}

		/* Request completed */
		if (Phone->RequestID==ID_None) {
			return Phone->DispatchError;
		}
		i++;
	} while (i<timeout);

	return ERR_TIMEOUT;
}
Ejemplo n.º 2
0
/**
 * @name action_send_messages:
 */
int action_send_messages(gammu_state_t **sp,
                         int argc, char *argv[]) {

  int rv = 0;
  char **argp = &argv[1];

  if (argc <= 2) {
    print_usage_error(U_ERR_ARGS_MISSING);
    return 1;
  }

  if (argc % 2 != 1) {
    print_usage_error(U_ERR_ARGS_ODD);
    return 2;
  }

  /* Lazy initialization of libgammu */
  gammu_state_t *s = gammu_create_if_necessary(sp);

  if (!s) {
    print_operation_error(OP_ERR_INIT);
    rv = 3; goto cleanup;
  }

  /* Allocate */
  smsc_t *smsc = allocate(sizeof(*smsc));
  multimessage_t *sms = allocate(sizeof(*sms));
  multimessage_info_t *info = allocate(sizeof(*info));

  /* Find SMSC number */
  smsc->Location = 1;

  if ((s->err = GSM_GetSMSC(s->sm, smsc)) != ERR_NONE) {
    print_operation_error(OP_ERR_SMSC);
    rv = 4; goto cleanup_sms;
  }

  transmit_status_t status;
  initialize_transmit_status(&status);

  GSM_SetSendSMSStatusCallback(
    s->sm, _message_transmit_callback, &status
  );

  boolean_t is_start = TRUE;
  unsigned int message_index = 0;

  printf("[");

  /* For each message... */
  while (*argp != NULL) {

    GSM_ClearMultiPartSMSInfo(info);
    GSM_Debug_Info *debug = GSM_GetGlobalDebug();

    /* Copy/convert destination phone number */
    char *sms_destination_number = convert_utf8_utf16be(*argp++, FALSE);

    if (!sms_destination_number) {
      status.err = "Invalid UTF-8 sequence in destination number";
      goto cleanup_end;
    }

    string_info_t nsi;
    utf16be_string_info(sms_destination_number, &nsi);

    /* Check size of phone number:
        We'll be decoding this in to a fixed-sized buffer. */

    if (nsi.units >= GSM_MAX_NUMBER_LENGTH) {
      status.err = "Phone number is too long";
      goto cleanup_transmit_status;
    }

    /* Missing message text:
        This shouldn't happen since we check `argc` above,
        but I'm leaving this here in case we refactor later. */

    if (*argp == NULL) {
      status.err = "No message body provided";
      goto cleanup_transmit_status;
    }

    /* UTF-8 message content */
    char *sms_message = *argp++;

    /* Convert message from UTF-8 to UTF-16-BE:
        Every symbol is two bytes long; the string is then
        terminated by a single 2-byte UTF-16 null character. */

    char *sms_message_utf16be = convert_utf8_utf16be(sms_message, FALSE);

    if (!sms_message_utf16be) {
      status.err = "Invalid UTF-8 sequence";
      goto cleanup_transmit_status;
    }

    /* Prepare message info structure:
        This information is used to encode the possibly-multipart SMS. */

    info->Class = 1;
    info->EntriesNum = 1;
    info->Entries[0].ID = SMS_ConcatenatedTextLong;
    info->Entries[0].Buffer = (uint8_t *) sms_message_utf16be;
    info->UnicodeCoding = !utf16be_is_gsm_string(sms_message_utf16be);

    if ((s->err = GSM_EncodeMultiPartSMS(debug, info, sms)) != ERR_NONE) {
      status.err = "Failed to encode message";
      goto cleanup_sms_text;
    }

    status.parts_sent = 0;
    status.parts_total = sms->Number;

    /* For each SMS part... */
    for (unsigned int i = 0; i < sms->Number; i++) {

      status.finished = FALSE;
      status.message_part_index = i;

      sms->SMS[i].PDU = SMS_Submit;

      /* Copy destination phone number:
           This is a fixed-size buffer; size was already checked above. */

      CopyUnicodeString(sms->SMS[i].SMSC.Number, smsc->Number);

      CopyUnicodeString(
        sms->SMS[i].Number, (unsigned char *) sms_destination_number
      );

      /* Transmit a single message part */
      if ((s->err = GSM_SendSMS(s->sm, &sms->SMS[i])) != ERR_NONE) {
        status.parts[i].err = "Message transmission failed";
        continue;
      }

      for (;;) {
        /* Wait for reply */
        GSM_ReadDevice(s->sm, TRUE);

        if (status.finished) {
          break;
        }
      }

      if (!status.parts[i].transmitted) {
        status.parts[i].err = "Message delivery failed";
        continue;
      }

      status.parts_sent++;
    }

    cleanup_sms_text:
      status.message_index = ++message_index;
      free(sms_message_utf16be);

    cleanup_transmit_status:
      print_json_transmit_status(s, sms, &status, is_start);
      free(sms_destination_number);

    cleanup_end:
      is_start = FALSE;
  }

  cleanup_sms:

    free(sms);
    free(smsc);
    free(info);
    printf("]\n");
  
  cleanup:

    return rv;
}
Ejemplo n.º 3
0
void DCT3SetDebug(int argc, char *argv[])
{
	int		x,count;
	unsigned int y;
	unsigned char 	reqDisable[] = {0x01, 0x01, 0x71};
	GSM_Error error;
//	unsigned char 	reqTest[] = {0x01, 0x01, 0x96, 0xFF, 0xFF};

	/* RPC testing packets: */

	/* RPC: Get version */
	//unsigned char 	reqTest2[] = {0x01, 0x01, 0x00, 0x03, 0x00};
	/* RPC: read I/O 0x6D mask 0xFF */
	//unsigned char 	reqTest2[] = {0x01, 0x01, 0x02, 0x01, 0x02, 0x6D, 0xFF}; /*  */
	/* RPC: write I/O 0x03 mask 0xFF value 0x31 */
	//unsigned char 	reqTest2[] = {0x01, 0x01, 0x01, 0x01, 0x07, 0x03, 0xFF, 0x31}; /* write I/O */

	/* RPC: write forged FBUS packet to MDISND */
//	unsigned char 	reqTest2[] = {0x01, 0x01, 0x16, 0x01, 0x06,
//	0x14, // R0 -- length
//	0x05, // R1 -- MDI type identifier 0x05(FBUS)
//	0x1e, 0x0c, 0x00, 0x66,
//	0x00, 0x0e, 0x01, 0x01,
//	0x66, 0x55, 0x44, 0x33,
//	0x0d, 0x01, 0x01, 0x01,
//	0x1b, 0x58, 0x01, 0x44};
//	1805 t=cb37 nr=e2 :D 05:

	/* debug enable packet */
	unsigned char reqEnable[] = {
		0x00, 0x01, 0x70,
		/* Debug bits
		   byte[bit>>3]&(1<<(7-(bit&7)))
		*/
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x00 */
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x40 */
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x80 */
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xC0 */
		/* Debug verbose bits
		   byte[bit>>3]&(1<<(7-(bit&7)))
		*/
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		};
	FILE *xout;

	#define ENABLE_BIT(bit,verbose) reqEnable[3 + (bit>>3)] |= 1<<(7-(bit&7)); if(verbose){reqEnable[3 + 32 + (bit>>3)] |= 1<<(7-(bit&7));}

	/* Enable some bit
	   TODO command line or GUI interface
	*/
	//ENABLE_BIT(0x18, 1);	/* Enable MDISND debugging */
	//ENABLE_BIT(0x19, 1);	/* Enable MDIRCV debugging */
	//ENABLE_BIT(0x31, 1);

	gsmdec = GSMDecoder_new();
	/* Open XML file .. needs to be argument */
	xout = fopen("out.xml", "w");
	if (xout == NULL) {
		return;
	}
	GSMDecoder_xmlout(gsmdec, xout);

	printf("Debug Trace Mode -- wumpus 2003\n");
	traces = wmx_tracestruct_load(argv[2]);
	if(traces == NULL)
		printf("Warning: could not load trace description file %s\n", argv[2]);
	printf("Activating ranges:\n");
	count = 0;
	for(x=3; x<argc; x++) {
		char 		*ptr = argv[x];
		unsigned 	from,to,verbose;

		while(*ptr) {
		 	verbose = 0;
			if(*ptr == 'v') {
				verbose = 1;
				ptr++;
			}
			to = from = strtol(ptr, &ptr, 16);
			if(*ptr == '-') {
				ptr ++;
				to = strtol(ptr, &ptr, 16);
			}
			if(*ptr != ',' && *ptr != 0) {
				printf("Invalid parameter '%s'\n", argv[x]);
				return;
			}
			if(*ptr == ',')
				ptr++;
			if(from > 0xFF) from=0xFF;
			if(to > 0xFF) to=0xFF;
			printf("  %02x-%02x verbose=%i\n",from,to,verbose);
			for(y=from; y<=to; y++) {
				ENABLE_BIT(y, verbose);
				count++;
			}
		}
	}
	if(count == 0) {
		printf("Nothing activated -- bailing out\n");
		return;
	}
	//ENABLE_BIT(0x20, 1);  /* SIM commands (literal) */
	//ENABLE_BIT(0x21, 1);  /* SIML2 commands (literal) */
	//ENABLE_BIT(0x22, 1);  /* SIM commands (literal) */
	//ENABLE_BIT(0x3B, 1);	/* PHCTRL state */

	GSM_Init(TRUE);

	/* We Need DCT3 */
	CheckDCT3();

	error=DCT3_EnableSecurity (gsm, 0x01);
	Print_Error(error);

	gsm->User.UserReplyFunctions=UserReplyFunctionsX;

	//error=GSM_WaitFor (gsm, reqTest, sizeof(reqTest), 0x40, 1, ID_DebugSwitch);

	//error=GSM_WaitFor (gsm, reqTest2, sizeof(reqTest2), 0xD1, 4, ID_RPC);

	/* Enable Debug Mode */
	error=GSM_WaitFor (gsm, reqEnable, sizeof(reqEnable), 0x40, 4, ID_DebugSwitch);

	Print_Error(error);
	signal(SIGINT, interrupt);
	printf("Press Ctrl+C to interrupt...\n");
	x=0;

	/*
	while(x<100) {
		//printf(": %02x\n",x);
		gsm->Phone.Data.RequestID	= ID_DebugTrace;
		res = gsm->Device.Functions->ReadDevice(gsm, buff, 255);
		if(res) {
			printf("%02x\n",x);
			for(y=0;y<res;y++) {
				//printf("%02x\n",x,buff[y]&0xFF);
				gsm->Protocol.Functions->StateMachine(gsm,buff[y]);
				x++;
			}
		}
	}
	*/
	;

	/* todo: wait and dump for some time */
	while (!gshutdown) {
		GSM_ReadDevice(gsm,TRUE);
		usleep(10000);
	}
	signal(SIGINT, SIG_DFL);
	printf("Disabling\n");
	error=GSM_WaitFor (gsm, reqDisable, sizeof(reqDisable), 0x40, 10, ID_DebugSwitch);
	Print_Error(error);

	GSMDecoder_free(gsmdec);
}