Skip to content

Commit

Permalink
WIP: userland proximity driver
Browse files Browse the repository at this point in the history
Signed-off-by: Baocheng Su <[email protected]>
  • Loading branch information
BaochengSu committed Jan 20, 2025
1 parent 1e7d4d5 commit a4a7b8b
Show file tree
Hide file tree
Showing 12 changed files with 540 additions and 0 deletions.
1 change: 1 addition & 0 deletions recipes-core/iot2050-proximity-driver/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is the userspace driver for the proximity sensor of IOT2050
26 changes: 26 additions & 0 deletions recipes-core/iot2050-proximity-driver/files/src/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# SPDX-License-Identifier: MIT
#
# Copyright (C) Siemens AG, 2025
#
# Author:
# - Su Bao Cheng <[email protected]>
#
CROSS_COMPILE = aarch64-linux-gnu-

CC = $(CROSS_COMPILE)gcc

LDFLAGS = -lsystemd

TARGET = iot2050-pxmtd

SRC = main.c \
dbus-service.c \
sensor.c

all: $(TARGET)

$(TARGET): $(SRC)
$(CC) $(CFLAGS) -o $(TARGET) $(SRC) $(LDFLAGS)

clean:
rm -f $(TARGET)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE busconfig PUBLIC
"-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
<policy user="root">
<allow own="com.siemens.iot2050.pxmt"/>
<allow send_interface="com.siemens.iot2050.pxmt"/>
</policy>
</busconfig>
94 changes: 94 additions & 0 deletions recipes-core/iot2050-proximity-driver/files/src/dbus-service.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/* SPDX-License-Identifier: MIT */
/*
* Copyright (C) Siemens AG, 2025
*
* Author:
* - Su Bao Cheng <[email protected]>
*/
#include <systemd/sd-bus.h>
#include <errno.h>
#include <sys/capability.h>
#include "dbus-service.h"

typedef int (*retrieve_callback_t)(uint16_t *);

static retrieve_callback_t iot2050_pxmtd_retrieve_cb = NULL;

static int iot2050_pxmtd_retrieve(sd_bus_message *m,
void *userdata,
sd_bus_error *ret_error)
{
if (!iot2050_pxmtd_retrieve_cb)
return -ENXIO;

uint16_t ps_val;
int ret = iot2050_pxmtd_retrieve_cb(&ps_val);
if (ret < 0)
return ret;

return sd_bus_reply_method_return(m, "q", ps_val);
}

static const sd_bus_vtable iot2050_pxmtd_vtable[] = {
SD_BUS_VTABLE_START(0),
SD_BUS_METHOD("Retrieve",
SD_BUS_NO_ARGS,
"q",
iot2050_pxmtd_retrieve,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_VTABLE_END
};

int dbus_serve(retrieve_callback_t cb)
{
sd_bus_slot *slot = NULL;
sd_bus *bus = NULL;
int ret;

ret = sd_bus_open_system(&bus);
if (ret < 0) {
fprintf(stderr, "Failed to connect to system bus: %s\n",
strerror(-ret));
goto finish;
}

iot2050_pxmtd_retrieve_cb = cb;

ret = sd_bus_add_object_vtable(bus, &slot,
"/com/siemens/iot2050/pxmt",
"com.siemens.iot2050.pxmt",
iot2050_pxmtd_vtable, NULL);
if (ret < 0) {
fprintf(stderr, "Failed to issue method call: %s\n", strerror(-ret));
goto finish;
}

ret = sd_bus_request_name(bus, "com.siemens.iot2050.pxmt", 0);
if (ret < 0) {
fprintf(stderr, "Failed to acquire service name: %s\n", strerror(-ret));
goto finish;
}

for (;;) {
ret = sd_bus_process(bus, NULL);
if (ret < 0) {
fprintf(stderr, "Failed to process bus: %s\n", strerror(-ret));
goto finish;
}
if (ret > 0)
continue;

ret = sd_bus_wait(bus, (uint64_t) -1);
if (ret < 0) {
fprintf(stderr, "Failed to wait on bus: %s\n", strerror(-ret));
goto finish;
}
}

ret = 0;
finish:
sd_bus_slot_unref(slot);
sd_bus_unref(bus);

return ret;
}
17 changes: 17 additions & 0 deletions recipes-core/iot2050-proximity-driver/files/src/dbus-service.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* SPDX-License-Identifier: MIT */
/*
* Copyright (C) Siemens AG, 2025
*
* Author:
* - Su Bao Cheng <[email protected]>
*/
#ifndef IOT2050_PXMTD_DBUS_SERVICE_H
#define IOT2050_PXMTD_DBUS_SERVICE_H

#include <stdint.h>

typedef int (*retrieve_callback_t)(uint16_t *);

int dbus_serve(retrieve_callback_t cb);

#endif /* IOT2050_PXMTD_DBUS_SERVICE_H */
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
switchserialmode (0.4) UNRELEASED; urgency=low

* generated by Isar

-- [email protected] Thu, 01 Jan 1970 01:00:00 +0000
15 changes: 15 additions & 0 deletions recipes-core/iot2050-proximity-driver/files/src/debian/control
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Source: iot2050-proximity-driver
Section: misc
Priority: optional
Standards-Version: 3.9.6
Maintainer: [email protected]
Build-Depends: debhelper-compat (= 13), libsystemd-dev, libcap-dev

Package: iot2050-proximity-driver
Architecture: arm64
Depends: ${shlibs:Depends}
Provides: iot2050-proximity-driver
Replaces:
Conflicts:
Multi-Arch: no
Description: Userspace driver for the proximity sensor of IOT2050
20 changes: 20 additions & 0 deletions recipes-core/iot2050-proximity-driver/files/src/debian/rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/make -f


deb_top_dir:=$(CURDIR)/debian
pxmtd_install_dir :=$(deb_top_dir)/iot2050-proximity-driver/usr/bin
export CROSS_COMPILE=$(DEB_HOST_GNU_TYPE)-

override_dh_auto_build:
$(MAKE) all

override_dh_auto_install:
echo "xxxxoverride_dh_auto_install"
mkdir -p $(pxmtd_install_dir)
cp iot2050-pxmtd $(pxmtd_install_dir)

override_dh_auto_clean:
$(MAKE) clean

%:
dh $@
20 changes: 20 additions & 0 deletions recipes-core/iot2050-proximity-driver/files/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* SPDX-License-Identifier: MIT */
/*
* Copyright (C) Siemens AG, 2025
*
* Author:
* - Su Bao Cheng <[email protected]>
*/
#include <stdio.h>
#include <stdlib.h>
#include "sensor.h"
#include "dbus-service.h"

int main() {
if (init_sensor() != 0) {
fprintf(stderr, "Failed to initialize the sensor\n");
exit(1);
}

return dbus_serve(retrieve_sensor_data) == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
Loading

0 comments on commit a4a7b8b

Please sign in to comment.