Skip to content

Commit 65fe0ba

Browse files
QR decode: try catching
1 parent c2b7b76 commit 65fe0ba

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

c_mpos/src/quirc_decode.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "py/obj.h"
66
#include "py/runtime.h"
77
#include "py/mperrno.h"
8+
#include "py/nlr.h" // Include for nlr_buf_t
89

910
#ifdef __xtensa__
1011
#include "freertos/FreeRTOS.h"
@@ -45,11 +46,13 @@ static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) {
4546
if (!qr) {
4647
mp_raise_OSError(MP_ENOMEM);
4748
}
49+
QRDECODE_DEBUG_PRINT("qrdecode: Allocated quirc object\n");
4850

4951
if (quirc_resize(qr, width, height) < 0) {
5052
quirc_destroy(qr);
5153
mp_raise_OSError(MP_ENOMEM);
5254
}
55+
QRDECODE_DEBUG_PRINT("qrdecode: Resized quirc object\n");
5356

5457
uint8_t *image;
5558
image = quirc_begin(qr, NULL, NULL);
@@ -59,6 +62,7 @@ static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) {
5962
int count = quirc_count(qr);
6063
if (count == 0) {
6164
quirc_destroy(qr);
65+
QRDECODE_DEBUG_PRINT("qrdecode: No QR code found, freed quirc object\n");
6266
mp_raise_ValueError(MP_ERROR_TEXT("no QR code found"));
6367
}
6468

@@ -67,6 +71,7 @@ static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) {
6771
quirc_destroy(qr);
6872
mp_raise_OSError(MP_ENOMEM);
6973
}
74+
QRDECODE_DEBUG_PRINT("qrdecode: Allocated quirc_code\n");
7075
quirc_extract(qr, 0, code);
7176

7277
struct quirc_data *data = (struct quirc_data *)malloc(sizeof(struct quirc_data));
@@ -75,11 +80,14 @@ static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) {
7580
quirc_destroy(qr);
7681
mp_raise_OSError(MP_ENOMEM);
7782
}
83+
QRDECODE_DEBUG_PRINT("qrdecode: Allocated quirc_data\n");
84+
7885
int err = quirc_decode(code, data);
7986
if (err != QUIRC_SUCCESS) {
8087
free(data);
8188
free(code);
8289
quirc_destroy(qr);
90+
QRDECODE_DEBUG_PRINT("qrdecode: Decode failed, freed data, code, and quirc object\n");
8391
mp_raise_TypeError(MP_ERROR_TEXT("failed to decode QR code"));
8492
}
8593

@@ -88,6 +96,7 @@ static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) {
8896
free(data);
8997
free(code);
9098
quirc_destroy(qr);
99+
QRDECODE_DEBUG_PRINT("qrdecode: Freed data, code, and quirc object, returning result\n");
91100
return result;
92101
}
93102

@@ -116,6 +125,7 @@ static mp_obj_t qrdecode_rgb565(mp_uint_t n_args, const mp_obj_t *args) {
116125
if (!gray_buffer) {
117126
mp_raise_OSError(MP_ENOMEM);
118127
}
128+
QRDECODE_DEBUG_PRINT("qrdecode_rgb565: Allocated gray_buffer (%u bytes)\n", width * height * sizeof(uint8_t));
119129

120130
uint16_t *rgb565 = (uint16_t *)bufinfo.buf;
121131
for (size_t i = 0; i < (size_t)(width * height); i++) {
@@ -132,8 +142,20 @@ static mp_obj_t qrdecode_rgb565(mp_uint_t n_args, const mp_obj_t *args) {
132142
mp_obj_new_int(height)
133143
};
134144

135-
mp_obj_t result = qrdecode(3, gray_args);
136-
free(gray_buffer);
145+
mp_obj_t result = MP_OBJ_NULL;
146+
nlr_buf_t exception_handler;
147+
if (nlr_push(&exception_handler) == 0) {
148+
result = qrdecode(3, gray_args);
149+
nlr_pop();
150+
QRDECODE_DEBUG_PRINT("qrdecode_rgb565: qrdecode succeeded, freeing gray_buffer\n");
151+
free(gray_buffer);
152+
} else {
153+
QRDECODE_DEBUG_PRINT("qrdecode_rgb565: Exception caught, freeing gray_buffer\n");
154+
free(gray_buffer);
155+
nlr_pop();
156+
nlr_raise(exception_handler.ret_val);
157+
}
158+
137159
return result;
138160
}
139161

internal_filesystem/apps/com.example.camtest/assets/camtest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def qrdecode_live():
5454
try:
5555
import qrdecode
5656
result = qrdecode.qrdecode_rgb565(current_cam_buffer, width, height)
57+
#raise ValueError('A very specific bad thing happened.')
5758
result = remove_bom(result)
5859
result = print_qr_buffer(result)
5960
print(f"QR decoding found: {result}")
@@ -65,6 +66,8 @@ def qrdecode_live():
6566
except TypeError as e:
6667
print("QR TypeError: ", e)
6768
status_label_text = status_label_text_found
69+
except Exception as e:
70+
print("QR got other error: ", e)
6871
time.sleep_ms(100)
6972

7073

0 commit comments

Comments
 (0)