Snap for 9989322 from 70c230a1d40db45a4bcb4da1fa355bbd2d3d0826 to mainline-rkpd-release
Change-Id: I25e311c15eac146643bc16934c5315e838b5661a
diff --git a/.clang-format b/.clang-format
new file mode 100644
index 0000000..c8e6173
--- /dev/null
+++ b/.clang-format
@@ -0,0 +1,21 @@
+#
+# Copyright (C) 2023 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+# See https://clang.llvm.org/docs/ClangFormatStyleOptions.html for the
+# various options that can be configured and for the definitions of each
+# of the below options.
+
+BasedOnStyle: Google
diff --git a/Android.bp b/Android.bp
index 97dbe73..de1b8cf 100644
--- a/Android.bp
+++ b/Android.bp
@@ -6,6 +6,7 @@
name: "external_wmediumd_license",
visibility: [":__subpackages__"],
license_kinds: [
+ "SPDX-license-identifier-Apache-2.0",
"SPDX-license-identifier-GPL-2.0",
],
license_text: [
@@ -24,13 +25,76 @@
"libconfig",
],
visibility: [
- "//device/google/cuttlefish/build",
+ "//device/google/cuttlefish/build",
+ ],
+}
+
+cc_library_host_static {
+ name: "libwmediumd_server",
+ shared_libs: [
+ "libgflags",
+ "libgrpc++_unsecure",
+ "libprotobuf-cpp-full",
+ ],
+ cflags: [
+ "-Wno-unused-parameter",
+ ],
+ generated_headers: [
+ "WmediumdServerProto_h",
+ ],
+ generated_sources: [
+ "WmediumdServerProto_cc",
+ ],
+ export_generated_headers: [
+ "WmediumdServerProto_h",
+ ],
+}
+
+filegroup {
+ name: "WmediumdServerProto",
+ srcs: [
+ "wmediumd_server/wmediumd.proto",
+ ],
+}
+
+// TODO(278065934): Add a module type for grpc service
+genrule {
+ name: "WmediumdServerProto_h",
+ tools: [
+ "aprotoc",
+ "protoc-gen-grpc-cpp-plugin",
+ ],
+ cmd: "$(location aprotoc) -Iexternal/wmediumd/wmediumd_server -Iexternal/protobuf/src --plugin=protoc-gen-grpc=$(location protoc-gen-grpc-cpp-plugin) $(in) --grpc_out=$(genDir) --cpp_out=$(genDir)",
+ srcs: [
+ ":WmediumdServerProto",
+ ],
+ out: [
+ "wmediumd.grpc.pb.h",
+ "wmediumd.pb.h",
+ ],
+}
+
+genrule {
+ name: "WmediumdServerProto_cc",
+ tools: [
+ "aprotoc",
+ "protoc-gen-grpc-cpp-plugin",
+ ],
+ cmd: "$(location aprotoc) -Iexternal/wmediumd/wmediumd_server -Iexternal/protobuf/src --plugin=protoc-gen-grpc=$(location protoc-gen-grpc-cpp-plugin) $(in) --grpc_out=$(genDir) --cpp_out=$(genDir)",
+ srcs: [
+ ":WmediumdServerProto",
+ ],
+ out: [
+ "wmediumd.grpc.pb.cc",
+ "wmediumd.pb.cc",
],
}
cc_binary_host {
name: "wmediumd",
srcs: [
+ "main.cc",
+ "wmediumd_server/wmediumd_server.cc",
"wmediumd/*.c",
"wmediumd/lib/*.c",
],
@@ -48,15 +112,22 @@
"-Wno-gnu-variable-sized-type-not-at-end",
"-Wno-unused-function",
],
- static_libs: [
+ shared_libs: [
+ "libbase",
"libnl",
+ "libgflags",
+ "libgrpc++_unsecure",
+ "libprotobuf-cpp-full",
+ ],
+ static_libs: [
"libconfig",
+ "libgrpc++_reflection",
+ "libwmediumd_server",
],
+ cpp_std: "c++17",
visibility: [
- "//device/google/cuttlefish/build",
+ "//device/google/cuttlefish/build",
],
- stl: "none",
- static_executable: true,
}
cc_binary_host {
@@ -68,7 +139,7 @@
"wmediumd/inc",
],
visibility: [
- "//device/google/cuttlefish/build",
+ "//device/google/cuttlefish/build",
],
stl: "none",
static_executable: true,
diff --git a/LICENSE b/LICENSE
index d159169..73a0519 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,3 +1,208 @@
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+END OF TERMS AND CONDITIONS
+
+APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+Copyright [yyyy] [name of copyright owner]
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+--------------------------------------------------------------------------------
+
GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
diff --git a/MODULE_LICENSE_APACHE2 b/MODULE_LICENSE_APACHE2
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/MODULE_LICENSE_APACHE2
diff --git a/PREUPLOAD.cfg b/PREUPLOAD.cfg
new file mode 100644
index 0000000..0b46545
--- /dev/null
+++ b/PREUPLOAD.cfg
@@ -0,0 +1,6 @@
+[Builtin Hooks]
+bpfmt = true
+clang_format = true
+
+[Builtin Hooks Options]
+clang_format = --commit ${PREUPLOAD_COMMIT} --style file --extensions c,h,cc,cpp
\ No newline at end of file
diff --git a/main.cc b/main.cc
new file mode 100644
index 0000000..3e0c5f2
--- /dev/null
+++ b/main.cc
@@ -0,0 +1,62 @@
+/*
+ *
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <android-base/logging.h>
+#include <android-base/strings.h>
+#include <sys/eventfd.h>
+#include <sys/msg.h>
+#include <wmediumd/wmediumd.h>
+#include <wmediumd_server/wmediumd_server.h>
+
+#include <string>
+#include <thread>
+#include <vector>
+
+constexpr char kGrpcUdsPathOption[] = "--grpc_uds_path=";
+
+int main(int argc, char* argv[]) {
+ std::vector<char*> wmediumd_args;
+ std::string grpc_uds_path;
+ for (int i = 0; i < argc; i++) {
+ if (android::base::StartsWith(argv[i], kGrpcUdsPathOption)) {
+ std::string current_arg(argv[i]);
+ grpc_uds_path = current_arg.substr(strlen(kGrpcUdsPathOption));
+ } else {
+ wmediumd_args.push_back(argv[i]);
+ }
+ }
+
+ int fd = eventfd(0, 0);
+ int msq_id = msgget(IPC_PRIVATE, IPC_CREAT | 0666);
+
+ std::thread wmediumd_server_thread;
+ if (!grpc_uds_path.empty()) {
+ wmediumd_server_thread =
+ std::thread(RunWmediumdServer, grpc_uds_path, fd, msq_id);
+ }
+
+ wmediumd_main(wmediumd_args.size(), wmediumd_args.data(), fd, msq_id);
+
+ if (!grpc_uds_path.empty()) {
+ wmediumd_server_thread.join();
+ }
+
+ msgctl(msq_id, IPC_RMID, 0);
+ close(fd);
+ return 0;
+}
\ No newline at end of file
diff --git a/util/wmediumd_gen_config.c b/util/wmediumd_gen_config.c
index f7aa25d..8984e7e 100644
--- a/util/wmediumd_gen_config.c
+++ b/util/wmediumd_gen_config.c
@@ -66,18 +66,24 @@
return 0;
}
-int add_cuttlefish_path_loss_model(config_setting_t *model, int instance_count) {
- config_setting_t *type = config_setting_add(model, "type", CONFIG_TYPE_STRING);
+int add_cuttlefish_path_loss_model(config_setting_t *model,
+ int instance_count) {
+ config_setting_t *type =
+ config_setting_add(model, "type", CONFIG_TYPE_STRING);
config_setting_set_string(type, "path_loss");
- config_setting_t *model_name = config_setting_add(model, "model_name", CONFIG_TYPE_STRING);
+ config_setting_t *model_name =
+ config_setting_add(model, "model_name", CONFIG_TYPE_STRING);
config_setting_set_string(model_name, "free_space");
- config_setting_t *positions = config_setting_add(model, "positions", CONFIG_TYPE_LIST);
- config_setting_t *tx_powers = config_setting_add(model, "tx_powers", CONFIG_TYPE_ARRAY);
+ config_setting_t *positions =
+ config_setting_add(model, "positions", CONFIG_TYPE_LIST);
+ config_setting_t *tx_powers =
+ config_setting_add(model, "tx_powers", CONFIG_TYPE_ARRAY);
for (int idx = 0; idx < instance_count; ++idx) {
- config_setting_t *position = config_setting_add(positions, NULL, CONFIG_TYPE_LIST);
+ config_setting_t *position =
+ config_setting_add(positions, NULL, CONFIG_TYPE_LIST);
config_setting_set_float_elem(position, APPEND_LAST, 0.0);
config_setting_set_float_elem(position, APPEND_LAST, 0.0);
@@ -282,7 +288,8 @@
print_help(-1);
}
- config_setting_t *model = config_setting_add(root, "model", CONFIG_TYPE_GROUP);
+ config_setting_t *model =
+ config_setting_add(root, "model", CONFIG_TYPE_GROUP);
add_cuttlefish_path_loss_model(model, config_setting_length(ids));
config_setting_set_int(count, config_setting_length(ids));
diff --git a/wmediumd/.clang-format b/wmediumd/.clang-format
new file mode 100644
index 0000000..c7bf22d
--- /dev/null
+++ b/wmediumd/.clang-format
@@ -0,0 +1,22 @@
+#
+# Copyright (C) 2023 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+# See https://clang.llvm.org/docs/ClangFormatStyleOptions.html for the
+# various options that can be configured and for the definitions of each
+# of the below options.
+
+# Upstream follows other formatting rule. Do not apply clang-format in this dir.
+BasedOnStyle: None
diff --git a/wmediumd/grpc.h b/wmediumd/grpc.h
new file mode 100644
index 0000000..69084f2
--- /dev/null
+++ b/wmediumd/grpc.h
@@ -0,0 +1,42 @@
+/*
+ *
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+
+#define GRPC_MSG_BUF_MAX 1024
+
+// Do not use_zero, the type of the message queue should be positive value.
+enum wmediumd_grpc_type {
+ GRPC_REQUEST = 1,
+ GRPC_RESPONSE,
+};
+
+// Do not use zero, writing zero to eventfd doesn't throw an event.
+enum wmediumd_grpc_request_type {
+ REQUEST_SET_POSITION = 1,
+};
+
+// Do not use zero, writing zero to eventfd doesn't throw an event.
+enum wmediumd_grpc_response_type {
+ RESPONSE_INVALID = 1,
+ RESPONSE_ACK,
+};
+
+struct wmediumd_grpc_message {
+ long type;
+ char data[GRPC_MSG_BUF_MAX];
+};
\ No newline at end of file
diff --git a/wmediumd/inc/usfstl/list.h b/wmediumd/inc/usfstl/list.h
index a07c9e9..079a591 100644
--- a/wmediumd/inc/usfstl/list.h
+++ b/wmediumd/inc/usfstl/list.h
@@ -37,18 +37,18 @@
}
static inline void usfstl_list_insert_before(struct usfstl_list_entry *existing,
- struct usfstl_list_entry *new)
+ struct usfstl_list_entry *new_entry)
{
- new->prev = existing->prev;
- existing->prev->next = new;
- existing->prev = new;
- new->next = existing;
+ new_entry->prev = existing->prev;
+ existing->prev->next = new_entry;
+ existing->prev = new_entry;
+ new_entry->next = existing;
}
static inline void usfstl_list_append(struct usfstl_list *list,
- struct usfstl_list_entry *new)
+ struct usfstl_list_entry *new_entry)
{
- usfstl_list_insert_before(&list->list, new);
+ usfstl_list_insert_before(&list->list, new_entry);
}
#define usfstl_list_item(element, type, member) \
diff --git a/wmediumd/list.h b/wmediumd/list.h
index a6212af..8671c64 100644
--- a/wmediumd/list.h
+++ b/wmediumd/list.h
@@ -7,9 +7,8 @@
/* Stripped down from Linux ~5.5 */
-#define POISON_POINTER_DELTA 0
-#define LIST_POISON1 ((void *) 0x00100100 + POISON_POINTER_DELTA)
-#define LIST_POISON2 ((void *) 0x00200200 + POISON_POINTER_DELTA)
+#define LIST_POISON1 ((void *) 0x00100100)
+#define LIST_POISON2 ((void *) 0x00200200)
#define WRITE_ONCE(p, v) do { p = v; } while (0)
#define READ_ONCE(p) (p)
#ifndef container_of
@@ -48,7 +47,7 @@
list->prev = list;
}
-static inline bool __list_add_valid(struct list_head *new,
+static inline bool __list_add_valid(struct list_head *new_elem,
struct list_head *prev,
struct list_head *next)
{
@@ -65,44 +64,44 @@
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
-static inline void __list_add(struct list_head *new,
+static inline void __list_add(struct list_head *new_elem,
struct list_head *prev,
struct list_head *next)
{
- if (!__list_add_valid(new, prev, next))
+ if (!__list_add_valid(new_elem, prev, next))
return;
- next->prev = new;
- new->next = next;
- new->prev = prev;
- WRITE_ONCE(prev->next, new);
+ next->prev = new_elem;
+ new_elem->next = next;
+ new_elem->prev = prev;
+ WRITE_ONCE(prev->next, new_elem);
}
/**
* list_add - add a new entry
- * @new: new entry to be added
+ * @new_elem: new entry to be added
* @head: list head to add it after
*
* Insert a new entry after the specified head.
* This is good for implementing stacks.
*/
-static inline void list_add(struct list_head *new, struct list_head *head)
+static inline void list_add(struct list_head *new_elem, struct list_head *head)
{
- __list_add(new, head, head->next);
+ __list_add(new_elem, head, head->next);
}
/**
* list_add_tail - add a new entry
- * @new: new entry to be added
+ * @new_elem: new entry to be added
* @head: list head to add it before
*
* Insert a new entry before the specified head.
* This is useful for implementing queues.
*/
-static inline void list_add_tail(struct list_head *new, struct list_head *head)
+static inline void list_add_tail(struct list_head *new_elem, struct list_head *head)
{
- __list_add(new, head->prev, head);
+ __list_add(new_elem, head->prev, head);
}
/*
@@ -149,37 +148,37 @@
static inline void list_del(struct list_head *entry)
{
__list_del_entry(entry);
- entry->next = LIST_POISON1;
- entry->prev = LIST_POISON2;
+ entry->next = (struct list_head *)LIST_POISON1;
+ entry->prev = (struct list_head *)LIST_POISON2;
}
/**
* list_replace - replace old entry by new one
* @old : the element to be replaced
- * @new : the new element to insert
+ * @new_elem : the new element to insert
*
* If @old was empty, it will be overwritten.
*/
static inline void list_replace(struct list_head *old,
- struct list_head *new)
+ struct list_head *new_elem)
{
- new->next = old->next;
- new->next->prev = new;
- new->prev = old->prev;
- new->prev->next = new;
+ new_elem->next = old->next;
+ new_elem->next->prev = new_elem;
+ new_elem->prev = old->prev;
+ new_elem->prev->next = new_elem;
}
/**
* list_replace_init - replace old entry by new one and initialize the old one
* @old : the element to be replaced
- * @new : the new element to insert
+ * @new_elem : the new element to insert
*
* If @old was empty, it will be overwritten.
*/
static inline void list_replace_init(struct list_head *old,
- struct list_head *new)
+ struct list_head *new_elem)
{
- list_replace(old, new);
+ list_replace(old, new_elem);
INIT_LIST_HEAD(old);
}
diff --git a/wmediumd/pmsr.h b/wmediumd/pmsr.h
index 74d6a68..3b408c5 100644
--- a/wmediumd/pmsr.h
+++ b/wmediumd/pmsr.h
@@ -65,6 +65,7 @@
struct pmsr_request {
uint32_t timeout;
+ // Only for mac randomization
uint8_t mac_addr[ETH_ALEN];
uint8_t mac_addr_mask[ETH_ALEN];
diff --git a/wmediumd/wmediumd.c b/wmediumd/wmediumd.c
index 5f456a3..0f4f050 100644
--- a/wmediumd/wmediumd.c
+++ b/wmediumd/wmediumd.c
@@ -39,6 +39,7 @@
#include <unistd.h>
#include <stdarg.h>
#include <endian.h>
+#include <sys/msg.h>
#include <usfstl/loop.h>
#include <usfstl/sched.h>
#include <usfstl/schedctrl.h>
@@ -50,6 +51,7 @@
#include "config.h"
#include "api.h"
#include "pmsr.h"
+#include "grpc.h"
USFSTL_SCHEDULER(scheduler);
@@ -834,7 +836,7 @@
if (memcmp(src, station->addr, ETH_ALEN) == 0)
continue;
- if (is_multicast_ether_addr(dest)) {
+ if (is_multicast_ether_addr(dest) && station->client != NULL) {
int snr, rate_idx, signal;
double error_prob;
@@ -1518,6 +1520,43 @@
static void init_pcapng(struct wmediumd *ctx, const char *filename);
+static void wmediumd_grpc_service_handler(struct usfstl_loop_entry *entry) {
+ struct wmediumd *ctx = entry->data;
+
+ // Receive request type from WmediumdService
+ uint64_t request_type;
+ read(entry->fd, &request_type, sizeof(uint64_t));
+
+ struct wmediumd_grpc_message request_body;
+ uint64_t response_type;
+
+ // Receive request body from WmediumdService and do the task.
+ // TODO(273384914): Support more request types.
+ switch (request_type) {
+ case REQUEST_SET_POSITION:
+ if (msgrcv(ctx->msq_id, &request_body, sizeof(struct wmediumd_set_position), GRPC_REQUEST, 0) != sizeof(struct wmediumd_set_position)) {
+ w_logf(ctx, LOG_ERR, "%s: failed to get set_position request body\n", __func__);
+ }
+
+ if (process_set_position_message(ctx, (struct wmediumd_set_position *)(request_body.data)) < 0) {
+ w_logf(ctx, LOG_ERR, "%s: failed to execute set_position\n", __func__);
+ response_type = RESPONSE_INVALID;
+ }
+ response_type = RESPONSE_ACK;
+ break;
+ default:
+ w_logf(ctx, LOG_ERR, "%s: unknown request type\n", __func__);
+ response_type = RESPONSE_INVALID;
+ break;
+ }
+
+ // TODO(273384914): Send response with response_type
+ return;
+}
+
+// TODO(273384914): Deprecate messages used in wmediumd_control after
+// implementing in wmediumd_grpc_service_handler to be used in the command
+// 'cvd env'.
static void wmediumd_api_handler(struct usfstl_loop_entry *entry)
{
struct client *client = container_of(entry, struct client, loop);
@@ -1871,7 +1910,7 @@
#define VIRTIO_F_VERSION_1 32
#endif
-int main(int argc, char *argv[])
+int wmediumd_main(int argc, char *argv[], int event_fd, int msq_id)
{
int opt;
struct wmediumd ctx = {};
@@ -2022,6 +2061,13 @@
usfstl_sched_wallclock_init(&scheduler, 1000);
}
+ // Control event_fd to communicate WmediumdService.
+ ctx.grpc_loop.handler = wmediumd_grpc_service_handler;
+ ctx.grpc_loop.data = &ctx;
+ ctx.grpc_loop.fd = event_fd;
+ usfstl_loop_register(&ctx.grpc_loop);
+ ctx.msq_id = msq_id;
+
while (1) {
if (time_socket) {
usfstl_sched_next(&scheduler);
diff --git a/wmediumd/wmediumd.h b/wmediumd/wmediumd.h
index c5459bc..695f871 100644
--- a/wmediumd/wmediumd.h
+++ b/wmediumd/wmediumd.h
@@ -214,9 +214,10 @@
struct wmediumd {
int timerfd;
+ int msq_id;
struct nl_sock *sock;
- struct usfstl_loop_entry nl_loop;
+ struct usfstl_loop_entry nl_loop, grpc_loop;
struct usfstl_sched_ctrl *ctrl;
@@ -306,4 +307,14 @@
int index_to_rate(size_t index, u32 freq);
int get_max_index(void);
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int wmediumd_main(int argc, char *argv[], int event_fd, int msq_id);
+
+#ifdef __cplusplus
+}
+#endif
+
#endif /* WMEDIUMD_H_ */
diff --git a/wmediumd_server/wmediumd.proto b/wmediumd_server/wmediumd.proto
new file mode 100644
index 0000000..592e708
--- /dev/null
+++ b/wmediumd_server/wmediumd.proto
@@ -0,0 +1,30 @@
+// Copyright (C) 2023 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+syntax = "proto3";
+
+package wmediumdserver;
+
+import "google/protobuf/empty.proto";
+
+service WmediumdService {
+ // TODO(273384914): Define a response type.
+ rpc SetPosition(SetPositionRequest) returns (google.protobuf.Empty) {}
+}
+
+message SetPositionRequest {
+ string mac_address = 1;
+ double x_pos = 2;
+ double y_pos = 3;
+}
diff --git a/wmediumd_server/wmediumd_server.cc b/wmediumd_server/wmediumd_server.cc
new file mode 100644
index 0000000..229d505
--- /dev/null
+++ b/wmediumd_server/wmediumd_server.cc
@@ -0,0 +1,147 @@
+/*
+ *
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <android-base/strings.h>
+#include <gflags/gflags.h>
+#include <grpcpp/ext/proto_server_reflection_plugin.h>
+#include <grpcpp/grpcpp.h>
+#include <grpcpp/health_check_service_interface.h>
+#include <sys/msg.h>
+#include <unistd.h>
+
+#include <algorithm>
+#include <array>
+#include <iostream>
+#include <memory>
+#include <string>
+
+#include "wmediumd.grpc.pb.h"
+#include "wmediumd/api.h"
+#include "wmediumd/grpc.h"
+
+using google::protobuf::Empty;
+using grpc::Server;
+using grpc::ServerBuilder;
+using grpc::ServerContext;
+using grpc::Status;
+using grpc::StatusCode;
+using wmediumdserver::SetPositionRequest;
+using wmediumdserver::WmediumdService;
+
+#define MAC_ADDR_LEN 6
+#define STR_MAC_ADDR_LEN 17
+
+template <class T>
+static void AppendBinaryRepresentation(std::string& buf, const T& data) {
+ std::copy(reinterpret_cast<const char*>(&data),
+ reinterpret_cast<const char*>(&data) + sizeof(T),
+ std::back_inserter(buf));
+}
+
+bool IsValidMacAddr(const std::string& mac_address) {
+ if (mac_address.size() != STR_MAC_ADDR_LEN) {
+ return false;
+ }
+
+ if (mac_address[2] != ':' || mac_address[5] != ':' || mac_address[8] != ':' ||
+ mac_address[11] != ':' || mac_address[14] != ':') {
+ return false;
+ }
+
+ for (int i = 0; i < STR_MAC_ADDR_LEN; ++i) {
+ if ((i - 2) % 3 == 0) continue;
+ char c = mac_address[i];
+
+ if (isupper(c)) {
+ c = tolower(c);
+ }
+
+ if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) return false;
+ }
+
+ return true;
+}
+
+static std::array<uint8_t, 6> ParseMacAddress(const std::string& mac_address) {
+ auto split_mac = android::base::Split(mac_address, ":");
+ std::array<uint8_t, 6> mac;
+ for (int i = 0; i < 6; i++) {
+ char* end_ptr;
+ mac[i] = (uint8_t)strtol(split_mac[i].c_str(), &end_ptr, 16);
+ }
+
+ return mac;
+}
+
+class WmediumdServiceImpl final : public WmediumdService::Service {
+ public:
+ WmediumdServiceImpl(int event_fd, int msq_id)
+ : event_fd_(event_fd), msq_id_(msq_id) {}
+
+ Status SetPosition(ServerContext* context, const SetPositionRequest* request,
+ Empty* reply) override {
+ // Validate parameters
+ if (!IsValidMacAddr(request->mac_address())) {
+ return Status(StatusCode::INVALID_ARGUMENT, "Got invalid mac address");
+ }
+ auto mac = ParseMacAddress(request->mac_address());
+
+ // Construct request data
+ struct wmediumd_set_position data;
+ memcpy(data.mac, &mac, sizeof(mac));
+ data.x = request->x_pos();
+ data.y = request->y_pos();
+
+ // Fill data in the message queue
+ struct wmediumd_grpc_message msg;
+ msg.type = GRPC_REQUEST;
+ memcpy(msg.data, &data, sizeof(data));
+ msgsnd(msq_id_, &msg, sizeof(data), 0);
+
+ // Throw an event to wmediumd
+ uint64_t value = REQUEST_SET_POSITION;
+ write(event_fd_, &value, sizeof(uint64_t));
+
+ return Status::OK;
+ }
+
+ private:
+ int event_fd_;
+ int msq_id_;
+};
+
+void RunWmediumdServer(std::string grpc_uds_path, int event_fd, int msq_id) {
+ std::string server_address("unix:" + grpc_uds_path);
+ WmediumdServiceImpl service(event_fd, msq_id);
+
+ grpc::EnableDefaultHealthCheckService(true);
+ grpc::reflection::InitProtoReflectionServerBuilderPlugin();
+ ServerBuilder builder;
+ // Listen on the given address without any authentication mechanism.
+ builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
+ // Register "service" as the instance through which we'll communicate with
+ // clients. In this case it corresponds to an *synchronous* service.
+ builder.RegisterService(&service);
+ // Finally assemble the server.
+ std::unique_ptr<Server> server(builder.BuildAndStart());
+ std::cout << "Server listening on " << server_address << std::endl;
+
+ // Wait for the server to shutdown. Note that some other thread must be
+ // responsible for shutting down the server for this call to ever return.
+ server->Wait();
+}
diff --git a/wmediumd_server/wmediumd_server.h b/wmediumd_server/wmediumd_server.h
new file mode 100644
index 0000000..31761ac
--- /dev/null
+++ b/wmediumd_server/wmediumd_server.h
@@ -0,0 +1,19 @@
+/*
+ *
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+void RunWmediumdServer(std::string grpc_uds_path, int event_fd, int msq_id);
\ No newline at end of file