Esempio n. 1
0
void reinitialize_mdm(void)
{
    // Initialize the modem
    printf(GRN "Modem RE-initializing..." DEF "\r\n");
    if (!mdm_init()) {
        printf(RED "\n\rModem RE-initialization failed!" DEF "\n");
    }
    printf("\r\n");
}
Esempio n. 2
0
int main() {
    int i;
    HTS221 hts221;
    pc.baud(115200);
    
    void hts221_init(void);

    // Set LED to RED until init finishes
    SetLedColor(0x1);

    pc.printf(BLU "Hello World from AT&T Shape!\r\n\n\r");
    pc.printf(GRN "Initialize the HTS221\n\r");

    i = hts221.begin();  
    if( i ) 
        pc.printf(BLU "HTS221 Detected! (0x%02X)\n\r",i);
    else
        pc.printf(RED "HTS221 NOT DETECTED!!\n\r");

    printf("Temp  is: %0.2f F \n\r",CTOF(hts221.readTemperature()));
    printf("Humid is: %02d %%\n\r",hts221.readHumidity());
    
    sensors_init();
    read_sensors();

    // Initialize the modem
    printf(GRN "Modem initializing... will take up to 60 seconds" DEF "\r\n");
    do {
        i=mdm_init();
        if (!i) {
            pc.printf(RED "Modem initialization failed!" DEF "\n");
        }
    } while (!i);
    
    //Software init
    software_init_mdm();
 
    // Resolve URL to IP address to connect to
    resolve_mdm();

    //Create a 1ms timer tick function:
    OneMsTicker.attach(OneMsFunction, 0.001f) ;

    iTimer1Interval_ms = SENSOR_UPDATE_INTERVAL_MS;

    // Open the socket (connect to the server)
    sockopen_mdm();

    // Set LED BLUE for partial init
    SetLedColor(0x4);

    // Send and receive data perpetually
    while(1) {
        static unsigned ledOnce = 0;
        if  (bTimerExpiredFlag)
        {
            bTimerExpiredFlag = false;
            sprintf(SENSOR_DATA.Temperature, "%0.2f", CTOF(hts221.readTemperature()));
            sprintf(SENSOR_DATA.Humidity, "%02d", hts221.readHumidity());
            read_sensors(); //read available external sensors from a PMOD and the on-board motion sensor
            char modem_string[512];
            GenerateModemString(&modem_string[0]);
            printf(BLU "Sending to modem : %s" DEF "\n", modem_string); 
            sockwrite_mdm(modem_string);
            sockread_mdm(&MySocketData, 1024, 20);
            
            // If any non-zero response from server, make it GREEN one-time
            //  then the actual FLOW responses will set the color.
            if ((!ledOnce) && (MySocketData.length() > 0))
            {
                ledOnce = 1;
                SetLedColor(0x2);
            }
            
            printf(BLU "Read back : %s" DEF "\n", &MySocketData[0]);
            char * myJsonResponse;
            if (extract_JSON(&MySocketData[0], &myJsonResponse[0]))
            {
                printf(GRN "JSON : %s" DEF "\n", &myJsonResponse[0]);
                parse_JSON(&myJsonResponse[0]);
            }
            else
            {
                printf(RED "JSON : %s" DEF "\n", &myJsonResponse[0]); //most likely an incomplete JSON string
                parse_JSON(&myJsonResponse[0]); //This is risky, as the string may be corrupted
            }
        } //bTimerExpiredFlag
    } //forever loop
}
Esempio n. 3
0
int main(int argc, char *argv[]) {
  modem_config cfg[64];
  int modem_count;
  int port=0;

  char *ip_addr = NULL; /* gwb */

  unsigned char all_busy[255];

  pthread_t thread_id;
  int i;
  int rc;

  int sSocket = 0;
  fd_set readfs; 
  int max_fd=0;
  int accept_pending=FALSE;

  int res=0;
  unsigned char buf[255];

  int cSocket;

  log_init();

  LOG_ENTER();

  log_set_level(LOG_FATAL);

  mdm_init();

  pb_init();
  
  signal(SIGIO,SIG_IGN); /* Some Linux variant term on SIGIO by default */

  modem_count = init(argc, argv, cfg, 64, &ip_addr, &port,all_busy,sizeof(all_busy)); /* gwb */

  sSocket = ip_init_server_conn(ip_addr, port);


  for(i=0;i<modem_count;i++) {
    if( -1 == pipe(cfg[i].data.mp[0])) {
      ELOG(LOG_FATAL,"Bridge task incoming IPC pipe could not be created");
      exit(-1);
    }
    if( -1 == pipe(cfg[i].data.mp[1])) {
      ELOG(LOG_FATAL,"Bridge task outgoing IPC pipe could not be created");
      exit(-1);
    }
    if(dce_init_conn(&cfg[i]) < 0) {
      LOG(LOG_FATAL,"Could not open serial port %s",cfg->dce_data.tty);
      exit(-1);
    }
    cfg[i].line_data.sfd=sSocket;

    rc=pthread_create(&thread_id,NULL,*run_bridge,(void *)&cfg[i]);
    if(rc < 0) {
        ELOG(LOG_FATAL,"IP thread could not be started");
        exit(-1);
    }

  }
  for(;;) {
    FD_ZERO(&readfs);
    max_fd=0;
    for(i=0;i<modem_count;i++) {
      FD_SET(cfg[i].data.mp[0][0], &readfs); 
      max_fd=MAX(max_fd,cfg[i].data.mp[0][0]);
    }
    if(accept_pending==FALSE) {
      max_fd=MAX(max_fd,sSocket);
      FD_SET(sSocket, &readfs); 
    }
    LOG(LOG_ALL,"Waiting for incoming connections and/or indicators");
    select(max_fd+1, &readfs, NULL, NULL, NULL);
    for(i=0;i<modem_count;i++) {
      if (FD_ISSET(cfg[i].data.mp[0][0],&readfs)) {  // child pipe
        res = read(cfg[i].data.mp[0][0],buf,sizeof(buf) -1);
        if(res > -1) {
          buf[res]=0;
          LOG(LOG_DEBUG,"modem core #%d sent response '%c'",i,buf[0]);
          accept_pending=FALSE;
        }
      }
    }
    if (FD_ISSET(sSocket,&readfs)) {  // IP traffic
      if(!accept_pending) {
        LOG(LOG_DEBUG,"Incoming connection pending");
        // first try for a modem that is listening.
        for(i=0;i<modem_count;i++) {
          if(cfg[i].s[0] != 0 && cfg[i].off_hook == FALSE) {
            // send signal to pipe saying pick up...
            LOG(LOG_DEBUG,"Sending incoming connection to listening modem #%d",i);
            writePipe(cfg[i].data.mp[1][1],MSG_ACCEPT);  
            accept_pending=TRUE;
            break;
          }
        }
        // now, send to any non-active modem.
        for(i=0;i<modem_count;i++) {
          if(cfg[i].off_hook== FALSE) {
            // send signal to pipe saying pick up...
            LOG(LOG_DEBUG,"Sending incoming connection to non-connected modem #%d",i);
            writePipe(cfg[i].data.mp[1][1],MSG_ACCEPT);  
            accept_pending=TRUE;
            break;
          }
        }
        if(i==modem_count) {
          LOG(LOG_DEBUG,"No open modem to send to, send notice and close");
          // no connections.., accept and print error
          cSocket=ip_accept(sSocket);
          if(cSocket > -1) {
            if(strlen(all_busy) < 1) {
              ip_write(cSocket,(unsigned char*)MDM_BUSY,strlen(MDM_BUSY));
            } else {
              writeFile(all_busy,cSocket);
            }
            close(cSocket);
          }
        }
      }
    }
  }
  LOG_EXIT();
  return rc;
}
/*!
 * Main entry point.
 */
