Exemple #1
0
void lightingRequestHandler(xPL_ServicePtr theService, xPL_MessagePtr theMessage, xPL_ObjectPtr userValue) {

	if (!xPL_doesMessageNamedValueExist(theMessage, "request")) {
		return;
	}

	if (xPL_strcmpIgnoreCase(xPL_getMessageNamedValue(theMessage, "request"), "devinfo") == 0) {
		sendDevInfo(theMessage);
	} else if (xPL_strcmpIgnoreCase(xPL_getMessageNamedValue(theMessage, "request"), "devlist") == 0) {
		sendDevList(theMessage);
	} else if (xPL_strcmpIgnoreCase(xPL_getMessageNamedValue(theMessage, "request"), "gateinfo") == 0) {
		sendGatewayInfo();
	} else if (xPL_strcmpIgnoreCase(xPL_getMessageNamedValue(theMessage, "request"), "netinfo") == 0) {
		sendNetInfo(theMessage);
	} else if (xPL_strcmpIgnoreCase(xPL_getMessageNamedValue(theMessage, "request"), "netlist") == 0) {
		sendNetList();
	} else {
/* 		fprintf(stdout, "Request: %s\n", xPL_getMessageNamedValue(theMessage, "request")); */
	}
}
Exemple #2
0
/* Return true if the passed message matches the passed filter */
static Bool doesFilterMatch(xPL_ServiceFilterPtr theFilter, xPL_MessagePtr theMessage) {
  /* If there is a message type filter, skip out if it does not match the message */
  if ((theFilter->matchOnMessageType != xPL_MESSAGE_ANY) && (theFilter->matchOnMessageType != theMessage->messageType)) return FALSE;

  /* If there is a vendor filter and it doesn't match the message, skip the message */
  if ((theFilter->matchOnVendor != NULL) && (xPL_strcmpIgnoreCase(theFilter->matchOnVendor, theMessage->sourceVendor) != 0)) return FALSE;

  /* If there is a device ID filter and it doesn't match the message, skip the message */
  if ((theFilter->matchOnDeviceID != NULL) && (xPL_strcmpIgnoreCase(theFilter->matchOnDeviceID, theMessage->sourceDeviceID) != 0)) return FALSE;

  /* If there is a instance ID filter and it doesn't match the message, skip the message */
  if ((theFilter->matchOnInstanceID != NULL) && (xPL_strcmpIgnoreCase(theFilter->matchOnInstanceID, theMessage->sourceInstanceID) != 0)) return FALSE;

  /* If there is a schema class filter and it doesn't match the message, skip the message */
  if ((theFilter->matchOnSchemaClass != NULL) && (xPL_strcmpIgnoreCase(theFilter->matchOnSchemaClass, theMessage->schemaClass) != 0)) return FALSE;

  /* If there is a schema type filter and it doesn't match the message, skip the message */
  if ((theFilter->matchOnSchemaType != NULL) && (xPL_strcmpIgnoreCase(theFilter->matchOnSchemaType, theMessage->schemaType) != 0)) return FALSE;

  /* Matches all selectors of this filter (if any) */
  return TRUE;
}
Exemple #3
0
void xPL_setServiceInstanceID(xPL_ServicePtr theService, String newInstanceID) {
  /* Skip if name is already this ID */
  if ((theService->serviceInstanceID != NULL) && !xPL_strcmpIgnoreCase(theService->serviceInstanceID, newInstanceID)) return;

  /* If the service is enabled, send a goodbye heartbeat with our old data */
  if (theService->serviceEnabled) xPL_sendGoodbyeHeartbeat(theService);

  /* Copy the ID */
  STR_FREE(theService->serviceInstanceID);
  theService->serviceInstanceID = xPL_StrDup(newInstanceID);

  /* If service was enabled, send new heartbeat */
  if (theService->serviceEnabled) xPL_sendHeartbeat(theService);
}
Exemple #4
0
void sendNetInfo(xPL_MessagePtr msg) {
	xPL_MessagePtr message = NULL;

	message = xPL_createBroadcastMessage(telldusService, xPL_MESSAGE_STATUS);
	xPL_setSchema(message, "lighting", "netinfo");

	xPL_setMessageNamedValue(message, "network", xPL_getMessageNamedValue(msg, "network"));

	if (xPL_strcmpIgnoreCase(xPL_getMessageNamedValue(msg, "network"), "1") == 0) {
		xPL_setMessageNamedValue(message, "status", "ok");
		xPL_setMessageNamedValue(message, "name", hostname );
		xPL_setMessageNamedValue(message, "device-count", xPL_intToStr(tdGetNumberOfDevices()) );
		xPL_setMessageNamedValue(message, "scene-count", "0" );
	} else {
		xPL_setMessageNamedValue(message, "status", "not-found");
	}

	xPL_sendMessage(message);

	xPL_releaseMessage(message);
}
Exemple #5
0
/* Process a message and see if it applies to this service */
static void handleMessage(xPL_ServicePtr theService, xPL_MessagePtr theMessage) {
  int filterIndex, groupIndex, responseDelay;
  Bool foundGroup = FALSE, foundFilter = FALSE;

  /* If we are not reporting our own messages, see if they */
  /* originated with us and if so, dump it */
  if (!theService->reportOwnMessages) {
    if (!strcmp(theService->serviceVendor, theMessage->sourceVendor) 
	&& !strcmp(theService->serviceDeviceID, theMessage->sourceDeviceID)
	&& !strcmp(theService->serviceInstanceID, theMessage->sourceInstanceID)) {
      xPL_Debug("Skipping message from self");
      return;
    }
  }

  /* Is this a broadcast message? */
  if (theMessage->isBroadcastMessage) {
    /* See if this is a request for a heartbeat */
    if ((theMessage->messageType == xPL_MESSAGE_COMMAND) 
	&& !xPL_strcmpIgnoreCase(theMessage->schemaClass, "hbeat")
	&& !xPL_strcmpIgnoreCase(theMessage->schemaType, "request")) {

      /* Compute a response delay (.5 to 2.5 seconds) */
      responseDelay = (int) (((double) random() / (double) RAND_MAX) * 2000.0) + 500;
      xPL_Debug("Sending heartbeat in response to discovery request after a %d millisecond delay", responseDelay);
      usleep(responseDelay);
      xPL_sendHeartbeat(theService);
    }

    /* If we have filters, see if they match */
    if (theService->filterCount != 0) {
      for (filterIndex = 0; filterIndex < theService->filterCount; filterIndex++) {
	if (doesFilterMatch(&(theService->messageFilterList[filterIndex]), theMessage)) {
	  foundFilter = TRUE;
	  break;
	}
      }

      /* If we had filters and none match, skip this service */
      if (!foundFilter) return;
    }
  } else {
    if (theMessage->isGroupMessage) {
      /* See if we have any groups and if any groups match */
      for (groupIndex = 0; groupIndex < theService->groupCount; groupIndex++) {
	if (!xPL_strcmpIgnoreCase(theMessage->groupName, theService->groupList[groupIndex])) {
	  foundGroup = TRUE;
	  break;
	}
      }
      
      /* If there is no group or no matching group in this service, skip it */
      if (!foundGroup) return;
    } else {
      /* Make sure this target matches */
      if ((xPL_strcmpIgnoreCase(theMessage->targetVendor, theService->serviceVendor) != 0) 
	  || (xPL_strcmpIgnoreCase(theMessage->targetDeviceID, theService->serviceDeviceID) != 0)
	  || (xPL_strcmpIgnoreCase(theMessage->targetInstanceID, theService->serviceInstanceID) != 0)) return;
    }
  }
  
  /* Message is a broadcast (that matches filters, if any) OR a group message */
  /* that matches a group assigned to this service OR is targeted to this     */
  /* service so dispatch it!                                                  */
  xPL_dispatchServiceEvent(theService, theMessage);
}
Exemple #6
0
/** -c - schema class/type */
Bool parseCmdLine( int *argc, char *argv[]) {
  int swptr;
  int newcnt = 0;
  String delimPtr;


  /* Handle each item of the command line.  If it starts with a '-', then */
  /* process it as a switch.  If not, then copy it to a new position in   */
  /* the argv list and up the new parm counter.                           */
  for(swptr = 0; swptr < *argc; swptr++) {
    /* If it doesn't begin with a '-', it's not a switch. */
    if (argv[swptr][0] != '-') {
      if (swptr != newcnt) argv[++newcnt] = argv[swptr];
    }
    else {
      if (!strcmp(argv[swptr],"-s")) {
	swptr++;
	strcpy(msgSource, argv[swptr]);
	continue;
      }

      if (!strcmp(argv[swptr],"-t")) {
	swptr++;
	msgTarget = argv[swptr];
	continue;
      }

      if (!strcmp(argv[swptr],"-b")) {
	msgTarget = NULL;
	continue;
      }

      if (!strcmp(argv[swptr],"-m")) {
	swptr++;

	if (xPL_strcmpIgnoreCase(argv[swptr], "cmnd") == 0) {
	  msgType = xPL_MESSAGE_COMMAND;
	} else if (xPL_strcmpIgnoreCase(argv[swptr], "trig") == 0) {
	  msgType = xPL_MESSAGE_TRIGGER;
	} else if (xPL_strcmpIgnoreCase(argv[swptr], "stat") == 0) {
	  msgType = xPL_MESSAGE_STATUS;
	} else {
	  fprintf(stderr, "Unknown message type of %s for -m", argv[swptr]);
	  return FALSE;
	}

	continue;
      }
      
      if (!strcmp(argv[swptr],"-c")) {
	swptr++;

	if ((delimPtr = strstr(argv[swptr], ".")) == NULL) {
	  fprintf(stderr, "Improperly formatted schema class.type of %s", argv[swptr]);
	  return FALSE;
	}

	*delimPtr++ = '\0';
	msgSchemaClass = strdup(argv[swptr]);
	msgSchemaType = strdup(delimPtr);
	continue;
      }


      /* Anything left is unknown */
      fprintf(stderr, "Unknown switch `%s'", argv[swptr] );
      return FALSE;
    }
  }

  /* Set in place the new argument count and exit */
  *argc = newcnt + 1;
  return TRUE;
}
Exemple #7
0
void lightingCommandHandler(xPL_ServicePtr theService, xPL_MessagePtr theMessage, xPL_ObjectPtr userValue) {
	Bool found = FALSE;
	int deviceCount = 0, deviceId = 0, i, level = 0;
	
	if (!xPL_doesMessageNamedValueExist(theMessage, "command")) {
		return;
	} else if (xPL_strcmpIgnoreCase(xPL_getMessageNamedValue(theMessage, "command"), "goto") != 0) {
		return;
	}

	if (!xPL_doesMessageNamedValueExist(theMessage, "network")) {
		return;
	} else if (xPL_strcmpIgnoreCase(xPL_getMessageNamedValue(theMessage, "network"), "1") != 0) {
		return;
	}

	if (!xPL_doesMessageNamedValueExist(theMessage, "device")) {
		return;
	} else {
		/* Loop the devices to see it realy exists */
		deviceCount = tdGetNumberOfDevices();

		xPL_strToInt(xPL_getMessageNamedValue(theMessage, "device"), &deviceId);
		for( i = 0; i < deviceCount; ++i ) {
			if (tdGetDeviceId(i) == deviceId) {
				found = TRUE;
				break;
			}
		}
		if (found == FALSE) {
			return;
		}
	}

	if (!xPL_doesMessageNamedValueExist(theMessage, "level")) {
		return;
	} else {
		xPL_strToInt(xPL_getMessageNamedValue(theMessage, "level"), &level);
		if (level < 0 || level > 100) {
			return;
		}
		level = (float)level * 255.0 / 100.0;
	}

	if (level > 0 && level < 255) {
		/* See if the device supports dim */
		if (!(tdMethods(deviceId, TELLSTICK_DIM) & TELLSTICK_DIM)) {
			/* Non dimmable device was dimmed */
			return;
		}
		tdDim(deviceId, (unsigned char)level);
	} else if (level == 255) {
		/* See if the device supports dim */
		if (!(tdMethods(deviceId, TELLSTICK_TURNON) & TELLSTICK_TURNON)) {
			/* Non dimmable device was dimmed */
			return;
		}
		tdTurnOn(deviceId);
	} else if (level == 0) {
		/* See if the device supports dim */
		if (!(tdMethods(deviceId, TELLSTICK_TURNOFF) & TELLSTICK_TURNOFF)) {
			/* Non dimmable device was dimmed */
			return;
		}
		tdTurnOff(deviceId);
	}

}