From 64006e638e2f1151d400490ca99ea6c96ddd0de1 Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Thu, 9 Jun 2022 17:12:42 +0900 Subject: [PATCH 01/16] ini-parser: check closing bracket when parsing section name Change-Id: Ic939bfd6f57c9ee20e41fe7adf34f0debe3539e6 Signed-off-by: Youngjae Cho --- src/libcommon/ini-parser.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/libcommon/ini-parser.c b/src/libcommon/ini-parser.c index f08b28a..0f145c6 100644 --- a/src/libcommon/ini-parser.c +++ b/src/libcommon/ini-parser.c @@ -166,6 +166,7 @@ int libsys_config_parse_by_section(const char *fname, int cb(const struct parse_ char tmp_sectname[128]; char tmp_key[128]; char tmp_value[128]; + char closing_bracket; if (!fname || !cb) return -EINVAL; @@ -178,16 +179,16 @@ int libsys_config_parse_by_section(const char *fname, int cb(const struct parse_ while (getline(&line, &len, fp) != EOF) { // find section - retval = sscanf_trim_whitespace_comment(line, "[%127[^]]]", tmp_sectname); - if (retval != 1) + retval = sscanf_trim_whitespace_comment(line, "[%127[^]]%c", tmp_sectname, &closing_bracket); + if (retval != 2 || closing_bracket != ']') continue; result.section = strndup(tmp_sectname, sizeof(tmp_sectname)); // parse properties within the section while((n_read = getline(&line, &len, fp)) != EOF) { - retval = sscanf_trim_whitespace_comment(line, "[%127[^]]]", tmp_sectname); - if (retval == 1) { // found next section. stop parsing properties + retval = sscanf_trim_whitespace_comment(line, "[%127[^]]%c", tmp_sectname, &closing_bracket); + if (retval == 2 && closing_bracket == ']') { // found next section. stop parsing properties fseek(fp, -n_read, SEEK_CUR); break; } -- 2.7.4 From 34ba9820f76a2af5e98aeb7f59a6ad48ee98e849 Mon Sep 17 00:00:00 2001 From: Hyotaek Shim Date: Thu, 18 Aug 2022 16:40:18 +0900 Subject: [PATCH 02/16] Relicense libsyscommon package as MIT to prevent license conflict issues For example, Apache-2.0 is in conflict with GPL-2.0. Change-Id: I52aff6d3d7083fa20939f1fd356c299ddc69e0cf Signed-off-by: Hyotaek Shim --- LICENSE.Apache-2.0 | 202 --------------------------------------- LICENSE.MIT | 9 ++ packaging/libsyscommon.spec | 8 +- src/libcommon/common.c | 30 +++--- src/libcommon/common.h | 30 +++--- src/libcommon/file.c | 30 +++--- src/libcommon/file.h | 37 +++---- src/libcommon/ini-parser.c | 30 +++--- src/libcommon/ini-parser.h | 31 +++--- src/libcommon/list.h | 30 +++--- src/libgdbus/dbus-iface-system.h | 30 +++--- src/libgdbus/libgdbus.c | 30 +++--- src/libgdbus/libgdbus.h | 30 +++--- src/libsystemd/libsystemd.c | 30 +++--- src/libsystemd/libsystemd.h | 31 +++--- src/shared/log-macro.h | 31 +++--- src/shared/log.h | 31 +++--- 17 files changed, 258 insertions(+), 392 deletions(-) delete mode 100644 LICENSE.Apache-2.0 create mode 100644 LICENSE.MIT diff --git a/LICENSE.Apache-2.0 b/LICENSE.Apache-2.0 deleted file mode 100644 index d645695..0000000 --- a/LICENSE.Apache-2.0 +++ /dev/null @@ -1,202 +0,0 @@ - - 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. diff --git a/LICENSE.MIT b/LICENSE.MIT new file mode 100644 index 0000000..80b056d --- /dev/null +++ b/LICENSE.MIT @@ -0,0 +1,9 @@ +The MIT License + +Copyright (c) 2022 Samsung Electronics Co., Ltd. + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/packaging/libsyscommon.spec b/packaging/libsyscommon.spec index 7be112d..d18dac8 100644 --- a/packaging/libsyscommon.spec +++ b/packaging/libsyscommon.spec @@ -2,7 +2,7 @@ Name: libsyscommon Summary: System Libraries Version: 4.1 Release: 0%{?release_flags} -License: Apache-2.0 +License: MIT Group: System/Libraries Source: %{name}-%{version}.tar.gz Source1001: %{name}.manifest @@ -24,7 +24,7 @@ System common utility libraries. %package -n libsyscommon-devel Summary: Header files for system common library -License: Apache-2.0 +License: MIT Requires: libsyscommon = %{version} Requires: pkgconfig(gio-2.0) Requires: pkgconfig(gio-unix-2.0) @@ -57,13 +57,13 @@ touch debugsources.list %files %defattr(-,root,root,-) %manifest %{name}.manifest -%license LICENSE.Apache-2.0 +%license LICENSE.MIT %{_libdir}/libsyscommon.so.* %files -n libsyscommon-devel %defattr(-,root,root,-) %manifest %{name}.manifest -%license LICENSE.Apache-2.0 +%license LICENSE.MIT %{_libdir}/libsyscommon.so %{_includedir}/libsyscommon/*.h %{_libdir}/pkgconfig/libsyscommon.pc diff --git a/src/libcommon/common.c b/src/libcommon/common.c index 8f9e973..50cdee6 100644 --- a/src/libcommon/common.c +++ b/src/libcommon/common.c @@ -1,18 +1,24 @@ -/* - * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved +/* MIT License * - * 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 + * Copyright (c) 2022 Samsung Electronics Co., Ltd. * - * http://www.apache.org/licenses/LICENSE-2.0 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: * - * 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. - */ + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. */ #include #include diff --git a/src/libcommon/common.h b/src/libcommon/common.h index 057bad4..bbeee46 100644 --- a/src/libcommon/common.h +++ b/src/libcommon/common.h @@ -1,18 +1,24 @@ -/* - * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved +/* MIT License * - * 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 + * Copyright (c) 2022 Samsung Electronics Co., Ltd. * - * http://www.apache.org/licenses/LICENSE-2.0 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: * - * 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. - */ + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. */ #ifndef __LIBCOMMON_COMMON_H__ #define __LIBCOMMON_COMMON_H__ diff --git a/src/libcommon/file.c b/src/libcommon/file.c index 2b0c6ba..e0a3b29 100644 --- a/src/libcommon/file.c +++ b/src/libcommon/file.c @@ -1,18 +1,24 @@ -/* - * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved +/* MIT License * - * 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 + * Copyright (c) 2022 Samsung Electronics Co., Ltd. * - * http://www.apache.org/licenses/LICENSE-2.0 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: * - * 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. - */ + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. */ #include #include diff --git a/src/libcommon/file.h b/src/libcommon/file.h index 7154494..84fca0c 100644 --- a/src/libcommon/file.h +++ b/src/libcommon/file.h @@ -1,19 +1,24 @@ -/* - * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved - * - * 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. - */ - +/* MIT License + * + * Copyright (c) 2022 Samsung Electronics Co., Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. */ #ifndef __LIBCOMMON_FILE_H__ #define __LIBCOMMON_FILE_H__ diff --git a/src/libcommon/ini-parser.c b/src/libcommon/ini-parser.c index 0f145c6..e7d7605 100644 --- a/src/libcommon/ini-parser.c +++ b/src/libcommon/ini-parser.c @@ -1,20 +1,24 @@ -/* - * libsyscommon +/* MIT License * - * Copyright (c) 2021 Samsung Electronics Co., Ltd. + * Copyright (c) 2022 Samsung Electronics Co., Ltd. * - * 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 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: * - * http://www.apache.org/licenses/LICENSE-2.0 + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. * - * 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. - */ + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. */ #include #include diff --git a/src/libcommon/ini-parser.h b/src/libcommon/ini-parser.h index 7af9f42..dccd1ac 100644 --- a/src/libcommon/ini-parser.h +++ b/src/libcommon/ini-parser.h @@ -1,21 +1,24 @@ -/* - * libsyscommon +/* MIT License * - * Copyright (c) 2021 Samsung Electronics Co., Ltd. + * Copyright (c) 2022 Samsung Electronics Co., Ltd. * - * 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 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: * - * http://www.apache.org/licenses/LICENSE-2.0 + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. * - * 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. - */ - + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. */ #ifndef __INI_PARSER_H__ #define __INI_PARSER_H__ diff --git a/src/libcommon/list.h b/src/libcommon/list.h index ab27843..aa9e947 100644 --- a/src/libcommon/list.h +++ b/src/libcommon/list.h @@ -1,20 +1,24 @@ -/* - * libsyscommon +/* MIT License * - * Copyright (c) 2021 Samsung Electronics Co., Ltd. All rights reserved. + * Copyright (c) 2022 Samsung Electronics Co., Ltd. * - * 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 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: * - * http://www.apache.org/licenses/LICENSE-2.0 + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. * - * 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. - */ + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. */ #ifndef __LIBCOMMON_LIST_H__ #define __LIBCOMMON_LIST_H__ diff --git a/src/libgdbus/dbus-iface-system.h b/src/libgdbus/dbus-iface-system.h index a5b838e..a4f69da 100644 --- a/src/libgdbus/dbus-iface-system.h +++ b/src/libgdbus/dbus-iface-system.h @@ -1,20 +1,24 @@ -/* - * libsyscommon +/* MIT License * - * Copyright (c) 2019 Samsung Electronics Co., Ltd. + * Copyright (c) 2022 Samsung Electronics Co., Ltd. * - * 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 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: * - * http://www.apache.org/licenses/LICENSE-2.0 + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. * - * 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. - */ + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. */ #ifndef __DBUS_SYSTEM_IFACE_H__ #define __DBUS_SYSTEM_IFACE_H__ diff --git a/src/libgdbus/libgdbus.c b/src/libgdbus/libgdbus.c index 917b98f..32c15ed 100644 --- a/src/libgdbus/libgdbus.c +++ b/src/libgdbus/libgdbus.c @@ -1,20 +1,24 @@ -/* - * libsyscommon +/* MIT License * - * Copyright (c) 2019 Samsung Electronics Co., Ltd. + * Copyright (c) 2022 Samsung Electronics Co., Ltd. * - * 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 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: * - * http://www.apache.org/licenses/LICENSE-2.0 + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. * - * 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. - */ + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. */ #include #include diff --git a/src/libgdbus/libgdbus.h b/src/libgdbus/libgdbus.h index 6828b76..f909d76 100644 --- a/src/libgdbus/libgdbus.h +++ b/src/libgdbus/libgdbus.h @@ -1,20 +1,24 @@ -/* - * libsyscommon +/* MIT License * - * Copyright (c) 2019 Samsung Electronics Co., Ltd. + * Copyright (c) 2022 Samsung Electronics Co., Ltd. * - * 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 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: * - * http://www.apache.org/licenses/LICENSE-2.0 + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. * - * 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. - */ + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. */ #ifndef __DBUS_SYSTEM_H__ #define __DBUS_SYSTEM_H__ diff --git a/src/libsystemd/libsystemd.c b/src/libsystemd/libsystemd.c index 8b8295c..3d2fbc2 100644 --- a/src/libsystemd/libsystemd.c +++ b/src/libsystemd/libsystemd.c @@ -1,20 +1,24 @@ -/* - * libsyscommon +/* MIT License * - * Copyright (c) 2019 Samsung Electronics Co., Ltd. + * Copyright (c) 2022 Samsung Electronics Co., Ltd. * - * 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 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: * - * http://www.apache.org/licenses/LICENSE-2.0 + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. * - * 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. - */ + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. */ #include #include diff --git a/src/libsystemd/libsystemd.h b/src/libsystemd/libsystemd.h index 1f807d3..9080fcf 100644 --- a/src/libsystemd/libsystemd.h +++ b/src/libsystemd/libsystemd.h @@ -1,21 +1,24 @@ -/* - * libsyscommon +/* MIT License * - * Copyright (c) 2019 Samsung Electronics Co., Ltd. + * Copyright (c) 2022 Samsung Electronics Co., Ltd. * - * 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 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: * - * http://www.apache.org/licenses/LICENSE-2.0 + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. * - * 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. - */ - + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. */ #ifndef __DBUS_SYSTEMD_H__ #define __DBUS_SYSTEMD_H__ diff --git a/src/shared/log-macro.h b/src/shared/log-macro.h index 8532b37..9893820 100644 --- a/src/shared/log-macro.h +++ b/src/shared/log-macro.h @@ -1,21 +1,24 @@ -/* - * deviced +/* MIT License * - * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. + * Copyright (c) 2022 Samsung Electronics Co., Ltd. * - * 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 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: * - * http://www.apache.org/licenses/LICENSE-2.0 + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. * - * 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. - */ - + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. */ #ifndef __LOG_MACRO_H__ #define __LOG_MACRO_H__ diff --git a/src/shared/log.h b/src/shared/log.h index ad914a3..09a9eb4 100644 --- a/src/shared/log.h +++ b/src/shared/log.h @@ -1,21 +1,24 @@ -/* - * deviced +/* MIT License * - * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. + * Copyright (c) 2022 Samsung Electronics Co., Ltd. * - * 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 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: * - * http://www.apache.org/licenses/LICENSE-2.0 + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. * - * 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. - */ - + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. */ #ifndef __LOG_H__ #define __LOG_H__ -- 2.7.4 From 52e7d17a5203b8b042989aa6d689d994835feb5a Mon Sep 17 00:00:00 2001 From: TaeminYeom Date: Wed, 24 Aug 2022 18:15:03 +0900 Subject: [PATCH 03/16] Add extern "C" keyword in header files Change-Id: Ie16f5ec1d1c18f5e20cd94d46ad63a3ad7ac58fc Signed-off-by: TaeminYeom --- src/libcommon/common.h | 8 ++++++++ src/libcommon/file.h | 8 ++++++++ src/libcommon/ini-parser.h | 9 +++++++++ src/libcommon/list.h | 8 ++++++++ src/libgdbus/dbus-iface-system.h | 7 +++++++ src/libgdbus/libgdbus.h | 8 +++++++- src/shared/log-macro.h | 9 +++++++++ src/shared/log.h | 8 ++++++++ 8 files changed, 64 insertions(+), 1 deletion(-) diff --git a/src/libcommon/common.h b/src/libcommon/common.h index bbeee46..8144929 100644 --- a/src/libcommon/common.h +++ b/src/libcommon/common.h @@ -25,6 +25,10 @@ #include +#ifdef __cplusplus +extern "C" { +#endif + /** * @brief Check if running on emulator * @@ -39,4 +43,8 @@ bool libsys_is_emulator(void); */ bool libsys_is_container(void); +#ifdef __cplusplus +} +#endif + #endif /* __LIBCOMMON_COMMON_H__ */ diff --git a/src/libcommon/file.h b/src/libcommon/file.h index 84fca0c..c933fc9 100644 --- a/src/libcommon/file.h +++ b/src/libcommon/file.h @@ -25,6 +25,10 @@ #include +#ifdef __cplusplus +extern "C" { +#endif + /** * @brief Open file, read and close * @@ -97,4 +101,8 @@ int sys_set_str(char *fname, char *val); */ int libsys_parse_cmdline_scanf(const char *format, ...); +#ifdef __cplusplus +} +#endif + #endif /* __LIBCOMMON_FILE_H__ */ diff --git a/src/libcommon/ini-parser.h b/src/libcommon/ini-parser.h index dccd1ac..9620103 100644 --- a/src/libcommon/ini-parser.h +++ b/src/libcommon/ini-parser.h @@ -28,6 +28,10 @@ #define MATCH(a, b) (!strncmp(a, b, strlen(a))) #define SET_CONF(a, b) (a = (b > 0.0 ? b : a)) +#ifdef __cplusplus +extern "C" { +#endif + struct parse_result { char *section; union { @@ -66,4 +70,9 @@ int config_parse(const char *file_name, int cb(struct parse_result *result, * @return 0 on success, negative if failed */ int libsys_config_parse_by_section(const char *file_name, int cb(const struct parse_result *, void *), void *user_data); + +#ifdef __cplusplus +} +#endif + #endif diff --git a/src/libcommon/list.h b/src/libcommon/list.h index aa9e947..dc646dc 100644 --- a/src/libcommon/list.h +++ b/src/libcommon/list.h @@ -25,6 +25,10 @@ #include +#ifdef __cplusplus +extern "C" { +#endif + #define SYS_G_LIST_PREPEND(a, b) \ a = g_list_prepend(a, (gpointer)b) #define SYS_G_LIST_APPEND(a, b) \ @@ -60,5 +64,9 @@ #define SYS_G_LIST_NEXT(a) \ g_list_next(a) +#ifdef __cplusplus +} +#endif + #endif diff --git a/src/libgdbus/dbus-iface-system.h b/src/libgdbus/dbus-iface-system.h index a4f69da..1a98990 100644 --- a/src/libgdbus/dbus-iface-system.h +++ b/src/libgdbus/dbus-iface-system.h @@ -23,6 +23,10 @@ #ifndef __DBUS_SYSTEM_IFACE_H__ #define __DBUS_SYSTEM_IFACE_H__ +#ifdef __cplusplus +extern "C" { +#endif + /* * Template * @@ -342,5 +346,8 @@ /* End of the Experimental for Specific device */ /***********************************************/ +#ifdef __cplusplus +} +#endif #endif diff --git a/src/libgdbus/libgdbus.h b/src/libgdbus/libgdbus.h index f909d76..659b2e4 100644 --- a/src/libgdbus/libgdbus.h +++ b/src/libgdbus/libgdbus.h @@ -32,6 +32,10 @@ #include "dbus-iface-system.h" +#ifdef __cplusplus +extern "C" { +#endif + typedef struct { const unsigned char *data; int size; @@ -274,6 +278,8 @@ GVariant *gdbus_make_simple_array (const char *sig, int check_systemd_active (void); - +#ifdef __cplusplus +} +#endif #endif diff --git a/src/shared/log-macro.h b/src/shared/log-macro.h index 9893820..882bee1 100644 --- a/src/shared/log-macro.h +++ b/src/shared/log-macro.h @@ -23,6 +23,10 @@ #ifndef __LOG_MACRO_H__ #define __LOG_MACRO_H__ +#ifdef __cplusplus +extern "C" { +#endif + #ifdef ENABLE_DLOG #include #define _D(fmt, arg...) \ @@ -51,4 +55,9 @@ #define _SW(...) do { } while (0) #define _SE(...) do { } while (0) #endif + +#ifdef __cplusplus +} #endif + +#endif /* __LOG_MACRO_H__ */ diff --git a/src/shared/log.h b/src/shared/log.h index 09a9eb4..7f5b6bb 100644 --- a/src/shared/log.h +++ b/src/shared/log.h @@ -23,6 +23,10 @@ #ifndef __LOG_H__ #define __LOG_H__ +#ifdef __cplusplus +extern "C" { +#endif + #ifdef ENABLE_LIBDEVICED_DLOG #define ENABLE_DLOG #endif @@ -30,4 +34,8 @@ #define LOG_TAG "LIBSYSCOMMON" #include "shared/log-macro.h" +#ifdef __cplusplus +} #endif + +#endif /* __LOG_H__ */ -- 2.7.4 From fde567b36a3cfcc53b3eecfc79bcdadb318d5eb0 Mon Sep 17 00:00:00 2001 From: Dongwoo Lee Date: Thu, 20 Oct 2022 16:27:30 +0900 Subject: [PATCH 04/16] resource-manager: add system resource manager library To provide system-wide unified resource framework, resource utilities in resource monitor is extracted into this library. Change-Id: I7ba90e3b1fda1b525a015bb46addd4d860045d6d Signed-off-by: Dongwoo Lee Signed-off-by: Youngjae Cho --- CMakeLists.txt | 6 + packaging/libsyscommon.spec | 2 + src/resource-manager/resource-device.c | 80 +++ src/resource-manager/resource-device.h | 38 ++ src/resource-manager/resource-manager.c | 1050 +++++++++++++++++++++++++++++++ src/resource-manager/resource-manager.h | 193 ++++++ src/resource-manager/resource-type.h | 55 ++ 7 files changed, 1424 insertions(+) create mode 100644 src/resource-manager/resource-device.c create mode 100644 src/resource-manager/resource-device.h create mode 100644 src/resource-manager/resource-manager.c create mode 100644 src/resource-manager/resource-manager.h create mode 100644 src/resource-manager/resource-type.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 2df65ef..482712d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,8 @@ SET(libsyscommon_SRCS src/libcommon/ini-parser.c src/libcommon/file.c src/libcommon/common.c + src/resource-manager/resource-manager.c + src/resource-manager/resource-device.c ) SET(HEADERS src/libgdbus/libgdbus.h @@ -26,6 +28,9 @@ SET(HEADERS src/libcommon/ini-parser.h src/libcommon/file.h src/libcommon/common.h + src/resource-manager/resource-manager.h + src/resource-manager/resource-type.h + src/resource-manager/resource-device.h ) # CHECK PKG @@ -35,6 +40,7 @@ pkg_check_modules(syscommon REQUIRED gio-2.0 gio-unix-2.0 dlog + json-c capi-system-info) FOREACH(flag ${syscommon_CFLAGS}) diff --git a/packaging/libsyscommon.spec b/packaging/libsyscommon.spec index d18dac8..c46dab1 100644 --- a/packaging/libsyscommon.spec +++ b/packaging/libsyscommon.spec @@ -13,6 +13,7 @@ BuildRequires: pkgconfig(glib-2.0) >= 2.44 BuildRequires: pkgconfig(gio-2.0) >= 2.44 BuildRequires: pkgconfig(gio-unix-2.0) BuildRequires: pkgconfig(dlog) +BuildRequires: pkgconfig(json-c) BuildRequires: pkgconfig(capi-system-info) Requires: /bin/cp @@ -29,6 +30,7 @@ Requires: libsyscommon = %{version} Requires: pkgconfig(gio-2.0) Requires: pkgconfig(gio-unix-2.0) Requires: pkgconfig(dlog) +Requires: pkgconfig(json-c) Requires: pkgconfig(capi-system-info) %description -n libsyscommon-devel diff --git a/src/resource-manager/resource-device.c b/src/resource-manager/resource-device.c new file mode 100644 index 0000000..fccc969 --- /dev/null +++ b/src/resource-manager/resource-device.c @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2022 Samsung Electronics Co., Ltd. + * + * 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 + +#include "resource-device.h" + +static GList *g_resource_device_head; + +int syscommon_resman_get_resource_device_count(int resource_type) +{ + GList *node; + struct syscommon_resman_resource_device *device; + int count = 0; + + for (node = g_resource_device_head; node != NULL; node = node->next) { + device = node->data; + if (device->type == resource_type) + count++; + } + + return count; +} + +const struct syscommon_resman_resource_device * +syscommon_resman_find_resource_device(int resource_type, int resource_index) +{ + GList *node; + const struct syscommon_resman_resource_device *device; + + for (node = g_resource_device_head; node != NULL; node = node->next) { + device = node->data; + if (device->type == resource_type && + device->index == resource_index) + return device; + } + + return NULL; +} + +int syscommon_resman_add_resource_device(struct syscommon_resman_resource_device *device) +{ + int count; + + if (!device) + return -EINVAL; + + count = syscommon_resman_get_resource_device_count(device->type); + if (count < 0) + return count; + + device->index = count; + + g_resource_device_head = + g_list_append(g_resource_device_head, (gpointer)device); + + return 0; +} + +void syscommon_resman_remove_resource_device(struct syscommon_resman_resource_device *device) +{ + if (!device) + return; + + g_resource_device_head = + g_list_remove(g_resource_device_head, (gpointer)device); +} diff --git a/src/resource-manager/resource-device.h b/src/resource-manager/resource-device.h new file mode 100644 index 0000000..fd8a99e --- /dev/null +++ b/src/resource-manager/resource-device.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2022 Samsung Electronics Co., Ltd. + * + * 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. + */ + + +#ifndef __RESOURCE_DEVICE_H__ +#define __RESOURCE_DEVICE_H__ + +struct syscommon_resman_resource_device { + char *name; + int type; + + /* + * Never initialize it by user of add_resource_device(). + * It will be initialized by add_resource_device function automatically. + */ + int index; +}; + +int syscommon_resman_get_resource_device_count(int resource_type); +const struct syscommon_resman_resource_device * +syscommon_resman_find_resource_device(int resource_type, int resource_index); +int syscommon_resman_add_resource_device(struct syscommon_resman_resource_device *resource_device); +void syscommon_resman_remove_resource_device(struct syscommon_resman_resource_device *resource_device); + +#endif diff --git a/src/resource-manager/resource-manager.c b/src/resource-manager/resource-manager.c new file mode 100644 index 0000000..7faa87b --- /dev/null +++ b/src/resource-manager/resource-manager.c @@ -0,0 +1,1050 @@ +/* + * Copyright (c) 2022 Samsung Electronics Co., Ltd. + * + * 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 +#include +#include + +#include "resource-manager.h" +#include "resource-type.h" + +#define RESOURCE_ATTR_MASK (ULLONG_MAX) +#define BIT64_INDEX(id) (63 - __builtin_clzll(id)) +#define RESOURCE_ATTR_INDEX(id) BIT64_INDEX(id) +#define RESOURCE_CTRL_INDEX(id) BIT64_INDEX(id) +#define RESOURCE_FLAG_VISIBILITY_MASK (SYSCOMMON_RESMAN_RESOURCE_FLAG_PRIVATE \ + | SYSCOMMON_RESMAN_RESOURCE_FLAG_PUBLIC) +#define RESOURCE_ATTR_FLAG_VISIBILITY_MASK (SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PRIVATE \ + | SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC) + + +static GList *g_resource_driver_head; + +static gint __compare_resource_type(gconstpointer data, gconstpointer input) +{ + struct syscommon_resman_resource_driver *driver; + int type = *(int *)input; + + driver = (struct syscommon_resman_resource_driver *)data; + + if (driver && driver->type == type) + return 0; + return -1; +} + +const struct syscommon_resman_resource_driver * +syscommon_resman_find_resource_driver(int resource_type) +{ + GList *node; + + node = g_list_find_custom(g_resource_driver_head, + &resource_type, __compare_resource_type); + + if (!node) + return NULL; + return (struct syscommon_resman_resource_driver *)node->data; +} + +void +syscommon_resman_add_resource_driver(const struct syscommon_resman_resource_driver *driver) +{ + if (!driver) + return; + + g_resource_driver_head = + g_list_append(g_resource_driver_head, (gpointer)driver); +} + +void +syscommon_resman_remove_resource_driver(const struct syscommon_resman_resource_driver *driver) +{ + if (!driver) + return; + + g_resource_driver_head = + g_list_remove(g_resource_driver_head, (gpointer)driver); +} + +static void do_delete_resource(struct syscommon_resman_resource *resource) +{ + if (!resource->name) + free(resource->name); + if (!resource->attrs_value) + free(resource->attrs_value); + resource->attrs = NULL; + resource->num_attrs = 0; + + free(resource); +} + +void +syscommon_resman_delete_resource(struct syscommon_resman_resource *resource) +{ + if (!resource) + return; + + if (resource->driver && resource->driver->ops.delete) + resource->driver->ops.delete(resource); + + syscommon_resman_unset_resource_attr_interest(resource, RESOURCE_ATTR_MASK); + + do_delete_resource(resource); +} + +int +syscommon_resman_create_resource(struct syscommon_resman_resource **res, + int resource_type) +{ + const struct syscommon_resman_resource_driver *driver = NULL; + struct syscommon_resman_resource *resource = NULL; + int i, ret; + + if (!res) + return -EINVAL; + + driver = syscommon_resman_find_resource_driver(resource_type); + if (!driver) + return -EINVAL; + + resource = calloc(1, sizeof(*resource)); + if (!resource) + return -ENOMEM; + + resource->type = resource_type; + resource->name = g_strdup(driver->name); + resource->driver = driver; + resource->num_attrs = driver->num_attrs; + resource->attrs = driver->attrs; + resource->attrs_value = calloc(resource->num_attrs, + sizeof(*resource->attrs_value)); + if (!resource->attrs_value) { + do_delete_resource(resource); + return -ENOMEM; + } + + for (i = 0; i < resource->num_attrs; i++) + resource->attrs_value[i].type = driver->attrs[i].type; + + resource->ctrls = driver->ctrls; + resource->num_ctrls = driver->num_ctrls; + resource->flag = SYSCOMMON_RESMAN_RESOURCE_FLAG_PRIVATE; + + if (driver->ops.create) { + ret = driver->ops.create(resource); + if (ret < 0) { + do_delete_resource(resource); + return ret; + } + } + + *res = resource; + + return 0; +} + +int +syscommon_resman_set_resource_flag(struct syscommon_resman_resource *resource, + u_int64_t flag_mask) +{ + if (!resource) + return -EINVAL; + + resource->flag = flag_mask; + return 0; +} + +int +syscommon_resman_set_resource_control(struct syscommon_resman_resource *resource, + u_int64_t ctrl_id, const void *data) +{ + const struct syscommon_resman_resource_control *ctrl; + int ctrl_index = RESOURCE_CTRL_INDEX(ctrl_id); + int ret; + + if (!resource || ctrl_index < 0 || ctrl_index >= resource->num_ctrls) + return -EINVAL; + + ctrl = &resource->ctrls[ctrl_index]; + if (!ctrl->ops.set) + return -ENOTSUP; + + ret = ctrl->ops.set(resource, ctrl, data); + if (ret < 0) + return ret; + + return 0; +} + +const char * +syscommon_resman_get_resource_control_name(struct syscommon_resman_resource *resource, + u_int64_t ctrl_id) +{ + const struct syscommon_resman_resource_control *ctrl; + int ctrl_index = RESOURCE_CTRL_INDEX(ctrl_id); + + if (!resource || ctrl_index < 0 || ctrl_index >= resource->num_ctrls) + return NULL; + + ctrl = &resource->ctrls[ctrl_index]; + + return ctrl->name; +} + +static int +update_resource_attr(struct syscommon_resman_resource *resource, + u_int64_t attr_id) +{ + int attr_index = RESOURCE_ATTR_INDEX(attr_id); + const struct syscommon_resman_resource_attribute *attr = NULL; + struct syscommon_resman_resource_attribute_value *attr_value = NULL; + int ret; + + if (!resource || attr_index < 0 || attr_index >= resource->num_attrs) + return -EINVAL; + + attr = &resource->attrs[attr_index]; + + if (!attr->ops.get) + return -EINVAL; + + attr_value = &resource->attrs_value[attr_index]; + + ret = attr->ops.get(resource, attr, attr_value->data); + if (ret < 0) + return ret; + + return 0; +} + +int +syscommon_resman_update_resource_attrs(struct syscommon_resman_resource *resource) +{ + int i, ret; + + if (!resource || !resource->type) + return -EINVAL; + + if (resource->driver && resource->driver->ops.prepare_update) { + ret = resource->driver->ops.prepare_update(resource); + if (ret < 0) + return ret; + } + + for (i = 0; i < resource->num_attrs; i++) { + if (!(resource->attrs[i].id & resource->attr_interest)) + continue; + update_resource_attr(resource, resource->attrs[i].id); + } + + return 0; +} + +const struct syscommon_resman_resource_attribute * +syscommon_resman_get_resource_attr(struct syscommon_resman_resource *resource, + u_int64_t attr_id) +{ + int attr_index = RESOURCE_ATTR_INDEX(attr_id); + + if (!resource || attr_index < 0 || attr_index >= resource->num_attrs) + return NULL; + + return &resource->attrs[attr_index]; +} + +struct syscommon_resman_resource_attribute_value * +syscommon_resman_get_resource_attr_value(struct syscommon_resman_resource *resource, + u_int64_t attr_id) +{ + int attr_index = RESOURCE_ATTR_INDEX(attr_id); + + if (!resource || attr_index < 0 || attr_index >= resource->num_attrs) + return NULL; + + return &resource->attrs_value[attr_index]; +} + +int +syscommon_resman_is_resource_attr_supported(struct syscommon_resman_resource *resource, + u_int64_t attr_id, bool *supported) +{ + const struct syscommon_resman_resource_attribute *attr = NULL; + int attr_index = RESOURCE_ATTR_INDEX(attr_id); + int ret; + bool is_supported = false; + + if (!resource || attr_index < 0 || attr_index >= resource->num_attrs) + return -EINVAL; + + attr = &resource->attrs[attr_index]; + + if (attr->id & resource->attr_supported) { + is_supported = true; + } else if (attr->ops.is_supported) { + is_supported = attr->ops.is_supported(resource, attr); + } else if (attr->ops.get) { + /* + * Optionally, if .is_supported ops is not implemented, + * use .get ops in order to check whether the resource attribute + * is supported or not. + */ + char data[SYSCOMMON_RESMAN_BUFF_MAX] = {0, }; + + ret = attr->ops.get(resource, attr, (void *)data); + is_supported = (ret < 0) ? false : true; + } + + if (is_supported) + resource->attr_supported |= attr->id; + + *supported = is_supported; + + return 0; +} + +static bool +check_attr_validate(struct syscommon_resman_resource *resource, + u_int64_t attr_id, int type) +{ + const struct syscommon_resman_resource_attribute *attr; + + attr = syscommon_resman_get_resource_attr(resource, attr_id); + if (!attr) + return false; + + if (attr->type != type) + return false; + + if (!(attr->id & resource->attr_interest)) + return false; + + return true; +} + +static json_object * +_get_resource_attr_json(const struct syscommon_resman_resource_attribute *attr, + const struct syscommon_resman_resource_attribute_value *attr_value) +{ + json_object *jobj_attr, *jobj_attr_name, *jobj_attr_type, *jobj_attr_value, *jobj_temp; + struct syscommon_resman_array_value *array; + int i; + + switch (attr->type) { + case SYSCOMMON_RESMAN_DATA_TYPE_INT: + jobj_attr_value = json_object_new_int(*((int32_t *)attr_value->data)); + break; + case SYSCOMMON_RESMAN_DATA_TYPE_UINT: + jobj_attr_value = json_object_new_int(*((u_int32_t *)attr_value->data)); + break; + case SYSCOMMON_RESMAN_DATA_TYPE_INT64: + jobj_attr_value = json_object_new_int64(*((int64_t *)attr_value->data)); + break; + case SYSCOMMON_RESMAN_DATA_TYPE_UINT64: + jobj_attr_value = json_object_new_uint64(*((u_int64_t *)attr_value->data)); + break; + case SYSCOMMON_RESMAN_DATA_TYPE_DOUBLE: + jobj_attr_value = json_object_new_double(*((double *)attr_value->data)); + break; + case SYSCOMMON_RESMAN_DATA_TYPE_STRING: + jobj_attr_value = json_object_new_string((char *)attr_value->data); + break; + case SYSCOMMON_RESMAN_DATA_TYPE_ARRAY: + array = attr_value->data; + + jobj_attr_value = json_object_new_array(); + + switch (array->type) { + case SYSCOMMON_RESMAN_DATA_TYPE_INT: + for (i = 0; i < array->length; i++) { + int32_t *item = array->data; + + jobj_temp = json_object_new_int(item[i]); + json_object_array_add(jobj_attr_value, jobj_temp); + } + break; + case SYSCOMMON_RESMAN_DATA_TYPE_UINT: + for (i = 0; i < array->length; i++) { + u_int32_t *item = array->data; + + jobj_temp = json_object_new_int(item[i]); + json_object_array_add(jobj_attr_value, jobj_temp); + } + break; + case SYSCOMMON_RESMAN_DATA_TYPE_INT64: + for (i = 0; i < array->length; i++) { + int64_t *item = array->data; + + jobj_temp = json_object_new_int64(item[i]); + json_object_array_add(jobj_attr_value, jobj_temp); + } + break; + case SYSCOMMON_RESMAN_DATA_TYPE_UINT64: + for (i = 0; i < array->length; i++) { + u_int64_t *item = array->data; + + jobj_temp = json_object_new_uint64(item[i]); + json_object_array_add(jobj_attr_value, jobj_temp); + } + break; + case SYSCOMMON_RESMAN_DATA_TYPE_DOUBLE: + for (i = 0; i < array->length; i++) { + double *item = array->data; + + jobj_temp = json_object_new_double(item[i]); + json_object_array_add(jobj_attr_value, jobj_temp); + } + break; + case SYSCOMMON_RESMAN_DATA_TYPE_STRING: + for (i = 0; i < array->length; i++) { + char **item = array->data; + + jobj_temp = json_object_new_string(item[i]); + json_object_array_add(jobj_attr_value, jobj_temp); + } + break; + default: + json_object_put(jobj_attr_value); + jobj_attr_value = json_object_new_null(); + } + break; + default: + jobj_attr_value = json_object_new_null(); + } + jobj_attr = json_object_new_object(); + + jobj_attr_name = json_object_new_string(attr->name); + /* + * Since actually JSON format has no data type limitation itself, in many + * cases except converting JSON into json-c, type is not required. So, + * for the case of converting JSON generated here to json-c object again + * json_type is stored in each attributes. + */ + jobj_attr_type = json_object_new_int(json_object_get_type(jobj_attr_value)); + + json_object_object_add(jobj_attr, "name", jobj_attr_name); + json_object_object_add(jobj_attr, "json_type", jobj_attr_type); + json_object_object_add(jobj_attr, "value", jobj_attr_value); + + return jobj_attr; +} + +static void _put_resource_attr_json(json_object *jobj_attr) +{ + json_object *jobj_attr_value; + + json_object_object_del(jobj_attr, "name"); + json_object_object_del(jobj_attr, "json_type"); + + if (json_object_object_get_ex(jobj_attr, "value", &jobj_attr_value) && + json_object_is_type(jobj_attr_value, json_type_array)) + json_object_array_del_idx(jobj_attr_value, 0, + json_object_array_length(jobj_attr_value)); + + json_object_object_del(jobj_attr, "value"); +} + +int +syscommon_resman_get_resource_attrs_json(struct syscommon_resman_resource *resource, + char **json_string) +{ + json_object *jobj_root, *jobj_res_name, *jobj_res_type, *jobj_res_attrs, *jobj_attr; + const struct syscommon_resman_resource_attribute *attr; + const struct syscommon_resman_resource_attribute_value *attr_value; + int i; + + if (!resource || !resource->type) + return -EINVAL; + + jobj_root = json_object_new_object(); + + jobj_res_name = json_object_new_string(resource->name); + jobj_res_type = json_object_new_int(resource->type); + jobj_res_attrs = json_object_new_array(); + + for (i = 0; i < resource->num_attrs; i++) { + if (!(resource->attrs[i].id & resource->attr_interest)) + continue; + + attr = &resource->attrs[i]; + attr_value = &resource->attrs_value[i]; + + jobj_attr = _get_resource_attr_json(attr, attr_value); + + json_object_array_add(jobj_res_attrs, jobj_attr); + } + + json_object_object_add(jobj_root, "res_name", jobj_res_name); + json_object_object_add(jobj_root, "res_type", jobj_res_type); + json_object_object_add(jobj_root, "res_attrs", jobj_res_attrs); + + *json_string = strdup(json_object_to_json_string(jobj_root)); + + for (i = 0; i < json_object_array_length(jobj_res_attrs); i++) { + jobj_attr = json_object_array_get_idx(jobj_res_attrs, i); + _put_resource_attr_json(jobj_attr); + } + json_object_array_del_idx(jobj_res_attrs, 0, json_object_array_length(jobj_res_attrs)); + + json_object_object_del(jobj_root, "res_attrs"); + json_object_object_del(jobj_root, "res_type"); + json_object_object_del(jobj_root, "res_name"); + json_object_put(jobj_root); + + return 0; +} + +int +syscommon_resman_get_resource_attr_json(struct syscommon_resman_resource *resource, + u_int64_t attr_id, char **json_string) +{ + const struct syscommon_resman_resource_attribute *attr; + const struct syscommon_resman_resource_attribute_value *attr_value; + json_object *jobj_attr; + + attr = syscommon_resman_get_resource_attr(resource, attr_id); + attr_value = syscommon_resman_get_resource_attr_value(resource, attr_id); + + if (!attr || !attr_value) + return -EINVAL; + + jobj_attr = _get_resource_attr_json(attr, attr_value); + + *json_string = strdup(json_object_to_json_string(jobj_attr)); + + _put_resource_attr_json(jobj_attr); + + return 0; +} + +static json_object * +get_resource_driver_json(const struct syscommon_resman_resource_driver *driver) +{ + json_object *jobj_drv, *jobj_drv_name, *jobj_drv_type; + json_object *jobj_drv_attrs_array, *jobj_drv_attr, *jobj_drv_ctrls_array, *jobj_drv_ctrl; + json_object *jobj_drv_attr_name, *jobj_drv_attr_type, *jobj_drv_attr_id; + json_object *jobj_drv_ctrl_name, *jobj_drv_ctrl_id; + const struct syscommon_resman_resource_attribute *attr; + const struct syscommon_resman_resource_control *ctrl; + int i; + + jobj_drv = json_object_new_object(); + + jobj_drv_name = json_object_new_string(driver->name); + jobj_drv_type = json_object_new_int(driver->type); + + jobj_drv_attrs_array = json_object_new_array(); + + for (i = 0; i < driver->num_attrs; i++) { + attr = &driver->attrs[i]; + + jobj_drv_attr = json_object_new_object(); + + jobj_drv_attr_name = json_object_new_string(attr->name); + jobj_drv_attr_type = json_object_new_int(attr->type); + jobj_drv_attr_id = json_object_new_uint64(attr->id); + + json_object_object_add(jobj_drv_attr, "name", jobj_drv_attr_name); + json_object_object_add(jobj_drv_attr, "type", jobj_drv_attr_type); + json_object_object_add(jobj_drv_attr, "id", jobj_drv_attr_id); + + json_object_array_add(jobj_drv_attrs_array, jobj_drv_attr); + } + + jobj_drv_ctrls_array = json_object_new_array(); + + for (i = 0; i < driver->num_ctrls; i++) { + ctrl = &driver->ctrls[i]; + + jobj_drv_ctrl = json_object_new_object(); + + jobj_drv_ctrl_name = json_object_new_string(ctrl->name); + jobj_drv_ctrl_id = json_object_new_uint64(ctrl->id); + + json_object_object_add(jobj_drv_ctrl, "name", jobj_drv_ctrl_name); + json_object_object_add(jobj_drv_ctrl, "id", jobj_drv_ctrl_id); + + json_object_array_add(jobj_drv_ctrls_array, jobj_drv_ctrl); + } + + json_object_object_add(jobj_drv, "name", jobj_drv_name); + json_object_object_add(jobj_drv, "type", jobj_drv_type); + json_object_object_add(jobj_drv, "attrs", jobj_drv_attrs_array); + json_object_object_add(jobj_drv, "ctrls", jobj_drv_ctrls_array); + + return jobj_drv; +} + +static void put_resource_driver_json(json_object *jobj_drv) +{ + json_object *jobj_array, *jobj_obj; + int i; + + if (json_object_object_get_ex(jobj_drv, "ctrls", &jobj_array)) { + for (i = 0; i < json_object_array_length(jobj_array); i++) { + jobj_obj = json_object_array_get_idx(jobj_array, i); + + json_object_object_del(jobj_obj, "id"); + json_object_object_del(jobj_obj, "name"); + } + json_object_array_del_idx(jobj_array, 0, json_object_array_length(jobj_array)); + } + + if (json_object_object_get_ex(jobj_drv, "attrs", &jobj_array)) { + for (i = 0; i < json_object_array_length(jobj_array); i++) { + jobj_obj = json_object_array_get_idx(jobj_array, i); + + json_object_object_del(jobj_obj, "id"); + json_object_object_del(jobj_obj, "type"); + json_object_object_del(jobj_obj, "name"); + } + json_object_array_del_idx(jobj_array, 0, json_object_array_length(jobj_array)); + } + + json_object_object_del(jobj_drv, "ctrls"); + json_object_object_del(jobj_drv, "attrs"); + json_object_object_del(jobj_drv, "type"); + json_object_object_del(jobj_drv, "name"); +} + +int syscommon_resman_get_resource_list_json(char **json_string) +{ + const struct syscommon_resman_resource_driver *driver; + json_object *jobj_root, *jobj_drvs_array, *jobj_drv; + int i; + + jobj_root = json_object_new_object(); + jobj_drvs_array = json_object_new_array(); + + for (i = 0; i < g_list_length(g_resource_driver_head); i++) { + driver = g_list_nth(g_list_first(g_resource_driver_head), i)->data; + + jobj_drv = get_resource_driver_json(driver); + json_object_array_add(jobj_drvs_array, jobj_drv); + } + + json_object_object_add(jobj_root, "resources", jobj_drvs_array); + + *json_string = strdup(json_object_to_json_string(jobj_root)); + + for (i = 0; i < json_object_array_length(jobj_drvs_array); i++) { + jobj_drv = json_object_array_get_idx(jobj_drvs_array, i); + + put_resource_driver_json(jobj_drv); + } + json_object_array_del_idx(jobj_drvs_array, 0, json_object_array_length(jobj_drvs_array)); + + json_object_object_del(jobj_root, "resources"); + json_object_put(jobj_root); + + return 0; +} + +int +syscommon_resman_get_resource_attr_int(struct syscommon_resman_resource *resource, + u_int64_t attr_id, int32_t *data) +{ + struct syscommon_resman_resource_attribute_value *attr_value = NULL; + + if (!check_attr_validate(resource, attr_id, SYSCOMMON_RESMAN_DATA_TYPE_INT)) + return -EINVAL; + + attr_value = syscommon_resman_get_resource_attr_value(resource, attr_id); + if (!attr_value) + return -EINVAL; + + *data = *((int32_t *)attr_value->data); + + return 0; +} + +int +syscommon_resman_get_resource_attr_int64(struct syscommon_resman_resource *resource, + u_int64_t attr_id, int64_t *data) +{ + struct syscommon_resman_resource_attribute_value *attr_value = NULL; + + if (!check_attr_validate(resource, attr_id, SYSCOMMON_RESMAN_DATA_TYPE_INT64)) + return -EINVAL; + + attr_value = syscommon_resman_get_resource_attr_value(resource, attr_id); + if (!attr_value) + return -EINVAL; + + *data = *((int64_t *)attr_value->data); + + return 0; +} + +int +syscommon_resman_get_resource_attr_uint(struct syscommon_resman_resource *resource, + u_int64_t attr_id, u_int32_t *data) +{ + struct syscommon_resman_resource_attribute_value *attr_value = NULL; + + if (!check_attr_validate(resource, attr_id, SYSCOMMON_RESMAN_DATA_TYPE_UINT)) + return -EINVAL; + + attr_value = syscommon_resman_get_resource_attr_value(resource, attr_id); + if (!attr_value) + return -EINVAL; + + *data = *((u_int32_t *)attr_value->data); + + return 0; +} + +int +syscommon_resman_get_resource_attr_uint64(struct syscommon_resman_resource *resource, + u_int64_t attr_id, u_int64_t *data) +{ + struct syscommon_resman_resource_attribute_value *attr_value = NULL; + + if (!check_attr_validate(resource, attr_id, SYSCOMMON_RESMAN_DATA_TYPE_UINT64)) + return -EINVAL; + + attr_value = syscommon_resman_get_resource_attr_value(resource, attr_id); + if (!attr_value) + return -EINVAL; + + *data = *((u_int64_t *)attr_value->data); + + return 0; +} + +int +syscommon_resman_get_resource_attr_double(struct syscommon_resman_resource *resource, + u_int64_t attr_id, double *data) +{ + struct syscommon_resman_resource_attribute_value *attr_value = NULL; + + if (!check_attr_validate(resource, attr_id, SYSCOMMON_RESMAN_DATA_TYPE_DOUBLE)) + return -EINVAL; + + attr_value = syscommon_resman_get_resource_attr_value(resource, attr_id); + if (!attr_value) + return -EINVAL; + + *data = *((double *)attr_value->data); + + return 0; +} + +int +syscommon_resman_get_resource_attr_string(struct syscommon_resman_resource *resource, + u_int64_t attr_id, char *data) +{ + struct syscommon_resman_resource_attribute_value *attr_value = NULL; + + if (!check_attr_validate(resource, attr_id, SYSCOMMON_RESMAN_DATA_TYPE_STRING)) + return -EINVAL; + + attr_value = syscommon_resman_get_resource_attr_value(resource, attr_id); + if (!attr_value) + return -EINVAL; + + strncpy(data, (char *)attr_value->data, SYSCOMMON_RESMAN_BUFF_MAX); + + return 0; +} + +int +syscommon_resman_get_resource_attr_array(struct syscommon_resman_resource *resource, + u_int64_t attr_id, + struct syscommon_resman_array_value **data) +{ + struct syscommon_resman_resource_attribute_value *attr_value = NULL; + + if (!check_attr_validate(resource, attr_id, SYSCOMMON_RESMAN_DATA_TYPE_ARRAY)) + return -EINVAL; + + attr_value = syscommon_resman_get_resource_attr_value(resource, attr_id); + if (!attr_value) + return -EINVAL; + + *data = (struct syscommon_resman_array_value *)attr_value->data; + + return 0; +} + +int +syscommon_resman_get_resource_attr_ptr(struct syscommon_resman_resource *resource, + u_int64_t attr_id, void **data) +{ + struct syscommon_resman_resource_attribute_value *attr_value = NULL; + + if (!check_attr_validate(resource, attr_id, SYSCOMMON_RESMAN_DATA_TYPE_PTR)) + return -EINVAL; + + attr_value = syscommon_resman_get_resource_attr_value(resource, attr_id); + if (!attr_value) + return -EINVAL; + + *data = attr_value->data; + + return 0; +} + +static inline bool +is_resource_attr_visible(struct syscommon_resman_resource *resource, + const struct syscommon_resman_resource_attribute *attr) +{ + u_int64_t res_visibility, attr_visibility; + + res_visibility = resource->flag & RESOURCE_FLAG_VISIBILITY_MASK; + attr_visibility = attr->flag & RESOURCE_ATTR_FLAG_VISIBILITY_MASK; + + /* bigger visibility means smaller privilege */ + if (res_visibility > attr_visibility) + return false; + + return true; +} + +int +syscommon_resman_set_resource_attr_interest(struct syscommon_resman_resource *resource, + u_int64_t interest_mask) +{ + struct syscommon_resman_resource_attribute_value *attr_value; + int i, ret; + bool supported; + + if (!resource) + return -EINVAL; + + for (i = 0; i < resource->num_attrs; i++) { + if (!(resource->attrs[i].id & interest_mask)) + continue; + + ret = syscommon_resman_is_resource_attr_supported(resource, + resource->attrs[i].id, + &supported); + if (ret < 0) { + goto err; + } else if (!supported) { + ret = -ENOTSUP; + goto err; + } + + if (!is_resource_attr_visible(resource, &resource->attrs[i])) { + ret = -EACCES; + goto err; + } + + attr_value = syscommon_resman_get_resource_attr_value(resource, + resource->attrs[i].id); + if (!attr_value) { + ret = -EINVAL; + goto err; + } + + /* + * In resource monitor, each resource has a lot of attributes, but + * only updated attributes are selected by clients on demand. So, + * instead of allocating memory at the resource creation, allocate + * at the set interest. + */ + if (!attr_value->data) { + switch (attr_value->type) { + case SYSCOMMON_RESMAN_DATA_TYPE_INT: + attr_value->data = calloc(1, sizeof(int32_t)); + break; + case SYSCOMMON_RESMAN_DATA_TYPE_INT64: + attr_value->data = calloc(1, sizeof(int64_t)); + break; + case SYSCOMMON_RESMAN_DATA_TYPE_UINT: + attr_value->data = calloc(1, sizeof(u_int32_t)); + break; + case SYSCOMMON_RESMAN_DATA_TYPE_UINT64: + attr_value->data = calloc(1, sizeof(u_int64_t)); + break; + case SYSCOMMON_RESMAN_DATA_TYPE_DOUBLE: + attr_value->data = calloc(1, sizeof(double)); + break; + case SYSCOMMON_RESMAN_DATA_TYPE_STRING: + attr_value->data = calloc(SYSCOMMON_RESMAN_BUFF_MAX, sizeof(char)); + break; + case SYSCOMMON_RESMAN_DATA_TYPE_ARRAY: + attr_value->data = calloc(1, sizeof(struct syscommon_resman_array_value)); + break; + default: + ret = -EINVAL; + goto err; + } + + if (!attr_value->data) { + ret = -ENOMEM; + goto err; + } + } + } + + resource->attr_interest |= interest_mask; + + return 0; + +err: + for (; i >= 0; i--) { + if (!(resource->attrs[i].id & interest_mask)) + continue; + + attr_value = syscommon_resman_get_resource_attr_value(resource, + resource->attrs[i].id); + if (!attr_value) + continue; + + if (attr_value->data) { + free(attr_value->data); + attr_value->data = NULL; + } + } + + return ret; +} + +int +syscommon_resman_unset_resource_attr_interest(struct syscommon_resman_resource *resource, + u_int64_t interest_mask) +{ + struct syscommon_resman_resource_attribute_value *attr_value; + int i; + + if (!resource) + return -EINVAL; + + if (!syscommon_resman_is_resource_attr_interested(resource, interest_mask)) + return -EINVAL; + + for (i = 0; i < resource->num_attrs; i++) { + if (!(resource->attrs[i].id & interest_mask)) + continue; + + attr_value = syscommon_resman_get_resource_attr_value(resource, + resource->attrs[i].id); + if (!attr_value) + return -EINVAL; + + if (attr_value->data) { + switch (attr_value->type) { + case SYSCOMMON_RESMAN_DATA_TYPE_ARRAY: + { + struct syscommon_resman_array_value *array = attr_value->data; + + if (array->data) { + free(array->data); + array->data = NULL; + } + /* fall through */ + } + case SYSCOMMON_RESMAN_DATA_TYPE_INT: + case SYSCOMMON_RESMAN_DATA_TYPE_INT64: + case SYSCOMMON_RESMAN_DATA_TYPE_UINT: + case SYSCOMMON_RESMAN_DATA_TYPE_UINT64: + case SYSCOMMON_RESMAN_DATA_TYPE_DOUBLE: + case SYSCOMMON_RESMAN_DATA_TYPE_STRING: + free(attr_value->data); + attr_value->data = NULL; + break; + default: + return -EINVAL; + } + } + } + + resource->attr_interest &= ~interest_mask; + + return 0; +} + +bool +syscommon_resman_is_resource_attr_interested(struct syscommon_resman_resource *resource, + u_int64_t interest_mask) +{ + if (!resource) + return false; + + if (resource->attr_interest != (resource->attr_interest | interest_mask)) + return false; + + return true; +} + +const char * +syscommon_resman_get_resource_attr_name(struct syscommon_resman_resource *resource, + u_int64_t attr_id) +{ + const struct syscommon_resman_resource_attribute *attr; + + attr = syscommon_resman_get_resource_attr(resource, attr_id); + if (!attr) + return NULL; + + return attr->name; +} + +const char *syscommon_resman_get_resource_name(struct syscommon_resman_resource *resource) +{ + return resource ? resource->name : NULL; +} + +void *syscommon_resman_get_resource_privdata(struct syscommon_resman_resource *resource) +{ + return resource ? resource->priv : NULL; +} + +int syscommon_resman_get_resource_type(struct syscommon_resman_resource *resource) +{ + return resource ? resource->type : -EINVAL; +} + +int +syscommon_resman_set_resource_privdata(struct syscommon_resman_resource *resource, + void *priv) +{ + if (!resource) + return -EINVAL; + + resource->priv = priv; + + return 0; +} + +void syscommon_resman_init_resource_drivers(void) +{ + struct syscommon_resman_resource_driver *driver; + int i, ret = 0; + + for (i = 0; i < g_list_length(g_resource_driver_head); i++) { + driver = g_list_nth(g_list_first(g_resource_driver_head), i)->data; + + if (driver->ops.init) { + ret = driver->ops.init(); + if (ret < 0) + syscommon_resman_remove_resource_driver(driver); + } + } +} + +void syscommon_resman_exit_resource_drivers(void) +{ + const struct syscommon_resman_resource_driver *driver; + int i; + + for (i = 0; i < g_list_length(g_resource_driver_head); i++) { + driver = g_list_nth(g_list_first(g_resource_driver_head), i)->data; + + if (driver->ops.exit) + driver->ops.exit(); + } +} diff --git a/src/resource-manager/resource-manager.h b/src/resource-manager/resource-manager.h new file mode 100644 index 0000000..f070dd5 --- /dev/null +++ b/src/resource-manager/resource-manager.h @@ -0,0 +1,193 @@ +/* + * Copyright (c) 2022 Samsung Electronics Co., Ltd. + * + * 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. + */ + +#ifndef __RESOURCE_MANAGER_H__ +#define __RESOURCE_MANAGER_H__ + +// common.h + +#include +#include +#include +#include + +#define SYSCOMMON_RESMAN_BUFF_MAX 255 + +struct syscommon_resman_resource; +struct syscommon_resman_resource_attribute; +struct syscommon_resman_resource_control; + +struct syscommon_resman_array_value { + int type; + int length; + void *data; +}; + +struct syscommon_resman_resource_attribute_value { + int type; + void *data; +}; + +struct syscommon_resman_resource_attribute_ops { + int (*set)(struct syscommon_resman_resource *res, + const struct syscommon_resman_resource_attribute *attr, + const void *data, int count); + int (*get)(struct syscommon_resman_resource *res, + const struct syscommon_resman_resource_attribute *attr, + void *data); + /* + * If .is_supported ops is not implemented, use .get ops in order to + * check whether the resource attribute is supported or not. + */ + bool (*is_supported)(struct syscommon_resman_resource *res, + const struct syscommon_resman_resource_attribute *attr); +}; + +struct syscommon_resman_resource_attribute { + const char name[SYSCOMMON_RESMAN_BUFF_MAX]; + const u_int64_t id; + const int type; + const u_int64_t flag; + const struct syscommon_resman_resource_attribute_ops ops; +}; + +struct syscommon_resman_resource_control_ops { + const int (*set)(struct syscommon_resman_resource *res, + const struct syscommon_resman_resource_control *ctrl, + const void *data); + const int (*get)(struct syscommon_resman_resource *res, + const struct syscommon_resman_resource_control *ctrl, + void **data); +}; + +struct syscommon_resman_resource_control { + const char name[SYSCOMMON_RESMAN_BUFF_MAX]; + const u_int64_t id; + const struct syscommon_resman_resource_control_ops ops; +}; + +struct syscommon_resman_resource_driver_ops { + int (*init)(void); + void (*exit)(void); + int (*create)(struct syscommon_resman_resource *res); + void (*delete)(struct syscommon_resman_resource *res); + /* + * If prepare_update is specified, it will be called + * at every update_resource_attrs(). + */ + int (*prepare_update)(struct syscommon_resman_resource *res); +}; + +struct syscommon_resman_resource_driver { + const char *name; + const int type; + const u_int64_t flag; + const int num_attrs; + const struct syscommon_resman_resource_attribute *attrs; + const int num_ctrls; + const struct syscommon_resman_resource_control *ctrls; + const struct syscommon_resman_resource_driver_ops ops; +}; + +struct syscommon_resman_resource { + char *name; + const struct syscommon_resman_resource_driver *driver; + int type; + + int num_attrs; + const struct syscommon_resman_resource_attribute *attrs; + struct syscommon_resman_resource_attribute_value *attrs_value; + int num_ctrls; + const struct syscommon_resman_resource_control *ctrls; + + u_int64_t flag; + + void *priv; + + u_int64_t attr_interest; + u_int64_t attr_supported; +}; + +#define SYSCOMMON_RESMAN_RESOURCE_DRIVER_REGISTER(resource_driver) \ +static void __attribute__((constructor)) __syscommon_resman_module_init(void) \ +{ \ + syscommon_resman_add_resource_driver(resource_driver); \ +} \ +static void __attribute__((destructor)) __syscommon_resman_module_exit(void) \ +{ \ + syscommon_resman_remove_resource_driver(resource_driver); \ +} + +/* Add/remove resource driver and device */ +const struct syscommon_resman_resource_driver * +syscommon_resman_find_resource_driver(int resource_type); +void syscommon_resman_add_resource_driver(const struct syscommon_resman_resource_driver *resource_driver); +void syscommon_resman_remove_resource_driver(const struct syscommon_resman_resource_driver *resource_driver); + +/* Create/delete resource instance */ +int syscommon_resman_create_resource(struct syscommon_resman_resource **res, int resource_type); +void syscommon_resman_delete_resource(struct syscommon_resman_resource *resource); + +/* Set flag of the resource to given flag mask */ +int syscommon_resman_set_resource_flag(struct syscommon_resman_resource *resource, u_int64_t flag_mask); + +/* Handle resource control */ +int syscommon_resman_set_resource_control(struct syscommon_resman_resource *resource, u_int64_t ctrl_id, const void *data); +const char *syscommon_resman_get_resource_control_name(struct syscommon_resman_resource *resource, u_int64_t ctrl_id); + +/* Handle resource attribute */ +int syscommon_resman_update_resource_attrs(struct syscommon_resman_resource *resource); +const struct syscommon_resman_resource_attribute * +syscommon_resman_get_resource_attr(struct syscommon_resman_resource *resource, u_int64_t attr_id); +struct syscommon_resman_resource_attribute_value * +syscommon_resman_get_resource_attr_value(struct syscommon_resman_resource *resource, u_int64_t attr_id); +int syscommon_resman_is_resource_attr_supported(struct syscommon_resman_resource *resource, u_int64_t attr_id, bool *supported); + +static inline bool +syscommon_resman_resource_attr_supported_always(struct syscommon_resman_resource *resource, + const struct syscommon_resman_resource_attribute *attr) +{ + return true; +} + +int syscommon_resman_get_resource_attrs_json(struct syscommon_resman_resource *resource, char **json_string); +int syscommon_resman_get_resource_attr_json(struct syscommon_resman_resource *resource, u_int64_t attr_id, char **json_string); +int syscommon_resman_get_resource_list_json(char **json_string); + +int syscommon_resman_get_resource_attr_int(struct syscommon_resman_resource *resource, u_int64_t attr_id, int32_t *data); +int syscommon_resman_get_resource_attr_int64(struct syscommon_resman_resource *resource, u_int64_t attr_id, int64_t *data); +int syscommon_resman_get_resource_attr_uint(struct syscommon_resman_resource *resource, u_int64_t attr_id, u_int32_t *data); +int syscommon_resman_get_resource_attr_uint64(struct syscommon_resman_resource *resource, u_int64_t attr_id, u_int64_t *data); +int syscommon_resman_get_resource_attr_double(struct syscommon_resman_resource *resource, u_int64_t attr_id, double *data); +int syscommon_resman_get_resource_attr_string(struct syscommon_resman_resource *resource, u_int64_t attr_id, char *data); +int syscommon_resman_get_resource_attr_array(struct syscommon_resman_resource *resource, u_int64_t attr_id, + struct syscommon_resman_array_value **data); +int syscommon_resman_get_resource_attr_ptr(struct syscommon_resman_resource *resource, u_int64_t attr_id, void **data); + +int syscommon_resman_set_resource_attr_interest(struct syscommon_resman_resource *resource, u_int64_t interest_mask); +int syscommon_resman_unset_resource_attr_interest(struct syscommon_resman_resource *resource, u_int64_t interest_mask); +bool syscommon_resman_is_resource_attr_interested(struct syscommon_resman_resource *resource, u_int64_t interest_mask); +const char *syscommon_resman_get_resource_attr_name(struct syscommon_resman_resource *resource, u_int64_t attr_id); + +const char *syscommon_resman_get_resource_name(struct syscommon_resman_resource *resource); +void *syscommon_resman_get_resource_privdata(struct syscommon_resman_resource *resource); +int syscommon_resman_get_resource_type(struct syscommon_resman_resource *resource); + +int syscommon_resman_set_resource_privdata(struct syscommon_resman_resource *resource, void *priv); + +void syscommon_resman_init_resource_drivers(void); +void syscommon_resman_exit_resource_drivers(void); +#endif diff --git a/src/resource-manager/resource-type.h b/src/resource-manager/resource-type.h new file mode 100644 index 0000000..2ee7b38 --- /dev/null +++ b/src/resource-manager/resource-type.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2022 Samsung Electronics Co., Ltd. + * + * 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. + */ + + +#ifndef __RESOURCE_TYPE_H__ +#define __RESOURCE_TYPE_H__ + +enum syscommon_resman_data_type { + SYSCOMMON_RESMAN_DATA_TYPE_UNKNOWN = 0, + SYSCOMMON_RESMAN_DATA_TYPE_INT, + SYSCOMMON_RESMAN_DATA_TYPE_INT64, + SYSCOMMON_RESMAN_DATA_TYPE_UINT, + SYSCOMMON_RESMAN_DATA_TYPE_UINT64, + SYSCOMMON_RESMAN_DATA_TYPE_DOUBLE, + SYSCOMMON_RESMAN_DATA_TYPE_STRING, + SYSCOMMON_RESMAN_DATA_TYPE_ARRAY, + SYSCOMMON_RESMAN_DATA_TYPE_PTR, + SYSCOMMON_RESMAN_DATA_TYPE_BOOLEAN, + SYSCOMMON_RESMAN_DATA_TYPE_NUM +}; + +#define BIT(x) (1ULL << x) + +/* + * Resource driver flags + * - RESOURCE_DRIVER_FLAG_COUNT_ONLY_ONE indicates the unique device which + * presents only one. + * - RESOURCE_DRIVER_FLAG_UNCOUNTABLE incidates that resource which is not + * bound physical h/w. + */ +#define SYSCOMMON_RESMAN_RESOURCE_DRIVER_FLAG_COUNT_ONLY_ONE BIT(0) +#define SYSCOMMON_RESMAN_RESOURCE_DRIVER_FLAG_UNCOUNTABLE BIT(1) + +/* Resource flags */ +#define SYSCOMMON_RESMAN_RESOURCE_FLAG_PRIVATE BIT(0) +#define SYSCOMMON_RESMAN_RESOURCE_FLAG_PUBLIC BIT(1) + +/* Flags for resource_attribute */ +#define SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PRIVATE BIT(0) +#define SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC BIT(1) + +#endif -- 2.7.4 From 022db845dcfd07050f071d193f9b95849fef4378 Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Fri, 4 Nov 2022 14:45:16 +0900 Subject: [PATCH 05/16] resource-manager: operate based on resource id All operations provided by resource-manager now work based on resource "id" instead of resource "instance" itself. All resource "instances" are managed within libsyscommon and it only exposes their id. The resource-manager source code has been implemented based on the coding rule below. - An actual operating function has no prefix syscommon_resman_. It has internal linkage and takes actual resource instance as a parameter. e.g., get_resource_attr(struct syscommon_resman_resource *resource, ...) - A function exposed through header is prefixed with syscommon_resman_ and it takes resource id as a parameter. It locates resource instance using resource id and invokes matching function. e.g., syscommon_resman_get_resource_attr(int resource_id, ...) Change-Id: If82c58593b363433188bf01809dc57a242ede24f Signed-off-by: Youngjae Cho --- src/resource-manager/resource-manager.c | 353 ++++++++++++++++++++++---------- src/resource-manager/resource-manager.h | 99 ++++----- 2 files changed, 288 insertions(+), 164 deletions(-) diff --git a/src/resource-manager/resource-manager.c b/src/resource-manager/resource-manager.c index 7faa87b..fca7743 100644 --- a/src/resource-manager/resource-manager.c +++ b/src/resource-manager/resource-manager.c @@ -17,6 +17,8 @@ #include #include #include +#include +#include #include "resource-manager.h" #include "resource-type.h" @@ -30,8 +32,116 @@ #define RESOURCE_ATTR_FLAG_VISIBILITY_MASK (SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PRIVATE \ | SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC) +struct syscommon_resman_resource { + char *name; + int id; + const struct syscommon_resman_resource_driver *driver; + int type; + + int num_attrs; + const struct syscommon_resman_resource_attribute *attrs; + struct syscommon_resman_resource_attribute_value *attrs_value; + int num_ctrls; + const struct syscommon_resman_resource_control *ctrls; + + u_int64_t flag; + + void *priv; + + u_int64_t attr_interest; + u_int64_t attr_supported; +}; + +static int unset_resource_attr_interest(struct syscommon_resman_resource *resource, u_int64_t interest_mask); +static bool is_resource_attr_interested(struct syscommon_resman_resource *resource, u_int64_t interest_mask); static GList *g_resource_driver_head; +static GHashTable *g_resource_hash_table; + +static int clear_sign_bit(unsigned int val) +{ + return (int)((val << 1) >> 1); +} + +static int init_resource_id(void) +{ + int val; + struct timeval tv; + + gettimeofday(&tv, NULL); + val = tv.tv_sec * 1000 + tv.tv_usec / 1000; + return clear_sign_bit(val); +} + +static int alloc_resource_id(void) +{ + static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; + static int resource_id = -1; + int ret; + + pthread_mutex_lock(&lock); + + if (resource_id < 0) + resource_id = init_resource_id(); + + ret = resource_id++; + clear_sign_bit(resource_id); + + pthread_mutex_unlock(&lock); + + return ret; +} + +static void free_resource(struct syscommon_resman_resource *resource) +{ + if (!resource->name) + free(resource->name); + if (!resource->attrs_value) + free(resource->attrs_value); + resource->attrs = NULL; + resource->num_attrs = 0; + + free(resource); +} + +static void delete_resource(void *data) +{ + struct syscommon_resman_resource *resource = (struct syscommon_resman_resource *) data; + + if (!resource) + return; + + if (resource->driver && resource->driver->ops.delete) + resource->driver->ops.delete(resource->id); + + unset_resource_attr_interest(resource, RESOURCE_ATTR_MASK); + + free_resource(resource); +} + +static int add_resource(struct syscommon_resman_resource *resource) +{ + if (!resource) + return -EINVAL; + + if (!g_resource_hash_table) { + g_resource_hash_table = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, delete_resource); + if (!g_resource_hash_table) + return -ENOMEM; + } + + g_hash_table_insert(g_resource_hash_table, GINT_TO_POINTER(resource->id), resource); + + return 0; +} + +static struct syscommon_resman_resource * find_resource(int id) +{ + if (!g_resource_hash_table) + return NULL; + + return g_hash_table_lookup(g_resource_hash_table, GINT_TO_POINTER(id)); +} static gint __compare_resource_type(gconstpointer data, gconstpointer input) { @@ -78,41 +188,22 @@ syscommon_resman_remove_resource_driver(const struct syscommon_resman_resource_d g_list_remove(g_resource_driver_head, (gpointer)driver); } -static void do_delete_resource(struct syscommon_resman_resource *resource) +void syscommon_resman_delete_resource(int resource_id) { - if (!resource->name) - free(resource->name); - if (!resource->attrs_value) - free(resource->attrs_value); - resource->attrs = NULL; - resource->num_attrs = 0; - - free(resource); -} - -void -syscommon_resman_delete_resource(struct syscommon_resman_resource *resource) -{ - if (!resource) + if (!g_resource_hash_table) return; - if (resource->driver && resource->driver->ops.delete) - resource->driver->ops.delete(resource); - - syscommon_resman_unset_resource_attr_interest(resource, RESOURCE_ATTR_MASK); - - do_delete_resource(resource); + g_hash_table_remove(g_resource_hash_table, GINT_TO_POINTER(resource_id)); } int -syscommon_resman_create_resource(struct syscommon_resman_resource **res, - int resource_type) +syscommon_resman_create_resource(int *resource_id, int resource_type) { const struct syscommon_resman_resource_driver *driver = NULL; struct syscommon_resman_resource *resource = NULL; int i, ret; - if (!res) + if (!resource_id) return -EINVAL; driver = syscommon_resman_find_resource_driver(resource_type); @@ -123,6 +214,7 @@ syscommon_resman_create_resource(struct syscommon_resman_resource **res, if (!resource) return -ENOMEM; + resource->id = alloc_resource_id(); resource->type = resource_type; resource->name = g_strdup(driver->name); resource->driver = driver; @@ -131,10 +223,16 @@ syscommon_resman_create_resource(struct syscommon_resman_resource **res, resource->attrs_value = calloc(resource->num_attrs, sizeof(*resource->attrs_value)); if (!resource->attrs_value) { - do_delete_resource(resource); + free_resource(resource); return -ENOMEM; } + ret = add_resource(resource); + if (ret < 0) { + free_resource(resource); + return ret; + } + for (i = 0; i < resource->num_attrs; i++) resource->attrs_value[i].type = driver->attrs[i].type; @@ -143,22 +241,23 @@ syscommon_resman_create_resource(struct syscommon_resman_resource **res, resource->flag = SYSCOMMON_RESMAN_RESOURCE_FLAG_PRIVATE; if (driver->ops.create) { - ret = driver->ops.create(resource); + ret = driver->ops.create(resource->id); if (ret < 0) { - do_delete_resource(resource); + free_resource(resource); return ret; } } - *res = resource; + *resource_id = resource->id; return 0; } int -syscommon_resman_set_resource_flag(struct syscommon_resman_resource *resource, - u_int64_t flag_mask) +syscommon_resman_set_resource_flag(int resource_id, u_int64_t flag_mask) { + struct syscommon_resman_resource *resource = find_resource(resource_id); + if (!resource) return -EINVAL; @@ -167,12 +266,12 @@ syscommon_resman_set_resource_flag(struct syscommon_resman_resource *resource, } int -syscommon_resman_set_resource_control(struct syscommon_resman_resource *resource, - u_int64_t ctrl_id, const void *data) +syscommon_resman_set_resource_control(int resource_id, u_int64_t ctrl_id, const void *data) { const struct syscommon_resman_resource_control *ctrl; int ctrl_index = RESOURCE_CTRL_INDEX(ctrl_id); int ret; + struct syscommon_resman_resource *resource = find_resource(resource_id); if (!resource || ctrl_index < 0 || ctrl_index >= resource->num_ctrls) return -EINVAL; @@ -181,7 +280,7 @@ syscommon_resman_set_resource_control(struct syscommon_resman_resource *resource if (!ctrl->ops.set) return -ENOTSUP; - ret = ctrl->ops.set(resource, ctrl, data); + ret = ctrl->ops.set(resource_id, ctrl, data); if (ret < 0) return ret; @@ -189,11 +288,11 @@ syscommon_resman_set_resource_control(struct syscommon_resman_resource *resource } const char * -syscommon_resman_get_resource_control_name(struct syscommon_resman_resource *resource, - u_int64_t ctrl_id) +syscommon_resman_get_resource_control_name(int resource_id, u_int64_t ctrl_id) { const struct syscommon_resman_resource_control *ctrl; int ctrl_index = RESOURCE_CTRL_INDEX(ctrl_id); + struct syscommon_resman_resource *resource = find_resource(resource_id); if (!resource || ctrl_index < 0 || ctrl_index >= resource->num_ctrls) return NULL; @@ -222,7 +321,7 @@ update_resource_attr(struct syscommon_resman_resource *resource, attr_value = &resource->attrs_value[attr_index]; - ret = attr->ops.get(resource, attr, attr_value->data); + ret = attr->ops.get(resource->id, attr, attr_value->data); if (ret < 0) return ret; @@ -230,15 +329,16 @@ update_resource_attr(struct syscommon_resman_resource *resource, } int -syscommon_resman_update_resource_attrs(struct syscommon_resman_resource *resource) +syscommon_resman_update_resource_attrs(int resource_id) { int i, ret; + struct syscommon_resman_resource *resource = find_resource(resource_id); if (!resource || !resource->type) return -EINVAL; if (resource->driver && resource->driver->ops.prepare_update) { - ret = resource->driver->ops.prepare_update(resource); + ret = resource->driver->ops.prepare_update(resource->id); if (ret < 0) return ret; } @@ -252,9 +352,8 @@ syscommon_resman_update_resource_attrs(struct syscommon_resman_resource *resourc return 0; } -const struct syscommon_resman_resource_attribute * -syscommon_resman_get_resource_attr(struct syscommon_resman_resource *resource, - u_int64_t attr_id) +static const struct syscommon_resman_resource_attribute * +get_resource_attr(struct syscommon_resman_resource *resource, u_int64_t attr_id) { int attr_index = RESOURCE_ATTR_INDEX(attr_id); @@ -264,9 +363,17 @@ syscommon_resman_get_resource_attr(struct syscommon_resman_resource *resource, return &resource->attrs[attr_index]; } -struct syscommon_resman_resource_attribute_value * -syscommon_resman_get_resource_attr_value(struct syscommon_resman_resource *resource, - u_int64_t attr_id) +const struct syscommon_resman_resource_attribute * +syscommon_resman_get_resource_attr(int resource_id, u_int64_t attr_id) +{ + struct syscommon_resman_resource *resource = find_resource(resource_id); + + return get_resource_attr(resource, attr_id); +} + + +static struct syscommon_resman_resource_attribute_value * +get_resource_attr_value(struct syscommon_resman_resource *resource, u_int64_t attr_id) { int attr_index = RESOURCE_ATTR_INDEX(attr_id); @@ -276,24 +383,32 @@ syscommon_resman_get_resource_attr_value(struct syscommon_resman_resource *resou return &resource->attrs_value[attr_index]; } -int -syscommon_resman_is_resource_attr_supported(struct syscommon_resman_resource *resource, - u_int64_t attr_id, bool *supported) +struct syscommon_resman_resource_attribute_value * +syscommon_resman_get_resource_attr_value(int resource_id, u_int64_t attr_id) +{ + struct syscommon_resman_resource *resource = find_resource(resource_id); + + return get_resource_attr_value(resource, attr_id); +} + +static int is_resource_attr_supported(struct syscommon_resman_resource *resource, u_int64_t attr_id, bool *supported) { const struct syscommon_resman_resource_attribute *attr = NULL; int attr_index = RESOURCE_ATTR_INDEX(attr_id); int ret; bool is_supported = false; - if (!resource || attr_index < 0 || attr_index >= resource->num_attrs) + if (!resource || attr_index < 0 || attr_index >= resource->num_attrs) { + *supported = false; return -EINVAL; + } attr = &resource->attrs[attr_index]; if (attr->id & resource->attr_supported) { is_supported = true; } else if (attr->ops.is_supported) { - is_supported = attr->ops.is_supported(resource, attr); + is_supported = attr->ops.is_supported(resource->id, attr); } else if (attr->ops.get) { /* * Optionally, if .is_supported ops is not implemented, @@ -302,7 +417,7 @@ syscommon_resman_is_resource_attr_supported(struct syscommon_resman_resource *re */ char data[SYSCOMMON_RESMAN_BUFF_MAX] = {0, }; - ret = attr->ops.get(resource, attr, (void *)data); + ret = attr->ops.get(resource->id, attr, (void *)data); is_supported = (ret < 0) ? false : true; } @@ -314,13 +429,21 @@ syscommon_resman_is_resource_attr_supported(struct syscommon_resman_resource *re return 0; } +int +syscommon_resman_is_resource_attr_supported(int resource_id, u_int64_t attr_id, bool *supported) +{ + struct syscommon_resman_resource *resource = find_resource(resource_id); + + return is_resource_attr_supported(resource, attr_id, supported); +} + static bool check_attr_validate(struct syscommon_resman_resource *resource, u_int64_t attr_id, int type) { const struct syscommon_resman_resource_attribute *attr; - attr = syscommon_resman_get_resource_attr(resource, attr_id); + attr = get_resource_attr(resource, attr_id); if (!attr) return false; @@ -456,12 +579,12 @@ static void _put_resource_attr_json(json_object *jobj_attr) } int -syscommon_resman_get_resource_attrs_json(struct syscommon_resman_resource *resource, - char **json_string) +syscommon_resman_get_resource_attrs_json(int resource_id, char **json_string) { json_object *jobj_root, *jobj_res_name, *jobj_res_type, *jobj_res_attrs, *jobj_attr; const struct syscommon_resman_resource_attribute *attr; const struct syscommon_resman_resource_attribute_value *attr_value; + struct syscommon_resman_resource *resource = find_resource(resource_id); int i; if (!resource || !resource->type) @@ -506,15 +629,15 @@ syscommon_resman_get_resource_attrs_json(struct syscommon_resman_resource *resou } int -syscommon_resman_get_resource_attr_json(struct syscommon_resman_resource *resource, - u_int64_t attr_id, char **json_string) +syscommon_resman_get_resource_attr_json(int resource_id, u_int64_t attr_id, char **json_string) { const struct syscommon_resman_resource_attribute *attr; const struct syscommon_resman_resource_attribute_value *attr_value; json_object *jobj_attr; + struct syscommon_resman_resource *resource = find_resource(resource_id); - attr = syscommon_resman_get_resource_attr(resource, attr_id); - attr_value = syscommon_resman_get_resource_attr_value(resource, attr_id); + attr = get_resource_attr(resource, attr_id); + attr_value = get_resource_attr_value(resource, attr_id); if (!attr || !attr_value) return -EINVAL; @@ -652,15 +775,15 @@ int syscommon_resman_get_resource_list_json(char **json_string) } int -syscommon_resman_get_resource_attr_int(struct syscommon_resman_resource *resource, - u_int64_t attr_id, int32_t *data) +syscommon_resman_get_resource_attr_int(int resource_id, u_int64_t attr_id, int32_t *data) { struct syscommon_resman_resource_attribute_value *attr_value = NULL; + struct syscommon_resman_resource *resource = find_resource(resource_id); if (!check_attr_validate(resource, attr_id, SYSCOMMON_RESMAN_DATA_TYPE_INT)) return -EINVAL; - attr_value = syscommon_resman_get_resource_attr_value(resource, attr_id); + attr_value = get_resource_attr_value(resource, attr_id); if (!attr_value) return -EINVAL; @@ -670,15 +793,15 @@ syscommon_resman_get_resource_attr_int(struct syscommon_resman_resource *resourc } int -syscommon_resman_get_resource_attr_int64(struct syscommon_resman_resource *resource, - u_int64_t attr_id, int64_t *data) +syscommon_resman_get_resource_attr_int64(int resource_id, u_int64_t attr_id, int64_t *data) { struct syscommon_resman_resource_attribute_value *attr_value = NULL; + struct syscommon_resman_resource *resource = find_resource(resource_id); if (!check_attr_validate(resource, attr_id, SYSCOMMON_RESMAN_DATA_TYPE_INT64)) return -EINVAL; - attr_value = syscommon_resman_get_resource_attr_value(resource, attr_id); + attr_value = get_resource_attr_value(resource, attr_id); if (!attr_value) return -EINVAL; @@ -688,15 +811,15 @@ syscommon_resman_get_resource_attr_int64(struct syscommon_resman_resource *resou } int -syscommon_resman_get_resource_attr_uint(struct syscommon_resman_resource *resource, - u_int64_t attr_id, u_int32_t *data) +syscommon_resman_get_resource_attr_uint(int resource_id, u_int64_t attr_id, u_int32_t *data) { struct syscommon_resman_resource_attribute_value *attr_value = NULL; + struct syscommon_resman_resource *resource = find_resource(resource_id); if (!check_attr_validate(resource, attr_id, SYSCOMMON_RESMAN_DATA_TYPE_UINT)) return -EINVAL; - attr_value = syscommon_resman_get_resource_attr_value(resource, attr_id); + attr_value = get_resource_attr_value(resource, attr_id); if (!attr_value) return -EINVAL; @@ -706,15 +829,15 @@ syscommon_resman_get_resource_attr_uint(struct syscommon_resman_resource *resour } int -syscommon_resman_get_resource_attr_uint64(struct syscommon_resman_resource *resource, - u_int64_t attr_id, u_int64_t *data) +syscommon_resman_get_resource_attr_uint64(int resource_id, u_int64_t attr_id, u_int64_t *data) { struct syscommon_resman_resource_attribute_value *attr_value = NULL; + struct syscommon_resman_resource *resource = find_resource(resource_id); if (!check_attr_validate(resource, attr_id, SYSCOMMON_RESMAN_DATA_TYPE_UINT64)) return -EINVAL; - attr_value = syscommon_resman_get_resource_attr_value(resource, attr_id); + attr_value = get_resource_attr_value(resource, attr_id); if (!attr_value) return -EINVAL; @@ -724,15 +847,15 @@ syscommon_resman_get_resource_attr_uint64(struct syscommon_resman_resource *reso } int -syscommon_resman_get_resource_attr_double(struct syscommon_resman_resource *resource, - u_int64_t attr_id, double *data) +syscommon_resman_get_resource_attr_double(int resource_id, u_int64_t attr_id, double *data) { struct syscommon_resman_resource_attribute_value *attr_value = NULL; + struct syscommon_resman_resource *resource = find_resource(resource_id); if (!check_attr_validate(resource, attr_id, SYSCOMMON_RESMAN_DATA_TYPE_DOUBLE)) return -EINVAL; - attr_value = syscommon_resman_get_resource_attr_value(resource, attr_id); + attr_value = get_resource_attr_value(resource, attr_id); if (!attr_value) return -EINVAL; @@ -742,15 +865,15 @@ syscommon_resman_get_resource_attr_double(struct syscommon_resman_resource *reso } int -syscommon_resman_get_resource_attr_string(struct syscommon_resman_resource *resource, - u_int64_t attr_id, char *data) +syscommon_resman_get_resource_attr_string(int resource_id, u_int64_t attr_id, char *data) { struct syscommon_resman_resource_attribute_value *attr_value = NULL; + struct syscommon_resman_resource *resource = find_resource(resource_id); if (!check_attr_validate(resource, attr_id, SYSCOMMON_RESMAN_DATA_TYPE_STRING)) return -EINVAL; - attr_value = syscommon_resman_get_resource_attr_value(resource, attr_id); + attr_value = get_resource_attr_value(resource, attr_id); if (!attr_value) return -EINVAL; @@ -760,16 +883,15 @@ syscommon_resman_get_resource_attr_string(struct syscommon_resman_resource *reso } int -syscommon_resman_get_resource_attr_array(struct syscommon_resman_resource *resource, - u_int64_t attr_id, - struct syscommon_resman_array_value **data) +syscommon_resman_get_resource_attr_array(int resource_id, u_int64_t attr_id, struct syscommon_resman_array_value **data) { struct syscommon_resman_resource_attribute_value *attr_value = NULL; + struct syscommon_resman_resource *resource = find_resource(resource_id); if (!check_attr_validate(resource, attr_id, SYSCOMMON_RESMAN_DATA_TYPE_ARRAY)) return -EINVAL; - attr_value = syscommon_resman_get_resource_attr_value(resource, attr_id); + attr_value = get_resource_attr_value(resource, attr_id); if (!attr_value) return -EINVAL; @@ -779,15 +901,15 @@ syscommon_resman_get_resource_attr_array(struct syscommon_resman_resource *resou } int -syscommon_resman_get_resource_attr_ptr(struct syscommon_resman_resource *resource, - u_int64_t attr_id, void **data) +syscommon_resman_get_resource_attr_ptr(int resource_id, u_int64_t attr_id, void **data) { struct syscommon_resman_resource_attribute_value *attr_value = NULL; + struct syscommon_resman_resource *resource = find_resource(resource_id); if (!check_attr_validate(resource, attr_id, SYSCOMMON_RESMAN_DATA_TYPE_PTR)) return -EINVAL; - attr_value = syscommon_resman_get_resource_attr_value(resource, attr_id); + attr_value = get_resource_attr_value(resource, attr_id); if (!attr_value) return -EINVAL; @@ -813,12 +935,12 @@ is_resource_attr_visible(struct syscommon_resman_resource *resource, } int -syscommon_resman_set_resource_attr_interest(struct syscommon_resman_resource *resource, - u_int64_t interest_mask) +syscommon_resman_set_resource_attr_interest(int resource_id, u_int64_t interest_mask) { struct syscommon_resman_resource_attribute_value *attr_value; int i, ret; bool supported; + struct syscommon_resman_resource *resource = find_resource(resource_id); if (!resource) return -EINVAL; @@ -827,7 +949,7 @@ syscommon_resman_set_resource_attr_interest(struct syscommon_resman_resource *re if (!(resource->attrs[i].id & interest_mask)) continue; - ret = syscommon_resman_is_resource_attr_supported(resource, + ret = is_resource_attr_supported(resource, resource->attrs[i].id, &supported); if (ret < 0) { @@ -842,8 +964,7 @@ syscommon_resman_set_resource_attr_interest(struct syscommon_resman_resource *re goto err; } - attr_value = syscommon_resman_get_resource_attr_value(resource, - resource->attrs[i].id); + attr_value = get_resource_attr_value(resource, resource->attrs[i].id); if (!attr_value) { ret = -EINVAL; goto err; @@ -899,8 +1020,7 @@ err: if (!(resource->attrs[i].id & interest_mask)) continue; - attr_value = syscommon_resman_get_resource_attr_value(resource, - resource->attrs[i].id); + attr_value = get_resource_attr_value(resource, resource->attrs[i].id); if (!attr_value) continue; @@ -913,9 +1033,8 @@ err: return ret; } -int -syscommon_resman_unset_resource_attr_interest(struct syscommon_resman_resource *resource, - u_int64_t interest_mask) +static int +unset_resource_attr_interest(struct syscommon_resman_resource *resource, u_int64_t interest_mask) { struct syscommon_resman_resource_attribute_value *attr_value; int i; @@ -923,14 +1042,14 @@ syscommon_resman_unset_resource_attr_interest(struct syscommon_resman_resource * if (!resource) return -EINVAL; - if (!syscommon_resman_is_resource_attr_interested(resource, interest_mask)) + if (!is_resource_attr_interested(resource, interest_mask)) return -EINVAL; for (i = 0; i < resource->num_attrs; i++) { if (!(resource->attrs[i].id & interest_mask)) continue; - attr_value = syscommon_resman_get_resource_attr_value(resource, + attr_value = get_resource_attr_value(resource, resource->attrs[i].id); if (!attr_value) return -EINVAL; @@ -967,8 +1086,16 @@ syscommon_resman_unset_resource_attr_interest(struct syscommon_resman_resource * return 0; } -bool -syscommon_resman_is_resource_attr_interested(struct syscommon_resman_resource *resource, +int +syscommon_resman_unset_resource_attr_interest(int resource_id, u_int64_t interest_mask) +{ + struct syscommon_resman_resource *resource = find_resource(resource_id); + + return unset_resource_attr_interest(resource, interest_mask); +} + +static bool +is_resource_attr_interested(struct syscommon_resman_resource *resource, u_int64_t interest_mask) { if (!resource) @@ -980,38 +1107,56 @@ syscommon_resman_is_resource_attr_interested(struct syscommon_resman_resource *r return true; } +bool +syscommon_resman_is_resource_attr_interested(int resource_id, u_int64_t interest_mask) +{ + struct syscommon_resman_resource *resource = find_resource(resource_id); + + return is_resource_attr_interested(resource, interest_mask); +} + const char * -syscommon_resman_get_resource_attr_name(struct syscommon_resman_resource *resource, - u_int64_t attr_id) +syscommon_resman_get_resource_attr_name(int resource_id, u_int64_t attr_id) { const struct syscommon_resman_resource_attribute *attr; + struct syscommon_resman_resource *resource = find_resource(resource_id); - attr = syscommon_resman_get_resource_attr(resource, attr_id); + attr = get_resource_attr(resource, attr_id); if (!attr) return NULL; return attr->name; } -const char *syscommon_resman_get_resource_name(struct syscommon_resman_resource *resource) +const char +*syscommon_resman_get_resource_name(int resource_id) { + struct syscommon_resman_resource *resource = find_resource(resource_id); + return resource ? resource->name : NULL; } -void *syscommon_resman_get_resource_privdata(struct syscommon_resman_resource *resource) +void * +syscommon_resman_get_resource_privdata(int resource_id) { + struct syscommon_resman_resource *resource = find_resource(resource_id); + return resource ? resource->priv : NULL; } -int syscommon_resman_get_resource_type(struct syscommon_resman_resource *resource) +int +syscommon_resman_get_resource_type(int resource_id) { + struct syscommon_resman_resource *resource = find_resource(resource_id); + return resource ? resource->type : -EINVAL; } int -syscommon_resman_set_resource_privdata(struct syscommon_resman_resource *resource, - void *priv) +syscommon_resman_set_resource_privdata(int resource_id, void *priv) { + struct syscommon_resman_resource *resource = find_resource(resource_id); + if (!resource) return -EINVAL; diff --git a/src/resource-manager/resource-manager.h b/src/resource-manager/resource-manager.h index f070dd5..4ebf694 100644 --- a/src/resource-manager/resource-manager.h +++ b/src/resource-manager/resource-manager.h @@ -26,7 +26,6 @@ #define SYSCOMMON_RESMAN_BUFF_MAX 255 -struct syscommon_resman_resource; struct syscommon_resman_resource_attribute; struct syscommon_resman_resource_control; @@ -42,17 +41,17 @@ struct syscommon_resman_resource_attribute_value { }; struct syscommon_resman_resource_attribute_ops { - int (*set)(struct syscommon_resman_resource *res, + int (*set)(int resource_id, const struct syscommon_resman_resource_attribute *attr, const void *data, int count); - int (*get)(struct syscommon_resman_resource *res, + int (*get)(int resource_id, const struct syscommon_resman_resource_attribute *attr, void *data); /* * If .is_supported ops is not implemented, use .get ops in order to * check whether the resource attribute is supported or not. */ - bool (*is_supported)(struct syscommon_resman_resource *res, + bool (*is_supported)(int resource_id, const struct syscommon_resman_resource_attribute *attr); }; @@ -65,10 +64,10 @@ struct syscommon_resman_resource_attribute { }; struct syscommon_resman_resource_control_ops { - const int (*set)(struct syscommon_resman_resource *res, + const int (*set)(int resource_id, const struct syscommon_resman_resource_control *ctrl, const void *data); - const int (*get)(struct syscommon_resman_resource *res, + const int (*get)(int resource_id, const struct syscommon_resman_resource_control *ctrl, void **data); }; @@ -82,13 +81,13 @@ struct syscommon_resman_resource_control { struct syscommon_resman_resource_driver_ops { int (*init)(void); void (*exit)(void); - int (*create)(struct syscommon_resman_resource *res); - void (*delete)(struct syscommon_resman_resource *res); + int (*create)(int resource_id); + void (*delete)(int resource_id); /* * If prepare_update is specified, it will be called * at every update_resource_attrs(). */ - int (*prepare_update)(struct syscommon_resman_resource *res); + int (*prepare_update)(int resource_id); }; struct syscommon_resman_resource_driver { @@ -102,25 +101,6 @@ struct syscommon_resman_resource_driver { const struct syscommon_resman_resource_driver_ops ops; }; -struct syscommon_resman_resource { - char *name; - const struct syscommon_resman_resource_driver *driver; - int type; - - int num_attrs; - const struct syscommon_resman_resource_attribute *attrs; - struct syscommon_resman_resource_attribute_value *attrs_value; - int num_ctrls; - const struct syscommon_resman_resource_control *ctrls; - - u_int64_t flag; - - void *priv; - - u_int64_t attr_interest; - u_int64_t attr_supported; -}; - #define SYSCOMMON_RESMAN_RESOURCE_DRIVER_REGISTER(resource_driver) \ static void __attribute__((constructor)) __syscommon_resman_module_init(void) \ { \ @@ -138,55 +118,54 @@ void syscommon_resman_add_resource_driver(const struct syscommon_resman_resource void syscommon_resman_remove_resource_driver(const struct syscommon_resman_resource_driver *resource_driver); /* Create/delete resource instance */ -int syscommon_resman_create_resource(struct syscommon_resman_resource **res, int resource_type); -void syscommon_resman_delete_resource(struct syscommon_resman_resource *resource); +int syscommon_resman_create_resource(int *resource_id, int resource_type); +void syscommon_resman_delete_resource(int resource_id); /* Set flag of the resource to given flag mask */ -int syscommon_resman_set_resource_flag(struct syscommon_resman_resource *resource, u_int64_t flag_mask); +int syscommon_resman_set_resource_flag(int resource_id, u_int64_t flag_mask); /* Handle resource control */ -int syscommon_resman_set_resource_control(struct syscommon_resman_resource *resource, u_int64_t ctrl_id, const void *data); -const char *syscommon_resman_get_resource_control_name(struct syscommon_resman_resource *resource, u_int64_t ctrl_id); +int syscommon_resman_set_resource_control(int resource_id, u_int64_t ctrl_id, const void *data); +const char *syscommon_resman_get_resource_control_name(int resource_id, u_int64_t ctrl_id); /* Handle resource attribute */ -int syscommon_resman_update_resource_attrs(struct syscommon_resman_resource *resource); +int syscommon_resman_update_resource_attrs(int resource_id); const struct syscommon_resman_resource_attribute * -syscommon_resman_get_resource_attr(struct syscommon_resman_resource *resource, u_int64_t attr_id); +syscommon_resman_get_resource_attr(int resource_id, u_int64_t attr_id); struct syscommon_resman_resource_attribute_value * -syscommon_resman_get_resource_attr_value(struct syscommon_resman_resource *resource, u_int64_t attr_id); -int syscommon_resman_is_resource_attr_supported(struct syscommon_resman_resource *resource, u_int64_t attr_id, bool *supported); +syscommon_resman_get_resource_attr_value(int resource_id, u_int64_t attr_id); +int syscommon_resman_is_resource_attr_supported(int resource_id, u_int64_t attr_id, bool *supported); static inline bool -syscommon_resman_resource_attr_supported_always(struct syscommon_resman_resource *resource, +syscommon_resman_resource_attr_supported_always(int resource_id, const struct syscommon_resman_resource_attribute *attr) { return true; } -int syscommon_resman_get_resource_attrs_json(struct syscommon_resman_resource *resource, char **json_string); -int syscommon_resman_get_resource_attr_json(struct syscommon_resman_resource *resource, u_int64_t attr_id, char **json_string); +int syscommon_resman_get_resource_attrs_json(int resource_id, char **json_string); +int syscommon_resman_get_resource_attr_json(int resource_id, u_int64_t attr_id, char **json_string); int syscommon_resman_get_resource_list_json(char **json_string); -int syscommon_resman_get_resource_attr_int(struct syscommon_resman_resource *resource, u_int64_t attr_id, int32_t *data); -int syscommon_resman_get_resource_attr_int64(struct syscommon_resman_resource *resource, u_int64_t attr_id, int64_t *data); -int syscommon_resman_get_resource_attr_uint(struct syscommon_resman_resource *resource, u_int64_t attr_id, u_int32_t *data); -int syscommon_resman_get_resource_attr_uint64(struct syscommon_resman_resource *resource, u_int64_t attr_id, u_int64_t *data); -int syscommon_resman_get_resource_attr_double(struct syscommon_resman_resource *resource, u_int64_t attr_id, double *data); -int syscommon_resman_get_resource_attr_string(struct syscommon_resman_resource *resource, u_int64_t attr_id, char *data); -int syscommon_resman_get_resource_attr_array(struct syscommon_resman_resource *resource, u_int64_t attr_id, - struct syscommon_resman_array_value **data); -int syscommon_resman_get_resource_attr_ptr(struct syscommon_resman_resource *resource, u_int64_t attr_id, void **data); - -int syscommon_resman_set_resource_attr_interest(struct syscommon_resman_resource *resource, u_int64_t interest_mask); -int syscommon_resman_unset_resource_attr_interest(struct syscommon_resman_resource *resource, u_int64_t interest_mask); -bool syscommon_resman_is_resource_attr_interested(struct syscommon_resman_resource *resource, u_int64_t interest_mask); -const char *syscommon_resman_get_resource_attr_name(struct syscommon_resman_resource *resource, u_int64_t attr_id); - -const char *syscommon_resman_get_resource_name(struct syscommon_resman_resource *resource); -void *syscommon_resman_get_resource_privdata(struct syscommon_resman_resource *resource); -int syscommon_resman_get_resource_type(struct syscommon_resman_resource *resource); - -int syscommon_resman_set_resource_privdata(struct syscommon_resman_resource *resource, void *priv); +int syscommon_resman_get_resource_attr_int(int resource_id, u_int64_t attr_id, int32_t *data); +int syscommon_resman_get_resource_attr_int64(int resource_id, u_int64_t attr_id, int64_t *data); +int syscommon_resman_get_resource_attr_uint(int resource_id, u_int64_t attr_id, u_int32_t *data); +int syscommon_resman_get_resource_attr_uint64(int resource_id, u_int64_t attr_id, u_int64_t *data); +int syscommon_resman_get_resource_attr_double(int resource_id, u_int64_t attr_id, double *data); +int syscommon_resman_get_resource_attr_string(int resource_id, u_int64_t attr_id, char *data); +int syscommon_resman_get_resource_attr_array(int resource_id, u_int64_t attr_id, struct syscommon_resman_array_value **data); +int syscommon_resman_get_resource_attr_ptr(int resource_id, u_int64_t attr_id, void **data); + +int syscommon_resman_set_resource_attr_interest(int resource_id, u_int64_t interest_mask); +int syscommon_resman_unset_resource_attr_interest(int resource_id, u_int64_t interest_mask); +bool syscommon_resman_is_resource_attr_interested(int resource_id, u_int64_t interest_mask); +const char *syscommon_resman_get_resource_attr_name(int resource_id, u_int64_t attr_id); + +const char *syscommon_resman_get_resource_name(int resource_id); +void *syscommon_resman_get_resource_privdata(int resource_id); +int syscommon_resman_get_resource_type(int resource_id); + +int syscommon_resman_set_resource_privdata(int resource_id, void *priv); void syscommon_resman_init_resource_drivers(void); void syscommon_resman_exit_resource_drivers(void); -- 2.7.4 From 6ecf4c037eba6d7200efbac3dbb9913ee0b78814 Mon Sep 17 00:00:00 2001 From: Dongwoo Lee Date: Thu, 20 Oct 2022 19:14:12 +0900 Subject: [PATCH 06/16] libsyscommon v5.0.0 Changes from v4.1 - Add libsyscommon-resource-manager feature to handle the resource and resource attribute based on ID. - Newly added header files for resource-manager 1. usr/include/libsyscommon/resource-type.h : Contain the supported resource data type and resource flag to indicate the specific characteristic. 2. usr/include/libsyscommon/resource-manager.h : Contain the resource handelr functions such as create/delete resource, update the resource attributes and get value of resource attributes. : Contain the structures for resource driver and attribute 3. usr/include/libsyscommon/resource-device.h : Contain the resource_device structure and function to handle the resource device data. Change-Id: Id43eb66dc1797cf8838497ff0aaa37625ca6fe30 Signed-off-by: Dongwoo Lee Signed-off-by: Chanwoo Choi --- packaging/libsyscommon.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/libsyscommon.spec b/packaging/libsyscommon.spec index c46dab1..3982d61 100644 --- a/packaging/libsyscommon.spec +++ b/packaging/libsyscommon.spec @@ -1,6 +1,6 @@ Name: libsyscommon Summary: System Libraries -Version: 4.1 +Version: 5.0.0 Release: 0%{?release_flags} License: MIT Group: System/Libraries -- 2.7.4 From db7612b3666fa5c4bb36ce6b1dd53f4367611041 Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Tue, 29 Nov 2022 11:30:19 +0900 Subject: [PATCH 07/16] packaging: couple .spec version with .pc version Change-Id: I2ccfce3f0afbfe66b2807858e7b081d7cbf022e5 Signed-off-by: Youngjae Cho --- CMakeLists.txt | 2 +- packaging/libsyscommon.spec | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 482712d..d9d7f07 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,7 @@ SET(PREFIX ${CMAKE_INSTALL_PREFIX}) SET(EXEC_PREFIX "${PREFIX}/bin") SET(LIBDIR ${LIB_INSTALL_DIR}) SET(INCLUDEDIR "${PREFIX}/include") -SET(VERSION 4.1) +SET(VERSION ${FULLVER}) SET(libsyscommon_SRCS src/libgdbus/libgdbus.c diff --git a/packaging/libsyscommon.spec b/packaging/libsyscommon.spec index 3982d61..41bf65c 100644 --- a/packaging/libsyscommon.spec +++ b/packaging/libsyscommon.spec @@ -41,7 +41,7 @@ Development header files for system common library. cp %{SOURCE1001} . %build -%cmake . +%cmake . -DFULLVER=%{version} make %{?_smp_mflags} %install -- 2.7.4 From f4c3c4b201dd7cfcba9ae136685e006369b536b3 Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Tue, 6 Dec 2022 11:33:07 +0900 Subject: [PATCH 08/16] resource-manager: add missing header Change-Id: I3c1d4fb91d0ebbf784075c5b7bb5f6e49a0cc51a Signed-off-by: Youngjae Cho --- src/resource-manager/resource-device.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/resource-manager/resource-device.c b/src/resource-manager/resource-device.c index fccc969..67cbf30 100644 --- a/src/resource-manager/resource-device.c +++ b/src/resource-manager/resource-device.c @@ -14,6 +14,7 @@ * limitations under the License. */ +#include #include #include "resource-device.h" -- 2.7.4 From 119ca93770cd77fbabb67c1534b2f1ced572b12a Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Tue, 6 Dec 2022 11:15:52 +0900 Subject: [PATCH 09/16] libgdbus: add dbus signal for power state change Change-Id: I8b4b22f7bfb5e4d662ee531d5b170ada2f09acdf Signed-off-by: Youngjae Cho --- src/libgdbus/dbus-iface-system.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/libgdbus/dbus-iface-system.h b/src/libgdbus/dbus-iface-system.h index 1a98990..f4d6a8d 100644 --- a/src/libgdbus/dbus-iface-system.h +++ b/src/libgdbus/dbus-iface-system.h @@ -78,6 +78,19 @@ extern "C" { /* Power service: set resetkey disable operations about power */ #define DEVICED_PATH_POWER DEVICED_OBJECT_PATH"/Power" #define DEVICED_INTERFACE_POWER DEVICED_INTERFACE_NAME".Power" +/* Power service : signals on changing power state */ +#define DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_START "PowerChangeStateToStart" +#define DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_NORMAL "PowerChangeStateToNormal" +#define DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_SLEEP "PowerChangeStateToSleep" +#define DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_POWEROFF "PowerChangeStateToPowerOff" +#define DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_REBOOT "PowerChangeStateToReboot" +#define DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_EXIT "PowerChangeStateToExit" +#define DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_RESUMING_EARLY "PowerChangeStateToResumingEarly" +#define DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_RESUMING "PowerChangeStateToResuming" +#define DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_RESUMING_LATE "PowerChangeStateToResumingLate" +#define DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_SUSPENDING_EARLY "PowerChangeStateToSuspendingEarly" +#define DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_SUSPENDING "PowerChangeStateToSuspending" +#define DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_SUSPENDING_LATE "PowerChangeStateToSuspendingLate" /* Storage service: get storage size operatioins about storage */ #define DEVICED_PATH_STORAGE DEVICED_OBJECT_PATH"/Storage" #define DEVICED_INTERFACE_STORAGE DEVICED_INTERFACE_NAME".storage" -- 2.7.4 From ad9493c0b5ae29ebaf483c97e54f3e37f2927152 Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Fri, 6 Jan 2023 12:04:34 +0900 Subject: [PATCH 10/16] resource-manager: introduce sync getter Sync getter fetches data after update. Change-Id: I4610f99f1b4039b985890f77abb827a1174bf5dd Signed-off-by: Youngjae Cho --- src/resource-manager/resource-manager.c | 96 +++++++++++++++++++++++++++++++++ src/resource-manager/resource-manager.h | 8 +++ 2 files changed, 104 insertions(+) diff --git a/src/resource-manager/resource-manager.c b/src/resource-manager/resource-manager.c index fca7743..36ec43f 100644 --- a/src/resource-manager/resource-manager.c +++ b/src/resource-manager/resource-manager.c @@ -793,6 +793,18 @@ syscommon_resman_get_resource_attr_int(int resource_id, u_int64_t attr_id, int32 } int +syscommon_resman_get_resource_attr_int_sync(int resource_id, u_int64_t attr_id, int32_t *data) +{ + int ret; + + ret = update_resource_attr(find_resource(resource_id), attr_id); + if (ret != 0) + return ret; + + return syscommon_resman_get_resource_attr_int(resource_id, attr_id, data); +} + +int syscommon_resman_get_resource_attr_int64(int resource_id, u_int64_t attr_id, int64_t *data) { struct syscommon_resman_resource_attribute_value *attr_value = NULL; @@ -811,6 +823,18 @@ syscommon_resman_get_resource_attr_int64(int resource_id, u_int64_t attr_id, int } int +syscommon_resman_get_resource_attr_int64_sync(int resource_id, u_int64_t attr_id, int64_t *data) +{ + int ret; + + ret = update_resource_attr(find_resource(resource_id), attr_id); + if (ret != 0) + return ret; + + return syscommon_resman_get_resource_attr_int64(resource_id, attr_id, data); +} + +int syscommon_resman_get_resource_attr_uint(int resource_id, u_int64_t attr_id, u_int32_t *data) { struct syscommon_resman_resource_attribute_value *attr_value = NULL; @@ -829,6 +853,18 @@ syscommon_resman_get_resource_attr_uint(int resource_id, u_int64_t attr_id, u_in } int +syscommon_resman_get_resource_attr_uint_sync(int resource_id, u_int64_t attr_id, u_int32_t *data) +{ + int ret; + + ret = update_resource_attr(find_resource(resource_id), attr_id); + if (ret != 0) + return ret; + + return syscommon_resman_get_resource_attr_uint(resource_id, attr_id, data); +} + +int syscommon_resman_get_resource_attr_uint64(int resource_id, u_int64_t attr_id, u_int64_t *data) { struct syscommon_resman_resource_attribute_value *attr_value = NULL; @@ -847,6 +883,18 @@ syscommon_resman_get_resource_attr_uint64(int resource_id, u_int64_t attr_id, u_ } int +syscommon_resman_get_resource_attr_uint64_sync(int resource_id, u_int64_t attr_id, u_int64_t *data) +{ + int ret; + + ret = update_resource_attr(find_resource(resource_id), attr_id); + if (ret != 0) + return ret; + + return syscommon_resman_get_resource_attr_uint64(resource_id, attr_id, data); +} + +int syscommon_resman_get_resource_attr_double(int resource_id, u_int64_t attr_id, double *data) { struct syscommon_resman_resource_attribute_value *attr_value = NULL; @@ -865,6 +913,18 @@ syscommon_resman_get_resource_attr_double(int resource_id, u_int64_t attr_id, do } int +syscommon_resman_get_resource_attr_double_sync(int resource_id, u_int64_t attr_id, double *data) +{ + int ret; + + ret = update_resource_attr(find_resource(resource_id), attr_id); + if (ret != 0) + return ret; + + return syscommon_resman_get_resource_attr_double(resource_id, attr_id, data); +} + +int syscommon_resman_get_resource_attr_string(int resource_id, u_int64_t attr_id, char *data) { struct syscommon_resman_resource_attribute_value *attr_value = NULL; @@ -883,6 +943,18 @@ syscommon_resman_get_resource_attr_string(int resource_id, u_int64_t attr_id, ch } int +syscommon_resman_get_resource_attr_string_sync(int resource_id, u_int64_t attr_id, char *data) +{ + int ret; + + ret = update_resource_attr(find_resource(resource_id), attr_id); + if (ret != 0) + return ret; + + return syscommon_resman_get_resource_attr_string(resource_id, attr_id, data); +} + +int syscommon_resman_get_resource_attr_array(int resource_id, u_int64_t attr_id, struct syscommon_resman_array_value **data) { struct syscommon_resman_resource_attribute_value *attr_value = NULL; @@ -901,6 +973,18 @@ syscommon_resman_get_resource_attr_array(int resource_id, u_int64_t attr_id, str } int +syscommon_resman_get_resource_attr_array_sync(int resource_id, u_int64_t attr_id, struct syscommon_resman_array_value **data) +{ + int ret; + + ret = update_resource_attr(find_resource(resource_id), attr_id); + if (ret != 0) + return ret; + + return syscommon_resman_get_resource_attr_array(resource_id, attr_id, data); +} + +int syscommon_resman_get_resource_attr_ptr(int resource_id, u_int64_t attr_id, void **data) { struct syscommon_resman_resource_attribute_value *attr_value = NULL; @@ -918,6 +1002,18 @@ syscommon_resman_get_resource_attr_ptr(int resource_id, u_int64_t attr_id, void return 0; } +int +syscommon_resman_get_resource_attr_ptr_sync(int resource_id, u_int64_t attr_id, void **data) +{ + int ret; + + ret = update_resource_attr(find_resource(resource_id), attr_id); + if (ret != 0) + return ret; + + return syscommon_resman_get_resource_attr_ptr(resource_id, attr_id, data); +} + static inline bool is_resource_attr_visible(struct syscommon_resman_resource *resource, const struct syscommon_resman_resource_attribute *attr) diff --git a/src/resource-manager/resource-manager.h b/src/resource-manager/resource-manager.h index 4ebf694..e838b8b 100644 --- a/src/resource-manager/resource-manager.h +++ b/src/resource-manager/resource-manager.h @@ -148,13 +148,21 @@ int syscommon_resman_get_resource_attr_json(int resource_id, u_int64_t attr_id, int syscommon_resman_get_resource_list_json(char **json_string); int syscommon_resman_get_resource_attr_int(int resource_id, u_int64_t attr_id, int32_t *data); +int syscommon_resman_get_resource_attr_int_sync(int resource_id, u_int64_t attr_id, int32_t *data); int syscommon_resman_get_resource_attr_int64(int resource_id, u_int64_t attr_id, int64_t *data); +int syscommon_resman_get_resource_attr_int64_sync(int resource_id, u_int64_t attr_id, int64_t *data); int syscommon_resman_get_resource_attr_uint(int resource_id, u_int64_t attr_id, u_int32_t *data); +int syscommon_resman_get_resource_attr_uint_sync(int resource_id, u_int64_t attr_id, u_int32_t *data); int syscommon_resman_get_resource_attr_uint64(int resource_id, u_int64_t attr_id, u_int64_t *data); +int syscommon_resman_get_resource_attr_uint64_sync(int resource_id, u_int64_t attr_id, u_int64_t *data); int syscommon_resman_get_resource_attr_double(int resource_id, u_int64_t attr_id, double *data); +int syscommon_resman_get_resource_attr_double_sync(int resource_id, u_int64_t attr_id, double *data); int syscommon_resman_get_resource_attr_string(int resource_id, u_int64_t attr_id, char *data); +int syscommon_resman_get_resource_attr_string_sync(int resource_id, u_int64_t attr_id, char *data); int syscommon_resman_get_resource_attr_array(int resource_id, u_int64_t attr_id, struct syscommon_resman_array_value **data); +int syscommon_resman_get_resource_attr_array_sync(int resource_id, u_int64_t attr_id, struct syscommon_resman_array_value **data); int syscommon_resman_get_resource_attr_ptr(int resource_id, u_int64_t attr_id, void **data); +int syscommon_resman_get_resource_attr_ptr_sync(int resource_id, u_int64_t attr_id, void **data); int syscommon_resman_set_resource_attr_interest(int resource_id, u_int64_t interest_mask); int syscommon_resman_unset_resource_attr_interest(int resource_id, u_int64_t interest_mask); -- 2.7.4 From 76dc73cf86cc9e591c36ebfab5f24d47c3ea74ac Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Wed, 11 Jan 2023 10:56:27 +0900 Subject: [PATCH 11/16] resource-manager: fix free_resource() Change-Id: I15855cc6ce7eef4859a94fbc0d959d91abdd0c06 Signed-off-by: Youngjae Cho --- src/resource-manager/resource-manager.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/resource-manager/resource-manager.c b/src/resource-manager/resource-manager.c index 36ec43f..629df21 100644 --- a/src/resource-manager/resource-manager.c +++ b/src/resource-manager/resource-manager.c @@ -23,7 +23,6 @@ #include "resource-manager.h" #include "resource-type.h" -#define RESOURCE_ATTR_MASK (ULLONG_MAX) #define BIT64_INDEX(id) (63 - __builtin_clzll(id)) #define RESOURCE_ATTR_INDEX(id) BIT64_INDEX(id) #define RESOURCE_CTRL_INDEX(id) BIT64_INDEX(id) @@ -94,10 +93,13 @@ static int alloc_resource_id(void) static void free_resource(struct syscommon_resman_resource *resource) { - if (!resource->name) - free(resource->name); - if (!resource->attrs_value) - free(resource->attrs_value); + free(resource->name); + resource->name = NULL; + + unset_resource_attr_interest(resource, resource->attr_interest); + free(resource->attrs_value); + resource->attrs_value = NULL; + resource->attrs = NULL; resource->num_attrs = 0; @@ -114,8 +116,6 @@ static void delete_resource(void *data) if (resource->driver && resource->driver->ops.delete) resource->driver->ops.delete(resource->id); - unset_resource_attr_interest(resource, RESOURCE_ATTR_MASK); - free_resource(resource); } -- 2.7.4 From 110a282dba69b3b5aa8b6c509f3e3f22d39b34dc Mon Sep 17 00:00:00 2001 From: Chanwoo Choi Date: Thu, 12 Jan 2023 22:48:30 +0900 Subject: [PATCH 12/16] resource-manager: fix wrong indentation in unset_resource_attr_interest Fix the wrong indentation in unset_resource_attr_interest internal function in order to improve the readability. Change-Id: I07f3d84060d14c0d5cc141d74949900dd9fe11f0 Signed-off-by: Chanwoo Choi --- src/resource-manager/resource-manager.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/resource-manager/resource-manager.c b/src/resource-manager/resource-manager.c index 629df21..b2f2fbf 100644 --- a/src/resource-manager/resource-manager.c +++ b/src/resource-manager/resource-manager.c @@ -1145,8 +1145,7 @@ unset_resource_attr_interest(struct syscommon_resman_resource *resource, u_int64 if (!(resource->attrs[i].id & interest_mask)) continue; - attr_value = get_resource_attr_value(resource, - resource->attrs[i].id); + attr_value = get_resource_attr_value(resource, resource->attrs[i].id); if (!attr_value) return -EINVAL; -- 2.7.4 From 136020b15e97cd6317e6dca85616fcc041639912 Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Mon, 6 Feb 2023 19:05:08 +0900 Subject: [PATCH 13/16] resource-manager: allocate resource id correctly It hasn't cleared sign bit on allocating resource id. Fix it to clear sign bit correctly. Change-Id: Ic429dd26bcd6082ed434aecf98b8a48eb0b52b47 Signed-off-by: Youngjae Cho --- src/resource-manager/resource-manager.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/resource-manager/resource-manager.c b/src/resource-manager/resource-manager.c index b2f2fbf..71d1a9d 100644 --- a/src/resource-manager/resource-manager.c +++ b/src/resource-manager/resource-manager.c @@ -83,8 +83,8 @@ static int alloc_resource_id(void) if (resource_id < 0) resource_id = init_resource_id(); - ret = resource_id++; - clear_sign_bit(resource_id); + ret = resource_id; + resource_id = clear_sign_bit(resource_id + 1); pthread_mutex_unlock(&lock); -- 2.7.4 From 51ac57d6286800ffcc4e658c6928c03c225e151a Mon Sep 17 00:00:00 2001 From: Yunhee Seo Date: Mon, 6 Feb 2023 14:42:42 +0900 Subject: [PATCH 14/16] libgdbus: add dbus signal for power lock state change In this context, power lock state is meaning of "locked or unlocked". Thus, If the power lock state of specific pmlock is changed, this signal will be used. Change-Id: I9db5527bf9668fe54ee9c0c19a2be23b42f91517 Signed-off-by: Yunhee Seo --- src/libgdbus/dbus-iface-system.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libgdbus/dbus-iface-system.h b/src/libgdbus/dbus-iface-system.h index f4d6a8d..f14227d 100644 --- a/src/libgdbus/dbus-iface-system.h +++ b/src/libgdbus/dbus-iface-system.h @@ -91,6 +91,8 @@ extern "C" { #define DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_SUSPENDING_EARLY "PowerChangeStateToSuspendingEarly" #define DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_SUSPENDING "PowerChangeStateToSuspending" #define DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_SUSPENDING_LATE "PowerChangeStateToSuspendingLate" +/* Power service : signals on changing power lock state */ +#define DEVICED_SIGNAL_POWER_LOCK_STATE_CHANGED "PowerLockStateChanged" /* Storage service: get storage size operatioins about storage */ #define DEVICED_PATH_STORAGE DEVICED_OBJECT_PATH"/Storage" #define DEVICED_INTERFACE_STORAGE DEVICED_INTERFACE_NAME".storage" -- 2.7.4 From 77dc6e9f73dffc1638c6d018c16843826cfc961c Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Thu, 9 Feb 2023 13:39:57 +0900 Subject: [PATCH 15/16] resource-manager: revamp init/exit of resource driver Change-Id: I24bf3bd1b93bbe53c170522bc039b082191989de Signed-off-by: Youngjae Cho --- src/resource-manager/resource-manager.c | 39 +++++++++++++++++---------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/src/resource-manager/resource-manager.c b/src/resource-manager/resource-manager.c index 71d1a9d..7167ca2 100644 --- a/src/resource-manager/resource-manager.c +++ b/src/resource-manager/resource-manager.c @@ -1260,31 +1260,32 @@ syscommon_resman_set_resource_privdata(int resource_id, void *priv) return 0; } -void syscommon_resman_init_resource_drivers(void) +static void init_resource_driver(gpointer data, gpointer udata) { - struct syscommon_resman_resource_driver *driver; - int i, ret = 0; - - for (i = 0; i < g_list_length(g_resource_driver_head); i++) { - driver = g_list_nth(g_list_first(g_resource_driver_head), i)->data; + struct syscommon_resman_resource_driver *driver = (struct syscommon_resman_resource_driver *) data; + int ret = 0; - if (driver->ops.init) { - ret = driver->ops.init(); - if (ret < 0) - syscommon_resman_remove_resource_driver(driver); - } + if (driver && driver->ops.init) { + ret = driver->ops.init(); + if (ret < 0) + syscommon_resman_remove_resource_driver(driver); } } -void syscommon_resman_exit_resource_drivers(void) +static void exit_resource_driver(gpointer data, gpointer udata) { - const struct syscommon_resman_resource_driver *driver; - int i; + struct syscommon_resman_resource_driver *driver = (struct syscommon_resman_resource_driver *) data; - for (i = 0; i < g_list_length(g_resource_driver_head); i++) { - driver = g_list_nth(g_list_first(g_resource_driver_head), i)->data; + if (driver && driver->ops.exit) + driver->ops.exit(); +} - if (driver->ops.exit) - driver->ops.exit(); - } +void syscommon_resman_init_resource_drivers(void) +{ + g_list_foreach(g_resource_driver_head, init_resource_driver, NULL); +} + +void syscommon_resman_exit_resource_drivers(void) +{ + g_list_foreach(g_resource_driver_head, exit_resource_driver, NULL); } -- 2.7.4 From e0026da02c35c8c39111f89b68d4b77836b7b99d Mon Sep 17 00:00:00 2001 From: Dongwoo Lee Date: Thu, 12 Jan 2023 10:01:22 +0900 Subject: [PATCH 16/16] resource-manager: Introduce attribute listener operations Now each attribute can have 'listener_ops' which consists 'init', 'exit', and 'action', to watch changes for underlying state. Each operation has a role as below: - init: allocate resources and register listeners - exit: deallocate resources and unregister listeners - action: change attribute values as following listener parameters 'init'/'exit' is called automatically when each attribute interest is set/unset each, on the other hand, 'action' should be called by each listener. Change-Id: I076f41f751b18f4c39d7548e4d78ddc02e8e26a2 Signed-off-by: Dongwoo Lee --- CMakeLists.txt | 1 + src/resource-manager/resource-listener.h | 28 ++++++++++++++++++++++++++++ src/resource-manager/resource-manager.c | 12 ++++++++++++ src/resource-manager/resource-manager.h | 10 ++++++++++ 4 files changed, 51 insertions(+) create mode 100644 src/resource-manager/resource-listener.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d9d7f07..50dabeb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,6 +31,7 @@ SET(HEADERS src/resource-manager/resource-manager.h src/resource-manager/resource-type.h src/resource-manager/resource-device.h + src/resource-manager/resource-listener.h ) # CHECK PKG diff --git a/src/resource-manager/resource-listener.h b/src/resource-manager/resource-listener.h new file mode 100644 index 0000000..980cf00 --- /dev/null +++ b/src/resource-manager/resource-listener.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Samsung Electronics Co., Ltd. + * + * 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. + */ + +#ifndef __RESOURCE_LISTENER_H__ +#define __RESOURCE_LISTENER_H__ + +enum syscommon_resman_listener_type { + SYSCOMMON_RESMAN_LISTENER_TYPE_UNKNOWN, + SYSCOMMON_RESMAN_LISTENER_TYPE_EPOLL, + SYSCOMMON_RESMAN_LISTENER_TYPE_UEVENT, + SYSCOMMON_RESMAN_LISTENER_TYPE_TIMER, + SYSCOMMON_RESMAN_LISTENER_TYPE_MAX, +}; + +#endif diff --git a/src/resource-manager/resource-manager.c b/src/resource-manager/resource-manager.c index 7167ca2..92d87ee 100644 --- a/src/resource-manager/resource-manager.c +++ b/src/resource-manager/resource-manager.c @@ -1066,6 +1066,12 @@ syscommon_resman_set_resource_attr_interest(int resource_id, u_int64_t interest_ goto err; } + if (resource->attrs[i].listener_ops.init) { + ret = resource->attrs[i].listener_ops.init(resource_id, &resource->attrs[i]); + if (ret < 0) + goto err; + } + /* * In resource monitor, each resource has a lot of attributes, but * only updated attributes are selected by clients on demand. So, @@ -1116,6 +1122,9 @@ err: if (!(resource->attrs[i].id & interest_mask)) continue; + if (resource->attrs[i].listener_ops.exit) + resource->attrs[i].listener_ops.exit(resource_id, &resource->attrs[i]); + attr_value = get_resource_attr_value(resource, resource->attrs[i].id); if (!attr_value) continue; @@ -1145,6 +1154,9 @@ unset_resource_attr_interest(struct syscommon_resman_resource *resource, u_int64 if (!(resource->attrs[i].id & interest_mask)) continue; + if (resource->attrs[i].listener_ops.exit) + resource->attrs[i].listener_ops.exit(resource->id, &resource->attrs[i]); + attr_value = get_resource_attr_value(resource, resource->attrs[i].id); if (!attr_value) return -EINVAL; diff --git a/src/resource-manager/resource-manager.h b/src/resource-manager/resource-manager.h index e838b8b..0f2ea53 100644 --- a/src/resource-manager/resource-manager.h +++ b/src/resource-manager/resource-manager.h @@ -26,6 +26,8 @@ #define SYSCOMMON_RESMAN_BUFF_MAX 255 +enum syscommon_resman_listener_type; + struct syscommon_resman_resource_attribute; struct syscommon_resman_resource_control; @@ -55,12 +57,20 @@ struct syscommon_resman_resource_attribute_ops { const struct syscommon_resman_resource_attribute *attr); }; +struct syscommon_resman_resource_attribute_listener_ops { + int (*init)(int resource_id, const struct syscommon_resman_resource_attribute *attr); + void (*exit)(int resource_id, const struct syscommon_resman_resource_attribute *attr); + void (*action)(int resource_id, const struct syscommon_resman_resource_attribute *attr, + void *param, enum syscommon_resman_listener_type listener_type); +}; + struct syscommon_resman_resource_attribute { const char name[SYSCOMMON_RESMAN_BUFF_MAX]; const u_int64_t id; const int type; const u_int64_t flag; const struct syscommon_resman_resource_attribute_ops ops; + const struct syscommon_resman_resource_attribute_listener_ops listener_ops; }; struct syscommon_resman_resource_control_ops { -- 2.7.4