Example #1
0
int main(int argc, String argv[]) {
  /* Handle a plea for help */
  if (argc == 1) {
    printUsage(argv[0]);
    exit(1);
  }

  /* Parse xPL & program command line parms */
  if (!xPL_parseCommonArgs(&argc, argv, FALSE)) exit(1);
  if (!parseCmdLine(&argc, argv)) exit(1);

  /* Ensure we have a class */
  if ((msgSchemaClass == NULL) || (msgSchemaType == NULL)) {
    fprintf(stderr, "The -c schema class.type is REQUIRED\n");
    exit(1);
  }

  /* Start xPL up */
  if (!xPL_initialize(xPL_getParsedConnectionType())) {
    fprintf(stderr, "Unable to start xPL");
    exit(1);
  }

  /* Parse the source */
  if (!parseSourceIdent()) exit(1);

  /* Parse the target */
  if (!parseTargetIdent()) exit(1);

  /* Send the message */
  if (!sendMessage(argc, argv)) exit(1);
  return 0;
}
Example #2
0
void initXpl() {
 
 if (!xPL_initialize(xPL_getParsedConnectionType())) {
    fprintf(stderr, "Unable to start xPL");
    exit(1);
  } 

  /* Initialze clock service */

  /* Create  a service for us */
  pmaxdService = xPL_createService("visonic", "powermaxplus", "default");
  xPL_setServiceVersion(pmaxdService, PMAXD_VERSION);
  
  /* Add a responder for time setting */
  xPL_addServiceListener(pmaxdService, pmaxdMessageHandler, xPL_MESSAGE_COMMAND, "security", NULL, NULL);

  

  /* Install signal traps for proper shutdown */
  signal(SIGTERM, shutdownHandler);
  signal(SIGINT, shutdownHandler);

  /* Enable the service */
    xPL_setServiceEnabled(pmaxdService, TRUE);

}
Example #3
0
int main(void)
{
	char *data;
	long m,n;
	printf("%s%c%c\n",
	"Content-Type:text/html;charset=iso-8859-1",13,10);

//	printf("<h3>Multiplication results</h3>\n");

	data = getenv("QUERY_STRING");

  

	stripSpace(data);
	char schemaclass[500];
	char schematype[500];
	char namedvaluearray[500];
	char name[500];
	char value[500];
	char command[500];
	char varcontent[500];
  char jsoncontent[500];
  char NVelement[500];
//  printf("URL %s<BR>\n",data);
  strcpy(command,data);
  unencode(command);
  findVarInURL(varcontent,data,"xplpacket");
  unencode(varcontent); 
  stripSpace(varcontent);
	
  xPL_initialize(xPL_getParsedConnectionType());
  webgateway = xPL_createService("viknet", "webgateway", "default");  
  xPL_setServiceVersion(webgateway, "1.0");

  
  /* Add a responder for time setting */
  xPL_addServiceListener(webgateway, webgatewayMessageHandler, xPL_MESSAGE_ANY, "security", NULL, NULL);
  xPL_setServiceEnabled(webgateway, TRUE);
  /* Create a message to send */
  xplMessage = xPL_createBroadcastMessage(webgateway, xPL_MESSAGE_COMMAND);

  if (JSONfindObject(schemaclass,varcontent,"msgschemaclass")!=NULL && JSONfindObject(schematype,varcontent,"msgschematype")!=NULL)
    xPL_setSchema(xplMessage, JSONtoString(schemaclass), JSONtoString(schematype));
	else
    xPL_setSchema(xplMessage, "schemaclass", "schematype");	
	
	JSONfindObject(namedvaluearray,varcontent,"namevaluelist");
  int i;
   for (i=0;i<JSONArrayLength(namedvaluearray);i++)
   {
     JSONArrayAt(NVelement,namedvaluearray,i);
	   if ( JSONfindObject(name,NVelement,"name") && JSONfindObject(value,NVelement,"value"))
	   xPL_addMessageNamedValue(xplMessage, JSONtoString(name), JSONtoString(value));	   
   }
   xPL_sendMessage(xplMessage);
//   xPL_processMessages(0);
	return 0;  
}       
Example #4
0
int main(int argc, String argv[]) {

	/* Parse command line parms */
	if (!xPL_parseCommonArgs(&argc, argv, FALSE)) {
		exit(1);
	}

	/* Start xPL up */
	if (!xPL_initialize(xPL_getParsedConnectionType())) {
		fprintf(stderr, "Unable to start xPL");
		exit(1);
	}
	
	/* Parse Hub command arguments */
	if (!parseCmdLine(&argc, argv)) {
		printUsage(argv[0]);
		exit(1);
	}

	if (daemonMode) {
		switch (fork()) {
			case 0: /* child */
				/* No io in child mode */
				close(fileno(stdin));
				close(fileno(stdout));
				close(fileno(stderr));
				setpgrp();

				startServer();
				break;
			case -1: /* error */
				fprintf(stderr, "Unable to spawn daemon, %s (%d)\n", strerror(errno), errno);
				exit(1);
		}
	} else {
		startServer();
	}
	return 0;
}
Example #5
0
int main(void)
{
  pmaxSystem.status[0]=0;
  pmaxSystem.pmaxstatus[0]=0;
  pmaxSystem.readytoarm[0]=0;
  pmaxSystem.PmaxStatus[0]=0;
  
  int k;
  for (k=0;k<31;k++)
  {
    pmaxSystem.sensor[k].type[0]=0;
    pmaxSystem.sensor[k].alert[0]=0;
    pmaxSystem.sensor[k].PmaxAlarmType[0]=0;
    pmaxSystem.sensor[k].armed[0]=0;                  
    pmaxSystem.sensor[k].alarmed[0]=0;
    pmaxSystem.sensor[k].lowbattery[0]=0;  
    pmaxSystem.sensor[k].enrolled[0]=0;
    pmaxSystem.sensor[k].tampered[0]=0;
    pmaxSystem.sensor[k].id[0]=0;
  } 
  
//	char *data;
	long m,n;
	printf("%s%c%c\n" ,
	"Content-Type:text/html;charset=iso-8859-1",13,10);

	
  xPL_initialize(xPL_getParsedConnectionType());
  webgateway = xPL_createService("viknet", "webgateway", "default");  
  xPL_setServiceVersion(webgateway, "1.0");

  /* Add a responder for time setting */
  xPL_addServiceListener(webgateway, webgatewayMessageHandler, xPL_MESSAGE_ANY, "security", NULL, NULL);
  xPL_setServiceEnabled(webgateway, TRUE);
//  int j;
  
//  for (j=0;j<10;j++)
//  {
  /* Create a message to send */
  xplMessage = xPL_createBroadcastMessage(webgateway, xPL_MESSAGE_COMMAND);
  xPL_setSchema(xplMessage, "security", "request");
  xPL_setMessageNamedValue(xplMessage, "request", "zonelist");	
  xPL_sendMessage(xplMessage);
//  printf("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n");
  int i;
  
  
  xplMessage = xPL_createBroadcastMessage(webgateway, xPL_MESSAGE_COMMAND);
  xPL_setSchema(xplMessage, "security", "request");
  xPL_setMessageNamedValue(xplMessage, "request", "gatestat");	
  xPL_sendMessage(xplMessage);
  
  
  for (i=1;i<1000;i++)
  {
    usleep(1000);
    if (nbzone!=0) break;
    xPL_processMessages(0);
  }
  
  
  strcpy(tpzonelist,zonelist);
  char * element;
//  printf("yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n");
//  printf("zone: %d, %s\n",nbzone,zonelist);
//  for (i=0;i<nbzone;i++)  {
//  char * pch;
  element = strtok (tpzonelist,",");
   while (element != NULL)
  {
//    printf ("%s\n",element);
   
   // xplListAt(element,zonelist,i);
  //  sprintf(element,"%d",i);
  //  printf("element: %s\n",element);
    xplMessage = xPL_createBroadcastMessage(webgateway, xPL_MESSAGE_COMMAND);
    xPL_setSchema(xplMessage, "security", "request");
    xPL_setMessageNamedValue(xplMessage, "request", "zoneinfo");	
    xPL_setMessageNamedValue(xplMessage, "zone", element);	
    xPL_sendMessage(xplMessage);
    
    
    xplMessage = xPL_createBroadcastMessage(webgateway, xPL_MESSAGE_COMMAND);
    xPL_setSchema(xplMessage, "security", "request");
    xPL_setMessageNamedValue(xplMessage, "request", "zonestat");	
    xPL_setMessageNamedValue(xplMessage, "zone", element);	
    xPL_sendMessage(xplMessage);
    
    
    usleep(40000);
    xPL_processMessages(0);
    element = strtok (NULL, ",");  
  }
//  printf("zonelist: %s\n",zonelist);
  for (i=0;i<10;i++)
  { 
  usleep(15000);
  xPL_processMessages(0);
  }      
  printf("{ \"status\":\"%s\",\"pmaxstatus\":\"%s\",\"readytoarm\":\"%s\",\"sensor\":[",pmaxSystem.status,pmaxSystem.pmaxstatus,pmaxSystem.readytoarm);
 
// strcpy(tpzonelist,zonelist);
//  element = strtok (tpzonelist,",");
  i=0;
//  while (element != NULL)
  char tmpbuff[200];
  char buff[6000];
  buff[0]=0;
  for (i=0;i<31;i++)
  {
//    printf ("%s\n",element);
    if ( strcmp(pmaxSystem.sensor[i].id,"")!=0 ) { 
    sprintf(tmpbuff,"{\"zone\":\"%s\",\"type\":\"%s\",\"alert\":\"%s\",\"armed\":\"%s\",\"tamper\":\"%s\",\"low-battery\":\"%s\",\"alarm\":\"%s\"},",
    pmaxSystem.sensor[i].id,
    pmaxSystem.sensor[i].type,
    pmaxSystem.sensor[i].alert,
    pmaxSystem.sensor[i].armed,
    pmaxSystem.sensor[i].tampered,
    pmaxSystem.sensor[i].lowbattery,
    pmaxSystem.sensor[i].alarmed);
    strcat(buff,tmpbuff);
    //element = strtok (NULL, ",");
    //if (element!=NULL)
    
    //i++;
    }
  }
  buff[strlen(buff)-1]=0;
  printf("%s",buff);
  printf("]}"); 
  nbzone=0;
  zonelist[0]=0;

//}  
  
	return 0;  
}       
Example #6
0
int main(int argc, char *argv[])
{
	int longindex;
	int optchar;
	String p;
	KeyEntryPtr_t e;
	zoneMapPtr_t zm;

	/* Set the program name */
	progName=argv[0];

	/* Parse the arguments. */
	while((optchar=getopt_long(argc, argv, SHORT_OPTIONS, longOptions, &longindex)) != EOF) {
		
		/* Handle each argument. */
		switch(optchar) {
			
			/* Was it a long option? */
			case 0:
				
				/* Hrmm, something we don't know about? */
				fatal("Unhandled long getopt option '%s'", longOptions[longindex].name);
			
			/* If it was an error, exit right here. */
			case '?':
				exit(1);

			/* Was it a config file path ? */
			case 'c':
				confreadStringCopy(configFile, optarg, WS_SIZE - 1);
				debug(DEBUG_ACTION,"New config file path is: %s", configFile);
				break;
		

		
			/* Was it a debug level set? */
			case 'd':

				/* Save the value. */
				debugLvl=atoi(optarg);
				if(debugLvl < 0 || debugLvl > DEBUG_MAX) {
					fatal("Invalid debug level");
				}

				break;

			/* Was it a pid file switch? */
			case 'f':
				confreadStringCopy(pidFile, optarg, WS_SIZE - 1);
				debug(DEBUG_ACTION,"New pid file path is: %s", pidFile);
				configOverride |= CO_PID_FILE;
				break;

			
			/* Was it a help request? */
			case 'h':
				showHelp();
				exit(0);

			/* Specify interface to broadcast on */
			case 'i': 
				confreadStringCopy(interface, optarg, WS_SIZE -1);
				xPL_setBroadcastInterface(interface);
				configOverride |= CO_INTERFACE;

				break;

			case 'u':
				/* Override debug path*/
				confreadStringCopy(debugFile, optarg, WS_SIZE - 1);
				debug(DEBUG_ACTION,"New debug path is: %s", debugFile);
				configOverride |= CO_DEBUG_FILE;
				break;

			/* Was it a no-backgrounding request? */
			case 'n':

				/* Mark that we shouldn't background. */
				noBackground = TRUE;

				break;
			case 'p':
				/* Override com port*/
				confreadStringCopy(comPort, optarg, WS_SIZE - 1);
				debug(DEBUG_ACTION,"New com port is: %s", comPort);
				configOverride |= CO_COM_PORT;
				break;

			/* Was it an instance ID ? */
			case 's':
				confreadStringCopy(instanceID, optarg, WS_SIZE);
				debug(DEBUG_ACTION,"New instance ID is: %s", instanceID);
				configOverride |= CO_INSTANCE_ID;
				break;


			/* Was it a version request? */
			case 'v':
				printf("Version: %s\n", VERSION);
				exit(0);
	

			
			/* It was something weird.. */
			default:
				fatal("Unhandled getopt return value %d", optchar);
		}
	}
	
	/* If there were any extra arguments, we should complain. */

	if(optind < argc) {
		fatal("Extra argument on commandline, '%s'", argv[optind]);
	}

	/* Load the config file */
	if(!(configEntry =confreadScan(configFile, NULL)))
		exit(1);

	/* Parse the general section */

	/* Com port */
	if(!(configOverride & CO_COM_PORT)){
		if((p = confreadValueBySectKey(configEntry, "general", "com-port"))){
			confreadStringCopy(comPort, p, WS_SIZE);
		}	
	}

	/* Debug file */
	if(!(configOverride & CO_DEBUG_FILE)){
		if((p = confreadValueBySectKey(configEntry, "general", "debug-file"))){
			confreadStringCopy(debugFile, p, WS_SIZE);
		}
	
	}

	/* PID file */
	if(!(configOverride & CO_PID_FILE)){
		if((p = confreadValueBySectKey(configEntry, "general", "pid-file"))){
			confreadStringCopy(pidFile, p, WS_SIZE);
		}
	
	}

	/* Instance ID */
	if(!(configOverride & CO_INSTANCE_ID)){
		if((p =  confreadValueBySectKey(configEntry, "general", "instance-id"))){
			confreadStringCopy(instanceID, p, WS_SIZE);
		}	
	}


	/* Interface */
	if(!(configOverride & CO_INTERFACE)){
		if((p = confreadValueBySectKey(configEntry, "general", "interface"))){
			confreadStringCopy(interface, p, WS_SIZE);
		}	
	}

	/* Build Zone Map */
	
	if(!(e = confreadGetFirstKeyBySection(configEntry, "zone-map")))
		fatal("A valid zone-map section and at least one entry must be defined in the config file");
	for(; e; e = confreadGetNextKey(e)){
		String plist[3];
		const String key = confreadGetKey(e);
		const String value = confreadGetValue(e);
		/* Allocate a zone struct */
		if(!(zm = mallocz(sizeof(zoneMap_t))))
			MALLOC_ERROR;
			
		/* Get the zone number */
		if(!str2uns(key, &zm->zone_num, 1, 99))
			syntax_error(e, configFile,"invalid zone number");
			
		/* Get the parameters */
		if(3 != splitString(value, plist, ',', 3))
			syntax_error(e, configFile, "3 parameters required");
		if(!(zm->zone_name = strdup(plist[0])))
			MALLOC_ERROR;
		if(!(zm->zone_type = strdup(plist[1])))
			MALLOC_ERROR;
		if(!(zm->alarm_type = strdup(plist[2])))
			MALLOC_ERROR;
			
		/* Hash the zone name */
		zm->zone_name_hash = confreadHash(zm->zone_name);
		
			
		/* Free the split string */
		free(plist[0]);
		
		/* Insert the entry into the zone list */
		if(!zoneMapHead)
			zoneMapHead = zoneMapTail = zm;
		else{
			zm->prev = zoneMapTail;
			zoneMapTail->next = zm;
			zoneMapTail = zm;
		}
		zoneCount++;
	}

	/* EXP zone mapping */

	for(e =  confreadGetFirstKeyBySection(configEntry, "exp-map"); e; e = confreadGetNextKey(e)){
		expMapPtr_t emp;
		const String keyString = confreadGetKey(e);
		const String zone = confreadGetValue(e);
		String plist[3];
		unsigned expaddr, expchannel;

		/* Check the key and zone strings */
		if(!(keyString) || (!zone))
			syntax_error(e, configFile, "key or zone missing");


		/* Split the address and channel */
		plist[0] = NULL;
		if(2 != splitString(keyString, plist, ',', 2))
			syntax_error(e, configFile, "left hand side needs 2 numbers separated by a comma");

		/* Convert and check address */
		if(!str2uns(plist[0], &expaddr, 1, 99))
			syntax_error(e, configFile,"address is limited from 1 - 99");


		/* Convert and check channel */
		if(!str2uns(plist[1], &expchannel, 1, 99))
			syntax_error(e, configFile,"channel is limited from 1 - 99");
			

		/* debug(DEBUG_ACTION, "Address: %u, channel: %u, zone: %s", expaddr, expchannel, zone); */
		
		/* Look up zone to ensure it is defined */
		
		if(!(zm = zoneLookup(zone)))
			syntax_error(e, configFile, "Zone must be defined in zone-map section");
		

		/* Get memory for entry */
		if(!(emp = mallocz(sizeof(expMap_t))))
			MALLOC_ERROR;

		/* Initialize entry */
		emp->zone_entry = zm;
		emp->addr = expaddr;
		emp->channel = expchannel;
		if(!(emp->zone = strdup(zone)))
			MALLOC_ERROR;

		/* Insert into list */
		if(!expMapHead){
			expMapHead = expMapTail = emp;
		}
		else{
			expMapTail->next = emp;
			emp->prev = expMapTail;
			expMapTail = emp;
		}

		/* Free parameter string */
		if(plist[0])
			free(plist[0]);
	}


	/* Turn on library debugging for level 5 */
	if(debugLvl >= 5)
		xPL_setDebugging(TRUE);

 	/* Make sure we are not already running (.pid file check). */
	if(pid_read(pidFile) != -1) {
		fatal("%s is already running", progName);
	}

	/* Check to see the serial device exists before we fork */
	if(!serio_check_node(comPort))
		fatal("Serial device %s does not exist or its permissions are not allowing it to be used.", comPort);

	/* Fork into the background. */

	if(!noBackground) {
		int retval;
		debug(DEBUG_STATUS, "Forking into background");

    		/* 
		* If debugging is enabled, and we are daemonized, redirect the debug output to a log file if
    		* the path to the logfile is defined
		*/

		if((debugLvl) && (debugFile[0]))                          
			notify_logpath(debugFile);

		/* Fork and exit the parent */

		if((retval = fork())){
      			if(retval > 0)
				exit(0);  /* Exit parent */
			else
				fatal_with_reason(errno, "parent fork");
    		}



		/*
		* The child creates a new session leader
		* This divorces us from the controlling TTY
		*/

		if(setsid() == -1)
			fatal_with_reason(errno, "creating session leader with setsid");


		/*
		* Fork and exit the session leader, this prohibits
		* reattachment of a controlling TTY.
		*/

		if((retval = fork())){
			if(retval > 0)
        			exit(0); /* exit session leader */
			else
				fatal_with_reason(errno, "session leader fork");
		}

		/* 
		* Change to the root of all file systems to
		* prevent mount/unmount problems.
		*/

		if(chdir("/"))
			fatal_with_reason(errno, "chdir to /");

		/* set the desired umask bits */

		umask(022);
		
		/* Close STDIN, STDOUT, and STDERR */

		close(0);
		close(1);
		close(2);
		} 

	/* Start xPL up */
	if (!xPL_initialize(xPL_getParsedConnectionType())) {
		fatal("Unable to start xPL lib");
	}

	/* Initialize xplrcs service */

	/* Create a service and set our application version */
	xplService = xPL_createService("hwstar", "xplademco", instanceID);
  	xPL_setServiceVersion(xplService, VERSION);

	/*
	* Create a status message object
	*/

  	xplStatusMessage = xPL_createBroadcastMessage(xplService, xPL_MESSAGE_STATUS);
  
	/*
	* Create trigger message objects
	*/

	/* security.gateway */
	if(!(xplEventTriggerMessage = xPL_createBroadcastMessage(xplService, xPL_MESSAGE_TRIGGER)))
		fatal("Could not initialize security.gateway trigger");
	xPL_setSchema(xplEventTriggerMessage, "security", "gateway");

	/* security.zone */
	if(!(xplZoneTriggerMessage = xPL_createBroadcastMessage(xplService, xPL_MESSAGE_TRIGGER)))
		fatal("Could not initialize security.zone trigger");
	xPL_setSchema(xplZoneTriggerMessage, "security", "zone");


  	/* Install signal traps for proper shutdown */
 	signal(SIGTERM, shutdownHandler);
 	signal(SIGINT, shutdownHandler);

	/* Initialize the COM port */
	
	if(!(serioStuff = serio_open(comPort, COM_BAUD_RATE)))
		fatal("Could not open com port: %s", comPort);


	/* Flush any partial commands */
	serio_printf(serioStuff, "\r");
	usleep(100000);
	serio_flush_input(serioStuff);

	/* Ask xPL to monitor our serial device */
	if(xPL_addIODevice(serioHandler, 1234, serio_fd(serioStuff), TRUE, FALSE, FALSE) == FALSE)
		fatal("Could not register serial I/O fd with xPL");

	/* Add 1 second tick service */
	xPL_addTimeoutHandler(tickHandler, 1, NULL);

  	/* And a listener for all xPL messages */
  	xPL_addMessageListener(xPLListener, NULL);


 	/* Enable the service */
  	xPL_setServiceEnabled(xplService, TRUE);

	/* Update pid file */
	if(pid_write(pidFile, getpid()) != 0) {
		debug(DEBUG_UNEXPECTED, "Could not write pid file '%s'.", pidFile);
	}




 	/** Main Loop **/

	for (;;) {
		/* Let XPL run forever */
		xPL_processMessages(-1);
  	}

	exit(1);
}