static void applyLedAnimationLayer(bool updateNow, uint32_t *timer) { static uint8_t frameCounter = 0; const int animationFrames = ledGridHeight; if(updateNow) { frameCounter = (frameCounter + 1 < animationFrames) ? frameCounter + 1 : 0; *timer += LED_STRIP_HZ(20); } if (ARMING_FLAG(ARMED)) return; int previousRow = frameCounter > 0 ? frameCounter - 1 : animationFrames - 1; int currentRow = frameCounter; int nextRow = (frameCounter + 1 < animationFrames) ? frameCounter + 1 : 0; for (int ledIndex = 0; ledIndex < ledCounts.count; ledIndex++) { const ledConfig_t *ledConfig = &masterConfig.ledConfigs[ledIndex]; if (ledGetY(ledConfig) == previousRow) { setLedHsv(ledIndex, getSC(LED_SCOLOR_ANIMATION)); scaleLedValue(ledIndex, 50); } else if (ledGetY(ledConfig) == currentRow) { setLedHsv(ledIndex, getSC(LED_SCOLOR_ANIMATION)); } else if (ledGetY(ledConfig) == nextRow) { scaleLedValue(ledIndex, 50); } } }
static void applyLedGpsLayer(bool updateNow, uint32_t *timer) { static uint8_t gpsFlashCounter = 0; static uint8_t gpsPauseCounter = 0; const uint8_t blinkPauseLength = 4; if (updateNow) { if (gpsPauseCounter > 0) { gpsPauseCounter--; } else if (gpsFlashCounter >= GPS_numSat) { gpsFlashCounter = 0; gpsPauseCounter = blinkPauseLength; } else { gpsFlashCounter++; gpsPauseCounter = 1; } *timer += LED_STRIP_HZ(2.5); } const hsvColor_t *gpsColor; if (GPS_numSat == 0 || !sensors(SENSOR_GPS)) { gpsColor = getSC(LED_SCOLOR_GPSNOSATS); } else { bool colorOn = gpsPauseCounter == 0; // each interval starts with pause if (STATE(GPS_FIX)) { gpsColor = colorOn ? getSC(LED_SCOLOR_GPSLOCKED) : getSC(LED_SCOLOR_BACKGROUND); } else { gpsColor = colorOn ? getSC(LED_SCOLOR_GPSNOLOCK) : getSC(LED_SCOLOR_GPSNOSATS); } } applyLedHsv(LED_MOV_FUNCTION(LED_FUNCTION_GPS), gpsColor); }
static void applyLedFixedLayers() { for (int ledIndex = 0; ledIndex < ledCounts.count; ledIndex++) { const ledConfig_t *ledConfig = &ledStripConfig()->ledConfigs[ledIndex]; hsvColor_t color = *getSC(LED_SCOLOR_BACKGROUND); int fn = ledGetFunction(ledConfig); int hOffset = HSV_HUE_MAX; switch (fn) { case LED_FUNCTION_COLOR: color = ledStripConfig()->colors[ledGetColor(ledConfig)]; break; case LED_FUNCTION_FLIGHT_MODE: for (unsigned i = 0; i < ARRAYLEN(flightModeToLed); i++) if (!flightModeToLed[i].flightMode || FLIGHT_MODE(flightModeToLed[i].flightMode)) { const hsvColor_t *directionalColor = getDirectionalModeColor(ledIndex, &ledStripConfig()->modeColors[flightModeToLed[i].ledMode]); if (directionalColor) { color = *directionalColor; } break; // stop on first match } break; case LED_FUNCTION_ARM_STATE: color = ARMING_FLAG(ARMED) ? *getSC(LED_SCOLOR_ARMED) : *getSC(LED_SCOLOR_DISARMED); break; case LED_FUNCTION_BATTERY: color = HSV(RED); hOffset += scaleRange(calculateBatteryPercentage(), 0, 100, -30, 120); break; case LED_FUNCTION_RSSI: color = HSV(RED); hOffset += scaleRange(rssi * 100, 0, 1023, -30, 120); break; default: break; } if (ledGetOverlayBit(ledConfig, LED_OVERLAY_THROTTLE)) { hOffset += scaledAux; } color.h = (color.h + hOffset) % (HSV_HUE_MAX + 1); setLedHsv(ledIndex, &color); } }
int tls_sc_add_ca_cert(lua_State *L) { tls_sc_t *ctx = getSC(L); X509 *x509; int newCAStore = FALSE; if (!ctx->ca_store) { ctx->ca_store = X509_STORE_new(); newCAStore = TRUE; } x509 = _lua_load_x509(L, 2); if (!x509) { lua_pushboolean(L, 0); return 1; } X509_STORE_add_cert(ctx->ca_store, x509); SSL_CTX_add_client_CA(ctx->ctx, x509); X509_free(x509); if (newCAStore) { SSL_CTX_set_cert_store(ctx->ctx, ctx->ca_store); } lua_pushboolean(L, 1); return 1; }
static void applyLedBatteryLayer(bool updateNow, uint32_t *timer) { static bool flash = false; int state; int timeOffset = 1; if (updateNow) { state = getBatteryState(); switch (state) { case BATTERY_OK: flash = false; timeOffset = 1; break; case BATTERY_WARNING: timeOffset = 2; break; default: timeOffset = 8; break; } flash = !flash; } *timer += LED_STRIP_HZ(timeOffset); if (!flash) { hsvColor_t *bgc = getSC(LED_SCOLOR_BACKGROUND); applyLedHsv(LED_MOV_FUNCTION(LED_FUNCTION_BATTERY), bgc); } }
static void applyLedRssiLayer(bool updateNow, uint32_t *timer) { static bool flash = false; int state; int timeOffset = 0; if (updateNow) { state = (rssi * 100) / 1023; if (state > 50) { flash = false; timeOffset = 1; } else if (state > 20) { timeOffset = 2; } else { timeOffset = 8; } flash = !flash; } *timer += LED_STRIP_HZ(timeOffset); if (!flash) { hsvColor_t *bgc = getSC(LED_SCOLOR_BACKGROUND); applyLedHsv(LED_MOV_FUNCTION(LED_FUNCTION_RSSI), bgc); } }
// blink twice, then wait ; either always or just when landing static void applyLedBlinkLayer(bool updateNow, uint32_t *timer) { const uint16_t blinkPattern = 0x8005; // 0b1000000000000101; static uint16_t blinkMask; if (updateNow) { blinkMask = blinkMask >> 1; if (blinkMask <= 1) blinkMask = blinkPattern; *timer += LED_STRIP_HZ(10); } bool ledOn = (blinkMask & 1); // b_b_____... if (!ledOn) { for (int i = 0; i < ledCounts.count; ++i) { const ledConfig_t *ledConfig = &ledConfigs[i]; if (ledGetOverlayBit(ledConfig, LED_OVERLAY_BLINK) || (ledGetOverlayBit(ledConfig, LED_OVERLAY_LANDING_FLASH) && scaledThrottle < 55 && scaledThrottle > 10)) { setLedHsv(i, getSC(LED_SCOLOR_BLINKBACKGROUND)); } } } }
static int tls_sc_set_cert(lua_State *L) { tls_sc_t *ctx; BIO *bio; const char *keystr = NULL; size_t klen = 0; int rv; ctx = getSC(L); keystr = luaL_checklstring(L, 2, &klen); bio = str2bio(keystr, klen); if (!bio) { return luaL_error(L, "tls_sc_set_key: Failed to convert Cert into a BIO"); } ERR_clear_error(); rv = SSL_CTX_use_certificate_chain(ctx->ctx, bio); if (!rv) { BIO_free(bio); return tls_fatal_error(L); } BIO_free(bio); return 0; }
static int tls_sc_set_key(lua_State *L) { tls_sc_t *ctx; EVP_PKEY* key; BIO *bio; const char *passpharse = NULL; const char *keystr = NULL; size_t klen = 0; size_t plen = 0; ctx = getSC(L); keystr = luaL_checklstring(L, 2, &klen); passpharse = luaL_optlstring(L, 3, NULL, &plen); bio = str2bio(keystr, klen); if (!bio) { return luaL_error(L, "tls_sc_set_key: Failed to convert Key into a BIO"); } ERR_clear_error(); /* If the 3rd arg is NULL, the 4th arg is treated as a const char* istead of void* */ key = PEM_read_bio_PrivateKey(bio, NULL, NULL, (void*)passpharse); if (!key) { return tls_fatal_error(L); } SSL_CTX_use_PrivateKey(ctx->ctx, key); EVP_PKEY_free(key); BIO_free(bio); return 0; }
static void applyLedRssiLayer(bool updateNow, timeUs_t *timer) { static bool flash = false; int timerDelay = HZ_TO_US(1); if (updateNow) { int state = (rssi * 100) / 1023; if (state > 50) { flash = true; timerDelay = HZ_TO_US(1); } else if (state > 20) { flash = !flash; timerDelay = HZ_TO_US(2); } else { flash = !flash; timerDelay = HZ_TO_US(8); } } *timer += timerDelay; if (!flash) { const hsvColor_t *bgc = getSC(LED_SCOLOR_BACKGROUND); applyLedHsv(LED_MOV_FUNCTION(LED_FUNCTION_RSSI), bgc); } }
static void applyLedBatteryLayer(bool updateNow, timeUs_t *timer) { static bool flash = false; int timerDelayUs = HZ_TO_US(1); if (updateNow) { switch (getBatteryState()) { case BATTERY_OK: flash = true; timerDelayUs = HZ_TO_US(1); break; case BATTERY_WARNING: flash = !flash; timerDelayUs = HZ_TO_US(2); break; default: flash = !flash; timerDelayUs = HZ_TO_US(8); break; } } *timer += timerDelayUs; if (!flash) { const hsvColor_t *bgc = getSC(LED_SCOLOR_BACKGROUND); applyLedHsv(LED_MOV_FUNCTION(LED_FUNCTION_BATTERY), bgc); } }
static int tls_sc_add_crl(lua_State *L) { tls_sc_t *ctx = getSC(L); X509_CRL *x509; BIO *bio; bio = _lua_load_bio(L, -1); if (!bio) { lua_pushboolean(L, 0); return 1; } x509 = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL); if (x509 == NULL) { BIO_free(bio); lua_pushboolean(L, 0); return 1; } X509_STORE_add_crl(ctx->ca_store, x509); X509_STORE_set_flags(ctx->ca_store, X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL); BIO_free(bio); X509_CRL_free(x509); lua_pushboolean(L, 1); return 1; }
static int tls_sc_set_options(lua_State *L) { tls_sc_t *ctx; uint64_t opts = 0; ctx = getSC(L); opts = luaL_checknumber(L, 2); SSL_CTX_set_options(ctx->ctx, opts); return 0; }
static int tls_sc_close(lua_State *L) { tls_sc_t *sc = getSC(L); if (sc->ctx) { if (sc->ctx->cert_store == root_cert_store) { sc->ctx->cert_store = NULL; } SSL_CTX_free(sc->ctx); sc->ctx = NULL; } return 0; }
static int tls_sc_add_root_certs(lua_State *L) { int i; tls_sc_t *ctx = getSC(L); ERR_clear_error(); if (!root_cert_store) { root_cert_store = X509_STORE_new(); for (i = 0; root_certs[i]; i++) { BIO *bp = BIO_new(BIO_s_mem()); X509 *x509; if (!BIO_write(bp, root_certs[i], strlen(root_certs[i]))) { printf("error writing cert %s\n", root_certs[i]); BIO_free(bp); lua_pushboolean(L, 0); return 1; } x509 = PEM_read_bio_X509(bp, NULL, 0, NULL); if (x509 == NULL) { char buf[1024]; ERR_error_string(ERR_get_error(), buf); printf("error writing x509 cert %s\n", buf); BIO_free(bp); lua_pushboolean(L, 0); return 1; } X509_STORE_add_cert(root_cert_store, x509); BIO_free(bp); X509_free(x509); } } ctx->ca_store = root_cert_store; SSL_CTX_set_cert_store(ctx->ctx, ctx->ca_store); lua_pushboolean(L, 1); return 1; }
static int tls_sc_set_ciphers(lua_State *L) { tls_sc_t *ctx; const char *cipherstr = NULL; int rv; ctx = getSC(L); cipherstr = luaL_checkstring(L, 2); ERR_clear_error(); rv = SSL_CTX_set_cipher_list(ctx->ctx, cipherstr); if (rv == 0) { return tls_fatal_error(L); } return 0; }
static int tls_sc_add_trusted_cert(lua_State *L) { tls_sc_t *ctx; BIO *bio; const char *certstr = NULL; size_t clen = 0; int rv; ctx = getSC(L); if (ctx->ca_store == NULL) { /* TODO: better handling of global CA cert list */ ctx->ca_store = X509_STORE_new(); SSL_CTX_set_cert_store(ctx->ctx, ctx->ca_store); } certstr = luaL_checklstring(L, 2, &clen); bio = str2bio(certstr, clen); if (!bio) { return luaL_error(L, "tls_sc_add_trusted_cert: Failed to convert Cert into a BIO"); } ERR_clear_error(); rv = X509_STORE_load_bio(ctx->ca_store, bio); if (!rv) { BIO_free(bio); return tls_fatal_error(L); } BIO_free(bio); return 0; }
// blink twice, then wait ; either always or just when landing static void applyLedBlinkLayer(bool updateNow, timeUs_t *timer) { const uint16_t blinkPattern = 0x8005; // 0b1000000000000101; static uint16_t blinkMask; if (updateNow) { blinkMask = blinkMask >> 1; if (blinkMask <= 1) blinkMask = blinkPattern; *timer += HZ_TO_US(10); } bool ledOn = (blinkMask & 1); // b_b_____... if (!ledOn) { for (int i = 0; i < ledCounts.count; ++i) { const ledConfig_t *ledConfig = &ledStripConfig()->ledConfigs[i]; if (ledGetOverlayBit(ledConfig, LED_OVERLAY_BLINK)) { setLedHsv(i, getSC(LED_SCOLOR_BLINKBACKGROUND)); } } } }
static void applyLedFixedLayers(void) { for (int ledIndex = 0; ledIndex < ledCounts.count; ledIndex++) { const ledConfig_t *ledConfig = &ledStripConfig()->ledConfigs[ledIndex]; hsvColor_t color = *getSC(LED_SCOLOR_BACKGROUND); int fn = ledGetFunction(ledConfig); int hOffset = HSV_HUE_MAX + 1; switch (fn) { case LED_FUNCTION_COLOR: color = ledStripConfig()->colors[ledGetColor(ledConfig)]; hsvColor_t nextColor = ledStripConfig()->colors[(ledGetColor(ledConfig) + 1 + LED_CONFIGURABLE_COLOR_COUNT) % LED_CONFIGURABLE_COLOR_COUNT]; hsvColor_t previousColor = ledStripConfig()->colors[(ledGetColor(ledConfig) - 1 + LED_CONFIGURABLE_COLOR_COUNT) % LED_CONFIGURABLE_COLOR_COUNT]; if (ledGetOverlayBit(ledConfig, LED_OVERLAY_THROTTLE)) { //smooth fade with selected Aux channel of all HSV values from previousColor through color to nextColor int centerPWM = (PWM_RANGE_MIN + PWM_RANGE_MAX) / 2; if (auxInput < centerPWM) { color.h = scaleRange(auxInput, PWM_RANGE_MIN, centerPWM, previousColor.h, color.h); color.s = scaleRange(auxInput, PWM_RANGE_MIN, centerPWM, previousColor.s, color.s); color.v = scaleRange(auxInput, PWM_RANGE_MIN, centerPWM, previousColor.v, color.v); } else { color.h = scaleRange(auxInput, centerPWM, PWM_RANGE_MAX, color.h, nextColor.h); color.s = scaleRange(auxInput, centerPWM, PWM_RANGE_MAX, color.s, nextColor.s); color.v = scaleRange(auxInput, centerPWM, PWM_RANGE_MAX, color.v, nextColor.v); } } break; case LED_FUNCTION_FLIGHT_MODE: for (unsigned i = 0; i < ARRAYLEN(flightModeToLed); i++) if (!flightModeToLed[i].flightMode || FLIGHT_MODE(flightModeToLed[i].flightMode)) { const hsvColor_t *directionalColor = getDirectionalModeColor(ledIndex, &ledStripConfig()->modeColors[flightModeToLed[i].ledMode]); if (directionalColor) { color = *directionalColor; } break; // stop on first match } break; case LED_FUNCTION_ARM_STATE: color = ARMING_FLAG(ARMED) ? *getSC(LED_SCOLOR_ARMED) : *getSC(LED_SCOLOR_DISARMED); break; case LED_FUNCTION_BATTERY: color = HSV(RED); hOffset += scaleRange(calculateBatteryPercentageRemaining(), 0, 100, -30, 120); break; case LED_FUNCTION_RSSI: color = HSV(RED); hOffset += scaleRange(getRssi() * 100, 0, 1023, -30, 120); break; default: break; } if ((fn != LED_FUNCTION_COLOR) && ledGetOverlayBit(ledConfig, LED_OVERLAY_THROTTLE)) { hOffset += scaleRange(auxInput, PWM_RANGE_MIN, PWM_RANGE_MAX, 0, HSV_HUE_MAX + 1); } color.h = (color.h + hOffset) % (HSV_HUE_MAX + 1); setLedHsv(ledIndex, &color); } }