-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Baocheng Su <[email protected]>
- Loading branch information
1 parent
1e7d4d5
commit eb172c8
Showing
9 changed files
with
635 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# SPDX-License-Identifier: MIT | ||
# SPDX-FileCopyrightText: Copyright (c) Siemens AG, 2025 | ||
# SPDX-FileContributor: Authored by Su Bao Cheng <[email protected]> | ||
|
||
CROSS_COMPILE ?= aarch64-linux-gnu- | ||
|
||
CC = $(CROSS_COMPILE)gcc | ||
|
||
LDFLAGS = -lsystemd | ||
|
||
TARGET = iot2050-pxmtd | ||
|
||
INSTALL ?= install | ||
|
||
DESTDIR ?= / | ||
|
||
SRC = main.c \ | ||
dbus-service.c \ | ||
sensor.c | ||
|
||
.PHONY: all clean install | ||
|
||
all: $(TARGET) | ||
|
||
$(TARGET): $(SRC) | ||
$(CC) $(CFLAGS) -o $(TARGET) $(SRC) $(LDFLAGS) | ||
|
||
clean: | ||
rm -f $(TARGET) | ||
|
||
install: | ||
$(INSTALL) -v -d $(DESTDIR)/usr/bin | ||
$(INSTALL) -v -d $(DESTDIR)/etc/dbus-1/system.d | ||
$(INSTALL) -v -m 0755 $(TARGET) $(DESTDIR)/usr/bin | ||
$(INSTALL) -v -m 0644 com.siemens.iot2050.pxmt.conf \ | ||
$(DESTDIR)/etc/dbus-1/system.d | ||
$(INSTALL) -v -d ${DESTDIR}/lib/systemd/system/ | ||
$(INSTALL) -v -m 0644 iot2050-proximity-driver.service\ | ||
${DESTDIR}/lib/systemd/system/ |
9 changes: 9 additions & 0 deletions
9
recipes-core/iot2050-proximity-driver/files/src/com.siemens.iot2050.pxmt.conf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
96 changes: 96 additions & 0 deletions
96
recipes-core/iot2050-proximity-driver/files/src/dbus-service.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* SPDX-License-Identifier: MIT | ||
* SPDX-FileCopyrightText: Copyright (c) Siemens AG, 2025 | ||
* SPDX-FileContributor: Authored by 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 out; | ||
} | ||
|
||
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 set dbus service property: %s\n", | ||
strerror(-ret)); | ||
goto out; | ||
} | ||
|
||
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 out; | ||
} | ||
|
||
for (;;) { | ||
ret = sd_bus_process(bus, NULL); | ||
if (ret < 0) { | ||
fprintf(stderr, "Failed to process bus: %s\n", | ||
strerror(-ret)); | ||
goto out; | ||
} | ||
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 out; | ||
} | ||
} | ||
|
||
ret = 0; | ||
out: | ||
sd_bus_slot_unref(slot); | ||
sd_bus_unref(bus); | ||
|
||
return ret; | ||
} |
15 changes: 15 additions & 0 deletions
15
recipes-core/iot2050-proximity-driver/files/src/dbus-service.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* SPDX-License-Identifier: MIT | ||
* SPDX-FileCopyrightText: Copyright (c) Siemens AG, 2025 | ||
* SPDX-FileContributor: Authored by 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 */ |
24 changes: 24 additions & 0 deletions
24
recipes-core/iot2050-proximity-driver/files/src/iot2050-proximity-driver.service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# SPDX-License-Identifier: MIT | ||
# SPDX-FileCopyrightText: Copyright (c) Siemens AG, 2025 | ||
# SPDX-FileContributor: Authored by Su Bao Cheng <[email protected]> | ||
|
||
[Unit] | ||
Description=Userspace driver for the proximity sensor of IOT2050 | ||
# Only run on IOT2050-SM variant | ||
ConditionFirmware=device-tree-compatible(siemens,iot2050-advanced-sm) | ||
ConditionVirtualization=!container | ||
|
||
[Service] | ||
Type=dbus | ||
BusName=com.siemens.iot2050.pxmt | ||
ExecStart=/usr/bin/iot2050-pxmtd | ||
Restart=always | ||
RestartSec=5 | ||
ProtectSystem=strict | ||
ProtectHome=true | ||
NoNewPrivileges=true | ||
User=root | ||
|
||
[Install] | ||
WantedBy=multi-user.target | ||
Alias=dbus-com.siemens.iot2050.pxmt.service |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* SPDX-License-Identifier: MIT | ||
* SPDX-FileCopyrightText: Copyright (c) Siemens AG, 2025 | ||
* SPDX-FileContributor: Authored by Su Bao Cheng <[email protected]> | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <string.h> | ||
#include <stdlib.h> | ||
#include "sensor.h" | ||
#include "dbus-service.h" | ||
|
||
int main() | ||
{ | ||
setbuf(stdout, NULL); | ||
|
||
int ret = init_sensor(); | ||
if (ret < 0) { | ||
fprintf(stderr, "Failed to initialize the sensor: %s\n", | ||
strerror(-ret)); | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
return dbus_serve(retrieve_sensor_data) == 0 ? EXIT_SUCCESS : EXIT_FAILURE; | ||
} |
Oops, something went wrong.