OreBolt-OS/modules/orebolt-probe/probe.c

94 lines
2.9 KiB
C
Executable File

/*
* probe.c -- I2C Bus Hardware Scanner module
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* I2C bus scanner. Probes /dev/i2c-0 address space 0x03-0x77
* using SMBus receive-byte probe. Displays responding devices
* in an LVGL list with green highlighting.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
#include <linux/input.h>
#include "h2_ui.h"
#include "log_manager.h"
#define I2C_DEV "/dev/i2c-0"
#define ADDR_START 0x03
#define ADDR_END 0x77
#define MAX_FOUND 16
static int i2c_fd = -1;
static int try_probe_addr(uint8_t addr) {
if (ioctl(i2c_fd, I2C_SLAVE, addr) < 0) return 0;
unsigned char dummy;
return (read(i2c_fd, &dummy, 1) == 1);
}
int main(void) {
init_h2_graphics_runtime("I2C CONTROLLER SCANNER");
LOG_INF("probe module started");
lv_obj_t *scr = lv_scr_act();
lv_obj_t *title = lv_label_create(scr);
lv_label_set_text(title, "I2C BUS HARDWARE SCAN");
lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 10);
lv_obj_set_style_text_color(title, COLOR_PRIMARY, LV_PART_MAIN);
lv_obj_t *list = lv_list_create(scr);
lv_obj_set_size(list, 280, 160);
lv_obj_align(list, LV_ALIGN_CENTER, 0, 15);
uint8_t found_addrs[MAX_FOUND];
int found_count = 0;
i2c_fd = open(I2C_DEV, O_RDWR);
if (i2c_fd < 0) {
lv_list_add_text(list, "ERROR: Cannot open /dev/i2c-0");
LOG_ERR("cannot open %s", I2C_DEV);
} else {
lv_list_add_text(list, "Probing 0x03 - 0x77 ...");
LOG_INF("scanning I2C bus 0x03-0x77");
for (int addr = ADDR_START; addr <= ADDR_END; addr++) {
if (try_probe_addr((uint8_t)addr) && found_count < MAX_FOUND) {
found_addrs[found_count++] = (uint8_t)addr;
}
}
if (found_count == 0) {
lv_list_add_text(list, "No devices responded.");
}
char buf[64];
for (int i = 0; i < found_count; i++) {
snprintf(buf, sizeof(buf), " [0x%02X] ACK", found_addrs[i]);
lv_obj_t *entry = lv_list_add_text(list, buf);
lv_obj_set_style_text_color(entry, COLOR_ACCENT, LV_PART_MAIN);
}
snprintf(buf, sizeof(buf), "Scan complete: %d device(s)", found_count);
lv_list_add_text(list, buf);
LOG_INF("I2C scan found %d device(s)", found_count);
close(i2c_fd);
i2c_fd = -1;
}
int input_fd = open("/dev/input/event0", O_RDONLY | O_NONBLOCK);
struct input_event ev;
while (1) {
lv_timer_handler();
if (input_fd >= 0 && read(input_fd, &ev, sizeof(struct input_event)) > 0) {
if (ev.type == EV_KEY && ev.code == H2_KEY_BACK && ev.value == 1) break;
}
usleep(20000);
}
if (input_fd >= 0) close(input_fd);
LOG_INF("probe module exited");
return 0;
}