From ef3492aafaad8648652dc575b6569020fb921ceb Mon Sep 17 00:00:00 2001 From: Bertho Stultiens Date: Thu, 29 Oct 2026 14:56:24 +0100 Subject: [PATCH] hal: Update matrix keyboard and lcd components to become 64-bit clean --- src/hal/components/lcd.c | 94 +++++++++++++++++++++------------- src/hal/components/matrix_kb.c | 48 +++++++++++------ 2 files changed, 88 insertions(+), 54 deletions(-) diff --git a/src/hal/components/lcd.c b/src/hal/components/lcd.c index 6c07c263a01..fa539cd6862 100644 --- a/src/hal/components/lcd.c +++ b/src/hal/components/lcd.c @@ -36,7 +36,7 @@ MODULE_DESCRIPTION("Hal-to-text component for Mesa 7I73 and similar"); MODULE_LICENSE("GPL"); #define MAX_CHAN 16 -#define MAX_ENTRY 20 +#define MAX_ENTRY 22 // Octal of 64-bit is 22 digits long typedef struct { hal_refs_u *args; @@ -47,7 +47,7 @@ typedef struct { typedef struct { lcd_page_t *pages; - unsigned num_pages; + rtapi_uint num_pages; hal_uint_t page_num; hal_uint_t out; hal_real_t contrast; @@ -104,9 +104,8 @@ int rtapi_app_main(void){ // allocate shared memory for data lcd = hal_malloc(sizeof(lcd_t)); - if (lcd == 0) { - rtapi_print_msg(RTAPI_MSG_ERR, - "lcd component: Out of Memory\n"); + if (NULL == lcd) { + rtapi_print_msg(RTAPI_MSG_ERR, "lcd: Out of Memory\n"); hal_exit(comp_id); return -1; } @@ -114,6 +113,11 @@ int rtapi_app_main(void){ // Count the instances. Very unlikely to be more than one, but... for (lcd->num_insts = 0; fmt_strings[lcd->num_insts];lcd->num_insts++){} lcd->insts = hal_malloc(lcd->num_insts * sizeof(lcd_inst_t)); + if (NULL == lcd->insts) { + rtapi_print_msg(RTAPI_MSG_ERR, "lcd: Out of Memory\n"); + hal_exit(comp_id); + return -1; + } for (i = 0; i < lcd->num_insts; i++){ lcd_inst_t *inst = &lcd->insts[i]; @@ -124,6 +128,11 @@ int rtapi_app_main(void){ if (fmt_strings[i][f] =='|') inst->num_pages++; } inst->pages = hal_malloc(inst->num_pages * sizeof(lcd_page_t)); + if (NULL == inst->pages) { + rtapi_print_msg(RTAPI_MSG_ERR, "lcd: Out of Memory\n"); + hal_exit(comp_id); + return -1; + } //second pass f1 = k = p = 0; @@ -143,6 +152,11 @@ int rtapi_app_main(void){ inst->pages[p].length = f - f1 + 2; inst->pages[p].args = hal_malloc(inst->pages[p].num_args * sizeof(*inst->pages[p].args)); + if (NULL == inst->pages[p].fmt || NULL == inst->pages[p].args) { + rtapi_print_msg(RTAPI_MSG_ERR, "lcd: Out of Memory\n"); + hal_exit(comp_id); + return -1; + } f1 = f + 2; { int a = -1, s = -1; @@ -165,7 +179,7 @@ int rtapi_app_main(void){ break; case 'u': case 'c': - retval = hal_pin_new_ui32(comp_id, HAL_IN, + retval = hal_pin_new_uint(comp_id, HAL_IN, &(inst->pages[p].args[a].u), 0, "lcd.%02i.page.%02i.arg.%02i", i, p, a); @@ -175,7 +189,7 @@ int rtapi_app_main(void){ break; case 's': - retval = hal_pin_new_si32(comp_id, HAL_IN, + retval = hal_pin_new_sint(comp_id, HAL_IN, &(inst->pages[p].args[a].s), 0, "lcd.%02i.page.%02i.arg.%02i", i, p, a); @@ -207,13 +221,13 @@ int rtapi_app_main(void){ } for (i = 0; i < lcd->num_insts; i++){ - retval = hal_pin_new_ui32(comp_id, HAL_IN, &(lcd->insts[i].page_num), 0, + retval = hal_pin_new_uint(comp_id, HAL_IN, &(lcd->insts[i].page_num), 0, "lcd.%02i.page_num", i); if (retval != 0) { return retval; } lcd->insts[i].last_page = -1; // force screen refresh - retval = hal_pin_new_ui32(comp_id, HAL_OUT, &(lcd->insts[i].out), 0, + retval = hal_pin_new_uint(comp_id, HAL_OUT, &(lcd->insts[i].out), 0, "lcd.%02i.out",i); if (retval != 0) { return retval; @@ -223,7 +237,7 @@ int rtapi_app_main(void){ if (retval != 0) { return retval; } - retval = hal_param_new_ui32(comp_id, HAL_RW, &(lcd->insts[i].dp), '.', + retval = hal_param_new_uint(comp_id, HAL_RW, &(lcd->insts[i].dp), '.', "lcd.%02i.decimal-separator",i); if (retval != 0) { return retval; @@ -276,18 +290,18 @@ static void write_one(lcd_inst_t *inst){ return;} if (inst->buff[inst->c_ptr] != 0){ - hal_set_ui32(inst->out, inst->buff[inst->c_ptr++]); + hal_set_uint(inst->out, inst->buff[inst->c_ptr++]); return; } inst->c_ptr = 0; inst->buff[0] = 0; - if (hal_get_ui32(inst->page_num) >= inst->num_pages) return; // should this error? + if (hal_get_uint(inst->page_num) >= inst->num_pages) return; // should this error? - if (hal_get_ui32(inst->page_num) != inst->last_page){ - inst->last_page = hal_get_ui32(inst->page_num); - hal_set_ui32(inst->out, 0x11); //cursor off + if (hal_get_uint(inst->page_num) != inst->last_page){ + inst->last_page = hal_get_uint(inst->page_num); + hal_set_uint(inst->out, 0x11); //cursor off inst->buff[0] = 0x1E; //cursor home inst->buff[1] = 0x1A; //clear screen inst->buff[2] = 0; // end @@ -297,8 +311,8 @@ static void write_one(lcd_inst_t *inst){ return; } - if (inst->f_ptr >= inst->pages[hal_get_ui32(inst->page_num)].length){ - hal_set_ui32(inst->out, 0x1B); // ESC + if (inst->f_ptr >= inst->pages[hal_get_uint(inst->page_num)].length){ + hal_set_uint(inst->out, 0x1B); // ESC inst->buff[0] = 0x3D; // = inst->buff[1] = 0x20; // Line 0 inst->buff[2] = 0x20; // Column 0 @@ -314,7 +328,7 @@ static void write_one(lcd_inst_t *inst){ if (c > 0xBF) c = 0xBF; if (c < 0x20) c = 0x20; inst->last_contrast = hal_get_real(inst->contrast); - hal_set_ui32(inst->out, 0x1B); + hal_set_uint(inst->out, 0x1B); inst->buff[0] = 'C'; inst->buff[1] = c; inst->buff[2] = 0; @@ -322,9 +336,9 @@ static void write_one(lcd_inst_t *inst){ return; } - switch (inst->pages[hal_get_ui32(inst->page_num)].fmt[inst->f_ptr]){ + switch (inst->pages[hal_get_uint(inst->page_num)].fmt[inst->f_ptr]){ case '\\': //escape chars - c1 = inst->pages[hal_get_ui32(inst->page_num)].fmt[++inst->f_ptr]; + c1 = inst->pages[hal_get_uint(inst->page_num)].fmt[++inst->f_ptr]; switch (c1){ case 'n': case 'N': @@ -350,7 +364,7 @@ static void write_one(lcd_inst_t *inst){ /* Fallthrough */ default: //check for hex - c2 = inst->pages[hal_get_ui32(inst->page_num)].fmt[++inst->f_ptr]; + c2 = inst->pages[hal_get_uint(inst->page_num)].fmt[++inst->f_ptr]; inst->f_ptr++; if (c1 > '9') c1 &= 0xDF; //upper case if (c2 > '9') c2 &= 0xDF; @@ -362,32 +376,32 @@ static void write_one(lcd_inst_t *inst){ inst->buff[0] = 0; } } - hal_set_ui32(inst->out, inst->buff[0]); + hal_set_uint(inst->out, inst->buff[0]); inst->c_ptr = 1; return; case '%': - retval = parse_fmt(inst->pages[hal_get_ui32(inst->page_num)].fmt, + retval = parse_fmt(inst->pages[hal_get_uint(inst->page_num)].fmt, &inst->f_ptr, inst->buff, - &(inst->pages[hal_get_ui32(inst->page_num)].args[inst->a_ptr++]), - (char)hal_get_ui32(inst->dp)); + &(inst->pages[hal_get_uint(inst->page_num)].args[inst->a_ptr++]), + (char)hal_get_uint(inst->dp)); if (retval >= 0) { - hal_set_ui32(inst->out, inst->buff[0]); + hal_set_uint(inst->out, inst->buff[0]); inst->c_ptr = 1; inst->f_ptr++; return; } /* Fallthrough */ default: - hal_set_ui32(inst->out, inst->pages[hal_get_ui32(inst->page_num)].fmt[inst->f_ptr++]); + hal_set_uint(inst->out, inst->pages[hal_get_uint(inst->page_num)].fmt[inst->f_ptr++]); } } -int num_digits_baseN(rtapi_u64 val, int base){ +static int num_digits_baseN(rtapi_u64 val, int base){ int n = 1; - rtapi_u64 m = 1; - while ((m *= base) <= val){ - n += 1; + while(val >= (rtapi_u64)base) { + val /= base; + n++; } return n; } @@ -473,9 +487,9 @@ static int parse_fmt(char *in, int *ptr, char *out, hal_refs_u *val, char dp){ case 'u': if (out == NULL || val == NULL) return 'u'; { - unsigned int tmp; + rtapi_uint tmp; int i; - unsigned int v = hal_get_ui32(val->u); + rtapi_uint v = hal_get_uint(val->u); if (c < 1) c = num_digits_baseN(v, base); @@ -501,16 +515,17 @@ static int parse_fmt(char *in, int *ptr, char *out, hal_refs_u *val, char dp){ case 'i': if (out == NULL || val == NULL) return 's'; { - int tmp, s = 0; + rtapi_sint tmp; + int s = 0; int i; - int v = hal_get_si32(val->s); + rtapi_sint v = hal_get_sint(val->s); if (sgn == '+') s = 1; if (v < 0) {s = 1; sgn = '-'; v = -v;} if (c < 1) c = num_digits_baseN(v, base) + s; - tmp = abs(v); + tmp = v < 0 ? -v : v; for (i = c - 1; i >= s; i--){ if (tmp != 0 || i == c - 1){ out[i] = digits[tmp % 10]; @@ -550,6 +565,11 @@ static int parse_fmt(char *in, int *ptr, char *out, hal_refs_u *val, char dp){ if (v < 0) {s = 1; sgn = '-'; v = -v;} if (m < 0) m = 4; + if (m >= (int)(sizeof(pow10) / sizeof(*pow10))) { + if (c < 1) c = MAX_ENTRY; + tmp = 2; + goto overflow; + } if (c < 1) c = num_digits_baseN(v, base) + m + 1 + s; @@ -607,7 +627,7 @@ static int parse_fmt(char *in, int *ptr, char *out, hal_refs_u *val, char dp){ if (out == NULL || val == NULL) return 'c'; { int i; - unsigned char v = hal_get_ui32(val->u); + unsigned char v = hal_get_uint(val->u) & 0xff; if (c == 0) c = 1; for (i = 0; i < c; i++){ diff --git a/src/hal/components/matrix_kb.c b/src/hal/components/matrix_kb.c index 1d49516ee09..968ae596a7d 100644 --- a/src/hal/components/matrix_kb.c +++ b/src/hal/components/matrix_kb.c @@ -45,8 +45,8 @@ typedef struct { char name[HAL_NAME_LEN + 1]; struct input_dev *key_dev; rtapi_u32 index; - unsigned keydown; - unsigned keyup; + rtapi_uint keydown; + rtapi_uint keyup; unsigned rowshift; unsigned row; unsigned num_keys; @@ -69,10 +69,10 @@ RTAPI_MP_ARRAY_STRING(names, MAX_CHAN, "component names"); void keyup(kb_inst_t *inst){ unsigned r, c; - unsigned keycode = hal_get_ui32(inst->hal.keycode) & ~(inst->keydown | inst->keyup); + rtapi_uint keycode = hal_get_uint(inst->hal.keycode) & ~(inst->keydown | inst->keyup); r = keycode >> inst->rowshift; - c = keycode & ~(0xFFFFFFFF << inst->rowshift); + c = keycode & ((1ul << inst->rowshift) - 1); if ( r >= inst->nrows || c >= inst->ncols @@ -86,10 +86,10 @@ void keyup(kb_inst_t *inst){ } void keydown(kb_inst_t *inst){ unsigned r, c; - unsigned keycode = hal_get_ui32(inst->hal.keycode) & ~(inst->keydown | inst->keyup); + rtapi_uint keycode = hal_get_uint(inst->hal.keycode) & ~(inst->keydown | inst->keyup); r = keycode >> inst->rowshift; - c = keycode & ~(0xFFFFFFFF << inst->rowshift); + c = keycode & ((1ul << inst->rowshift) - 1); if ( r >= inst->nrows || c >= inst->ncols @@ -97,7 +97,7 @@ void keydown(kb_inst_t *inst){ return; } - if (inst->num_keys >= hal_get_ui32(inst->param.rollover)) return; + if (inst->num_keys >= hal_get_uint(inst->param.rollover)) return; inst->num_keys++; hal_set_bool(inst->hal.key[r * inst->ncols + c], 1); @@ -118,13 +118,13 @@ void loop(void *arg, long period){ for (c = 0; c < inst->ncols; c++){ int mask = 1 << c; if ((inst->then[inst->row] & mask) && !(scan & mask)){ //keyup - hal_set_ui32(inst->hal.keycode, inst->keyup + hal_set_uint(inst->hal.keycode, inst->keyup + (inst->row << inst->rowshift) + c); keyup(inst); } else if (!(inst->then[inst->row] & mask) && (scan & mask)){//keydown - hal_set_ui32(inst->hal.keycode, inst->keydown + hal_set_uint(inst->hal.keycode, inst->keydown + (inst->row << inst->rowshift) + c); @@ -133,7 +133,7 @@ void loop(void *arg, long period){ } } else { - hal_set_ui32(inst->hal.keycode, 0x40);//nochange + hal_set_uint(inst->hal.keycode, 0x40);//nochange } inst->then[inst->row] = inst->now[inst->row]; @@ -146,7 +146,7 @@ void loop(void *arg, long period){ } else { - rtapi_u32 keycode = hal_get_ui32(inst->hal.keycode); + rtapi_uint keycode = hal_get_uint(inst->hal.keycode); if (keycode == 0x40) return; if ((keycode & inst->keydown) == inst->keydown){ keydown(inst); @@ -170,9 +170,8 @@ int rtapi_app_main(void){ // allocate shared memory for data kb = hal_malloc(sizeof(*kb)); - if (kb == 0) { - rtapi_print_msg(RTAPI_MSG_ERR, - "matrix_kb component: Out of Memory\n"); + if (NULL == kb) { + rtapi_print_msg(RTAPI_MSG_ERR, "matrix_kb: Out of Memory\n"); hal_exit(comp_id); return -1; } @@ -190,6 +189,11 @@ int rtapi_app_main(void){ } kb->insts = hal_malloc(kb->num_insts * sizeof(*kb->insts)); + if (NULL == kb->insts) { + rtapi_print_msg(RTAPI_MSG_ERR, "matrix_kb: Out of Memory\n"); + hal_exit(comp_id); + return -1; + } for (i = 0; i < kb->num_insts; i++){ int a = 0; @@ -239,6 +243,11 @@ int rtapi_app_main(void){ inst->hal.key = hal_malloc(inst->nrows * inst->ncols * sizeof(*inst->hal.key)); inst->now = hal_malloc(inst->nrows * sizeof(*inst->now)); inst->then = hal_malloc(inst->nrows * sizeof(*inst->then)); + if (NULL == inst->hal.key || NULL == inst->now || NULL == inst->then) { + rtapi_print_msg(RTAPI_MSG_ERR, "matrix_kb: Out of Memory\n"); + hal_exit(comp_id); + return -1; + } inst->row = 0; @@ -269,6 +278,11 @@ int rtapi_app_main(void){ if (inst->scan){ //internally generated scanning inst->hal.rows = hal_malloc(inst->nrows * sizeof(*inst->hal.rows)); inst->hal.cols = hal_malloc(inst->ncols * sizeof(*inst->hal.cols)); + if (NULL == inst->hal.rows || NULL == inst->hal.cols) { + rtapi_print_msg(RTAPI_MSG_ERR, "matrix_kb: Out of Memory\n"); + hal_exit(comp_id); + return -1; + } for (r = 0; r < inst->nrows; r++){ retval = hal_pin_new_bool(comp_id, HAL_OUT, @@ -293,7 +307,7 @@ int rtapi_app_main(void){ } } - retval = hal_pin_new_ui32(comp_id, HAL_OUT, + retval = hal_pin_new_uint(comp_id, HAL_OUT, &(inst->hal.keycode), 0, "%s.keycode",inst->name); if (retval != 0) { @@ -314,7 +328,7 @@ int rtapi_app_main(void){ } - retval = hal_param_new_ui32(comp_id, HAL_RW, + retval = hal_param_new_uint(comp_id, HAL_RW, &(inst->param.rollover), 2, "%s.key_rollover",inst->name); if (retval != 0) { @@ -327,7 +341,7 @@ int rtapi_app_main(void){ } else // scanning by 7i73 or similar { - retval = hal_pin_new_ui32(comp_id, HAL_IN, + retval = hal_pin_new_uint(comp_id, HAL_IN, &(inst->hal.keycode), 0, "%s.keycode",inst->name); if (retval != 0) {