int
main(int argc, char *argv[])
{
	int retcode = EXIT_SUCCESS;
	mdm_device_t device;
	const char *cmd_name;
	int i = 0;
	mdm_operation_result_t status;
	mdm_device_dslam_zte_8426_options_t options;
	int cmd;

	/* Check command line arguments. */
	if(argc < 2)
	{
		fprintf(stderr, "Use: %s <list | cmd args...>\n", argv[0]);
		exit(EXIT_FAILURE);
	}

	/* Global MDM init. */
	mdm_init(LOGLEVEL, LOGSYSLOG, LOGFILENAME, LOGFULL, LOGPATH);
	mdm_device_new(&device, MDM_DEVICE_DSLAM_ZTE_8426, &status);
	if(status.status == MDM_OP_ERROR)
	{
		fprintf(stderr, "Error: %s\n", status.status_message);
		retcode = 254;
		goto done;
	}
	
	/* See what we got in the arguments. */
	if(strcmp(argv[1], "list") == 0)
	{
		fprintf(stdout, "Start.\n");
		while((
			cmd_name = mdm_devicecmd_2stringname(MDM_DEVICE_DSLAM_ZTE_8426, i
		)) != NULL)
		{
			fprintf(
				stdout, "(%.2d) - !!%s!!%s!!\n",
				i, cmd_name, mdm_devicecmd_2string(MDM_DEVICE_DSLAM_ZTE_8426, i)
			);
			i++;
		}	
		goto done;
	} else {
		if(argc < 8)
		{
			fprintf(
				stderr,
				"Use: %s contype conn_to read_to target user pass cmd cmdargs\n",
				argv[0]
			);
			retcode = 254;
			goto done;
		}
		fprintf(stdout, "Start.\n");
		cmd = strtol(argv[7], NULL, 10);
		if(!mdm_devicecmd_isvalid(MDM_DEVICE_DSLAM_ZTE_8426, cmd))
		{
			fprintf(stderr, "Invalid command.\n");
			retcode = 254;
			goto done;
		}
		snprintf(options.target, sizeof(options.target), "%s", argv[4]);
		snprintf(options.username, sizeof(options.username), "%s", argv[5]);
		snprintf(options.password, sizeof(options.password), "%s", argv[6]);
		options.to_connect = strtol(argv[2], NULL, 10);
		options.to_recv = strtol(argv[3], NULL, 10);

		mdm_device_open(
			&device, strtol(argv[1], NULL, 10),
			(mdm_device_options_t)&options,
			sizeof(mdm_device_dslam_zte_8426_options_t), 
			&status
		);
		if(status.status == MDM_OP_ERROR)
		{
			fprintf(stderr, "Error: %s\n", status.status_message);
			retcode = 254;
			goto done;
		}
		mdm_device_exec(&device, cmd, argc == 9 ? argv[8] : NULL, &status);
		if(status.status == MDM_OP_ERROR)
		{
			fprintf(stderr, "App Error: %s\n", status.status_message);
			retcode = 254;
		}
		if(mdm_device_get_buffer_post_len(&device) > 0)
		{
			fprintf(
				stdout, "%.*s\n",
				mdm_device_get_buffer_post_len(&device), 
				mdm_device_get_buffer_post(&device)
			);
		}
		if(mdm_device_get_buffer_len(&device) > 0)
		{
			fprintf(
				stdout, "Original Response:|%.*s|\n",
				mdm_device_get_buffer_len(&device), 
				mdm_device_get_buffer(&device)
			);
		}
		mdm_device_close(&device, &status);
	}
done:
	mdm_device_free(&device);
	/* Global MDM cleanup. */
	mdm_cleanup();
	fprintf(stdout, "Done.\n");
	exit(retcode);
}