void xsToolResolvePath(xsMachine* the) { #if mxWindows char* srcPath; char* aSlash; char aPath[1024]; DWORD attributes; int aResult = 0; srcPath = xsToString(xsArg(0)); aSlash = srcPath; while (*aSlash) { if (*aSlash == '/') *aSlash = '\\'; aSlash++; } if (_fullpath(aPath, srcPath, 1024) != NULL) { attributes = GetFileAttributes(aPath); if (attributes != 0xFFFFFFFF) { if (xsToInteger(xsArgc) == 1) aResult = 1; else if (xsToBoolean(xsArg(1))) aResult = (attributes & FILE_ATTRIBUTE_DIRECTORY) ? 1 : 0; else aResult = (attributes & FILE_ATTRIBUTE_DIRECTORY) ? 0 : 1; } } #else char* srcPath; char aPath[PATH_MAX]; struct stat a_stat; int aResult = 0; srcPath = xsToString(xsArg(0)); if (realpath(srcPath, aPath) != NULL) { if (stat(aPath, &a_stat) == 0) { if (xsToInteger(xsArgc) == 1) aResult = 1; else if (xsToBoolean(xsArg(1))) aResult = S_ISDIR(a_stat.st_mode) ? 1 : 0; else aResult = S_ISREG(a_stat.st_mode) ? 1 : 0; } } #endif if (aResult) { if (xsToBoolean(xsArg(1))) { aResult = strlen(aPath) - 1; if (aPath[aResult] == mxSeparator) aPath[aResult] = 0; } xsResult = xsString(aPath); } }
void xs_dbg_xstrace(xsMachine *the) { #if mxTrace gxDoTrace = xsToBoolean(xsArg(0)); #endif }
void KPR_Host(xsMachine* the) { KprCoordinatesRecord coordinates; KprHost self; xsStringValue url = xsToString(xsArg(1)); xsStringValue id = xsToString(xsArg(2)); xsBooleanValue breakOnStart = false, breakOnException = false; if (xsToInteger(xsArgc) > 3) { breakOnStart = xsToBoolean(xsArg(3)); if (xsToInteger(xsArgc) > 4) breakOnException = xsToBoolean(xsArg(4)); } xsSlotToKprCoordinates(the, &xsArg(0), &coordinates); xsThrowIfFskErr(KprHostNew(&self, &coordinates, url, id, breakOnStart, breakOnException)); kprContentConstructor(KPR_Host); }
void xs_i2c_readWord(xsMachine *the) { wm_i2c *i2c = xsGetHostData(xsThis); int reg; int nbytes = xsToInteger(xsArg(1)); int lsbFirst = xsToInteger(xsArgc) > 2 ? xsToBoolean(xsArg(2)) : 0; uint8_t data[4]; switch (xsTypeOf(xsArg(0))) { case xsNumberType: case xsIntegerType: reg = xsToInteger(xsArg(0)); break; default: reg = -1; break; } if (nbytes > 4) nbytes = 4; if (mc_i2c_read(i2c, reg, data, nbytes) == 0) { uint32_t res = 0; int i; if (lsbFirst) { for (i = 0; i < nbytes; i++) res |= data[i] << (i * 8); } else { for (i = 0; i < nbytes; i++) res = (res << 8) | data[i]; } xsSetInteger(xsResult, res); } }
void xs_env_dump(xsMachine *the) { mc_env_t *env = xsGetHostData(xsThis); mc_env_dump(env, xsToInteger(xsArgc) > 0 && xsToBoolean(xsArg(0))); }
void xs_dbg_stress(xsMachine *the) { #ifdef mxStress gxStress = xsToBoolean(xsArg(0)); #endif }
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); }
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); } }
void xs_ecb_init(xsMachine *the) { crypt_mode_t *mode = xsGetHostData(xsThis); mode->encrypt = ecb_encrypt; mode->decrypt = ecb_decrypt; mode->setIV = NULL; mode->maxSlop = mode->cipher->blockSize; mode->direction = -1; mode->padding = xsToInteger(xsArgc) > 0 && xsToBoolean(xsArg(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)); }
void KPR_host_set_debugging(xsMachine* the) { #ifdef mxDebug KprHost self = xsGetHostData(xsThis); KprApplication application = (KprApplication)self->first; Boolean flag = xsToBoolean(xsArg(0)); xsBeginHost(application->the); { (void)xsCall1(xsGet(xsGet(xsGlobal, xsID("xs")), xsID("debug")), xsID("setConnected"), xsBoolean(flag)); } xsEndHost(application->the); #endif }
void KPR_host_set_profiling(xsMachine* the) { KprHost self = xsGetHostData(xsThis); KprApplication application = (KprApplication)self->first; Boolean flag = xsToBoolean(xsArg(0)); xsBeginHost(application->the); { if (flag) (void)xsCall0(xsGet(xsGlobal, xsID("xs")), xsID("startProfiling")); else (void)xsCall0(xsGet(xsGlobal, xsID("xs")), xsID("stopProfiling")); } xsEndHost(application->the); }
void xs_cbc_init(xsMachine *the) { crypt_mode_t *mode = xsGetHostData(xsThis); int ac = xsToInteger(xsArgc); mode->encrypt = cbc_encrypt; mode->decrypt = cbc_decrypt; mode->direction = -1; mode->maxSlop = mode->cipher->blockSize; memset(mode->em_buf, 0, sizeof(mode->em_buf)); if (ac > 0 && xsTest(xsArg(0))) { /* iv */ size_t ivsize = xsGetArrayBufferLength(xsArg(0)); void *iv = xsToArrayBuffer(xsArg(0)); cbc_setIV(mode, iv, ivsize); } mode->padding = ac > 1 && xsToBoolean(xsArg(1)); }
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); } }
void xs_i2c_constructor(xsMachine *the) { wm_i2c *i2c; int ac = xsToInteger(xsArgc); int port = xsToInteger(xsArg(0)); uint8_t addr = (uint8_t)xsToInteger(xsArg(1)); int fast = ac > 2 ? xsToBoolean(xsArg(2)) : false; if ((i2c = mc_malloc(sizeof(wm_i2c))) == NULL) mc_xs_throw(the, "I2C: no mem"); i2c->port = port; i2c->reg = (i2c_reg_t *)i2cAddr[port]; i2c->slave_addr = addr; i2c->fast = fast; i2c->txtout = i2c->rxtout = DEFAULT_TOUT; i2c->scl_low = fast ? MIN_SCL_LOW_TIME_FAST : MIN_SCL_LOW_TIME_STANDARD; i2c->scl_high = fast ? MIN_SCL_HIGH_TIME_FAST : MIN_SCL_HIGH_TIME_STANDARD; i2c_init(i2c); xsSetHostData(xsThis, i2c); }
FskErr FskSSLFlush(void *a) { FskSSL *fssl = a; FskErr err = kFskErrNone; if (fssl->skt == NULL) return kFskErrOperationFailed; xsBeginHost(fssl->vm->the); xsTry { xsVars(1); xsCall1_noResult(fssl->socket, xsID("attachData"), xsHostData(fssl->skt)); xsResult = xsCall1(fssl->ssl, xsID("flush"), fssl->socket); err = xsToBoolean(xsResult) ? kFskErrNone: kFskErrNoData; } xsCatch { if (xsHas(xsException, xsID("code"))) err = xsToInteger(xsGet(xsException, xsID("code"))); if (err == kFskErrNone) err = kFskErrOperationFailed; } xsEndHost(fssl->vm->the); return err; }
void KPR_mqttclient_connect(xsMachine* the) { KPR_MQTTClientRecord *self = xsGetHostData(xsThis); xsIntegerValue c = xsToInteger(xsArgc); char *host; UInt16 port; KprMQTTClientConnectOptions options; if (c >= 1) { host = xsToString(xsArg(0)); } else { host = "localhost"; } if (c >= 2) { port = xsToInteger(xsArg(1)); } else { port = 1883; } options.isSecure = (port == 1884); options.keepAlive = 60; options.password = NULL; options.username = NULL; options.willIsRetained = false; options.willQualityOfService = 0; options.willTopic = NULL; options.willPayload = NULL; options.willPayloadLength = 0; if (c >= 3) { xsVars(1); xsEnterSandbox(); { if (xsHas(xsArg(2), xsID("secure"))) options.isSecure = xsToBoolean(the->scratch); if (xsHas(xsArg(2), xsID("keepAlive"))) options.keepAlive = xsToInteger(the->scratch); if (xsHas(xsArg(2), xsID("username"))) options.username = xsToString(the->scratch); if (xsHas(xsArg(2), xsID("password"))) options.password = xsToString(the->scratch); if (xsHas(xsArg(2), xsID("will"))) { xsVar(0) = the->scratch; if (xsHas(xsVar(0), xsID("topic"))) options.willTopic = xsToString(the->scratch); if (xsHas(xsVar(0), xsID("qos"))) options.willQualityOfService = xsToInteger(the->scratch); if (xsHas(xsVar(0), xsID("retain"))) options.willIsRetained = xsToBoolean(the->scratch); if (xsHas(xsVar(0), xsID("data"))) { xsVar(0) = the->scratch; if (isChunk(xsVar(0))) { options.willPayload = xsGetHostData(xsVar(0)); options.willPayloadLength = xsToInteger(xsGet(xsVar(0), xsID_length)); } else { options.willPayload = xsToString(xsVar(0)); options.willPayloadLength = FskStrLen(options.willPayload); } } } } xsLeaveSandbox(); } if (options.willQualityOfService > 2 || (options.willTopic && !KprMQTTIsValidTopic(options.willTopic, false))) { xsThrowIfFskErr(kFskErrBadData); } xsThrowIfFskErr(KprMQTTClientConnect(self->client, host, port, &options)); }
void KPR_mqttclient_publish(xsMachine* the) { KPR_MQTTClientRecord *self = xsGetHostData(xsThis); FskErr err = kFskErrNone; xsIntegerValue c = xsToInteger(xsArgc); char *topic = NULL; void *payload = NULL; UInt32 payloadLength = 0; UInt8 qos = 0; Boolean retain = false, hasPayload = false, hasQos = false, hasRetain = false; UInt16 token; /* Case 1. topic, string_or_chunk, qos, retain Case 2 topic, { payload: string_or_chunk}, qos, retain Case 3 topic, { payload: string_or_chunk, qos: 0, retain: true } */ if (c < 1) goto invalidParams; topic = xsToString(xsArg(0)); if (c >= 2) { if (isChunk(xsArg(1))) { payload = xsGetHostData(xsArg(1)); payloadLength = xsToInteger(xsGet(xsArg(1), xsID_length)); } else if (isObject(xsArg(1))) { xsVars(1); xsEnterSandbox(); { hasPayload = xsHas(xsArg(1), xsID("data")); if (hasPayload) xsVar(0) = the->scratch; hasQos = xsHas(xsArg(1), xsID("qos")); if (hasQos) qos = xsToInteger(the->scratch); hasRetain = xsHas(xsArg(1), xsID("retain")); if (hasRetain) retain = xsToInteger(the->scratch); } xsLeaveSandbox(); if (hasPayload) { if (isChunk(xsVar(0))) { payload = xsGetHostData(xsVar(0)); payloadLength = xsToInteger(xsGet(xsVar(0), xsID_length)); } else { payload = xsToString(xsVar(0)); payloadLength = FskStrLen(payload); } } } else { payload = xsToString(xsArg(1)); payloadLength = FskStrLen(payload); } } if (c >= 3 && !hasQos) { qos = xsToInteger(xsArg(2)); } if (c >= 4 && !hasRetain) { retain = xsToBoolean(xsArg(3)); } if (!KprMQTTIsValidTopic(topic, false)) goto badParam; if (qos > 2) goto badParam; bailIfError(KprMQTTClientPublish(self->client, topic, payload, payloadLength, qos, retain, &token)); xsResult = xsInteger(token); goto bail; invalidParams: err = kFskErrInvalidParameter; goto bail; badParam: err = kFskErrBadData; goto bail; bail: xsThrowIfFskErr(err); }
void xs_dbg_gcEnable(xsMachine *the) { xsBooleanValue flag = xsToBoolean(xsArg(0)); xsEnableGarbageCollection(flag); }