From patchwork Wed May 12 22:57:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= X-Patchwork-Id: 1477872 X-Patchwork-Delegate: zajec5@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.openwrt.org (client-ip=2001:8b0:10b:1:d65d:64ff:fe57:4e05; helo=desiato.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=desiato.20200630 header.b=WwXE/m/r; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=teDqw9wB; dkim-atps=neutral Received: from desiato.infradead.org (desiato.infradead.org [IPv6:2001:8b0:10b:1:d65d:64ff:fe57:4e05]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4FgVct1TSzz9sWP for ; Thu, 13 May 2021 09:00:27 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding :Content-Type:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender :Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Owner; bh=xthiIa4tNSiRx2dFc4To0xGZ7RVOI9WTDPCWM/Z+jXY=; b=WwXE/m/rQx9n4kuVQ7YzSX0HnP 7xfk8uOmVQ7z5eFR38j2PsNoWGc/N4kQbb5Lwf3SGrbera079nJbf04Axcf7Ftxj8MBP4BcQjTSbQ Zj8n2iPJlDY3a3M4yKSHxeUNwqid3G5vv+o/8JQGqUfAr8YUXwS9z92rIdb1elaYGYtC/c/kSzgmV JV6Xl+kgNxDl1O2p2dYtCRDiVuMLRwkaw+Z3+PfQbvokx4+vxyeBociL/9fN38cFVftannlPLDjDe nAg1agC2FyL8IN+HUgL9DndwY7OVDxTTyhrfxMme6gn2H4ArfDOAzY9gtP7/m9NafJE0CAdSqjWRJ 7GiVDpGQ==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lgxnx-004CGr-LE; Wed, 12 May 2021 22:58:14 +0000 Received: from mail-lf1-x134.google.com ([2a00:1450:4864:20::134]) by desiato.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lgxnp-004CF5-JK for openwrt-devel@lists.openwrt.org; Wed, 12 May 2021 22:58:10 +0000 Received: by mail-lf1-x134.google.com with SMTP id j10so35981334lfb.12 for ; Wed, 12 May 2021 15:58:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=ncCvwCMD12YS/dpvpcdrVth4Zl/QLTFb9D6m3+BfkFo=; b=teDqw9wBkt7Ka3oezO4W312y9ZM/DaIhpKEr+FFcxyP56dfhmlksfsuwK7x8NwcKtF rhtgWGRtKEYBSSY1GIF0MP8orWNfK81yeiA1XwKUWjsMLu5hAFIw+CQReUsDM7dAbuhI cD1cUQphMhjlmodQGkq0fqReVrv1q2z2+QCs/9SlA6enuQMs3yTzUn1Py7aSHN2cPu5e uot0B852oS8MuPkA2pW3W7kYfN5cN0XRCihvdjP7t/gYqGjqA7ahkipf1TslQQADqqA0 zTCap1DZ52qsJKPQ/T6S/wZWJ4NZUsj7xVy7H+l6Iz7cAeevTKEYioCZgmqHCsA0vEAo mPcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=ncCvwCMD12YS/dpvpcdrVth4Zl/QLTFb9D6m3+BfkFo=; b=fKGsx0VKiA/sZ9DjyNDY8RW4dg8kPemGriTQCsfqMcOoD7z59jz62iUlbU3XgfPKaz PB6CjiiIHnz7U7KVlU8YQ4XeMle2SNq/uSun9FiW9lacPvhDcKe9krF3/fXUpOfe6Bku 0Fwtw4cNqD+Tq2z9bJMX6rvur7MyXbHGo/7j2uZDmD7V+3vSbEZUuD1AcJ8YfmSDHtD9 QGIQwQr505TGhEGDOBM+wRIY7J04MNeLBfOdP/m92jK6b03SDcBdPsQPNvtBPvn5fooW YxVRsVtoHzi1tOhxrFTqiVZVVk6oHigVn4nb2Pz87tXb6qJj6wwscXI0WEJT3S3YlfTk gbtg== X-Gm-Message-State: AOAM530fjjpQn78kiDOl4tHA6HXl5CAJoICfVWZMbhfcPT7VAHepQJt3 QOcChCG8NLylCFD/KDfGaEKUqsn9ms8= X-Google-Smtp-Source: ABdhPJxdusR52s/FX9aFq2C7lxvczWLHy6Hu6AICxTaaoiOe00XKcyQvur+nmoI+NykF+PbLbkiRSA== X-Received: by 2002:a05:6512:452:: with SMTP id y18mr27716080lfk.429.1620860279341; Wed, 12 May 2021 15:57:59 -0700 (PDT) Received: from localhost.lan (ip-194-187-74-233.konfederacka.maverick.com.pl. [194.187.74.233]) by smtp.gmail.com with ESMTPSA id r3sm184083ljc.32.2021.05.12.15.57.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 May 2021 15:57:58 -0700 (PDT) From: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= To: openwrt-devel@lists.openwrt.org, John Crispin , =?utf-8?q?Bj=C3=B8rn_Mork?= , =?utf-8?q?Petr_=C5=A0tetiar?= , Birger Koblitz Cc: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= Subject: [PATCH DRAFT RFC] upoe: tiny daemon for PoE devices Date: Thu, 13 May 2021 00:57:37 +0200 Message-Id: <20210512225737.31745-1-zajec5@gmail.com> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210512_235808_236302_FA4C5BB3 X-CRM114-Status: GOOD ( 18.94 ) X-Spam-Score: 0.1 (/) X-Spam-Report: =?unknown-8bit?q?Spam_detection_software=2C_running_on_the_sy?= =?unknown-8bit?q?stem_=22desiato=2Einfradead=2Eorg=22=2C?= =?unknown-8bit?q?_has_NOT_identified_this_incoming_email_as_spam=2E__The_ori?= =?unknown-8bit?q?ginal?= =?unknown-8bit?q?_message_has_been_attached_to_this_so_you_can_view_it_or_la?= =?unknown-8bit?q?bel?= =?unknown-8bit?q?_similar_future_email=2E__If_you_have_any_questions=2C_see?= =?unknown-8bit?q?_the_administrator_of_that_system_for_details=2E?= =?unknown-8bit?q?_?= =?unknown-8bit?q?_Content_preview=3A__From=3A_Rafa=C5=82_Mi=C5=82ecki_This_i?= =?unknown-8bit?q?s_a_tiny_app_that_reads_list_of?= =?unknown-8bit?q?_PoE_devices_and_initializes_them_using_built-in_drivers=2E?= =?unknown-8bit?q?_PoE_status_can_be?= =?unknown-8bit?q?_read_using_ubus=2E_Signed-off-by=3A_Rafa=C5=82_Mi=C5=82eck?= =?unknown-8bit?q?i_---_This_is_a_***_DRAFT_***?= =?unknown-8bit?q?_for_how_we_could_handle_PoE_devices_in_OpenWrt=2E_Please_f?= =?unknown-8bit?q?eel_free_to_comment?= =?unknown-8bit?q?_if_this_EARLY_=26_INCOMPLETE_design_looks_sane_enough=2E_-?= =?unknown-8bit?q?--_CMakeL_=5B=2E=2E=2E=5D_?= =?unknown-8bit?q?_?= =?unknown-8bit?q?_Content_analysis_details=3A___=280=2E1_points=2C_5=2E0_req?= =?unknown-8bit?q?uired=29?= =?unknown-8bit?q?_?= =?unknown-8bit?q?_pts_rule_name______________description?= =?unknown-8bit?q?_----_----------------------_------------------------------?= =?unknown-8bit?q?--------------------?= =?unknown-8bit?q?_-0=2E0_SPF=5FPASS_______________SPF=3A_sender_matches_SPF_?= =?unknown-8bit?q?record?= =?unknown-8bit?q?_-0=2E0_RCVD=5FIN=5FDNSWL=5FNONE_____RBL=3A_Sender_listed_a?= =?unknown-8bit?q?t_https=3A//www=2Ednswl=2Eorg/=2C?= =?unknown-8bit?q?_no_trust?= =?unknown-8bit?b?IFsyYTAwOjE0NTA6NDg2NDoyMDowOjA6MDoxMzQgbGlzdGVkIGluXQ==?= =?unknown-8bit?b?IFtsaXN0LmRuc3dsLm9yZ10=?= =?unknown-8bit?q?_0=2E0_SPF=5FHELO=5FNONE__________SPF=3A_HELO_does_not_publ?= =?unknown-8bit?q?ish_an_SPF_Record?= =?unknown-8bit?q?_0=2E2_FREEMAIL=5FENVFROM=5FEND=5FDIGIT_Envelope-from_freem?= =?unknown-8bit?q?ail_username_ends?= =?unknown-8bit?q?_in_digit?= =?unknown-8bit?q?_=5Bzajec5=5Bat=5Dgmail=2Ecom=5D?= =?unknown-8bit?q?_0=2E0_FREEMAIL=5FFROM__________Sender_email_is_commonly_ab?= =?unknown-8bit?q?used_enduser_mail?= =?unknown-8bit?q?_provider?= =?unknown-8bit?q?_=5Bzajec5=5Bat=5Dgmail=2Ecom=5D?= =?unknown-8bit?q?_-0=2E1_DKIM=5FVALID_____________Message_has_at_least_one_v?= =?unknown-8bit?q?alid_DKIM_or_DK_signature?= =?unknown-8bit?q?_-0=2E1_DKIM=5FVALID=5FAU__________Message_has_a_valid_DKIM?= =?unknown-8bit?q?_or_DK_signature_from?= =?unknown-8bit?q?_author=27s_domain?= =?unknown-8bit?q?_-0=2E1_DKIM=5FVALID=5FEF__________Message_has_a_valid_DKIM?= =?unknown-8bit?q?_or_DK_signature_from?= =?unknown-8bit?q?_envelope-from_domain?= =?unknown-8bit?q?_0=2E1_DKIM=5FSIGNED____________Message_has_a_DKIM_or_DK_si?= =?unknown-8bit?q?gnature=2C_not_necessarily?= =?unknown-8bit?q?_valid?= X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org From: Rafał Miłecki This is a tiny app that reads list of PoE devices and initializes them using built-in drivers. PoE status can be read using ubus. Signed-off-by: Rafał Miłecki --- This is a *** DRAFT *** for how we could handle PoE devices in OpenWrt. Please feel free to comment if this EARLY & INCOMPLETE design looks sane enough. --- CMakeLists.txt | 9 ++++ dummy.c | 24 +++++++++ upoe.c | 144 +++++++++++++++++++++++++++++++++++++++++++++++++ upoe.h | 36 +++++++++++++ 4 files changed, 213 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 dummy.c create mode 100644 upoe.c create mode 100644 upoe.h diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..6474663 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required(VERSION 2.6) + +project(upoe) +set(CMAKE_C_FLAGS "-std=c99 -D_GNU_SOURCE -Wall") + +add_executable(upoe upoe.c dummy.c) +target_link_libraries(upoe ubox ubus uci) + +install(TARGETS upoe RUNTIME DESTINATION bin) diff --git a/dummy.c b/dummy.c new file mode 100644 index 0000000..de0abfb --- /dev/null +++ b/dummy.c @@ -0,0 +1,24 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "upoe.h" + +static int upoe_dummy_probe_device(struct upoe *upoe, struct uci_element *e) +{ + struct device *dev = calloc(1, sizeof(*dev)); + + dev->drv = &dummy; + upoe_device_register(upoe, dev); + + return 0; +} + +static int upoe_dummy_get_status(struct upoe *upoe, struct device *dev) +{ + return 0; +} + +struct driver dummy = { + .type = "dummy", + .probe_device = upoe_dummy_probe_device, + .get_status = upoe_dummy_get_status, +}; diff --git a/upoe.c b/upoe.c new file mode 100644 index 0000000..3dfa834 --- /dev/null +++ b/upoe.c @@ -0,0 +1,144 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "upoe.h" + +/************************************************** + * Device + **************************************************/ + +static int upoe_device_status(struct ubus_context *ctx, + struct ubus_object *obj, + struct ubus_request_data *req, + const char *method, + struct blob_attr *msg) +{ + struct upoe *upoe = container_of(ctx, struct upoe, ubus); + struct device *dev = container_of(obj, struct device, ubus_obj); + struct blob_buf b = { }; + //void *c; + + blob_buf_init(&b, 0); + + dev->drv->get_status(upoe, dev); + blobmsg_add_string(&b, "foo", "bar"); + + ubus_send_reply(ctx, req, b.head); + + blob_buf_free(&b); + + return 0; +} + +static const struct ubus_method upoe_ubus_device_methods[] = { + UBUS_METHOD_NOARG("status", upoe_device_status), +}; + +static struct ubus_object_type upoe_ubus_device_object_type = + UBUS_OBJECT_TYPE("upoe-device", upoe_ubus_device_methods); + +int upoe_device_register(struct upoe *upoe, struct device *dev) +{ + struct ubus_object *obj = &dev->ubus_obj; + char *name = NULL; + int err; + + if (asprintf(&name, "upoe.%s", "dummy0") == -1) + return -ENOMEM; + + obj->name = name; + obj->type = &upoe_ubus_device_object_type; + obj->methods = upoe_ubus_device_methods; + obj->n_methods = ARRAY_SIZE(upoe_ubus_device_methods); + + err = ubus_add_object(&upoe->ubus, obj); + if (err) { + fprintf(stderr, "Failed to add ubus object\n"); + free(name); + } + + return err; +} + +/************************************************** + * main() + **************************************************/ + +static struct driver *drivers[] = { + &dummy, +}; + +static int upoe_probe_devices(struct upoe *upoe) { + struct uci_package *p; + struct uci_element *e; + + uci_load(upoe->uci, "upoe", &p); + if (!p) { + fprintf(stderr, "Failed to load upoe config\n"); + return -ENOENT; + } + + uci_foreach_element(&p->sections, e) { + struct uci_section *s; + const char *type; + int i; + + s = uci_to_section(e); + + if (strcmp(s->type, "device")) + continue; + + type = uci_lookup_option_string(upoe->uci, s, "type"); + if (!type) + continue; + + for (i = 0; i < ARRAY_SIZE(drivers); i++) { + struct driver *drv = drivers[i]; + + if (!strcmp(drv->type, type)) { + drv->probe_device(upoe, e); + break; + } + } + } + + uci_unload(upoe->uci, p); + + return 0; +} + +int main(int argc, char **argv) { + struct upoe *upoe = calloc(1, sizeof(*upoe)); + int err; + + if (!upoe) { + err = -ENOMEM; + goto err_out; + } + + upoe->uci = uci_alloc_context(); + if (!upoe->uci) { + err = -ENOMEM; + goto err_free_upoe; + } + + if (ubus_connect_ctx(&upoe->ubus, NULL)) { + fprintf(stderr, "Failed to connect to ubus\n"); + err = -ENXIO; + goto err_uci_free; + } + + upoe_probe_devices(upoe); + + uloop_init(); + ubus_add_uloop(&upoe->ubus); + uloop_run(); + uloop_done(); + + ubus_shutdown(&upoe->ubus); +err_uci_free: + uci_free_context(upoe->uci); +err_free_upoe: + free(upoe); +err_out: + return err; +} diff --git a/upoe.h b/upoe.h new file mode 100644 index 0000000..c3fd18d --- /dev/null +++ b/upoe.h @@ -0,0 +1,36 @@ +// SPDX-License-Identifier: GPL-2.0 + +#ifndef UPOE_H_ +#define UPOE_H_ + +#include +#include + +struct upoe; +struct device; + +struct driver { + const char *type; + int (*probe_device)(struct upoe *upoe, struct uci_element *e); + int (*get_status)(struct upoe *upoe, struct device *dev); +}; + +struct device { + struct driver *drv; + struct ubus_object ubus_obj; + void (*get_status)(struct device *dev); + + struct device *next; +}; + +struct upoe { + struct uci_context *uci; + struct ubus_context ubus; + struct device *devices; +}; + +int upoe_device_register(struct upoe *upoe, struct device *dev); + +struct driver dummy; + +#endif