pax_global_header00006660000000000000000000000064146235010230014506gustar00rootroot0000000000000052 comment=94f964b00f34d7b87fb35bb2bb66888c6ee4ed66 81voltd-v1.0.0/000077500000000000000000000000001462350102300131735ustar00rootroot0000000000000081voltd-v1.0.0/LICENSE000066400000000000000000000432541462350102300142100ustar00rootroot00000000000000 GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Lesser General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. 81voltd-v1.0.0/README.md000066400000000000000000000007301462350102300144520ustar00rootroot00000000000000This is a simple implementation of the IMS Data service on QMI/QRTR. It handles the known and important requests to start and stop a connection through the ModemManager D-Bus API, treating other requests as no operation. Some known issues remain: - Stopping the implementation does not release or disconnect any bearers. This makes it hard to debug, requiring the modem be set to low-power, then back online. - ModemManager errors are not communicated to the modem. 81voltd-v1.0.0/imsd.qmi000066400000000000000000000023331462350102300146400ustar00rootroot00000000000000# SPDX-License-Identifier: GPL-2.0-or-later # # Copyright (c) 2024, Richard Acayan. All rights reserved. # package imsd; struct wds_spec { string apn; u32 unkfield_0_5; u32 profiles_select; u32 ip_family; u16 profile_idx_3gpp; u16 profile_idx_3gpp2; }; struct address { u32 family; string addr; }; struct op_res { u16 err_status; u16 err_code; }; request start_connection_req { required wds_spec conn_params = 0x1; required u32 connection = 0x10; required u32 subscription = 0x13; } = 0x20; response start_connection_resp { required op_res res = 0x2; required u8 connection = 0x10; required u32 orig_conn_id = 0x11; required u32 subscription = 0x12; } = 0x20; indication connection_changed_ind { required op_res res = 0x2; required u8 connection = 0x1; required u32 orig_conn_id = 0x10; optional address ip_addr = 0x11; required u32 subscription = 0x12; } = 0x20; request stop_connection_req { required u8 connection = 0x1; required u32 unkecho_01 = 0x10; } = 0x21; response stop_connection_resp { required op_res res = 0x2; required u8 unkfield_FA = 0x10; required u32 unkecho_01 = 0x11; } = 0x21; request no_op_req { optional u8 data[255] = 0x1; }; response no_op_resp { required op_res res = 0x2; }; 81voltd-v1.0.0/main.c000066400000000000000000000013231462350102300142620ustar00rootroot00000000000000// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) 2024, Richard Acayan. All rights reserved. */ #include #include "qvd-modem.h" #include "qvd-server.h" int main(void) { GMainLoop *loop; GError *err = NULL; QVDModem *modem; QVDServer *server; modem = qvd_modem_new(&err); if (err != NULL) { g_printerr("Failed to create connection manager: %s\n", err->message); g_error_free(err); return 1; } server = qvd_server_new(modem, &err); if (err != NULL) { g_printerr("Failed to create IMSD server: %s\n", err->message); g_error_free(err); return 1; } loop = g_main_loop_new(NULL, FALSE); g_main_loop_run(loop); g_object_unref(server); g_object_unref(modem); return 0; } 81voltd-v1.0.0/meson.build000066400000000000000000000012641462350102300153400ustar00rootroot00000000000000project('81voltd', 'c', version : '1.0.0') libmm = dependency('mm-glib') libqrtr = meson.get_compiler('c').find_library('qrtr') qmic = find_program('qmic', required : false) wrapper = files('qmic_wrapper.sh') if qmic.found() qmi = custom_target('qmi_imsd', command : [wrapper, qmic, '@INPUT@', '@OUTDIR@'], input : 'imsd.qmi', output : ['qmi_imsd.c', 'qmi_imsd.h']) else qmi = custom_target('qmi_imsd', command : ['cp', '@INPUT@', '@OUTDIR@'], input : ['qmi_imsd.c', 'qmi_imsd.h'], output : ['qmi_imsd.c', 'qmi_imsd.h']) endif executable('81voltd', 'main.c', 'qvd-connection.c', 'qvd-modem.c', 'qvd-server.c', qmi, dependencies : [libmm, libqrtr]) 81voltd-v1.0.0/qmi_imsd.c000066400000000000000000000135731462350102300151520ustar00rootroot00000000000000#include #include #include "qmi_imsd.h" struct qmi_elem_info imsd_wds_spec_ei[] = { { .data_type = QMI_STRING, .elem_len = 256, .elem_size = sizeof(char), .offset = offsetof(struct imsd_wds_spec, apn) }, { .data_type = QMI_UNSIGNED_4_BYTE, .elem_len = 1, .elem_size = sizeof(uint32_t), .offset = offsetof(struct imsd_wds_spec, unkfield_0_5), }, { .data_type = QMI_UNSIGNED_4_BYTE, .elem_len = 1, .elem_size = sizeof(uint32_t), .offset = offsetof(struct imsd_wds_spec, profiles_select), }, { .data_type = QMI_UNSIGNED_4_BYTE, .elem_len = 1, .elem_size = sizeof(uint32_t), .offset = offsetof(struct imsd_wds_spec, ip_family), }, { .data_type = QMI_UNSIGNED_2_BYTE, .elem_len = 1, .elem_size = sizeof(uint16_t), .offset = offsetof(struct imsd_wds_spec, profile_idx_3gpp), }, { .data_type = QMI_UNSIGNED_2_BYTE, .elem_len = 1, .elem_size = sizeof(uint16_t), .offset = offsetof(struct imsd_wds_spec, profile_idx_3gpp2), }, {} }; struct qmi_elem_info imsd_address_ei[] = { { .data_type = QMI_UNSIGNED_4_BYTE, .elem_len = 1, .elem_size = sizeof(uint32_t), .offset = offsetof(struct imsd_address, family), }, { .data_type = QMI_STRING, .elem_len = 256, .elem_size = sizeof(char), .offset = offsetof(struct imsd_address, addr) }, {} }; struct qmi_elem_info imsd_op_res_ei[] = { { .data_type = QMI_UNSIGNED_2_BYTE, .elem_len = 1, .elem_size = sizeof(uint16_t), .offset = offsetof(struct imsd_op_res, err_status), }, { .data_type = QMI_UNSIGNED_2_BYTE, .elem_len = 1, .elem_size = sizeof(uint16_t), .offset = offsetof(struct imsd_op_res, err_code), }, {} }; struct qmi_elem_info imsd_start_connection_req_ei[] = { { .data_type = QMI_STRUCT, .elem_len = 1, .elem_size = sizeof(struct imsd_wds_spec), .tlv_type = 1, .offset = offsetof(struct imsd_start_connection_req, conn_params), .ei_array = imsd_wds_spec_ei, }, { .data_type = QMI_UNSIGNED_4_BYTE, .elem_len = 1, .elem_size = sizeof(uint32_t), .tlv_type = 16, .offset = offsetof(struct imsd_start_connection_req, connection), }, { .data_type = QMI_UNSIGNED_4_BYTE, .elem_len = 1, .elem_size = sizeof(uint32_t), .tlv_type = 19, .offset = offsetof(struct imsd_start_connection_req, subscription), }, {} }; struct qmi_elem_info imsd_start_connection_resp_ei[] = { { .data_type = QMI_STRUCT, .elem_len = 1, .elem_size = sizeof(struct imsd_op_res), .tlv_type = 2, .offset = offsetof(struct imsd_start_connection_resp, res), .ei_array = imsd_op_res_ei, }, { .data_type = QMI_UNSIGNED_1_BYTE, .elem_len = 1, .elem_size = sizeof(uint8_t), .tlv_type = 16, .offset = offsetof(struct imsd_start_connection_resp, connection), }, { .data_type = QMI_UNSIGNED_4_BYTE, .elem_len = 1, .elem_size = sizeof(uint32_t), .tlv_type = 17, .offset = offsetof(struct imsd_start_connection_resp, orig_conn_id), }, { .data_type = QMI_UNSIGNED_4_BYTE, .elem_len = 1, .elem_size = sizeof(uint32_t), .tlv_type = 18, .offset = offsetof(struct imsd_start_connection_resp, subscription), }, {} }; struct qmi_elem_info imsd_connection_changed_ind_ei[] = { { .data_type = QMI_STRUCT, .elem_len = 1, .elem_size = sizeof(struct imsd_op_res), .tlv_type = 2, .offset = offsetof(struct imsd_connection_changed_ind, res), .ei_array = imsd_op_res_ei, }, { .data_type = QMI_UNSIGNED_1_BYTE, .elem_len = 1, .elem_size = sizeof(uint8_t), .tlv_type = 1, .offset = offsetof(struct imsd_connection_changed_ind, connection), }, { .data_type = QMI_UNSIGNED_4_BYTE, .elem_len = 1, .elem_size = sizeof(uint32_t), .tlv_type = 16, .offset = offsetof(struct imsd_connection_changed_ind, orig_conn_id), }, { .data_type = QMI_OPT_FLAG, .elem_len = 1, .elem_size = sizeof(bool), .tlv_type = 17, .offset = offsetof(struct imsd_connection_changed_ind, ip_addr_valid), }, { .data_type = QMI_STRUCT, .elem_len = 1, .elem_size = sizeof(struct imsd_address), .tlv_type = 17, .offset = offsetof(struct imsd_connection_changed_ind, ip_addr), .ei_array = imsd_address_ei, }, { .data_type = QMI_UNSIGNED_4_BYTE, .elem_len = 1, .elem_size = sizeof(uint32_t), .tlv_type = 18, .offset = offsetof(struct imsd_connection_changed_ind, subscription), }, {} }; struct qmi_elem_info imsd_stop_connection_req_ei[] = { { .data_type = QMI_UNSIGNED_1_BYTE, .elem_len = 1, .elem_size = sizeof(uint8_t), .tlv_type = 1, .offset = offsetof(struct imsd_stop_connection_req, connection), }, { .data_type = QMI_UNSIGNED_4_BYTE, .elem_len = 1, .elem_size = sizeof(uint32_t), .tlv_type = 16, .offset = offsetof(struct imsd_stop_connection_req, unkecho_01), }, {} }; struct qmi_elem_info imsd_stop_connection_resp_ei[] = { { .data_type = QMI_STRUCT, .elem_len = 1, .elem_size = sizeof(struct imsd_op_res), .tlv_type = 2, .offset = offsetof(struct imsd_stop_connection_resp, res), .ei_array = imsd_op_res_ei, }, { .data_type = QMI_UNSIGNED_1_BYTE, .elem_len = 1, .elem_size = sizeof(uint8_t), .tlv_type = 16, .offset = offsetof(struct imsd_stop_connection_resp, unkfield_FA), }, { .data_type = QMI_UNSIGNED_4_BYTE, .elem_len = 1, .elem_size = sizeof(uint32_t), .tlv_type = 17, .offset = offsetof(struct imsd_stop_connection_resp, unkecho_01), }, {} }; struct qmi_elem_info imsd_no_op_req_ei[] = { { .data_type = QMI_OPT_FLAG, .elem_len = 1, .elem_size = sizeof(bool), .tlv_type = 1, .offset = offsetof(struct imsd_no_op_req, data_valid), }, { .data_type = QMI_UNSIGNED_1_BYTE, .elem_len = 255, .elem_size = sizeof(uint8_t), .array_type = STATIC_ARRAY, .tlv_type = 1, .offset = offsetof(struct imsd_no_op_req, data), }, {} }; struct qmi_elem_info imsd_no_op_resp_ei[] = { { .data_type = QMI_STRUCT, .elem_len = 1, .elem_size = sizeof(struct imsd_op_res), .tlv_type = 2, .offset = offsetof(struct imsd_no_op_resp, res), .ei_array = imsd_op_res_ei, }, {} }; 81voltd-v1.0.0/qmi_imsd.h000066400000000000000000000031341462350102300151470ustar00rootroot00000000000000#ifndef __QMI_IMSD_H__ #define __QMI_IMSD_H__ #include #include #include "libqrtr.h" struct imsd_wds_spec { uint32_t apn_len; char apn[256]; uint32_t unkfield_0_5; uint32_t profiles_select; uint32_t ip_family; uint16_t profile_idx_3gpp; uint16_t profile_idx_3gpp2; }; struct imsd_address { uint32_t family; uint32_t addr_len; char addr[256]; }; struct imsd_op_res { uint16_t err_status; uint16_t err_code; }; struct imsd_start_connection_req { struct imsd_wds_spec conn_params; uint32_t connection; uint32_t subscription; }; struct imsd_start_connection_resp { struct imsd_op_res res; uint8_t connection; uint32_t orig_conn_id; uint32_t subscription; }; struct imsd_connection_changed_ind { struct imsd_op_res res; uint8_t connection; uint32_t orig_conn_id; bool ip_addr_valid; struct imsd_address ip_addr; uint32_t subscription; }; struct imsd_stop_connection_req { uint8_t connection; uint32_t unkecho_01; }; struct imsd_stop_connection_resp { struct imsd_op_res res; uint8_t unkfield_FA; uint32_t unkecho_01; }; struct imsd_no_op_req { bool data_valid; uint32_t data_len; uint8_t data[255]; }; struct imsd_no_op_resp { struct imsd_op_res res; }; extern struct qmi_elem_info imsd_start_connection_req_ei[]; extern struct qmi_elem_info imsd_start_connection_resp_ei[]; extern struct qmi_elem_info imsd_connection_changed_ind_ei[]; extern struct qmi_elem_info imsd_stop_connection_req_ei[]; extern struct qmi_elem_info imsd_stop_connection_resp_ei[]; extern struct qmi_elem_info imsd_no_op_req_ei[]; extern struct qmi_elem_info imsd_no_op_resp_ei[]; #endif 81voltd-v1.0.0/qmic_wrapper.sh000077500000000000000000000011311462350102300162170ustar00rootroot00000000000000#!/bin/sh # SPDX-License-Identifier: GPL-2.0-or-later # # QMI interface compiler wrapper from sensh # # Copyright (C) 2023 Richard Acayan # qmic="$1" # # Usage: qmic_wrapper.sh QMIC INPUT OUTPUT_DIRECTORY # # Example: qmic_wrapper.sh /usr/bin/qmic ../qmi/sns_client.qmi qmi/ # # # We need to move into the output directory so QMIC puts the files in the # correct place, but the path to the input might be relative to the directory # we are already in. Open the file now and pipe it to stdin so the pathname # gets processed relative to the correct directory. # exec < "$2" cd "$3" exec "$qmic" -k 81voltd-v1.0.0/qvd-connection.c000066400000000000000000000113711462350102300162710ustar00rootroot00000000000000// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) 2024, Richard Acayan. All rights reserved. */ #include #include #include "qvd-connection.h" enum { ADDR_CHANGED, CONNECT_ERR, DISCONNECT_ERR, N_SIGNALS, }; struct _QVDConnection { GObject parent_instance; MMBearer *bearer; MMBearerIpFamily ip_type; gchar *addr; gboolean requested; guint32 sub_id, conn_id; guint8 local_id; }; G_DEFINE_TYPE(QVDConnection, qvd_connection, G_TYPE_OBJECT) static guint conn_signals[N_SIGNALS]; static void qvd_connection_dispose(GObject *obj); static void qvd_connection_class_init(QVDConnectionClass *c) { GObjectClass *obj_class = G_OBJECT_CLASS(c); obj_class->dispose = qvd_connection_dispose; conn_signals[ADDR_CHANGED] = g_signal_new("qvd-address-changed", QVD_TYPE_CONNECTION, G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 2, G_TYPE_INT, G_TYPE_STRING); conn_signals[CONNECT_ERR] = g_signal_new("qvd-connect-error", QVD_TYPE_CONNECTION, G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 1, G_TYPE_ERROR); conn_signals[DISCONNECT_ERR] = g_signal_new("qvd-disconnect-error", QVD_TYPE_CONNECTION, G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 1, G_TYPE_ERROR); } static void qvd_connection_init(QVDConnection *conn) { } static void qvd_connection_dispose(GObject *obj) { QVDConnection *conn = QVD_CONNECTION(obj); if (conn->addr != NULL) g_free(g_steal_pointer(&conn->addr)); g_clear_object(&conn->bearer); } QVDConnection *qvd_connection_new(MMBearerIpFamily ip_type) { QVDConnection *conn; conn = QVD_CONNECTION(g_object_new(QVD_TYPE_CONNECTION, NULL)); conn->ip_type = ip_type; return conn; } void qvd_connection_get_handles(const QVDConnection *conn, guint32 *sub_id, guint32 *conn_id, guint8 *local_id) { g_assert(conn != NULL); if (sub_id != NULL) *sub_id = conn->sub_id; if (conn_id != NULL) *conn_id = conn->conn_id; if (local_id != NULL) *local_id = conn->local_id; } static void try_notify_address(QVDConnection *conn, GVariant *changed, MMBearerIpFamily ip_type, const gchar *conf_name) { GVariant *ip_conf; gchar *addr = NULL; ip_conf = g_variant_lookup_value(changed, conf_name, G_VARIANT_TYPE_DICTIONARY); if (ip_conf == NULL) return; g_variant_lookup(ip_conf, "address", "s", &addr); if (g_strcmp0(addr, conn->addr)) { g_signal_emit(conn, conn_signals[ADDR_CHANGED], 0, ip_type, addr); conn->addr = addr; } else { g_free(addr); } } void qvd_connection_set_handles(QVDConnection *conn, guint32 sub_id, guint32 conn_id, guint8 local_id) { g_assert(conn != NULL); conn->sub_id = sub_id; conn->conn_id = conn_id; conn->local_id = local_id; } static void on_connect(GObject *obj, GAsyncResult *res, gpointer data) { QVDConnection *conn = QVD_CONNECTION(data); GError *err = NULL; mm_bearer_connect_finish(MM_BEARER(obj), res, &err); if (err != NULL) { g_printerr("Failed to connect: %s\n", err->message); g_signal_emit(conn, conn_signals[CONNECT_ERR], 0, err); g_clear_error(&err); } } void qvd_connection_connect(QVDConnection *conn) { g_assert(conn != NULL); conn->requested = TRUE; if (conn->bearer != NULL) mm_bearer_connect(conn->bearer, NULL, on_connect, conn); } static void on_disconnect(GObject *obj, GAsyncResult *res, gpointer data) { QVDConnection *conn = QVD_CONNECTION(data); GError *err = NULL; mm_bearer_disconnect_finish(MM_BEARER(obj), res, &err); if (err != NULL) { g_printerr("Failed to disconnect: %s\n", err->message); g_signal_emit(conn, conn_signals[DISCONNECT_ERR], 0, err); g_clear_error(&err); } } void qvd_connection_disconnect(QVDConnection *conn) { g_assert(conn != NULL); g_assert(conn->bearer != NULL); conn->requested = FALSE; if (conn->bearer != NULL) mm_bearer_disconnect(conn->bearer, NULL, on_disconnect, conn); } static void on_bearer_properties_changed(GDBusProxy* bearer, GVariant* changed, char** invalidated, gpointer data) { QVDConnection *conn = QVD_CONNECTION(data); g_assert(conn->ip_type == MM_BEARER_IP_FAMILY_IPV4 || conn->ip_type == MM_BEARER_IP_FAMILY_IPV6); if (conn->ip_type == MM_BEARER_IP_FAMILY_IPV4) try_notify_address(conn, changed, MM_BEARER_IP_FAMILY_IPV4, "Ip4Config"); else try_notify_address(conn, changed, MM_BEARER_IP_FAMILY_IPV6, "Ip6Config"); } void qvd_connection_set_bearer(QVDConnection *conn, MMBearer *bearer) { g_assert(conn != NULL); g_assert(conn->bearer == NULL); conn->bearer = g_object_ref(bearer); g_signal_connect(bearer, "g-properties-changed", G_CALLBACK(on_bearer_properties_changed), conn); if (conn->requested) mm_bearer_connect(conn->bearer, NULL, on_connect, conn); } 81voltd-v1.0.0/qvd-connection.h000066400000000000000000000015751462350102300163030ustar00rootroot00000000000000/* SPDX-License-Identifier: GPL-2.0-or-later */ /* * Copyright (c) 2024, Richard Acayan. All rights reserved. */ #ifndef QVD_CONNECTION_H #define QVD_CONNECTION_H #include #include #include G_BEGIN_DECLS #define QVD_TYPE_CONNECTION qvd_connection_get_type() G_DECLARE_FINAL_TYPE(QVDConnection, qvd_connection, QVD, CONNECTION, GObject) QVDConnection *qvd_connection_new(MMBearerIpFamily ip_type); void qvd_connection_get_handles(const QVDConnection *conn, guint32 *sub_id, guint32 *conn_id, guint8 *local_id); void qvd_connection_set_handles(QVDConnection *conn, guint32 sub_id, guint32 conn_id, guint8 local_id); void qvd_connection_connect(QVDConnection *conn); void qvd_connection_disconnect(QVDConnection *conn); void qvd_connection_set_bearer(QVDConnection *conn, MMBearer *bearer); G_END_DECLS #endif /* QVD_CONNECTION_H */ 81voltd-v1.0.0/qvd-modem.c000066400000000000000000000160201462350102300152270ustar00rootroot00000000000000// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) 2024, Richard Acayan. All rights reserved. */ #include #include #include #include #include #include "qvd-connection.h" #include "qvd-modem.h" enum { ADDR_CHANGED, CONNECT_ERR, DISCONNECT_ERR, N_SIGNALS, }; struct _QVDModem { GObject parent_instance; GDBusConnection *dbus; MMManager *manager; MMModem *modem; GHashTable *bearers; }; struct qvd_bearer_config { MMBearerIpFamily ip_type; gchar apn[256]; }; static void qvd_modem_initable_iface_init(GInitableIface *iface); G_DEFINE_TYPE_WITH_CODE(QVDModem, qvd_modem, G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE(G_TYPE_INITABLE, qvd_modem_initable_iface_init)) static gboolean qvd_modem_initable_init(GInitable *initable, GCancellable *cancellable, GError **out_err); static void qvd_modem_dispose(GObject *obj); static guint modem_signals[N_SIGNALS]; static void qvd_modem_class_init(QVDModemClass *c) { GObjectClass *obj_class = G_OBJECT_CLASS(c); obj_class->dispose = qvd_modem_dispose; modem_signals[ADDR_CHANGED] = g_signal_new("qvd-connection-address-changed", QVD_TYPE_MODEM, G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 3, QVD_TYPE_CONNECTION, G_TYPE_INT, G_TYPE_STRING); modem_signals[CONNECT_ERR] = g_signal_new("qvd-connection-connect-error", QVD_TYPE_MODEM, G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 2, QVD_TYPE_CONNECTION, G_TYPE_ERROR); modem_signals[DISCONNECT_ERR] = g_signal_new("qvd-connection-disconnect-error", QVD_TYPE_MODEM, G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 2, QVD_TYPE_CONNECTION, G_TYPE_ERROR); } static void qvd_modem_initable_iface_init(GInitableIface *iface) { iface->init = qvd_modem_initable_init; } static void qvd_modem_init(QVDModem *self) { } static void on_object_added(QVDConnection *conn, GDBusObject *object, gpointer data) { MMModem *modem; const MMModemPortInfo *ports; guint n_ports = 0, i; gboolean is_qrtr = FALSE; QVDModem *ctx = QVD_MODEM(data); modem = mm_object_get_modem(MM_OBJECT(object)); if (modem == NULL) return; mm_modem_peek_ports(modem, &ports, &n_ports); for (i = 0; i < n_ports; i++) { if (ports[i].type == MM_MODEM_PORT_TYPE_QMI && !strcmp(ports[i].name, "qrtr0")) { is_qrtr = TRUE; break; } } if (is_qrtr) { g_info("ModemManager QRTR modem detected\n"); ctx->modem = g_object_ref(modem); } } static void find_modem(gpointer memb, gpointer data) { MMModem **out = data; MMModem *modem; const MMModemPortInfo *ports; guint n_ports = 0, i; gboolean is_qrtr = FALSE; modem = mm_object_get_modem(memb); if (modem == NULL) return; mm_modem_peek_ports(modem, &ports, &n_ports); for (i = 0; i < n_ports; i++) { if (ports[i].type == MM_MODEM_PORT_TYPE_QMI && !strcmp(ports[i].name, "qrtr0")) { is_qrtr = TRUE; break; } } if (is_qrtr) *out = g_object_ref(modem); } static guint qvd_bearer_config_hash(gconstpointer key) { const struct qvd_bearer_config *conf = key; return g_int_hash(&conf->ip_type) ^ g_str_hash(conf->apn); } static gboolean qvd_bearer_config_equal(gconstpointer va, gconstpointer vb) { const struct qvd_bearer_config *a = va; const struct qvd_bearer_config *b = vb; return g_int_equal(&a->ip_type, &b->ip_type) && g_str_equal(a->apn, b->apn); } static gboolean qvd_modem_initable_init(GInitable *initable, GCancellable *cancellable, GError **out_err) { QVDModem *ctx = QVD_MODEM(initable); GError *err = NULL; GList *objs; ctx->dbus = g_bus_get_sync(G_BUS_TYPE_SYSTEM, cancellable, &err); if (err != NULL) { g_propagate_error(out_err, err); return FALSE; } ctx->manager = mm_manager_new_sync(ctx->dbus, G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_DO_NOT_AUTO_START, NULL, &err); if (err != NULL) { g_propagate_error(out_err, err); return FALSE; } objs = g_dbus_object_manager_get_objects(G_DBUS_OBJECT_MANAGER(ctx->manager)); g_list_foreach(objs, find_modem, &ctx->modem); g_list_free_full(objs, g_object_unref); if (ctx->modem == NULL) g_warning("ModemManager QRTR modem not detected on startup\n"); g_signal_connect(ctx->manager, "object-added", G_CALLBACK(on_object_added), ctx); ctx->bearers = g_hash_table_new_full(qvd_bearer_config_hash, qvd_bearer_config_equal, g_free, g_object_unref); return TRUE; } static void qvd_modem_dispose(GObject *obj) { QVDModem *ctx = QVD_MODEM(obj); if (ctx->bearers != NULL) g_hash_table_destroy(g_steal_pointer(&ctx->bearers)); g_clear_object(&ctx->manager); g_clear_object(&ctx->dbus); } QVDModem *qvd_modem_new(GError **err) { return g_initable_new(QVD_TYPE_MODEM, NULL, err, NULL); } static void on_address_changed(QVDConnection *conn, MMBearerIpFamily ip_type, const gchar *addr, gpointer data) { QVDModem *ctx = QVD_MODEM(data); g_signal_emit(ctx, modem_signals[ADDR_CHANGED], 0, conn, ip_type, addr); } static void on_connect_error(QVDConnection *conn, const GError *err, gpointer data) { QVDModem *ctx = QVD_MODEM(data); g_signal_emit(ctx, modem_signals[CONNECT_ERR], 0, conn, err); } static void on_disconnect_error(QVDConnection *conn, const GError *err, gpointer data) { QVDModem *ctx = QVD_MODEM(data); g_signal_emit(ctx, modem_signals[DISCONNECT_ERR], 0, conn, err); } static void create_bearer_ready(GObject *obj, GAsyncResult *res, gpointer data) { QVDConnection *conn = QVD_CONNECTION(data); GError *err = NULL; MMBearer *bearer; bearer = mm_modem_create_bearer_finish(MM_MODEM(obj), res, &err); if (err != NULL) { g_printerr("Failed to create bearer: %s\n", err->message); g_clear_error(&err); return; } qvd_connection_set_bearer(conn, bearer); } QVDConnection *qvd_modem_get_connection(QVDModem *ctx, const gchar *apn, MMBearerIpFamily ip_family) { struct qvd_bearer_config *conf; QVDConnection *conn; MMBearerProperties *props; g_assert(ctx != NULL); if (ctx->modem == NULL) return NULL; conf = g_malloc(sizeof(struct qvd_bearer_config)); conf->ip_type = ip_family; strncpy(conf->apn, apn, 255); conf->apn[255] = '\0'; if (!g_hash_table_contains(ctx->bearers, conf)) { conn = qvd_connection_new(ip_family); g_signal_connect(conn, "qvd-address-changed", G_CALLBACK(on_address_changed), ctx); g_signal_connect(conn, "qvd-connect-error", G_CALLBACK(on_connect_error), ctx); g_signal_connect(conn, "qvd-disconnect-error", G_CALLBACK(on_disconnect_error), ctx); g_hash_table_insert(ctx->bearers, conf, conn); props = mm_bearer_properties_new(); mm_bearer_properties_set_apn(props, apn); mm_bearer_properties_set_ip_type(props, ip_family); mm_bearer_properties_set_allow_roaming(props, FALSE); mm_modem_create_bearer(ctx->modem, props, NULL, create_bearer_ready, conn); g_object_unref(props); } else { conn = g_hash_table_lookup(ctx->bearers, conf); g_free(conf); } return g_object_ref(conn); } 81voltd-v1.0.0/qvd-modem.h000066400000000000000000000010661462350102300152400ustar00rootroot00000000000000/* SPDX-License-Identifier: GPL-2.0-or-later */ /* * Copyright (c) 2024, Richard Acayan. All rights reserved. */ #ifndef QVD_MODEM_H #define QVD_MODEM_H #include #include #include #include "qvd-connection.h" G_BEGIN_DECLS #define QVD_TYPE_MODEM qvd_modem_get_type() G_DECLARE_FINAL_TYPE(QVDModem, qvd_modem, QVD, MODEM, GObject) QVDModem *qvd_modem_new(GError **err); QVDConnection *qvd_modem_get_connection(QVDModem *ctx, const gchar *apn, MMBearerIpFamily ip_family); G_END_DECLS #endif /* QVD_MODEM_H */ 81voltd-v1.0.0/qvd-server.c000066400000000000000000000265731462350102300154520ustar00rootroot00000000000000// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) 2024, Richard Acayan. All rights reserved. */ #include #include #include #include #include #include #include #include "qmi_imsd.h" #include "qvd-modem.h" #include "qvd-server.h" #define IMSD_START_CONNECTION 0x20 #define IMSD_STOP_CONNECTION 0x21 #define IMSD_CONNECTION_CHANGED 0x20 enum imsd_address_family { IMSD_AF_IPV4 = 0, IMSD_AF_IPV6 = 1, }; enum imsd_profiles { IMSD_PROFILES_3GPP2 = 0, IMSD_PROFILES_3GPP = 1, IMSD_PROFILES_3GPP_3GPP2 = 2, }; struct _QVDServer { GObject parent_instance; GSocket *sock; GSource *source; GHashTable *clients; GPtrArray *conns; QVDModem *modem; gulong modem_sig; struct qrtr_packet inpkt; struct qrtr_packet outpkt; char *inbuf; char *outbuf; guint16 ind_txn; }; static void qvd_server_initable_iface_init(GInitableIface *iface); static gboolean qvd_server_initable_init(GInitable *initable, GCancellable *cancellable, GError **out_err); static void qvd_server_dispose(GObject *obj); static gboolean receive_message(GSocket *sock, GIOCondition cond, gpointer data); static void connection_address_changed(QVDModem *modem, QVDConnection *conn, MMBearerIpFamily ip_type, const gchar *addr, gpointer data); G_DEFINE_TYPE_WITH_CODE(QVDServer, qvd_server, G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE(G_TYPE_INITABLE, qvd_server_initable_iface_init)) static void qvd_server_class_init(QVDServerClass *c) { GObjectClass *obj_class = G_OBJECT_CLASS(c); obj_class->dispose = qvd_server_dispose; } static void qvd_server_initable_iface_init(GInitableIface *iface) { iface->init = qvd_server_initable_init; } static void qvd_server_init(QVDServer *self) { } static gboolean qvd_server_initable_init(GInitable *initable, GCancellable *cancellable, GError **out_err) { QVDServer *server = QVD_SERVER(initable); GError *err = NULL; int fd, ret; server->ind_txn = 1; server->inbuf = g_malloc(65536); server->outbuf = g_malloc(65536); fd = qrtr_open(0); if (fd == -1) { g_set_error(out_err, G_FILE_ERROR, G_FILE_ERROR_FAILED, "QRTR open failed"); return FALSE; } server->sock = g_socket_new_from_fd(fd, &err); if (server->sock == NULL) { g_propagate_error(out_err, err); qrtr_close(fd); return FALSE; } server->source = g_socket_create_source(server->sock, G_IO_IN, cancellable); g_source_set_callback(server->source, G_SOURCE_FUNC(receive_message), server, NULL); g_source_attach(server->source, NULL); server->conns = g_ptr_array_new_full(0, g_object_unref); server->clients = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, NULL); ret = qrtr_new_server(fd, 770, 1, 0); if (ret) { // NOPUSH g_set_error(out_err, G_FILE_ERROR, G_FILE_ERROR_FAILED, "QRTR server publish failed"); return FALSE; } return TRUE; } static void qvd_server_dispose(GObject *obj) { QVDServer *server = QVD_SERVER(obj); GSource *src; if (server->clients != NULL) g_hash_table_destroy(g_steal_pointer(&server->clients)); if (server->conns != NULL) g_ptr_array_unref(g_steal_pointer(&server->conns)); if (server->source != NULL) { src = g_steal_pointer(&server->source); g_source_destroy(src); g_source_unref(src); } if (server->inbuf != NULL) g_free(g_steal_pointer(&server->inbuf)); if (server->outbuf != NULL) g_free(g_steal_pointer(&server->outbuf)); // Close the socket and decrease the modem reference counter. g_clear_object(&server->sock); g_clear_object(&server->modem); } QVDServer *qvd_server_new(QVDModem *modem, GError **out_err) { QVDServer *server; GError *err = NULL; g_assert(modem != NULL); server = g_initable_new(QVD_TYPE_SERVER, NULL, &err, NULL); if (err != NULL) { g_propagate_error(out_err, err); return NULL; } server->modem = g_object_ref(modem); server->modem_sig = g_signal_connect(server->modem, "qvd-connection-address-changed", G_CALLBACK(connection_address_changed), server); return server; } static int encode_error(unsigned int msg_id, unsigned int txn, guint16 code, struct qrtr_packet *outpkt) { struct imsd_no_op_resp resp; resp.res.err_status = 1; resp.res.err_code = code; return qmi_encode_message(outpkt, QMI_RESPONSE, msg_id, txn, &resp, imsd_no_op_resp_ei); } static int process_no_op(unsigned int msg_id, const struct qrtr_packet *inpkt, struct qrtr_packet *outpkt) { struct imsd_no_op_req req; struct imsd_no_op_resp resp = { .res = { .err_status = 0, .err_code = 0, }, }; unsigned int txn; int ret; ret = qmi_decode_message(&req, &txn, inpkt, QMI_REQUEST, msg_id, imsd_no_op_req_ei); if (ret < 0) return ret; return qmi_encode_message(outpkt, QMI_RESPONSE, msg_id, txn, &resp, imsd_no_op_resp_ei); } static void register_connection(QVDServer *server, QVDConnection *conn, guint32 connection, guint32 subscription) { guint i; for (i = 0; i < server->conns->len; i++) { if (server->conns->pdata[i] == NULL) { server->conns->pdata[i] = conn; break; } } if (i == server->conns->len) g_ptr_array_add(server->conns, conn); qvd_connection_set_handles(conn, connection, subscription, i); } static ssize_t process_start_connection(QVDServer *server, const struct qrtr_packet *inpkt, struct qrtr_packet *outpkt) { MMBearerIpFamily ip_family; QVDConnection *conn; struct imsd_start_connection_req req; struct imsd_start_connection_resp resp; unsigned int txn; guint8 local_id; gint *client; int ret; ret = qmi_decode_message(&req, &txn, inpkt, QMI_REQUEST, IMSD_START_CONNECTION, imsd_start_connection_req_ei); if (ret < 0) { g_printerr("IMSD Start Connection: failed to decode request: %s\n", strerror(-errno)); return encode_error(IMSD_START_CONNECTION, txn, 1, outpkt); } if (req.conn_params.ip_family == IMSD_AF_IPV4) { ip_family = MM_BEARER_IP_FAMILY_IPV4; } else if (req.conn_params.ip_family == IMSD_AF_IPV6) { ip_family = MM_BEARER_IP_FAMILY_IPV6; } else { g_printerr("IMSD Start Connection: unknown IP family: %u\n", req.conn_params.ip_family); return encode_error(IMSD_START_CONNECTION, txn, 1, outpkt); } // Register the client for indications. client = g_malloc(sizeof(gint)); *client = (inpkt->node << 16) | inpkt->port; g_hash_table_add(server->clients, client); conn = qvd_modem_get_connection(server->modem, req.conn_params.apn, ip_family); if (conn == NULL) { g_printerr("IMSD Start Connection: modem not present\n"); return encode_error(IMSD_START_CONNECTION, txn, 1, outpkt); } qvd_connection_connect(conn); register_connection(server, conn, req.connection, req.subscription); qvd_connection_get_handles(conn, NULL, NULL, &local_id); resp.res.err_status = 0; resp.res.err_code = 0; resp.connection = local_id; resp.orig_conn_id = req.connection; resp.subscription = req.subscription; return qmi_encode_message(outpkt, QMI_RESPONSE, IMSD_START_CONNECTION, txn, &resp, imsd_start_connection_resp_ei); } static ssize_t process_stop_connection(QVDServer *server, const struct qrtr_packet *inpkt, struct qrtr_packet *outpkt) { QVDConnection *conn; struct imsd_stop_connection_req req; struct imsd_stop_connection_resp resp; unsigned int txn; gint *client; int ret; ret = qmi_decode_message(&req, &txn, inpkt, QMI_REQUEST, IMSD_STOP_CONNECTION, imsd_stop_connection_req_ei); if (ret < 0) { g_printerr("IMSD Stop Connection: failed to decode request: %s\n", strerror(-errno)); return encode_error(IMSD_STOP_CONNECTION, txn, 1, outpkt); } if (req.connection >= server->conns->len || server->conns->pdata[req.connection] == NULL) { g_printerr("IMSD Stop Connection: unknown connection ID %u\n", req.connection); return encode_error(IMSD_STOP_CONNECTION, txn, 9, outpkt); } // Register the client for indications. client = g_malloc(sizeof(gint)); *client = (inpkt->node << 16) | inpkt->port; g_hash_table_add(server->clients, client); conn = g_steal_pointer(&server->conns->pdata[req.connection]); qvd_connection_disconnect(conn); g_object_unref(conn); resp.res.err_status = 0; resp.res.err_code = 0; resp.unkfield_FA = 0xFA; resp.unkecho_01 = req.unkecho_01; return qmi_encode_message(outpkt, QMI_RESPONSE, IMSD_STOP_CONNECTION, txn, &resp, imsd_stop_connection_resp_ei); } static int process_message(QVDServer *server, const struct qrtr_packet *inpkt, struct qrtr_packet *outpkt) { unsigned int msg_id; ssize_t ret; qmi_decode_header(inpkt, &msg_id); if (msg_id == IMSD_START_CONNECTION) ret = process_start_connection(server, inpkt, outpkt); else if (msg_id == IMSD_STOP_CONNECTION) ret = process_stop_connection(server, inpkt, outpkt); else ret = process_no_op(msg_id, inpkt, outpkt); if (ret < 0) return ret; return 0; } static gboolean receive_message(GSocket *sock, GIOCondition cond, gpointer data) { QVDServer *server = QVD_SERVER(data); struct sockaddr_qrtr peer = { .sq_family = AF_QIPCRTR, }; gint *client; int fd, ret, inlen; fd = g_socket_get_fd(sock); inlen = qrtr_recvfrom(fd, server->inbuf, 65536, &peer.sq_node, &peer.sq_port); if (inlen < 0) return TRUE; qrtr_decode(&server->inpkt, server->inbuf, inlen, &peer); if (server->inpkt.type == QRTR_TYPE_DATA) { server->outpkt.data = server->outbuf; server->outpkt.data_len = 65536; ret = process_message(server, &server->inpkt, &server->outpkt); if (ret) goto free_addr; // The MSG_NOSIGNAL flag is not supported by the QRTR kernel driver. qrtr_sendto(fd, peer.sq_node, peer.sq_port, server->outpkt.data, server->outpkt.data_len); } else if (server->inpkt.type == QRTR_TYPE_DEL_CLIENT) { client = g_malloc(sizeof(gint)); *client = (server->inpkt.node << 16) | server->inpkt.port; g_hash_table_remove(server->clients, client); g_free(client); } free_addr: return TRUE; } static void send_staged_message(gpointer k, gpointer v, gpointer data) { QVDServer *server = QVD_SERVER(data); guint *addr = k; uint16_t node = *addr >> 16; uint16_t port = *addr & 0xFFFF; int fd; // The MSG_NOSIGNAL flag is not supported by the QRTR kernel driver. fd = g_socket_get_fd(server->sock); qrtr_sendto(fd, node, port, server->outpkt.data, server->outpkt.data_len); } static void connection_address_changed(QVDModem *modem, QVDConnection *conn, MMBearerIpFamily ip_type, const gchar *addr, gpointer data) { QVDServer *server = QVD_SERVER(data); struct imsd_connection_changed_ind ind; guint32 conn_id, sub_id; guint8 local_id; g_assert(ip_type == MM_BEARER_IP_FAMILY_IPV4 || ip_type == MM_BEARER_IP_FAMILY_IPV6); qvd_connection_get_handles(conn, &conn_id, &sub_id, &local_id); server->outpkt.data = server->outbuf; server->outpkt.data_len = 65536; ind.res.err_status = 0; ind.connection = local_id; ind.orig_conn_id = conn_id; ind.subscription = sub_id; if (addr != NULL) { ind.res.err_code = 0; ind.ip_addr_valid = true; strncpy(ind.ip_addr.addr, addr, 255); ind.ip_addr.addr[255] = '\0'; if (ip_type == MM_BEARER_IP_FAMILY_IPV4) ind.ip_addr.family = IMSD_AF_IPV4; else if (ip_type == MM_BEARER_IP_FAMILY_IPV6) ind.ip_addr.family = IMSD_AF_IPV6; } else { ind.res.err_code = 13; ind.ip_addr_valid = false; } qmi_encode_message(&server->outpkt, QMI_INDICATION, IMSD_CONNECTION_CHANGED, server->ind_txn, &ind, imsd_connection_changed_ind_ei); server->ind_txn++; g_hash_table_foreach(server->clients, send_staged_message, server); } 81voltd-v1.0.0/qvd-server.h000066400000000000000000000010071462350102300154400ustar00rootroot00000000000000/* SPDX-License-Identifier: GPL-2.0-or-later */ /* * Copyright (c) 2024, Richard Acayan. All rights reserved. */ #ifndef QVD_SERVER_H #define QVD_SERVER_H #include #include #include "qvd-modem.h" G_BEGIN_DECLS #define QVD_TYPE_SERVER qvd_server_get_type() G_DECLARE_FINAL_TYPE(QVDServer, qvd_server, QVD, SERVER, GObject) QVDServer *qvd_server_new(QVDModem *modem, GError **err); void qvd_server_set_modem(QVDServer *server, QVDModem *modem); G_END_DECLS #endif /* QVD_SERVER_H */