Example #1
0
FskErr KprMQTTClientNew(KprMQTTClient* it, char *clientId, Boolean cleanSession, void *refcon)
{
	FskErr err = kFskErrNone;
	KprMQTTClient self = NULL;

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

	bailIfError(KprMQTTQueueNew(&self->queue, 15, KprMQTTClientSendMessageViaDelivery, self));

	FskTimeCallbackNew(&self->pingRequestCallaback);
	bailIfNULL(self->pingRequestCallaback);

	FskTimeCallbackNew(&self->pingResponseCallaback);
	bailIfNULL(self->pingResponseCallaback);

	self->clientIdentifier = FskStrDoCopy(clientId);
	bailIfNULL(self->clientIdentifier);

	self->cleanSession = cleanSession;
	self->refcon = refcon;
	self->state = kKprMQTTStateDisconnected;

	KprMQTTQueueStart(self->queue);

	*it = self;
bail:
	if (err) KprMQTTClientDispose(self);
	return err;
}
Example #2
0
static FskErr httpServerListenerStart(FskHTTPServerListener listener, FskSocket skt) {
	FskErr err = kFskErrNone;
	FskHTTPServerRequest request;

	if (listener->http->stopped) {
		FskInstrumentedItemSendMessage(listener->http, kFskHTTPInstrMsgConnectionRefusedStopped, listener);
		listener->http->stats.connectionsRefused++;
		FskNetSocketClose(skt);
		goto bail;
	}
		
	err = FskMemPtrNewClear(sizeof(FskHTTPServerRequestRecord), (FskMemPtr*)&request);
	BAIL_IF_ERR(err);

	sFskHTTPServerRequestUpUse(request);

	request->http = listener->http;
	request->skt = skt;	
	FskNetSocketGetRemoteAddress(skt, (UInt32 *)&request->requesterAddress, &request->requesterPort);
	FskNetSocketMakeNonblocking(request->skt);
	err = FskHeaderStructNew(&request->requestHeaders);
	BAIL_IF_ERR(err);
	err = FskHeaderStructNew(&request->responseHeaders);
	BAIL_IF_ERR(err);
	request->in.bufferSize = request->http->defaultBufferSize;
	request->out.bufferSize = request->http->defaultBufferSize;
	err = FskMemPtrNew(request->in.bufferSize, (FskMemPtr*)&request->in.buf);
	BAIL_IF_ERR(err);
	err = FskMemPtrNew(request->out.bufferSize, (FskMemPtr*)&request->out.buf);
	BAIL_IF_ERR(err);

	FskListAppend((FskList*)&request->http->activeRequests, request);
	FskTimeCallbackNew(&request->cycleCallback);
	FskTimeCallbackNew(&request->keepAliveKillCallback);

	listener->http->stats.connectionsMade++;

	request->state = kHTTPNewSession;

	FskInstrumentedItemNew(request, NULL, &gFskHTTPServerRequestTypeInstrumentation);
	FskInstrumentedItemSetOwner(request, request->http);
	
	FskTimeCallbackScheduleNextRun(request->cycleCallback, httpServerTimeCycle, request);
	doCallCondition(request->http->callbacks->requestCondition, request, kFskHTTPConditionConnectionInitialized, request->refCon);
	FskTimeCallbackNew(&request->timer);
	FskTimeCallbackScheduleFuture(request->timer, 1, 0, KprHTTPServerTimerCallback, request);
bail:
	if (err)
		FskHTTPServerRequestDispose(request);
	return err;
}
Example #3
0
void xs_gpio_repeat(xsMachine* the)
{
    FskGPIO gpio = xsGetHostData(xsThis);

    if (gpio) {
        if (in != gpio->direction)
            xsThrowDiagnosticIfFskErr(kFskErrUnimplemented, "Digital pin %d cannot repeat on output pin", (int)gpio->pinNum);

        if (gpio->poller)
            FskListRemove(&gGPIOPollers, gpio);
        
        gpio->poller = (xsTest(xsArg(0))) ? xsGetHostData(xsArg(0)) : NULL;

        if (gpio->poller) {
            FskListAppend(&gGPIOPollers, gpio);
            gpio->pollerValue = -1;     // won't match
            if (NULL == gGPIOPoller) {
                FskTimeCallbackNew(&gGPIOPoller);
                FskTimeCallbackScheduleNextRun(gGPIOPoller, gpioPoller, NULL);
            }
        }
        else if ((NULL == gGPIOPollers) && (NULL != gGPIOPoller)) {
            FskTimeCallbackDispose(gGPIOPoller);
            gGPIOPoller = NULL;
        }
    }
}
Example #4
0
void KprSoundSetup()
{
	FskErr err = kFskErrNone;
	FskMediaPropertyValueRecord ambientAudio = {kFskMediaPropertyTypeInteger, {kFskAudioOutCategoryAmbient}};
#if FSK_EXTENSION_EMBED
#else
	char* applicationPath = NULL;
	char* libraryPath = NULL;
#if TARGET_OS_WIN32
	char* libraryName = "mediareader.dll";
#else
	char* libraryName = "mediareader.so";
#endif
	FskLibrary library = NULL;
#endif
	
	if (gSoundChannel) goto bail;
	
#if FSK_EXTENSION_EMBED
	FskMediaReaderNewProc = FskMediaReaderNew;
	FskMediaReaderDisposeProc = FskMediaReaderDispose;
	FskMediaReaderExtractProc = FskMediaReaderExtract;
	FskMediaReaderGetPropertyProc = FskMediaReaderGetProperty;
	FskMediaReaderGetTrackProc = FskMediaReaderGetTrack;
	FskMediaReaderStartProc = FskMediaReaderStart;
	FskMediaReaderStopProc = FskMediaReaderStop;
	FskMediaReaderTrackGetPropertyProc = FskMediaReaderTrackGetProperty;
#else
	applicationPath = FskGetApplicationPath();
	bailIfNULL(applicationPath);
	bailIfError(FskMemPtrNewClear(FskStrLen(applicationPath) + FskStrLen(libraryName) + 1, &libraryPath));
	FskStrCopy(libraryPath, applicationPath);
	FskStrCat(libraryPath, libraryName);
	bailIfError(FskLibraryLoad(&library, libraryPath));
	bailIfError(FskLibraryGetSymbolAddress(library, "FskMediaReaderNew", &FskMediaReaderNewProc));
	bailIfError(FskLibraryGetSymbolAddress(library, "FskMediaReaderDispose", &FskMediaReaderDisposeProc));
	bailIfError(FskLibraryGetSymbolAddress(library, "FskMediaReaderExtract", &FskMediaReaderExtractProc));
	bailIfError(FskLibraryGetSymbolAddress(library, "FskMediaReaderGetProperty", &FskMediaReaderGetPropertyProc));
	bailIfError(FskLibraryGetSymbolAddress(library, "FskMediaReaderGetTrack", &FskMediaReaderGetTrackProc));
	bailIfError(FskLibraryGetSymbolAddress(library, "FskMediaReaderStart", &FskMediaReaderStartProc));
	bailIfError(FskLibraryGetSymbolAddress(library, "FskMediaReaderStop", &FskMediaReaderStopProc));
	bailIfError(FskLibraryGetSymbolAddress(library, "FskMediaReaderTrackGetProperty", &FskMediaReaderTrackGetPropertyProc));
#endif
	bailIfError(FskSndChannelNew(&gSoundChannel, 0, kFskAudioFormatUndefined, 0));
	FskTimeCallbackNew(&gSoundTimer);

	FskSndChannelSetProperty(gSoundChannel, kFskMediaPropertyAudioCategory, &ambientAudio);

bail:
#if FSK_EXTENSION_EMBED
#else
	if (err)
		FskLibraryUnload(library);
	else
		FskMemPtrDispose(library);
	FskMemPtrDispose(libraryPath);
	FskMemPtrDispose(applicationPath);
#endif
	return;
}
Example #5
0
FskErr androidAudioOutStart(FskAudioOut audioOut, FskSampleTime atSample) {
	FskErr err = kFskErrNone;
	androidAudioExt *ext = (androidAudioExt*)audioOut->ext;
	SLresult	res;

	FskAudioNativePrintfVerbose("audioOutStart %x - atSample %lld", audioOut, atSample);
	if (audioOut->playing)
		goto bail;
	if (!ext->playItf) {
		FskAudioNativePrintfMinimal("huh? No playItf");
		err = kFskErrOperationFailed;
		goto bail;
	}

	audioOut->zeroTime = atSample;
	ext->stoppedAtSamplePosition = 0;

	FskTimeCallbackNew(&ext->flushTimer);
	FskTimeCallbackScheduleFuture(ext->flushTimer, 0, kFlushAndRefillTime, flushNowCallback, audioOut);
	FskAudioNativePrintfVerbose("androidAudioOutStart %x - zeroTime %d", audioOut, audioOut->zeroTime);

	// in case volume has changed when audio was off
//	androidAudioOutSetVolume(audioOut, audioOut->leftVolume, audioOut->rightVolume);

	refillQueue(audioOut);
	fillBuffers(audioOut);
	audioOut->playing = true;
//	refillQueue(audioOut);

    FskTimeGetNow(&ext->lastTime);

	FskMutexAcquire(gActiveAudioMutex);
	gActiveAudioOut = audioOut;
	FskMutexRelease(gActiveAudioMutex);

	res = (*ext->playItf)->SetPlayState(ext->playItf, SL_PLAYSTATE_PLAYING);
	CheckErr(" audioOutStart - set playstate playing", res);

	{
		SLmillisecond msec;
		res = (*ext->playItf)->GetPosition(ext->playItf, &msec);
		CheckErr(" androidAudioOutStart", res);
		ext->startedAtSamplePosition = MSToSamples(audioOut, msec);
	}

bail:
	return err;
}
Example #6
0
static void
xs_handshake_finished_callback(xsMachine *the)
{
	FskSSL *fssl = xsGetHostData(xsGet(xsThis, xsID("_hostData")));
	int ac = xsToInteger(xsArgc);
	FskSocket skt = ac > 0 && xsTest(xsArg(0)) ? (FskSocket)xsGetHostData(xsArg(0)): NULL;
	FskErr err = ac > 1 && xsTypeOf(xsArg(0)) == xsIntegerType ? (FskErr)xsToInteger(xsArg(1)): kFskErrNone;

	if (fssl->socketCallback == NULL)
		return;	/* callback is the only way to get a socket so no callback no socket */

	fssl->skt = skt;
	if (skt != NULL) {
		skt->lastErr = (err == kFskErrNone) ? kFskErrNone : kFskErrSSLHandshakeFailed;	// ignore the detailed error
		skt->fssl = fssl;
	}
	/* it is possible that the callback calls FskSSLDispose which destroys not only all objects but the VM, so we need to be free from VM when the callback is called */
	FskTimeCallbackNew(&fssl->timer);
	FskTimeCallbackScheduleNextRun(fssl->timer, time_callback, fssl);
}
Example #7
0
FskErr KprLabelInsertStringWithLength(KprLabel self, char* text, UInt32 size)
{
	FskErr err = kFskErrNone;
	UInt32 oldSize = FskStrLen(self->text);
	SInt32 fromSize = fxUnicodeToUTF8Offset(self->text, self->from);
	SInt32 toSize = fxUnicodeToUTF8Offset(self->text, self->to);
	UInt32 newSize = oldSize - (toSize - fromSize) + size;
	char* buffer;
	bailIfError(FskMemPtrNew(newSize + 1, &buffer));
	if (fromSize > 0)
		FskMemMove(buffer, self->text, fromSize); 
	if (size > 0)
		FskMemMove(buffer + fromSize, text, size); 
	if (oldSize - toSize > 0)
		FskMemMove(buffer + fromSize + size, self->text + toSize, oldSize - toSize);
	buffer[newSize] = 0;
	FskMemPtrDispose(self->text);
	if (self->flags & kprTextHidden) {
		if ((self->from == self->length) && (self->to == self->length) && (fxUTF8ToUnicodeOffset(text, size) == 1)) {
			self->flags |= kprTextShowLast;
			if (!self->showLastCallback)
				FskTimeCallbackNew(&self->showLastCallback);
			FskTimeCallbackScheduleFuture(self->showLastCallback, 1, 0, KprLabelShowLastCallback, self);		
		}
		else {
			if (self->showLastCallback)
				FskTimeCallbackRemove(self->showLastCallback);
			self->flags &= ~kprTextShowLast;
		}
	}
	self->text = buffer;
	self->length = fxUnicodeLength(buffer);
	self->from = self->to = fxUTF8ToUnicodeOffset(buffer, fromSize + size);
bail:
	if (self->shell) {
		KprLabelMeasureSelection(self);
		KprContentInvalidate((KprContent)self);
		KprContentReflow((KprContent)self, kprSizeChanged);
	}
	return err;
}
Example #8
0
FskErr KprZeroconfPlatformServiceNew(KprZeroconfPlatformService* it, KprZeroconfPlatformService owner, DNSServiceRef serviceRef, const char* name, UInt32 port)
{
	FskErr err = kFskErrNone;
	KprZeroconfPlatformService self = NULL;
	SInt32 fd = DNSServiceRefSockFD(serviceRef);
	bailIfError(FskMemPtrNewClear(sizeof(KprZeroconfPlatformServiceRecord), &self));
	FskInstrumentedItemNew(self, NULL, &KprZeroconfPlatformServiceInstrumentation);
	self->owner = owner;
	self->serviceRef = serviceRef;
	if (name) {
		self->name = FskStrDoCopy(name);
		bailIfNULL(self->name);
	}
	self->port = port;
#if KPR_ZEROCONF_EMBEDDED
	if (!gEmbeddedServiceCount) {
		FskTimeCallbackNew(&gEmbeddedServiceTimer);
		FskTimeCallbackScheduleFuture(gEmbeddedServiceTimer, 0, 0, KprZeroconfEmbeddedCallback, NULL);
	}
	gEmbeddedServiceCount++;
#elif TARGET_OS_MAC
	CFSocketContext context;
	FskMemSet(&context, 0, sizeof(context));
	context.info = (void*)serviceRef;
	self->socket = CFSocketCreateWithNative(kCFAllocatorDefault, fd, kCFSocketReadCallBack, KprZeroconfPlatformCallBack, &context);
	self->source = CFSocketCreateRunLoopSource(NULL, self->socket, 0);
	CFRunLoopAddSource(CFRunLoopGetCurrent(), self->source, kCFRunLoopCommonModes);
#else
	self->source = FskThreadCreateDataSource(fd);
	FskSocketActivate(self->source, true);
	FskThreadAddDataHandler(&self->handler, self->source, KprZeroconfPlatformCallBack, true, false, serviceRef);
#endif
	*it = self;
	return err;
bail:
	if (err) {
		KprZeroconfPlatformServiceDispose(self);
	}
	return err;
}
Example #9
0
FskErr KprMQTTQueueNew(KprMQTTQueue *it, UInt16 resendInterval, KprMQTTQueueCallback callback, void *refcon)
{
	FskErr err = kFskErrNone;
	KprMQTTQueue self;

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

	FskTimeCallbackNew(&self->resendCallback);
	bailIfNULL(self->resendCallback);

	self->refcon = refcon;
	self->callback = callback;
	self->resendInterval = resendInterval;
	self->messageId = 1;
	self->pause = true;

	*it = self;
	self = NULL;

bail:
	KprMQTTQueueDispose(self);
	return err;
}