Beispiel #1
0
void KPR_mqttclient_publish(xsMachine* the)
{
	KPR_MQTTClientRecord *self = xsGetHostData(xsThis);
	FskErr err = kFskErrNone;
	char *topic;
	void *payload = NULL;
	UInt32 payloadLength = 0;
	UInt8 qos;
	Boolean retain;
	UInt16 token;

	if (xsToInteger(xsArgc) != 4) xsThrowIfFskErr(kFskErrParameterError);

	topic = xsToString(xsArg(0));

	if (xsTest(xsArg(1))) {
		if (isArrayBuffer(xsArg(1))) {
			payload = xsToArrayBuffer(xsArg(1));
			payloadLength = xsGetArrayBufferLength(xsArg(1));
		} else {
			payload = xsToString(xsArg(1));
			payloadLength = FskStrLen(payload);
		}
	}

	qos = xsToInteger(xsArg(2));
	retain = xsToBoolean(xsArg(3));

	bailIfError(KprMQTTClientPublish(self->client, topic, payload, payloadLength, qos, retain, &token));
	xsResult = xsInteger(token);

bail:
	xsThrowIfFskErr(err);
}
Beispiel #2
0
void KPR_mqttclient_unsubscribe(xsMachine* the)
{
	KPR_MQTTClientRecord *self = xsGetHostData(xsThis);
	FskErr err = kFskErrNone;
	xsIntegerValue c = xsToInteger(xsArgc);
	char **topics = NULL;
	int i, count;
	UInt16 token;

	if (c == 0) xsThrowIfFskErr(kFskErrParameterError);

	count = c;

	bailIfError(FskMemPtrNew(sizeof(char*) * count, &topics));

	for (i = 0; i < count; i++) {
		topics[i] = xsToString(xsArg(i));
	}

	bailIfError(KprMQTTClientUnsubscribeTopics(self->client, topics, count, &token));
	xsResult = xsInteger(token);

bail:
	FskMemPtrDispose(topics);
	xsThrowIfFskErr(err);
}
Beispiel #3
0
void KPR_message_set_method(xsMachine *the)
{
	KprMessage self = xsGetHostData(xsThis);
	if (xsTest(xsArg(0)))
		xsThrowIfFskErr(KprMessageSetMethod(self, xsToString(xsArg(0))));
	else
		xsThrowIfFskErr(KprMessageSetMethod(self, NULL));
}
Beispiel #4
0
void KPRLibrarySniffPodcast(xsMachine *the)
{
    KprMessage message;
    KprFunctionTarget target;
    xsThrowIfFskErr(KprMessageNew(&message, xsToString(xsArg(1))));
    xsThrowIfFskErr(KprMessageSetRequestHeader(message, "Range", "bytes=0-4095"));
    xsThrowIfFskErr(KprFunctionTargetNew(&target, the, &xsArg(2)));
    xsThrowIfFskErr(KprMessageInvoke(message, KPRLibrarySniffPodcastComplete, KprFunctionTargetDispose, target));
}
Beispiel #5
0
static void KPR_Message_invoke_executor(xsMachine* the)
{
	KprMessage message;
	KprPromiseTarget target;
	xsResult = xsGet(xsFunction, xsID_message);
	message = kprGetHostData(xsResult, this, message);
	xsThrowIfFskErr(KprPromiseTargetNew(&target, the, &xsArg(0), &xsArg(1)));
	xsThrowIfFskErr(KprMessageInvoke(message, KprPromiseTargetComplete, KprPromiseTargetDispose, target));
}
Beispiel #6
0
void KPR_message_set_responseText(xsMachine* the)
{
	KprMessage self = xsGetHostData(xsThis);
	if (xsTest(xsArg(0))) {
		xsStringValue body = xsToString(xsArg(0));
		xsThrowIfFskErr(KprMessageSetResponseBody(self, body, FskStrLen(body)));
	}
	else
		xsThrowIfFskErr(KprMessageSetResponseBody(self, NULL, 0));
}
Beispiel #7
0
void KPR_MQTTClient(xsMachine* the)
{
	FskErr err;
	KPR_MQTTClientRecord *self = NULL;
	xsIntegerValue c = xsToInteger(xsArgc);
	KprMQTTClient client = NULL;
	char clientIdentifier[kKprMQTTClientIdentifierMaxLength + 1];
	Boolean cleanSession = true;

	if (c >= 1) {
		char *arg = xsToString(xsArg(0));
		UInt32 len = FskStrLen(arg);

		if (len < 1 || len > kKprMQTTClientIdentifierMaxLength) xsThrowIfFskErr(kFskErrBadData);
		FskStrCopy(clientIdentifier, arg);
	} else {
		char hex[9];
		FskStrNumToHex(FskRandom(), hex, 8);
		FskStrCopy(clientIdentifier, kKprMQTTClientIdentifierPrefix);
		FskStrCat(clientIdentifier, hex);

	}

	if (c >= 2) {
		cleanSession = xsToBoolean(xsArg(1));
	}

	bailIfError(FskMemPtrNewClear(sizeof(KPR_MQTTClientRecord), &self));

	bailIfError(KprMQTTClientNew(&client, clientIdentifier, cleanSession, self));

	client->connectCallback = KPR_mqttclient_onConnect;
	client->subscribeCallback = KPR_mqttclient_onSubscribe;
	client->unsubscribeCallback = KPR_mqttclient_onUnsubscribe;
	client->publishCallback = KPR_mqttclient_onPublish;
	client->messageCallback = KPR_mqttclient_onMessage;
	client->disconnectCallback = KPR_mqttclient_onDisconnect;
	client->errorCallback = KPR_mqttclient_onError;

	self->client = client;
	self->the = the;
	self->slot = xsThis;
	self->code = the->code;
	xsSetHostData(self->slot, self);
	// xsCall1(xsGet(xsGlobal, xsID_Object), xsID_seal, self->slot);

	xsRemember(self->slot);

bail:
	if (err) {
		KprMQTTClientDispose(client);
		FskMemPtrDispose(self);
		xsThrowIfFskErr(err);
	}
}
Beispiel #8
0
void KPR_message_set_requestChunk(xsMachine* the)
{
	KprMessage self = xsGetHostData(xsThis);
	if (xsTest(xsArg(0))) {
		void* data = xsGetHostData(xsArg(0));
		xsIntegerValue size = xsToInteger(xsGet(xsArg(0), xsID_length));
		xsThrowIfFskErr(KprMessageSetRequestBody(self, data, size));
	}
	else
		xsThrowIfFskErr(KprMessageSetRequestBody(self, NULL, 0));
}
Beispiel #9
0
void KPR_message_set_responseBuffer(xsMachine* the)
{
	KprMessage self = xsGetHostData(xsThis);
	if (xsTest(xsArg(0))) {
		void* data = xsToArrayBuffer(xsArg(0));
		xsIntegerValue size = xsGetArrayBufferLength(xsArg(0));
		xsThrowIfFskErr(KprMessageSetResponseBody(self, data, size));
	}
	else
		xsThrowIfFskErr(KprMessageSetResponseBody(self, NULL, 0));
}
Beispiel #10
0
void KPR_message_set_responseChunk(xsMachine* the)
{
	KprMessage self = xsGetHostData(xsThis);
	//xsTrace("message.responseChunk deprecated, use responseBuffer\n");
	if (xsTest(xsArg(0))) {
		void* data = xsGetHostData(xsArg(0));
		xsIntegerValue size = xsToInteger(xsGet(xsArg(0), xsID_length));
		xsThrowIfFskErr(KprMessageSetResponseBody(self, data, size));
	}
	else
		xsThrowIfFskErr(KprMessageSetResponseBody(self, NULL, 0));
}
Beispiel #11
0
void KPR_websocketclient_send(xsMachine* the)
{
	KPR_WebSocketClientRecord *self = xsGetHostData(xsThis);

	if (isChunk(xsArg(0))) {
		void *data = xsGetHostData(xsArg(0));
		UInt32 length = xsToInteger(xsGet(xsArg(0), xsID_length));

		xsThrowIfFskErr(KprWebSocketEndpointSendBinary(self->endpoint, data, length));
	} else {
		xsThrowIfFskErr(KprWebSocketEndpointSendString(self->endpoint, xsToString(xsArg(0))));
	}
}
Beispiel #12
0
void KPR_debug_file(xsMachine *the)
{
	KprDebug self = xsGetHostData(xsThis);
	KprDebugMachine machine = NULL;
	int command = mxNoCommand;
	char* path = NULL;
	char* value = NULL;
	xsIntegerValue line = -1;
	xsIntegerValue c = xsToInteger(xsArgc);
	xsVars(4);
	if ((c >= 2) && (xsTypeOf(xsArg(0)) == xsStringType) && (xsTypeOf(xsArg(1)) == xsIntegerType)) {
		char* address = xsToString(xsArg(0));
		command = xsToInteger(xsArg(1));
		machine = KprDebugFindMachine(self, address);
	}
	if ((c >= 4) && (xsTypeOf(xsArg(2)) == xsStringType) && (xsTypeOf(xsArg(3)) == xsIntegerType)) {
		path = FskStrDoCopy(xsToString(xsArg(2)));
		line = xsToInteger(xsArg(3));
	}
	if ((c >= 5) && (xsTypeOf(xsArg(4)) == xsStringType)) {
		value = FskStrDoCopy(xsToString(xsArg(4)));
	}
	if (machine && ((command == mxFramesView) || (command == mxFilesView) || (command == mxLogView) || (command == mxBreakpointsView))) {
		xsEnterSandbox();
		KprDebugMachineDispatchCommand(machine, command, path, value, line);
		xsLeaveSandbox();
	}
	else
		xsThrowIfFskErr(kFskErrInvalidParameter);
	FskMemPtrDispose(value);
	FskMemPtrDispose(path);
}
Beispiel #13
0
void KprDebugMachineLoadView(KprDebugMachine self, int theView, char* thePath, int theLine)
{
	FskErr err = kFskErrNone;
	KprDebug debug = self->debug;
	xsMachine* the = debug->the;
//	FskFileInfo info;
//	FskFileMapping map = NULL;
//	FskDebugStr("%s: %d - %s:%d", __FUNCTION__, theView, thePath, theLine);
	if (xsTypeOf(debug->behavior) == xsUndefinedType) goto bail;
	xsVar(0) = xsAccess(debug->behavior);
	
	if (xsFindResult(xsVar(0), xsID("onMachineFileChanged"))) {
		(void)xsCallFunction5(xsResult, xsVar(0), xsString(self->address), xsInteger(theView), xsNull, thePath ? xsString(thePath) : xsNull, xsInteger(theLine));
	}
//	if (kFskErrNone == FskFileGetFileInfo(thePath, &info) && (kFskDirectoryItemIsFile == info.filetype)) {
//		unsigned char *data;
//		FskInt64 size;
//		BAIL_IF_ERR(err = FskFileMap(thePath, &data, &size, 0, &map));
//		xsVar(0) = xsAccess(debug->behavior);
//		
//		if (xsFindResult(xsVar(0), xsID("onMachineFileChanged"))) {
//			(void)xsCallFunction5(xsResult, xsVar(0), xsString(self->address), xsInteger(theView), xsNull, xsString(thePath), xsInteger(theLine));
//		}
//	}
bail:
//	FskFileDisposeMap(map);
	xsThrowIfFskErr(err);
}
Beispiel #14
0
void fxLoadModuleXML(txMachine* the, txString path, txID moduleID)
{
	FskFileMapping map = NULL;
	txFileMapStream fileMapStream;
	txStringStream stringStream;
	txScript* script = NULL;
	
	mxTry(the) {
		fxBeginHost(the);
		xsThrowIfFskErr(FskFileMap(path, &fileMapStream.buffer, &fileMapStream.size, 0, &map));
		fileMapStream.offset = 0;
		mxPushInteger(0);
		mxPushInteger(0);
		mxPushInteger(0);
		mxPushInteger(0);
		mxPushInteger(3);
		fxParse(the, &fileMapStream, fxFileMapGetter, path, 1, xsSourceFlag | xsDebugFlag);
		fxCallID(the, fxID(the, "generate"));
		fxToString(the, the->stack);
		stringStream.slot = the->stack;
		stringStream.size = c_strlen(stringStream.slot->value.string);
		stringStream.offset = 0;
		script = fxParseScript(the, &stringStream, fxStringGetter, mxDebugFlag);
		fxEndHost(the);
	}
	mxCatch(the) {
		break;
	}
	FskFileDisposeMap(map);
	fxResolveModule(the, moduleID, script, NULL, NULL);
}
Beispiel #15
0
void KPR_mqttclient_unsubscribe(xsMachine* the)
{
	KPR_MQTTClientRecord *self = xsGetHostData(xsThis);
	FskErr err = kFskErrNone;
	xsIntegerValue c = xsToInteger(xsArgc);
	char *topic = NULL;

	if (c < 1) goto invalidParams;

	topic = xsToString(xsArg(0));
	if (!KprMQTTIsValidTopic(topic, true)) goto badParam;

	bailIfError(KprMQTTClientUnsubscribeTopic(self->client, topic));
	goto bail;

invalidParams:
	err = kFskErrInvalidParameter;
	goto bail;

badParam:
	err = kFskErrBadData;
	goto bail;

bail:
	xsThrowIfFskErr(err);
}
Beispiel #16
0
void KPR_message_setRequestCertificate(xsMachine* the)
{
	KprMessage self = xsGetHostData(xsThis);
	FskSocketCertificateRecord* certs = NULL;
	FskSocketCertificateRecord certsRecord = {
		NULL, 0,
		NULL,
		NULL,
		NULL, 0,
	};
	if (xsIsInstanceOf(xsArg(0), xsObjectPrototype)) {
		certs = &certsRecord;
		if (xsHas(xsArg(0), xsID("certificates"))) {
			certs->certificates = (void*)xsToString(xsGet(xsArg(0), xsID("certificates")));
			certs->certificatesSize = FskStrLen(certs->certificates);
		}
		if (xsHas(xsArg(0), xsID("policies"))) {
			certs->policies = xsToString(xsGet(xsArg(0), xsID("policies")));
		}
		if (xsHas(xsArg(0), xsID("key"))) {
			certs->key = (void*)xsToString(xsGet(xsArg(0), xsID("key")));
			certs->keySize = FskStrLen(certs->key);
		}
	}
	else { // compatibility
		certs = &certsRecord;
		if (xsTest(xsArg(0))) {
			certs->certificates = xsGetHostData(xsArg(0));
			certs->certificatesSize = xsToInteger(xsGet(xsArg(0), xsID_length));
		}
		if (xsTest(xsArg(1)))
			certs->policies = xsToString(xsArg(1));
	}
	xsThrowIfFskErr(KprMessageSetRequestCertificate(self, certs));
}
Beispiel #17
0
void KPR_Message_URI(xsMachine* the)
{
	char* url = NULL;
	xsThrowIfFskErr(KprMessageURL(xsGetContext(the), xsToString(xsArg(0)), &url));
	xsResult = xsString(url);
	FskMemPtrDispose(url);
}
Beispiel #18
0
void KPR_Message(xsMachine* the)
{
	xsStringValue url = NULL;
	KprMessage self = NULL;
	xsTry {
		xsThrowIfFskErr(KprMessageURL(xsGetContext(the), xsToString(xsArg(0)), &url));
		xsThrowIfFskErr(KprMessageNew(&self, url));
		xsSetHostData(xsThis, self);
		FskInstrumentedItemSendMessageDebug(self, kprInstrumentedMessageConstruct, self);
		self->usage++; // host
		FskMemPtrDispose(url);
	}
	xsCatch {
		FskMemPtrDispose(url);
		xsThrow(xsException);
	}
}
Beispiel #19
0
void KPR_Canvas(xsMachine *the)
{
	KprCoordinatesRecord coordinates;
	KprCanvas self;
	xsSlotToKprCoordinates(the, &xsArg(0), &coordinates);
	xsThrowIfFskErr(KprCanvasNew(&self, &coordinates));
	kprContentConstructor(KPR_Content);
}
Beispiel #20
0
void xs_gpio_set_direction(xsMachine* the)
{
    FskGPIO gpio = xsGetHostData(xsThis);
    FskErr err;
    GPIOdirection dir = stringToDirection(the, xsToString(xsArg(0)), gpio);

    err = FskGPIOPlatformSetDirection(gpio, dir);
    xsThrowIfFskErr(err);
}
Beispiel #21
0
void KPR_mergeURI(xsMachine *the)
{
	xsStringValue base = xsToString(xsArg(0));
	xsStringValue url = xsToString(xsArg(1));
	xsStringValue target;
	xsThrowIfFskErr(KprURLMerge(base, url, &target));
	xsResult = xsString(target);
	FskMemPtrDispose(target);
}
Beispiel #22
0
void KPR_websocketclient_close(xsMachine* the)
{
	KPR_WebSocketClientRecord *self = xsGetHostData(xsThis);
	xsIntegerValue c = xsToInteger(xsArgc);
	UInt16 code = 1000;
	char *reason = "closed by script";

	if (c >= 1) {
		code = xsToInteger(xsArg(0));
		if (code != 1000 && !(code >= 3000 && code <= 4999)) xsThrowIfFskErr(kFskErrInvalidParameter);
	}

	if (c >= 2) {
		reason = xsToString(xsArg(1));
		if (FskStrLen(reason) > 123) xsThrowIfFskErr(kFskErrInvalidParameter);
	}

	xsThrowIfFskErr(KprWebSocketEndpointClose(self->endpoint, code, reason));
}
Beispiel #23
0
void KPR_application_get_di(xsMachine* the)
{
	KprApplication self = xsGetHostData(xsThis);
	if (self->id) {
		char* di;
		xsThrowIfFskErr(KprAuthorityReverse(self->id, &di));
		xsResult = xsString(di);
		FskMemPtrDispose(di);
	}
}
Beispiel #24
0
void KprMessageScriptTargetSet(KprMessage self, xsMachine* the, xsSlot* slot)
{
	KprMessageScriptTarget stream = NULL;
	if (xsTypeOf(*slot) == xsStringType) {
		xsThrowIfFskErr(KprMessageScriptTargetNew(&stream, xsToString(*slot)));
	}
	else {
		stream = xsGetHostData(*slot);
    }
	KprMessageSetStream(self, (KprStream)stream);
}
Beispiel #25
0
void KPR_MQTTClient(xsMachine* the)
{
	FskErr err;
	KPR_MQTTClientRecord *self = NULL;
	KprMQTTClient client = NULL;
	FskDeferrer deferrer = NULL;
	char *clientIdentifier;
	Boolean cleanSession;

	if (xsToInteger(xsArgc) != 2) xsThrowIfFskErr(kFskErrParameterError);

	clientIdentifier = xsToString(xsArg(0));
	cleanSession = xsToBoolean(xsArg(1));

	bailIfError(FskMemPtrNewClear(sizeof(KPR_MQTTClientRecord), &self));
	bailIfError(FskDeferrerNew(&deferrer));
	bailIfError(KprMQTTClientNew(&client, clientIdentifier, cleanSession, kKprMQTTProtocol311, self));

	client->connectCallback = KPR_mqttclient_onConnect;
	client->subscribeCallback = KPR_mqttclient_onSubscribe;
	client->unsubscribeCallback = KPR_mqttclient_onUnsubscribe;
	client->publishCallback = KPR_mqttclient_onPublish;
	client->messageCallback = KPR_mqttclient_onMessage;
	client->disconnectCallback = KPR_mqttclient_onDisconnect;
	client->errorCallback = KPR_mqttclient_onError;

	self->client = client;
	self->deferrer = deferrer;
	self->the = the;
	self->slot = xsThis;
	xsSetHostData(self->slot, self);

bail:

	if (err) {
		KprMQTTClientDispose(client);
		FskDeferrerDispose(deferrer);
		FskMemPtrDispose(self);
		xsThrowIfFskErr(err);
	}
}
Beispiel #26
0
void KPR_mqttclient_connect(xsMachine* the)
{
	KPR_MQTTClientRecord *self = xsGetHostData(xsThis);
	char *host;
	UInt16 port;
	KprMQTTClientConnectOptions options;

	if (xsToInteger(xsArgc) != 10) xsThrowIfFskErr(kFskErrParameterError);

	host = xsToString(xsArg(0));
	port = xsToInteger(xsArg(1));

	options.isSecure = xsToBoolean(xsArg(2));
	options.keepAlive = xsToInteger(xsArg(3));
	options.username = xsTest(xsArg(4)) ? xsToString(xsArg(4)) : NULL;
	options.password = xsTest(xsArg(5)) ? xsToString(xsArg(5)) : NULL;
	options.willIsRetained = false;
	options.willQualityOfService = 0;
	options.willTopic = NULL;
	options.willPayload = NULL;
	options.willPayloadLength = 0;

	if (xsTest(xsArg(6))) {
		options.willTopic = xsToString(xsArg(6));
		options.willQualityOfService = xsToInteger(xsArg(7));
		options.willIsRetained = xsToBoolean(xsArg(8));

		if (xsTest(xsArg(9))) {
			if (isArrayBuffer(xsArg(9))) {
				options.willPayload = xsToArrayBuffer(xsArg(9));
				options.willPayloadLength = xsGetArrayBufferLength(xsArg(9));
			} else {
				options.willPayload = xsToString(xsArg(9));
				options.willPayloadLength = FskStrLen(options.willPayload);
			}
		}
	}

	xsThrowIfFskErr(KprMQTTClientConnect(self->client, host, port, &options));
}
Beispiel #27
0
void KPR_debug_trigger(xsMachine *the, UInt32 command)
{
	KprDebug self = xsGetHostData(xsThis);
	KprDebugMachine machine = NULL;
	xsIntegerValue c = xsToInteger(xsArgc);
	if ((c >= 1) && (xsTypeOf(xsArg(0)) == xsStringType)) {
		char* address = xsToString(xsArg(0));
		machine = KprDebugFindMachine(self, address);
	}
	if (machine && (command >= mxAbortCommand) && (command <= mxStepOutCommand))
		KprDebugMachineDispatchCommand(machine, command, NULL, NULL, 0);
	else
		xsThrowIfFskErr(kFskErrInvalidParameter);
}
Beispiel #28
0
void KPR_message_setRequestCertificate(xsMachine* the)
{
	KprMessage self = xsGetHostData(xsThis);
	void *data = NULL;
	xsStringValue policies = NULL;
	xsIntegerValue size = 0;
	if (xsTest(xsArg(0))) {
		data = xsGetHostData(xsArg(0));
		size = xsToInteger(xsGet(xsArg(0), xsID_length));
	}
	if (xsTest(xsArg(1)))
		policies = xsToString(xsArg(1));
	xsThrowIfFskErr(KprMessageSetRequestCertificate(self, data, size, policies));
}
Beispiel #29
0
void KPR_debug_resetBreakpoints(xsMachine *the)
{
	KprDebug self = xsGetHostData(xsThis);
	KprDebugMachine machine = NULL;
	xsIntegerValue c = xsToInteger(xsArgc);
	if ((c >= 1) && (xsTypeOf(xsArg(0)) == xsStringType)) {
		char* address = xsToString(xsArg(0));
		machine = KprDebugFindMachine(self, address);
	}
	if (machine)
		KprDebugMachineDispatchCommand(machine, mxClearAllBreakpointsCommand, NULL, NULL, 0);
	else
		xsThrowIfFskErr(kFskErrInvalidParameter);
}
Beispiel #30
0
void KPR_WebSocketClient(xsMachine* the)
{
	FskErr err;
	xsIntegerValue c = xsToInteger(xsArgc);
	KPR_WebSocketClientRecord *self = NULL;
	KprWebSocketEndpoint endpoint = NULL;
	
	bailIfError(FskMemPtrNewClear(sizeof(KPR_WebSocketClientRecord), &self));
	
	xsThrowIfFskErr(KprWebSocketEndpointNew(&endpoint, self));
	
	endpoint->openCallback = KPR_WebSocketClient_onOpen;
	endpoint->closeCallback = KPR_WebSocketClient_onClose;
	endpoint->textCallback = KPR_WebSocketClient_onTextMessage;
	endpoint->binaryCallback = KPR_WebSocketClient_onBinaryMessage;
	endpoint->errorCallback = KPR_WebSocketClient_onError;
	
	self->endpoint = endpoint;
	self->the = the;
	self->slot = xsThis;
	self->code = the->code;
	xsSetHostData(self->slot, self);
	// xsCall1(xsGet(xsGlobal, xsID_Object), xsID_seal, self->slot);
	
	if (c >= 1) {
		bailIfError(KprWebSocketEndpointConnect(endpoint, xsToString(xsArg(0)), NULL));
	}

	xsRemember(self->slot);

	return;
	
bail:
	KprWebSocketEndpointDispose(endpoint);
	FskMemPtrDispose(self);
	xsThrowIfFskErr(err);
}