Просмотр исходного кода

rework atomic dice roller to use mbedtls and fix otg

MX 2 лет назад
Родитель
Сommit
9976b9ee18
2 измененных файлов с 71 добавлено и 84 удалено
  1. 1 0
      application.fam
  2. 70 84
      flipper_atomicdiceroller.c

+ 1 - 0
application.fam

@@ -9,5 +9,6 @@ App(
     ],
     stack_size=2 * 1024,
     fap_icon="atomicdiceroller.png",
+    fap_libs=["mbedtls"],
     fap_category="GPIO",
 )

+ 70 - 84
flipper_atomicdiceroller.c

@@ -10,7 +10,7 @@
 #include <furi_hal_power.h>
 #include <locale/locale.h>
 #include <toolbox/crc32_calc.h>
-#include <lib/toolbox/md5.h>
+#include <mbedtls/md5.h>
 
 #define SCREEN_SIZE_X 128
 #define SCREEN_SIZE_Y 64
@@ -38,8 +38,7 @@ typedef struct {
     uint8_t pause;
 } mutexStruct;
 
-static void draw_callback(Canvas* canvas, void* ctx) 
-{
+static void draw_callback(Canvas* canvas, void* ctx) {
     mutexStruct* mutexVal = ctx;
     mutexStruct mutexDraw;
     furi_mutex_acquire(mutexVal->mutex, FuriWaitForever);
@@ -54,19 +53,19 @@ static void draw_callback(Canvas* canvas, void* ctx)
     snprintf(buffer, sizeof(buffer), "%lu/64", mutexDraw.diceAvailiable);
     canvas_draw_str_aligned(canvas, SCREEN_SIZE_X, 10, AlignRight, AlignBottom, buffer);
 
-    if (mutexDraw.method == 0) canvas_draw_str_aligned(canvas, 0, 20, AlignLeft, AlignBottom, "Hash: CRC32");
-    else canvas_draw_str_aligned(canvas, 0, 20, AlignLeft, AlignBottom, "Hash: MD5");
+    if(mutexDraw.method == 0)
+        canvas_draw_str_aligned(canvas, 0, 20, AlignLeft, AlignBottom, "Hash: CRC32");
+    else
+        canvas_draw_str_aligned(canvas, 0, 20, AlignLeft, AlignBottom, "Hash: MD5");
 
-    if (mutexDraw.dice != 0 && mutexDraw.pause == 0)
-    {
+    if(mutexDraw.dice != 0 && mutexDraw.pause == 0) {
         canvas_set_font(canvas, FontBigNumbers);
         snprintf(buffer, sizeof(buffer), "%u", mutexDraw.dice);
-        canvas_draw_str_aligned(canvas, SCREEN_SIZE_X/2, 50, AlignCenter, AlignBottom, buffer);
+        canvas_draw_str_aligned(canvas, SCREEN_SIZE_X / 2, 50, AlignCenter, AlignBottom, buffer);
     }
 }
 
-static void input_callback(InputEvent* input_event, void* ctx) 
-{
+static void input_callback(InputEvent* input_event, void* ctx) {
     furi_assert(ctx);
     FuriMessageQueue* event_queue = ctx;
     EventApp event = {.type = EventTypeInput, .input = *input_event};
@@ -96,8 +95,7 @@ static void gpiocallback(void* ctx) {
     furi_message_queue_put(queue, &event, 0);
 }
 
-int32_t flipper_atomicdiceroller_app() 
-{
+int32_t flipper_atomicdiceroller_app() {
     furi_hal_bus_enable(FuriHalBusTIM2);
     LL_TIM_SetCounterMode(TIM2, LL_TIM_COUNTERMODE_UP);
     LL_TIM_SetClockDivision(TIM2, LL_TIM_CLOCKDIVISION_DIV1);
@@ -118,7 +116,7 @@ int32_t flipper_atomicdiceroller_app()
     mutexVal.method = 0;
     uint32_t counter = 0;
 
-    mutexVal.mutex= furi_mutex_alloc(FuriMutexTypeNormal);
+    mutexVal.mutex = furi_mutex_alloc(FuriMutexTypeNormal);
     if(!mutexVal.mutex) {
         furi_message_queue_free(event_queue);
         return 255;
@@ -139,10 +137,15 @@ int32_t flipper_atomicdiceroller_app()
     FuriTimer* timerPause = furi_timer_alloc(clock_tick_pause, FuriTimerTypePeriodic, event_queue);
 
     // ENABLE 5V pin
-    furi_hal_power_enable_otg();
+    uint8_t attempts = 0;
+    bool otg_was_enabled = furi_hal_power_is_otg_enabled();
+    while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
+        furi_hal_power_enable_otg();
+        furi_delay_ms(10);
+    }
 
     uint8_t diceBuffer[64];
-    for (uint8_t i=0;i<64;i++) diceBuffer[i] = 0;
+    for(uint8_t i = 0; i < 64; i++) diceBuffer[i] = 0;
 
     uint8_t diceBufferCounter = 0;
     uint8_t diceBufferPositionWrite = 0;
@@ -152,51 +155,43 @@ int32_t flipper_atomicdiceroller_app()
     uint8_t method = 0;
 
     // MD5
-    md5_context* md5_ctx = malloc(sizeof(md5_context));
+    mbedtls_md5_context md5_ctx;
     uint8_t* hash = malloc(sizeof(uint8_t) * 16);
     uint8_t* bufferTim2 = malloc(4);
-    md5_starts(md5_ctx);
+    mbedtls_md5_init(&md5_ctx);
+    mbedtls_md5_starts(&md5_ctx);
 
     uint8_t pause = 0;
 
-    while(1) 
-    {
+    while(1) {
         FuriStatus event_status = furi_message_queue_get(event_queue, &event, FuriWaitForever);
-        
+
         uint8_t screenRefresh = 0;
 
-        if (event_status == FuriStatusOk)
-        {   
-            if(event.type == EventTypeInput) 
-            {
-                if(event.input.key == InputKeyBack && event.input.type == InputTypeLong) 
-                {
+        if(event_status == FuriStatusOk) {
+            if(event.type == EventTypeInput) {
+                if(event.input.key == InputKeyBack && event.input.type == InputTypeLong) {
                     break;
-                }
-                else if (pause == 0)
-                {
-                    if (event.input.key == InputKeyOk  && event.input.type == InputTypeShort)
-                    {
-                        if (diceBufferCounter > 0)
-                        {
+                } else if(pause == 0) {
+                    if(event.input.key == InputKeyOk && event.input.type == InputTypeShort) {
+                        if(diceBufferCounter > 0) {
                             furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
                             mutexVal.dice = diceBuffer[diceBufferPositionRead];
                             mutexVal.diceAvailiable = --diceBufferCounter;
                             mutexVal.pause = 1;
                             furi_mutex_release(mutexVal.mutex);
 
-                            if (diceBufferPositionRead != 63) diceBufferPositionRead++;
-                            else diceBufferPositionRead = 0;
+                            if(diceBufferPositionRead != 63)
+                                diceBufferPositionRead++;
+                            else
+                                diceBufferPositionRead = 0;
 
                             pause = 1;
                             furi_timer_start(timerPause, 500);
                             screenRefresh = 1;
                         }
-                    }
-                    else if (event.input.key == InputKeyLeft  && event.input.type == InputTypeLong)
-                    {
-                        if (method == 1)
-                        {
+                    } else if(event.input.key == InputKeyLeft && event.input.type == InputTypeLong) {
+                        if(method == 1) {
                             method = 0;
                             diceBufferPositionWrite = 0;
                             diceBufferPositionRead = 0;
@@ -210,16 +205,13 @@ int32_t flipper_atomicdiceroller_app()
                             furi_mutex_release(mutexVal.mutex);
                             screenRefresh = 1;
                         }
-                    }
-                    else if (event.input.key == InputKeyRight && event.input.type == InputTypeLong)
-                    {
-                        if (method == 0)
-                        {
+                    } else if(event.input.key == InputKeyRight && event.input.type == InputTypeLong) {
+                        if(method == 0) {
                             method = 1;
                             diceBufferPositionWrite = 0;
                             diceBufferPositionRead = 0;
                             diceBufferCounter = 0;
-                            md5_starts(md5_ctx);
+                            mbedtls_md5_starts(&md5_ctx);
                             tickCounter = 0;
                             furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
                             mutexVal.method = 1;
@@ -230,18 +222,14 @@ int32_t flipper_atomicdiceroller_app()
                         }
                     }
                 }
-            }
-            else if (event.type == ClockEventTypeTick)
-            {
+            } else if(event.type == ClockEventTypeTick) {
                 furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
                 mutexVal.cps = counter;
                 furi_mutex_release(mutexVal.mutex);
 
                 counter = 0;
                 screenRefresh = 1;
-            }
-            else if (event.type == ClockEventTypeTickPause)
-            {
+            } else if(event.type == ClockEventTypeTickPause) {
                 furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
                 mutexVal.pause = 0;
                 furi_mutex_release(mutexVal.mutex);
@@ -250,14 +238,10 @@ int32_t flipper_atomicdiceroller_app()
 
                 pause = 0;
                 screenRefresh = 1;
-            }
-            else if (event.type == EventGPIO)
-            {
-                if (diceBufferCounter < 64)
-                {
+            } else if(event.type == EventGPIO) {
+                if(diceBufferCounter < 64) {
                     // CRC32
-                    if (method == 0)
-                    {
+                    if(method == 0) {
                         uint32_t TIM2Tick = TIM2->CNT;
                         bufferTim2[0] = (uint8_t)(TIM2Tick >> 24);
                         bufferTim2[1] = (uint8_t)(TIM2Tick >> 16);
@@ -266,21 +250,20 @@ int32_t flipper_atomicdiceroller_app()
                         CRC32 = crc32_calc_buffer(CRC32, bufferTim2, 4);
                         tickCounter++;
 
-                        if (tickCounter == 8)
-                        {
+                        if(tickCounter == 8) {
                             uint8_t localDice = CRC32 & 0b111;
 
-                            if (localDice == 0 || localDice == 7)
-                            {
+                            if(localDice == 0 || localDice == 7) {
                                 localDice = (diceBuffer[diceBufferPositionRead] >> 3) & 0b111;
                             }
 
-                            if (localDice >= 1 && localDice <= 6)
-                            {
+                            if(localDice >= 1 && localDice <= 6) {
                                 diceBuffer[diceBufferPositionWrite] = localDice;
                                 diceBufferCounter++;
-                                if (diceBufferPositionWrite != 63) diceBufferPositionWrite++;
-                                else diceBufferPositionWrite = 0;
+                                if(diceBufferPositionWrite != 63)
+                                    diceBufferPositionWrite++;
+                                else
+                                    diceBufferPositionWrite = 0;
 
                                 furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
                                 mutexVal.diceAvailiable = diceBufferCounter;
@@ -294,31 +277,29 @@ int32_t flipper_atomicdiceroller_app()
                         }
                     }
                     // MD5
-                    else
-                    {
+                    else {
                         uint32_t tick = TIM2->CNT;
                         bufferTim2[0] = (uint8_t)(tick >> 24);
                         bufferTim2[1] = (uint8_t)(tick >> 16);
                         bufferTim2[2] = (uint8_t)(tick >> 8);
                         bufferTim2[3] = (uint8_t)tick;
-                        md5_update(md5_ctx, bufferTim2, 4);
+                        mbedtls_md5_update(&md5_ctx, bufferTim2, 4);
 
                         tickCounter++;
 
-                        if (tickCounter == 32)
-                        {
-                            md5_finish(md5_ctx, hash);
+                        if(tickCounter == 32) {
+                            mbedtls_md5_finish(&md5_ctx, hash);
                             uint8_t localDice = 0;
 
-                            for (uint8_t i=0;i<16;i++)
-                            {
+                            for(uint8_t i = 0; i < 16; i++) {
                                 localDice = hash[i] & 0b111;
-                                if (localDice >= 1 && localDice <= 6)
-                                {
+                                if(localDice >= 1 && localDice <= 6) {
                                     diceBuffer[diceBufferPositionWrite] = localDice;
                                     diceBufferCounter++;
-                                    if (diceBufferPositionWrite != 63) diceBufferPositionWrite++;
-                                    else diceBufferPositionWrite = 0;
+                                    if(diceBufferPositionWrite != 63)
+                                        diceBufferPositionWrite++;
+                                    else
+                                        diceBufferPositionWrite = 0;
 
                                     furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
                                     mutexVal.diceAvailiable = diceBufferCounter;
@@ -329,7 +310,7 @@ int32_t flipper_atomicdiceroller_app()
                                 }
                             }
 
-                            md5_starts(md5_ctx);
+                            mbedtls_md5_starts(&md5_ctx);
                             tickCounter = 0;
                         }
                     }
@@ -339,23 +320,28 @@ int32_t flipper_atomicdiceroller_app()
             }
         }
 
-        if (screenRefresh == 1) view_port_update(view_port);
+        if(screenRefresh == 1) view_port_update(view_port);
     }
 
     LL_TIM_DisableCounter(TIM2);
     furi_hal_bus_disable(FuriHalBusTIM2);
 
-    free(md5_ctx);
+    mbedtls_md5_free(&md5_ctx);
+    //free(md5_ctx);
     free(bufferTim2);
     free(hash);
-        
+
     furi_record_close(RECORD_NOTIFICATION);
 
-    furi_hal_power_disable_otg();
+    if(furi_hal_power_is_otg_enabled() && !otg_was_enabled) {
+        furi_hal_power_disable_otg();
+    }
 
     furi_hal_gpio_disable_int_callback(&gpio_ext_pa7);
     furi_hal_gpio_remove_int_callback(&gpio_ext_pa7);
 
+    furi_hal_gpio_init_simple(&gpio_ext_pa7, GpioModeAnalog);
+
     furi_message_queue_free(event_queue);
     furi_mutex_free(mutexVal.mutex);
     gui_remove_view_port(gui, view_port);