diff options
author | Ian Campbell <ijc@debian.org> | 2018-05-27 11:22:20 +0100 |
---|---|---|
committer | Ian Campbell <ijc@debian.org> | 2018-05-27 11:22:20 +0100 |
commit | 51386957dab312a638e442be30b6511ca831b8eb (patch) | |
tree | c9a59111fc18096fb34fb0799f062dc23ef717e2 |
Import qcontrol_0.5.6.orig.tar.xz
[dgit import orig qcontrol_0.5.6.orig.tar.xz]
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | COPYING | 674 | ||||
-rw-r--r-- | HACKING | 45 | ||||
-rw-r--r-- | Makefile | 47 | ||||
-rw-r--r-- | README | 18 | ||||
-rw-r--r-- | a125.c | 378 | ||||
-rw-r--r-- | evdev.c | 131 | ||||
-rw-r--r-- | examples/mv2120.lua | 60 | ||||
-rw-r--r-- | examples/synology.lua | 45 | ||||
-rw-r--r-- | examples/ts209.lua | 124 | ||||
-rw-r--r-- | examples/ts219.lua | 194 | ||||
-rw-r--r-- | examples/ts409.lua | 181 | ||||
-rw-r--r-- | examples/ts41x.lua | 248 | ||||
-rw-r--r-- | picmodule.h | 43 | ||||
-rw-r--r-- | qcontrol.c | 770 | ||||
-rw-r--r-- | qnap-pic.c | 393 | ||||
-rw-r--r-- | qnap-pic.h | 115 | ||||
-rw-r--r-- | synology.c | 332 | ||||
-rw-r--r-- | synology.h | 52 | ||||
-rw-r--r-- | system.c | 49 | ||||
-rw-r--r-- | systemd/qcontrol.service | 19 | ||||
-rw-r--r-- | systemd/qcontrold.service | 12 | ||||
-rw-r--r-- | systemd/qcontrold.socket | 5 | ||||
-rw-r--r-- | ts209.c | 98 | ||||
-rw-r--r-- | ts219.c | 111 | ||||
-rw-r--r-- | ts409.c | 105 | ||||
-rw-r--r-- | ts41x.c | 111 |
27 files changed, 4362 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d75f8ae --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.o +qcontrol @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/> + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + <one line to give the program's name and a brief idea of what it does.> + Copyright (C) <year> <name of author> + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + <program> Copyright (C) <year> <name of author> + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +<http://www.gnu.org/licenses/>. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +<http://www.gnu.org/philosophy/why-not-lgpl.html>. @@ -0,0 +1,45 @@ +Remote Protocol +=============== + +When run in daemon mode qcontrol listens on an AF_UNIX socket located at +/var/run/qcontrol.sock. By default (without --direct) qcontrol will act as a +client to this server. + +There are two basic data types: + +INTEGER: An unsigned 32-bit value, in host byte order. + +STRING: An INTEGER length (including the NUL terminator) followed by + exactly that number of bytes. The NUL is included. + +The protocol is initiated by the client. The client sends a request and then +waits for a response. The request and response are expected to be packetized +(that is, that read() will return ether a single complete message or nothing). + +Request: + + C->S INTEGER $argc (== n) + C->S STRING $argv[0] + C->S ... + C->S STRING $argv[n-1] + + The request consists of a C style $argc+$argv[], that is an argument + count followed by the given number of strings. + +Response: + + C<-S INTEGER $result + C<-S OPTIONAL STRING $message + + The response consists of an INTEGER $result and an optional $message. + + If $result < 0 then an error has occured and $message will be present + and contains the help string for the attempted command. + + If $result == 0 then if the total length of the response is exactly + 32-bits then this indicates success. If the length of the response is + longer than 32-bits then the remainder is a single STRING $message + containing a list of available commands. + + Otherwise $result > 0 indicates success. + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1034036 --- /dev/null +++ b/Makefile @@ -0,0 +1,47 @@ +VERSION=0.5.6 + +CFLAGS += -c -g -Os -Wall -Wextra +CPPFLAGS += -DQCONTROL_VERSION=\"$(VERSION)\" + +LDFLAGS += -g +LIBS += -lpthread +LIBS_STATIC += /usr/lib/liblua5.1.a -lpthread -lm -ldl + +CFLAGS += $(shell pkg-config --cflags lua5.1) +LIBS += $(shell pkg-config --libs lua5.1) + +ifeq ($(shell pkg-config --exists libsystemd-daemon 2>/dev/null && echo 1),1) +CPPFLAGS += -DHAVE_SYSTEMD +CFLAGS += $(shell pkg-config --cflags libsystemd-daemon) +LIBS += $(shell pkg-config --libs libsystemd-daemon) +endif + +SOURCES=qcontrol.c system.c qnap-pic.c ts209.c ts219.c ts409.c ts41x.c evdev.c a125.c synology.c +OBJECTS=$(SOURCES:.c=.o) +EXECUTABLE=qcontrol + +all: $(SOURCES) $(EXECUTABLE) + +$(EXECUTABLE): $(OBJECTS) + $(CC) $(LDFLAGS) $(OBJECTS) $(LIBS) -o $@ + +$(EXECUTABLE)-static: $(OBJECTS) + $(CC) $(LDFLAGS) $(OBJECTS) $(LIBS_STATIC) -o $@ + +.c.o: + $(CC) $(CPPFLAGS) $(CFLAGS) $< -o $@ + +clean: + rm -f $(OBJECTS) $(EXECUTABLE) $(EXECUTABLE)-static + +dist: RELEASES := $(PWD)/../releases/ +dist: TARBALL := qcontrol-$(VERSION).tar +dist: TAG := v$(VERSION) +dist: + git tag -v $(TAG) + git archive --format=tar --prefix=qcontrol-$(VERSION)/ --output $(RELEASES)/$(TARBALL) $(TAG) + cat $(RELEASES)/$(TARBALL) | gzip -c9 > $(RELEASES)/$(TARBALL).gz + xz --stdout --compress $(RELEASES)/$(TARBALL) > $(RELEASES)/$(TARBALL).xz + cd $(RELEASES) && sha256sum $(TARBALL) $(TARBALL).gz $(TARBALL).xz > $(TARBALL).sha256 + +$(OBJECTS): picmodule.h @@ -0,0 +1,18 @@ +Qcontrol +======== + +Qcontrol is a daemon and command line tool which controls the various +peripherals that are present on many embedded NAS devices. It supports any +peripheral which is driven by either serial port of GPIO, including +lights, fans, LCDs and watchdogs. Although it primarily supports QNAP TS +devices it is designed to be modular and adding support for other devices +should be easy. + +Supported devices: + - QNAP TS-109, TS-109 II, TS-209 and TS-209 II (ts209) + - QNAP TS-409 and TS-409U (ts409) + - QNAP TS-11x, TS-12x, HS-210, TS-21x and TS-22x (ts219) + - QNAP TS-41x and TS-42x (ts41x) + - Synology Diskstation and Rackstation (synology) + +Homepage: https://www.hellion.org.uk/qcontrol/ @@ -0,0 +1,378 @@ +/* + * Copyright (C) 2007-2008 Byron Bradley (byron.bbradley@gmail.com) + * Copyright (C) 2008, 2009 Martin Michlmayr (tbm@cyrius.com) + * Copyright (C) 2009 Bernhard R. Link (brlink@debian.org) + * Copyright (C) 2013 Ian Campbell (ijc@hellion.org.uk) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <errno.h> +#include <fcntl.h> +#include <lua.h> +#include <lualib.h> +#include <lauxlib.h> +#include <poll.h> +#include <pthread.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <syslog.h> +#include <termios.h> +#include <unistd.h> +#include <linux/input.h> + +#include "picmodule.h" + +static int serial; +static struct termios oldtio, newtio; +static pthread_t a125_thread; +static int a125_detected = 0; +static unsigned int button_state = 0; + +static int serial_read(unsigned char *buf, int len) +{ + int err, got = 0; + static int empty_count = 0; + + while (len > 0) { + errno = 0; + err = read(serial, buf, len); + if (err >= 0) { + if (err > len) + err = len; + got += err; + buf[err] = 0; + buf += err; + len -= err; + empty_count = 0; + } else if (errno == EAGAIN) { + struct pollfd p = { + .fd = serial, + .events = POLLIN, + .revents = 0 + }; + + empty_count++; + if (empty_count == 5) + print_log(LOG_WARNING, +"Contradicting information about data available to be read from A125 LCD.\n" +"Please make sure nothing else is reading things there."); + /* as we are doing non-blocking read, + wait till something is there. + (If we did not care to detect something eating our + input, we could just do blocking read, though) */ + do { + err = poll(&p, 1, -1); + } while (err < 0 && errno == EAGAIN); + continue; + } else { + print_log(LOG_ERR, "Error reading from A125: %s", + strerror(errno)); + return err; + } + if (err == 0) { + print_log(LOG_ERR, "EOF from A125????"); + a125_detected = -2; + return -1; + } + } + return got; +} + +static int serial_write(unsigned char *buf, int len) +{ + int err; + + err = write(serial, buf, len); + + return err; +} + +static int a125_read_serial_events(void) +{ + unsigned char buf[101]; + unsigned int state; + int err = serial_read(buf, 1); + if (err <= 0) + return err; + if (buf[0] != 0x53) { + if (a125_detected == 0) { + print_log(LOG_ERR, +"Unknown command 0x%x from A125!\n" +"Disabling reading to avoid disrupting another device!\n" +"LCD buttons will not work!\n", buf[0]); + a125_detected = -1; + return -1; + } + print_log(LOG_ERR, +"Unknown command 0x%x from A125! stream out of sync\n", buf[0]); + return -1; + } + err = serial_read(buf, 1); + if (err <= 0) + return err; + switch ((unsigned char)buf[0]) { + case 0x01: + err = serial_read(buf, 2); + if (err < 0) + return -1; + print_log(LOG_DEBUG, "A125 ID is %04x\n", buf[0]*256+buf[1]); + break; + case 0x05: + err = serial_read(buf, 2); + if (err < 0) + return -1; + state = buf[0]*256+buf[1]; + if (a125_detected == 0) { + a125_detected = 1; + } else { + unsigned int down, up; + /* newly pressed buttons */ + down = state & ~button_state; + /* newly released buttons */ + up = button_state & ~state; + + call_function("lcd_button", "%d%d%d", state, down, up); + } + button_state = state; + break; + case 0x08: + err = serial_read(buf, 2); + if (err < 0) + return -1; + print_log(LOG_DEBUG, "A125 Protocol version is %04x\n", buf[0]*256+buf[1]); + break; + case 0xAA: + print_log(LOG_DEBUG, "A125 Reset OK\n"); + break; + case 0xFB: + err = serial_read(buf, 1); + if (err < 0) + return -1; + print_log(LOG_NOTICE, "A125 NACKs command %x\n", buf[0]); + break; + default: + print_log(LOG_NOTICE, "Unknown message 0x%02x from A125!", buf[0]); + } + + return -1; +} + +static void *serial_poll(void *tmp UNUSED) +{ + int err; + fd_set rset; + + FD_ZERO(&rset); + FD_SET(serial, &rset); + + for (;;) { + err = select(serial + 1, &rset, NULL, NULL, NULL); + if (err <= 0) { + FD_SET(serial, &rset); + continue; + } + a125_read_serial_events(); + if (a125_detected < 0) + break; + FD_SET(serial, &rset); + } + + return NULL; +} + +static int set_nonblock(int fd) +{ + int flags = fcntl(fd, F_GETFL, 0); + if (flags < 0) + flags = 0; + return fcntl(fd, F_SETFL, flags | O_NONBLOCK); +} + +static int serial_open(const char *device) +{ + unsigned char buf[2] = { 0x4D, 0x06 }; + int err; + + serial = open(device , O_RDWR); + if (serial < 0) { + print_log(LOG_ERR, "a125: Error opening '%s': %s", + device, strerror(errno)); + return -1; + } + err = set_nonblock(serial); + if (err < 0) { + print_log(LOG_ERR, "a125: Error setting nonblock: %s", + strerror(errno)); + return -1; + } + tcgetattr(serial, &oldtio); + memset(&newtio, 0, sizeof(newtio)); + + newtio.c_iflag |= IGNBRK; + newtio.c_lflag &= ~(ISIG | ICANON | ECHO); + newtio.c_cflag = B1200 | CS8 | CLOCAL | CREAD; + newtio.c_cc[VMIN] = 1; + newtio.c_cc[VTIME] = 0; + cfsetospeed(&newtio, B1200); + cfsetispeed(&newtio, B1200); + + err = tcsetattr(serial, TCSAFLUSH, &newtio); + if (err < 0) { + print_log(LOG_ERR, "Failed to set attributes for %s: %s", + device, strerror(errno)); + return -1; + } + /* tell LCD to get the current button state. + This also tests if there is a LCD, to avoid fighting with a serial + console. */ + a125_detected = 0; + err = serial_write(buf, 2); + if (err < 0) { + print_log(LOG_ERR, "Error sending commands to A125 LCD at %s: %s", + device, strerror(errno)); + return -1; + } + return 0; +} + +static void serial_close(void) +{ + tcsetattr(serial, TCSANOW, &oldtio); + close(serial); +} + +static int a125_backlight(int argc, const char **argv) +{ + unsigned char code[3] = { 0x4D, 0x5E, 0x00 }; + + if (argc != 1) + return -1; + + if (strcmp(argv[0], "on") == 0) + code[2] = 0x01; + else if (strcmp(argv[0], "off") == 0) + code[2] = 0x00; + else + return -1; + + return serial_write(code, 3); +} + +static int a125_line(int id, const char *line) +{ + size_t l; + unsigned char code[20] = { 0x4D, 0x0C, 0x00, 0x10, + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}; + + code[2] = id; + + l = strlen(line); + if (l > 16) + l = 16; + memcpy(code + 4, line, l); + + return serial_write(code, 20); +} + + +static int a125_line0(int argc, const char **argv) +{ + if (argc < 0 || argc > 1) + return -1; + + return a125_line(0, (argc > 0) ? argv[0] : ""); +} + +static int a125_line1(int argc, const char **argv) +{ + if (argc < 0 || argc > 1) + return -1; + + return a125_line(1, (argc > 0) ? argv[0] : ""); +} + +static int a125_reset(int argc, const char **argv UNUSED) +{ + unsigned char code[3] = { 0x4D, 0xFF }; + + if (argc != 0) + return -1; + + return serial_write(code, 2); +} + +static int a125_clear(int argc, const char **argv UNUSED) +{ + unsigned char code[3] = { 0x4D, 0x0D }; + + if (argc != 0) + return -1; + + return serial_write(code, 2); +} + +static int a125_init(int argc, const char **argv) +{ + int err; + const char *devicename; + + if (argc > 1) { + print_log(LOG_ERR, "a125: takes at most one argument"); + return -1; + } + if (argc > 0 ) + devicename = argv[0]; + else + devicename = "/dev/ttyS0"; + err = serial_open(devicename); + if (err < 0) + return err; + + err = register_command("lcd-reset", + "Reset the LCD", + "Reset the LCD\n", + a125_reset); + err = register_command("lcd-backlight", + "Set the LCD backlight", + "Set the LCD backlight, options are:\n" + "\ton\n\toff\n", + a125_backlight); + err = register_command("lcd-clear", + "Clear the LCD", + "Clean the LCD\n", + a125_clear); + err = register_command("lcd-line0", + "Set LCD line 0", + "Set LCD line 0", + a125_line0); + err = register_command("lcd-line1", + "Set LCD line 1", + "Set LCD line 1", + a125_line1); + return pthread_create(&a125_thread, NULL, serial_poll, NULL); +} + +static void a125_exit(void) +{ + serial_close(); +} + +struct picmodule a125_module = { + .name = "a125", + .init = a125_init, + .exit = a125_exit, +}; @@ -0,0 +1,131 @@ +/* + * Copyright (C) 2008 Byron Bradley (byron.bbradley@gmail.com) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <errno.h> +#include <fcntl.h> +#include <lua.h> +#include <lualib.h> +#include <lauxlib.h> +#include <pthread.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/signal.h> +#include <syslog.h> +#include <termios.h> +#include <unistd.h> +#include <linux/input.h> + +#include "picmodule.h" + +struct event_listen { + int code; + char *command; + time_t pressed; +}; +static struct event_listen **events; +static pthread_t evdev_thread; +static int event; + +static int evdev_event(void) +{ + struct input_event ie; + char buf[100]; + int err, i, down; + + err = read(event, buf, 100); + if (err < 0) + return err; + + memcpy(&ie, buf, sizeof(struct input_event)); + + for (i = 0; events[i]; ++i) { + if (ie.code == events[i]->code) { + if (ie.value == 1) { + events[i]->pressed = time(NULL); + } else { + down = time(NULL) - events[i]->pressed; + call_function(events[i]->command, "%d", down); + } + break; + } + } + + if (!events[i]) + print_log(LOG_WARNING, "evdev: Unknown event %d", ie.code); + + return 0; +} + +static void *evdev_poll(void *tmp UNUSED) +{ + for (;;) + evdev_event(); + + return NULL; +} + +static int evdev_init(int argc, const char **argv) +{ + int i, evcount; + struct event_listen *el; + + evcount = (argc - 1) / 2; + if (evcount < 1 || (argc - 1) % 2 != 0) { + print_log(LOG_ERR, "evdev: got an uneven number of arguments"); + return -1; + } + + events = calloc(evcount + 1, sizeof(struct event_listen*)); + for (i = 1; i < argc; i += 2) { + el = malloc(sizeof(struct event_listen)); + el->code = atoi(argv[i]); + el->command = malloc(strlen(argv[i+1]) + 1); + strncpy(el->command, argv[i+1], strlen(argv[i+1]) + 1); + events[(i-1)/2] = el; + } + + event = open(argv[0], 0); + if (event < 0) { + print_log(LOG_ERR, "evdev: Error opening %s: %s", + argv[0], strerror(errno)); + for (i = 0; events[i]; ++i) + free(events[i]); + free(events); + return -1; + } + + return pthread_create(&evdev_thread, NULL, evdev_poll, NULL); +} + +static void evdev_exit(void) +{ + int i; + + pthread_kill(evdev_thread, SIGINT); + close(event); + + for (i = 0; events[i]; ++i) + free(events[i]); + free(events); +} + +struct picmodule evdev_module = { + .name = "evdev", + .init = evdev_init, + .exit = evdev_exit, +}; diff --git a/examples/mv2120.lua b/examples/mv2120.lua new file mode 100644 index 0000000..4bcc911 --- /dev/null +++ b/examples/mv2120.lua @@ -0,0 +1,60 @@ +--[[ + Configuration file for qcontrol (LUA syntax) + Supports the HP Media Vault mv2120. +--]] + +register("system-status") + +function system_status( status ) + logprint("System status: "..status) + if status == "start" then + -- Nothing... + elseif status == "stop" then + -- Nothing + else + logprint("Unknown system status") + end +end + +-- Requires CONFIG_KEYBOARD_GPIO enabled in the kernel and +-- the kernel module gpio_keys to be loaded. + +-- Different kernel versions use platform-gpio_keys-event or +-- platform-gpio-keys-event, find the right one. + +function find_device( options ) + for index,option in ipairs(options) do + local f=io.open(option) + if f then + f:close() + return option + end + end + return nil +end + +evdev = find_device ( { "/dev/input/by-path/platform-gpio_keys-event", + "/dev/input/by-path/platform-gpio-keys-event" } ) +if evdev then + logprint("Register evdev on "..evdev) + register("evdev", evdev, + 116, "power_button", + 408, "restart_button") +else + logprint("No evdev device found") +end + +function power_button( time ) + os.execute("poweroff") +end + +function restart_button( time ) + os.execute("reboot") +end + +confdir("/etc/qcontrol.d") +-- +-- Local variables: +-- mode: lua +-- indent-level: 8 +-- End: diff --git a/examples/synology.lua b/examples/synology.lua new file mode 100644 index 0000000..3181f18 --- /dev/null +++ b/examples/synology.lua @@ -0,0 +1,45 @@ +--[[ + Configuration file for qcontrol (LUA syntax) + Supports Synology DiskStation and RackStation NASes. +--]] + +register("synology") + +register("system-status") + +-- Set to "false" to suppress the sounding of the buzzer +buzzer = true + +function system_status( status ) + logprint("System status: "..status) + if status == "start" then + piccmd("statusled", "greenon") + piccmd("powerled", "on") + if buzzer then piccmd("buzzer", "short") end + elseif status == "stop" then + piccmd("statusled", "redon") + piccmd("powerled", "2hz") + if buzzer then piccmd("buzzer", "short") end + else + logprint("Unknown system status") + end +end + +function power_button( time ) + os.execute("poweroff") +end + +function restart_button( time ) + os.execute("reboot") +end + +function media_button( time ) + piccmd("usbled", "2hz") +end + +confdir("/etc/qcontrol.d") +-- +-- Local variables: +-- mode: lua +-- indent-level: 8 +-- End: diff --git a/examples/ts209.lua b/examples/ts209.lua new file mode 100644 index 0000000..51991c8 --- /dev/null +++ b/examples/ts209.lua @@ -0,0 +1,124 @@ +--[[ + Configuration file for qcontrol (LUA syntax) + Supports both QNAP TS-109 and TS-209. +--]] + +register("ts209") + +-- Requires CONFIG_KEYBOARD_GPIO enabled in the kernel and +-- the kernel module gpio_keys to be loaded. + +-- Different kernel versions use platform-gpio_keys-event or +-- platform-gpio-keys-event, find the right one. + +function find_device( options ) + for index,option in ipairs(options) do + local f=io.open(option) + if f then + f:close() + return option + end + end + return nil +end + +evdev = find_device ( { "/dev/input/by-path/platform-gpio_keys-event", + "/dev/input/by-path/platform-gpio-keys-event" } ) +if evdev then + logprint("Register evdev on "..evdev) + register("evdev", evdev, + 408, "restart_button", + 133, "media_button") +else + logprint("No evdev device found") +end + +register("system-status") + +-- Set to "false" to suppress the sounding of the buzzer +buzzer = true +-- Set to "false" if your device doesn't have a fan (TS-109 and TS-109 II) +has_fan = true + +-- Turn off fan if there is no fan to avoid fan_error() being called +if not has_fan then + piccmd("fanspeed", "stop") +end + +function system_status( status ) + logprint("System status: "..status) + if status == "start" then + piccmd("statusled", "greenon") + piccmd("powerled", "on") + if buzzer then piccmd("buzzer", "short") end + elseif status == "stop" then + piccmd("statusled", "redon") + piccmd("powerled", "1hz") + if buzzer then piccmd("buzzer", "short") end + else + logprint("Unknown system status") + end +end + +function power_button( time ) + os.execute("poweroff") +end + +function restart_button( time ) + os.execute("reboot") +end + +function media_button( time ) + piccmd("usbled", "8hz") +end + +fanfail = 0 + +function fan_error( ) + fanfail = fanfail + 1 + if fanfail == 3 then + logprint("ts209: fan error") + piccmd("statusled", "red2hz") + piccmd("buzzer", "long") + else + if fanfail == 10 then + fanfail = 0 + end + end +end + +function fan_normal( ) + piccmd("statusled", "greenon") + fanfail = 0 +end + +last_fan_setting = nil + +function setfan( speed ) + -- Do nothing if there's no fan + if not has_fan then + return + end + + if ( ( not last_fan_setting ) or + ( last_fan_setting ~= speed ) ) then + logprint(string.format("ts209: setting fan to \"%s\"", speed)) + end + piccmd("fanspeed", speed) + last_fan_setting = speed +end + +function temp_low( ) + setfan("silence") +end + +function temp_high( ) + setfan("full") +end + +confdir("/etc/qcontrol.d") +-- +-- Local variables: +-- mode: lua +-- indent-level: 8 +-- End: diff --git a/examples/ts219.lua b/examples/ts219.lua new file mode 100644 index 0000000..97d9b8f --- /dev/null +++ b/examples/ts219.lua @@ -0,0 +1,194 @@ +--[[ + Configuration file for qcontrol (LUA syntax) + Supports QNAP TS-110, TS-119, TS-210, TS-219 and TS-219P. +--]] + +register("ts219") + +-- Requires CONFIG_KEYBOARD_GPIO enabled in the kernel and +-- the kernel module gpio_keys to be loaded. + +-- Different kernel versions use platform-gpio_keys-event or +-- platform-gpio-keys-event, find the right one. + +function find_device( options ) + for index,option in ipairs(options) do + local f=io.open(option) + if f then + f:close() + return option + end + end + return nil +end + +evdev = find_device ( { "/dev/input/by-path/platform-gpio_keys-event", + "/dev/input/by-path/platform-gpio-keys-event" } ) +if evdev then + logprint("Register evdev on "..evdev) + register("evdev", evdev, + 408, "restart_button", + 133, "media_button") +else + logprint("No evdev device found") +end + +register("system-status") + +-- Set to "false" to suppress the sounding of the buzzer +buzzer = true + +-- Set to "false" if your device doesn't have a fan (TS-119 and HS-210) +has_fan = true + +-- Turn off fan if there is no fan to avoid fan_error() being called +if not has_fan then + piccmd("fanspeed", "stop") +end + +function system_status( status ) + logprint("System status: "..status) + if status == "start" then + piccmd("statusled", "greenon") + piccmd("powerled", "on") + if buzzer then piccmd("buzzer", "short") end + elseif status == "stop" then + piccmd("statusled", "redon") + piccmd("powerled", "1hz") + if buzzer then piccmd("buzzer", "short") end + else + logprint("Unknown system status") + end +end + +function power_button( time ) + os.execute("poweroff") +end + +function restart_button( time ) + os.execute("reboot") +end + +function media_button( time ) + piccmd("usbled", "8hz") +end + +fanfail = 0 + +function fan_error( ) + fanfail = fanfail + 1 + if fanfail == 3 then + logprint("ts219: fan error") + piccmd("statusled", "red2hz") + piccmd("buzzer", "long") + else + if fanfail == 10 then + fanfail = 0 + end + end +end + +function fan_normal( ) + piccmd("statusled", "greenon") + fanfail = 0 +end + +last_temp_log = nil +last_temp_value = 0 + +function logtemp( temp ) + local now = os.time() + + local function should_log( ) + local delta_temp = math.abs(temp - last_temp_value) + local delta_time = os.difftime(now, last_temp_log) + + -- Haven't previously logged, log now for the first time + if ( not last_temp_log ) then + return true + end + -- Temperature has changed by more than 5 + if ( delta_temp >= 5 ) then + return true + end + -- More than 5 minutes have elapsed... + if ( delta_time >= 300 ) then + if ( delta_temp > 1 ) then + --- ...and the change is by more than +/-1 + return true + else + --- ...insignificant change, wait another 5 minutes + last_temp_log = now + return false + end + end + -- Otherwise no need to log + return false + end + + if ( should_log() ) then + logprint(string.format("ts219: temperature %d", temp)) + last_temp_log = now + last_temp_value = temp + end +end + +last_fan_setting = nil + +function setfan( temp, speed ) + -- Do nothing if there's no fan + if not has_fan then + return + end + + if ( ( not last_fan_setting ) or + ( last_fan_setting ~= speed ) ) then + logprint(string.format("ts219: temperature %d setting fan to \"%s\"", temp, speed)) + end + piccmd("fanspeed", speed) + last_fan_setting = speed +end + +-- hysteresis implementation: +-- - - > 35 > - - - > 40 > - - - > 50 > - - - > 65 > - - - +-- silence -- low -- medium -- high -- full | +-- - - < 32 < - - - < 35 < - - - < 45 < - - - < 55 < - - - +function temp( temp ) + logtemp(temp) + if last_fan_setting == "full" then + if temp < 55 then + setfan(temp, "high") + end + elseif last_fan_setting == "high" then + if temp > 65 then + setfan(temp, "full") + elseif temp < 45 then + setfan(temp, "medium") + end + elseif last_fan_setting == "medium" then + if temp > 50 then + setfan(temp, "high") + elseif temp < 35 then + setfan(temp, "low") + end + elseif last_fan_setting == "low" then + if temp > 40 then + setfan(temp, "medium") + elseif temp < 32 then + setfan(temp, "silence") + end + elseif last_fan_setting == "silence" then + if temp > 35 then + setfan(temp, "low") + end + else + setfan(temp, "high") + end +end + +confdir("/etc/qcontrol.d") +-- +-- Local variables: +-- mode: lua +-- indent-level: 8 +-- End: diff --git a/examples/ts409.lua b/examples/ts409.lua new file mode 100644 index 0000000..fb4f4cb --- /dev/null +++ b/examples/ts409.lua @@ -0,0 +1,181 @@ +--[[ + Configuration file for qcontrol (LUA syntax) + Supports the QNAP TS-409. +--]] + +register("ts409") + +-- Requires CONFIG_KEYBOARD_GPIO enabled in the kernel and +-- the kernel module gpio_keys to be loaded. + +-- Different kernel versions use platform-gpio_keys-event or +-- platform-gpio-keys-event, find the right one. + +function find_device( options ) + for index,option in ipairs(options) do + local f=io.open(option) + if f then + f:close() + return option + end + end + return nil +end + +evdev = find_device ( { "/dev/input/by-path/platform-gpio_keys-event", + "/dev/input/by-path/platform-gpio-keys-event" } ) +if evdev then + logprint("Register evdev on "..evdev) + register("evdev", evdev, + 408, "restart_button", + 133, "media_button") +else + logprint("No evdev device found") +end + +register("system-status") + +-- Set to "false" to suppress the sounding of the buzzer +buzzer = true + +function system_status( status ) + logprint("System status: "..status) + if status == "start" then + piccmd("statusled", "greenon") + piccmd("powerled", "on") + if buzzer then piccmd("buzzer", "short") end + elseif status == "stop" then + piccmd("statusled", "redon") + piccmd("powerled", "1hz") + if buzzer then piccmd("buzzer", "short") end + else + logprint("Unknown system status") + end +end + +function power_button( time ) + os.execute("poweroff") +end + +function restart_button( time ) + os.execute("reboot") +end + +function media_button( time ) + piccmd("usbled", "8hz") +end + +fanfail = 0 + +function fan_error( ) + fanfail = fanfail + 1 + if fanfail == 3 then + logprint("ts409: fan error") + piccmd("statusled", "red2hz") + piccmd("buzzer", "long") + else + if fanfail == 10 then + fanfail = 0 + end + end +end + +function fan_normal( ) + piccmd("statusled", "greenon") + fanfail = 0 +end + +last_temp_log = nil +last_temp_value = 0 + +function logtemp( temp ) + local now = os.time() + + local function should_log( ) + local delta_temp = math.abs(temp - last_temp_value) + local delta_time = os.difftime(now, last_temp_log) + + -- Haven't previously logged, log now for the first time + if ( not last_temp_log ) then + return true + end + -- Temperature has changed by more than 5 + if ( delta_temp >= 5 ) then + return true + end + -- More than 5 minutes have elapsed... + if ( delta_time >= 300 ) then + if ( delta_temp > 1 ) then + --- ...and the change is by more than +/-1 + return true + else + --- ...insignificant change, wait another 5 minutes + last_temp_log = now + return false + end + end + -- Otherwise no need to log + return false + end + + if ( should_log() ) then + logprint(string.format("ts409: temperature %d", temp)) + last_temp_log = now + last_temp_value = temp + end +end + +last_fan_setting = nil + +function setfan( temp, speed ) + if ( ( not last_fan_setting ) or + ( last_fan_setting ~= speed ) ) then + logprint(string.format("ts409: temperature %d setting fan to \"%s\"", temp, speed)) + end + piccmd("fanspeed", speed) + last_fan_setting = speed +end + +-- hysteresis implementation: +-- - - > 35 > - - - > 40 > - - - > 50 > - - - > 65 > - - - +-- silence -- low -- medium -- high -- full | +-- - - < 32 < - - - < 35 < - - - < 45 < - - - < 55 < - - - +function temp( temp ) + logtemp(temp) + if last_fan_setting == "full" then + if temp < 55 then + setfan(temp, "high") + end + elseif last_fan_setting == "high" then + if temp > 65 then + setfan(temp, "full") + elseif temp < 45 then + setfan(temp, "medium") + end + elseif last_fan_setting == "medium" then + if temp > 50 then + setfan(temp, "high") + elseif temp < 35 then + setfan(temp, "low") + end + elseif last_fan_setting == "low" then + if temp > 40 then + setfan(temp, "medium") + elseif temp < 32 then + setfan(temp, "silence") + end + elseif last_fan_setting == "silence" then + if temp > 35 then + setfan(temp, "low") + end + else + setfan(temp, "high") + end +end + +confdir("/etc/qcontrol.d") +-- +-- Local variables: +-- mode: lua +-- indent-level: 8 +-- End: diff --git a/examples/ts41x.lua b/examples/ts41x.lua new file mode 100644 index 0000000..c9d4cd9 --- /dev/null +++ b/examples/ts41x.lua @@ -0,0 +1,248 @@ +--[[ + Configuration file for qcontrol (LUA syntax) + Supports the QNAP TS-410, TS-410U, TS-419P and TS-419U. +--]] + +register("ts41x") + +-- Requires CONFIG_KEYBOARD_GPIO enabled in the kernel and +-- the kernel module gpio_keys to be loaded. + +-- Different kernel versions use platform-gpio_keys-event or +-- platform-gpio-keys-event, find the right one. + +function find_device( options ) + for index,option in ipairs(options) do + local f=io.open(option) + if f then + f:close() + return option + end + end + return nil +end + +evdev = find_device ( { "/dev/input/by-path/platform-gpio_keys-event", + "/dev/input/by-path/platform-gpio-keys-event" } ) +if evdev then + logprint("Register evdev on "..evdev) + register("evdev", evdev, + 408, "restart_button", + 133, "media_button") +else + logprint("No evdev device found") +end + +register("system-status") + +-- Set to "false" to suppress the sounding of the buzzer +buzzer = true + +function system_status( status ) + logprint("System status: "..status) + if status == "start" then + piccmd("statusled", "greenon") + piccmd("powerled", "on") + if buzzer then piccmd("buzzer", "short") end + elseif status == "stop" then + piccmd("statusled", "redon") + piccmd("powerled", "1hz") + if buzzer then piccmd("buzzer", "short") end + else + logprint("Unknown system status") + end +end + +function power_button( time ) + os.execute("poweroff") +end + +function restart_button( time ) + os.execute("reboot") +end + +function media_button( time ) + piccmd("usbled", "8hz") +end + +function lcd_button( state, down, up ) +-- exactly key 1 and 2 pressed switches backlight off + if state == 3 then + piccmd("lcd-backlight", "off") + else +-- any key pressed activates the backlight + if down ~= 0 then + piccmd("lcd-backlight", "on") + end +-- pressing key 1 switches off the usbled the media-key enables... + if down == 1 then + piccmd("usbled", "off") + end + end +end + +-- If argument is a function then call it, else return it. +function evalfn(f) + if type(f) == "function" then + return f( ) + else + return f + end +end + +-- Select a value based on the state of a GPIO pin. +-- +-- results == list of result to return for each state +-- value=0 => results[1] +-- value=1 => results[2] +-- value unknown => results[3] +-- +-- If result[N] is a function it will be called, +-- otherwise it is simply returned as is. +function gpio_select(number, results) + local path=string.format("/sys/class/gpio/gpio%d/value", number) + local gpio=io.open(path) + if not gpio then + logprint("ts41x: "..path.." does not exist, trying to enable") + gpioctl = "/sys/class/gpio/export" + g, err = io.open(gpioctl, "w") + if not g then + logprint("ts41x: unable to open gpio control file "..gpioctl..": "..err) + return evalfn(results[3]) + end + g:write(number) + gpio=io.open(path) + end + if not gpio then + logprint("ts41x: unable to open gpio file") + return evalfn(results[3]) + end + + local v=gpio:read("*n") + + if v == 0 then return evalfn(results[1]) + elseif v == 1 then return evalfn(results[2]) + else return evalfn(results[3]) + end +end + +-- MPP45_GPIO, JP1: 0: LCD, 1: serial console +gpio_select(45, { + function () register("a125", "/dev/ttyS0") end, + nil, + nil}) + +fanfail = 0 + +function fan_error( ) + fanfail = fanfail + 1 + if fanfail == 3 then + logprint("ts41x: fan error") + piccmd("statusled", "red2hz") + piccmd("buzzer", "long") + else + if fanfail == 10 then + fanfail = 0 + end + end +end + +function fan_normal( ) + piccmd("statusled", "greenon") + fanfail = 0 +end + +last_temp_log = nil +last_temp_value = 0 + +function logtemp( temp ) + local now = os.time() + + local function should_log( ) + local delta_temp = math.abs(temp - last_temp_value) + local delta_time = os.difftime(now, last_temp_log) + + -- Haven't previously logged, log now for the first time + if ( not last_temp_log ) then + return true + end + -- Temperature has changed by more than 5 + if ( delta_temp >= 5 ) then + return true + end + -- More than 5 minutes have elapsed... + if ( delta_time >= 300 ) then + if ( delta_temp > 1 ) then + --- ...and the change is by more than +/-1 + return true + else + --- ...insignificant change, wait another 5 minutes + last_temp_log = now + return false + end + end + -- Otherwise no need to log + return false + end + + if ( should_log() ) then + logprint(string.format("ts41x: temperature %d", temp)) + last_temp_log = now + last_temp_value = temp + end +end + +last_fan_setting = nil + +function setfan( temp, speed ) + if ( ( not last_fan_setting ) or + ( last_fan_setting ~= speed ) ) then + logprint(string.format("ts41x: temperature %d setting fan to \"%s\"", temp, speed)) + end + piccmd("fanspeed", speed) + last_fan_setting = speed +end + +-- hysteresis implementation: +-- - - > 35 > - - - > 40 > - - - > 50 > - - - > 65 > - - - +-- silence -- low -- medium -- high -- full | +-- - - < 32 < - - - < 35 < - - - < 45 < - - - < 55 < - - - +function temp( temp ) + logtemp(temp) + if last_fan_setting == "full" then + if temp < 55 then + setfan(temp, "high") + end + elseif last_fan_setting == "high" then + if temp > 65 then + setfan(temp, "full") + elseif temp < 45 then + setfan(temp, "medium") + end + elseif last_fan_setting == "medium" then + if temp > 50 then + setfan(temp, "high") + elseif temp < 35 then + setfan(temp, "low") + end + elseif last_fan_setting == "low" then + if temp > 40 then + setfan(temp, "medium") + elseif temp < 32 then + setfan(temp, "silence") + end + elseif last_fan_setting == "silence" then + if temp > 35 then + setfan(temp, "low") + end + else + setfan(temp, "high") + end +end + +confdir("/etc/qcontrol.d") +-- +-- Local variables: +-- mode: lua +-- indent-level: 8 +-- End: diff --git a/picmodule.h b/picmodule.h new file mode 100644 index 0000000..0a4b8fa --- /dev/null +++ b/picmodule.h @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2007-2008 Byron Bradley (byron.bbradley@gmail.com) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef _PICMODULE_H_ +#define _PICMODULE_H_ + +struct picmodule { + char *name; + int (*init)(int, const char**); + void (*exit)(void); +}; + +int print_log(int priority, const char *format, ...) +#ifdef __GNUC__ +__attribute__ ((format (printf, 2, 3))) +#endif +; +int get_args(int *argc, const char ***argv); +int register_command(const char *cmd, const char *shorthelp, const char *help, + int (*call)(int argc, const char **argv)); +int call_function(const char *fname, const char *fmt, ...); + +#ifdef __GNUC__ +#define UNUSED __attribute__ ((unused)) +#else +#define UNUSE +#endif + +#endif /* Not _PICMODULE_H_ */ diff --git a/qcontrol.c b/qcontrol.c new file mode 100644 index 0000000..1c911c8 --- /dev/null +++ b/qcontrol.c @@ -0,0 +1,770 @@ +/* + * Copyright (C) 2007-2008 Byron Bradley (byron.bbradley@gmail.com) + * Copyright (C) 2013 Ian Campbell (ijc@hellion.org.uk) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <errno.h> +#include <lua.h> +#include <lualib.h> +#include <lauxlib.h> +#include <setjmp.h> +#include <stdarg.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <syslog.h> +#include <unistd.h> +#include <getopt.h> +#include <glob.h> +#include <sys/socket.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <sys/un.h> + +#ifdef HAVE_SYSTEMD +#include <systemd/sd-daemon.h> +#endif + +#include "picmodule.h" + +#define PIC_SOCKET "/var/run/qcontrol.sock" +#define MAX_NET_BUF 1000 +#define MALLOC_SIZE 100 +#define MAX_CMD_NAME 16 + +struct piccommand { + const char *name; + const char *shorthelp; + const char *help; + int (*call)(int argc, const char **argv); +}; + +bool g_use_syslog = false; +char *version = "qcontrol " QCONTROL_VERSION "\n"; +char *usage = "Usage: qcontrol [OPTION...] [command] [args...]\n" + "PIC Controller\n\n" + " -d, --daemon Run the server as a daemon\n" + " -f, --foreground Run the server in the foreground\n" + " --direct Run commands directly\n" + " -c, --config=PATH Use PATH as config file\n" + " --help Give this help list\n" + " -V, --version Print program version\n\n" + "Mandatory or optional arguments to long options are also " + "mandatory or optional\nfor any corresponding short options.\n"; +static const char *configfilename = "/etc/qcontrol.conf"; +static lua_State *lua = NULL; +unsigned int commandcount = 0; +struct piccommand **commands; + +extern struct picmodule system_module; +extern struct picmodule ts209_module; +extern struct picmodule ts219_module; +extern struct picmodule ts409_module; +extern struct picmodule ts41x_module; +extern struct picmodule a125_module; +extern struct picmodule evdev_module; +extern struct picmodule synology_module; + +struct picmodule *modules[] = { + &system_module, + &ts209_module, + &ts219_module, + &ts409_module, + &ts41x_module, + &a125_module, + &evdev_module, + &synology_module, + NULL +}; + +/** + * Print an error message, either to the console or syslog depending on the + * value of g_use_syslog + */ +int print_log(int priority, const char *format, ...) +{ + int err = 0; + va_list ap; + + va_start(ap, format); + if (g_use_syslog == true) { + vsyslog(priority, format, ap); + } else { + FILE *f = (priority == LOG_ERR) ? stderr : stdout; + err = vfprintf(f, format, ap); + fprintf(f, "\n"); + fflush(f); + } + + va_end(ap); + return err; +} + +/** + * Calls a function in the lua config file + */ +int call_function(const char *fname, const char *fmt, ...) +{ + int i; + va_list s; + + lua_getglobal(lua, fname); + + va_start(s, fmt); + for (i = 0; fmt[i]; ) { + if (fmt[i++] != '%') + return -1; + switch (fmt[i]) { + case 'd': + lua_pushinteger(lua, va_arg(s, int)); + break; + case 's': + lua_pushstring(lua, va_arg(s, char*)); + break; + default: + print_log(LOG_WARNING, "Unrecognised format"); + return -1; + } + ++i; + } + va_end(s); + + if (lua_pcall(lua, i / 2, 0, 0) == LUA_ERRRUN) { + print_log(LOG_ERR, "Error calling lua function %s: %s", + fname, lua_tostring(lua, -1)); + return -1; + } + + return 0; +} + +/** + * Return an error to lua + */ +static void return_error(const char *error) +{ + lua_pushstring(lua, error); + lua_error(lua); +} + +/** + * Load files from a configuration dir. + */ +static int confdir(lua_State *L UNUSED) +{ + static const char gl_match[] = "/*.conf"; + const char *path = lua_tostring(lua, 1); + glob_t gl; + char *gl_path; + size_t p; + int rc; + + if (!path) { + print_log(LOG_ERR, "confdir: no path given"); + return -1; + } + + print_log(LOG_ERR, "confdir: loading from %s...", path); + + gl_path = lua_newuserdata(lua, strlen(path) + sizeof(gl_match)); + if (!gl_path) { + print_log(LOG_ERR, "confdir: out of memory allocating path"); + return -1; + } + strcpy(gl_path, path); + strcat(gl_path, gl_match); + + rc = glob(gl_path, 0, NULL, &gl); + + switch (rc) { + case 0: /* Success */ break; + case GLOB_NOMATCH: /* No matches, this is ok */ + return 0; + + case GLOB_NOSPACE: + print_log(LOG_ERR, "confdir: out of memory during glob"); + return -1; + case GLOB_ABORTED: + print_log(LOG_ERR, "confdir: read error during glob"); + return -1; + default: + print_log(LOG_ERR, "confdir: unknown error %d during glob", rc); + return -1; + } + + for (p=0; p<gl.gl_pathc; p++) { + struct stat st_buf; + + path = gl.gl_pathv[p]; + + rc = stat(path, &st_buf); + if (rc < 0) { + print_log(LOG_ERR, "confdir: stat(%s) failed: %s", + path, strerror(errno)); + continue; + } + if (!S_ISREG(st_buf.st_mode) && !S_ISLNK(st_buf.st_mode)) { + print_log(LOG_DEBUG, + "confdir: %s not a file or symlink", + path); + continue; + } + print_log(LOG_ERR, "confdir: including %s", path); + + rc = luaL_dofile(lua, path); + if (rc != 0) { + print_log(LOG_ERR, "%s", lua_tostring(lua, -1)); + lua_pop(lua, 1); + } + } + + globfree(&gl); + return 0; +} + +/** + * Get the arguments of a function call from lua + */ +int get_args(int *argc, const char ***argv) +{ + int i; + + *argc = lua_gettop(lua); + *argv = (const char **) lua_newuserdata(lua, *argc * sizeof(char*)); + for (i = 1; i <= *argc; ++i) { + const char *arg = (const char*) lua_tostring(lua, i); + if (!arg) + return -1; + *(*argv + i - 1) = arg; + } + return 0; +} + +static int register_module(lua_State *L UNUSED) +{ + int i, argc, err; + const char **argv; + + err = get_args(&argc, &argv); + if (err < 0) { + print_log(LOG_ERR, "register() - Error getting arguments"); + return err; + } + + for (i = 0; modules[i]; ++i) { + if (strcmp(argv[0], modules[i]->name) != 0) + continue; + err = modules[i]->init(argc - 1, argv + 1); + break; + } + if (err < 0) + return_error("register() - Error loading module"); + return err; +} + +int register_command(const char *cmd, const char *shorthelp, const char *help, + int (*call)(int argc, const char **argv)) +{ + struct piccommand *c = malloc(sizeof(struct piccommand)); + + if (!c || strlen(cmd) > MAX_CMD_NAME) { + free(c); + return -1; + } + c->name = cmd; + c->shorthelp = shorthelp; + c->help = help; + c->call = call; + + if (commandcount == 0) + commands = malloc(++commandcount * sizeof(struct piccommand*)); + else + commands = realloc(commands, + ++commandcount * sizeof(struct piccommand*)); + + commands[commandcount - 1] = c; + + return 0; +} + +static int shorthelp_fmt(char *buf, size_t size, struct piccommand *c) +{ + return snprintf(buf, size, "%-16s %s\n", c->name, c->shorthelp); +} + +static void shorthelp_commands(char **buf, int *len) +{ + unsigned int i, off=0; + + *len = sizeof(int) + 1; /* Length field + NUL terminator */ + for (i = 0; i < commandcount; ++i) + *len += shorthelp_fmt(NULL, 0, commands[i]); + + *buf = malloc(*len); + memset(*buf, 0, sizeof(int)); + off += sizeof(int); /* Skip length */ + for (i = 0; i < commandcount; ++i) + off += shorthelp_fmt(*buf+off, *len-off, commands[i]); +} + +static void shorthelp_commands_direct(void) +{ + char *buf = NULL; + int offs = 0; + + shorthelp_commands(&buf, &offs); + printf("\nAvailable commands are:\n%s", buf+sizeof(int)); + free(buf); +} + +static int run_command(const char *cmd, int argc, const char **argv) +{ + unsigned int i; + + for (i = 0; i < commandcount; ++i) + if (strcmp(cmd, commands[i]->name) == 0) + return commands[i]->call(argc, argv); + + return -1; +} + +static int run_command_lua(lua_State *L UNUSED) +{ + int argc, err; + const char **argv; + + err = get_args(&argc, &argv); + if (err < 0) { + return_error("piccmd() - Error getting arguments"); + return -1; + } + + return run_command(argv[0], argc-1, argv+1); +} + +static int run_command_direct(const char *cmd, int argc, const char **argv) +{ + int err = run_command(cmd, argc, argv); + + if (err < 0) + return -1; + + return 0; +} + +static int script_print(lua_State *L UNUSED) +{ + int argc, err; + const char **argv; + + err = get_args(&argc, &argv); + if (err < 0 || argc != 1) { + return_error("logprint() - Error getting arguments"); + return -1; + } + + print_log(LOG_NOTICE, "%s", argv[0]); + + return 0; +} + +static const char *help_command(const char *cmd) +{ + unsigned int i; + + for (i = 0; i < commandcount; ++i) + if (strcmp(cmd, commands[i]->name) == 0) + return commands[i]->help; + + return "Command not found\n"; +} + +static void pic_lua_close(void) +{ + lua_close(lua); +} + +static int pic_lua_setup(lua_State **L UNUSED) +{ + int err; + + lua = lua_open(); + atexit(pic_lua_close); + luaL_openlibs(lua); + + lua_register(lua, "register", register_module); + lua_register(lua, "piccmd", run_command_lua); + lua_register(lua, "logprint", script_print); + lua_register(lua, "confdir", confdir); + + err = luaL_dofile(lua, configfilename); + if (err != 0) { + print_log(LOG_ERR, "%s", lua_tostring(lua, -1)); + lua_pop(lua, 1); + } + + return err; +} + +static int write_uint32(uint32_t i, char **buf, int off, unsigned int *maxlen) +{ + unsigned int ilen = sizeof(uint32_t); + + if ((unsigned int)off + ilen > *maxlen) { + *maxlen += ilen + MALLOC_SIZE; + *buf = realloc(*buf, *maxlen); + if (!buf) + return -1; + } + memcpy((*buf)+off, &i, ilen); + return off + ilen; +} + +static int read_uint32(uint32_t *i, char *buf, int off) +{ + memcpy(i, buf+off, sizeof(uint32_t)); + return off + sizeof(uint32_t); +} + +static int write_string(const char *str, char **buf, int off, unsigned int *maxlen) +{ + int slen = strlen(str) + 1; + + if (off + sizeof(int) + slen > *maxlen) { + *maxlen += slen + MALLOC_SIZE; + *buf = realloc(*buf, *maxlen); + if (!buf) + return -1; + } + off = write_uint32(slen, buf, off, maxlen); + memcpy((*buf)+off, str, slen); + return off+slen; +} + +static int read_string(char **str, char *buf, int off) +{ + uint32_t len; + + off = read_uint32(&len, buf, off); + *str = malloc(len); + memcpy(*str, buf+off, len); + return off+len; +} + +static int network_send(int argc, const char **argv) +{ + int sock, err, i, off=0, rlen; + struct sockaddr_un remote; + unsigned int maxlen = MALLOC_SIZE; + char *buf, retbuf[MAX_NET_BUF], *errstr; + + sock = socket(AF_UNIX, SOCK_STREAM, 0); + if (sock < 0) { + print_log(LOG_ERR, "Error opening socket: %s", strerror(errno)); + return -1; + } + + remote.sun_family = AF_UNIX; + strcpy(remote.sun_path, PIC_SOCKET); + err = connect(sock, (struct sockaddr*)&remote, + sizeof(struct sockaddr_un)); + if (err < 0) { + print_log(LOG_ERR, "Error connecting to socket: %s", + strerror(errno)); + return -1; + } + + buf = malloc(maxlen); + off = write_uint32(argc, &buf, off, &maxlen); + for (i = 0; i < argc; ++i) { + off = write_string(argv[i], &buf, off, &maxlen); + } + + send(sock, buf, off, 0); + free(buf); + rlen = read(sock, retbuf, MAX_NET_BUF); + if (rlen < 0) { + print_log(LOG_ERR, "Error during read: %s", strerror(errno)); + return -1; + } + + off = 0; + off = read_uint32((uint32_t*)&err, retbuf, off); + if (err < 0) { + off = read_string(&errstr, retbuf, off); + print_log(LOG_ERR, "%s", errstr); + } else if (err == 0 && rlen > (int)sizeof(int)) { + print_log(LOG_ERR, "\nAvailable commands are:\n%s", + retbuf + sizeof(int)); + err = 1; + } else { + err = 0; + } + + close(sock); + + return err; +} + +static int open_socket(void) +{ + int sock, err; + struct sockaddr_un name; + +#ifdef HAVE_SYSTEMD + int fds; + /* When qcontrol gets socket-activated by systemd, fds is > 0 to indicate + * that a socket was inherited which we can just use, i.e. we don’t need to + * create and listen() on a new socket on our own. */ + fds = sd_listen_fds(0); + if (fds < 0) { + print_log(LOG_ERR, "Error in sd_listen_fds()"); + return -1; + } + return SD_LISTEN_FDS_START; +#endif + + sock = socket(AF_UNIX, SOCK_STREAM, 0); + if (sock < 0) { + print_log(LOG_ERR, "Error creating socket: %s", + strerror(errno)); + return -1; + } + + name.sun_family = AF_UNIX; + strcpy(name.sun_path, PIC_SOCKET); + err = bind(sock, (struct sockaddr*) &name, sizeof(struct sockaddr_un)); + if (err != 0) { + err = connect(sock, (struct sockaddr*)&name, + sizeof(struct sockaddr_un)); + if (err < 0) { + err = unlink(PIC_SOCKET); + if (err != 0) + print_log(LOG_ERR, "Error deleting socket: %s", + strerror(errno)); + err = bind(sock, (struct sockaddr*) &name, + sizeof(struct sockaddr_un)); + if (err != 0) + print_log(LOG_ERR, "Error creating socket: %s", + strerror(errno)); + } else { + print_log(LOG_ERR, "server already running"); + close(sock); + return -1; + } + } + + err = listen(sock, 10); + if (err < 0) { + print_log(LOG_ERR, "Error listening on socket: %s", + strerror(errno)); + return -1; + } + + return sock; +} + +static int network_listen(void) +{ + int err, con, argc, off=0, i; + char **argv; + socklen_t remotelen; + char buf[MAX_NET_BUF], *rbuf; + int maxlen = MALLOC_SIZE; + struct sockaddr_un remote; + + int sock = open_socket(); + + remotelen = sizeof(remote); + for (;;) { + con = accept(sock, (struct sockaddr*)&remote, &remotelen); + if (con < 0) { + print_log(LOG_ERR, "Error accepting connection: %s", + strerror(errno)); + break; + } + err = read(con, buf, MAX_NET_BUF); + if (err < 0) { + print_log(LOG_ERR, "Error during read: %s", + strerror(errno)); + break; + } else if (err == 0) { + /* read nothing, probably just somebody checking we're + alive */ + close(con); + continue; + } + + /* Process the arguments */ + off = read_uint32((uint32_t*)&argc, buf, off); + argv = malloc(argc * sizeof(char*)); + if (!argv) { + print_log(LOG_ERR, "read failed: %s", strerror(errno)); + return -1; + } + for (i = 0; i < argc; ++i) + off = read_string(&argv[i], buf, off); + off = 0; + + if (argc > 0 && (strcmp(argv[0], "--help") == 0 + ||strcmp(argv[0], "-h") == 0)) { + /* Return the short helps */ + shorthelp_commands(&rbuf, &off); + } else { + /* Run the command */ + err = run_command(argv[0], argc-1, (const char**)argv+1); + rbuf = malloc(maxlen); + off = write_uint32(err, &rbuf, off, (uint32_t*)&maxlen); + if (err < 0) + off = write_string(help_command(argv[0]), &rbuf, + off, (uint32_t*)&maxlen); + } + send(con, rbuf, off, 0); + off = 0; + + + for (i = 0; i < argc; ++i) { + free(argv[i]); + argv[i] = NULL; + } + free(argv); + argv = NULL; + free(rbuf); + rbuf = NULL; + close(con); + } + + close(sock); + unlink(PIC_SOCKET); + + return 0; +} + +static int start_daemon(bool daemon_mode) +{ + int err; + pid_t pid, sid; + + if (daemon_mode == true) { + g_use_syslog = true; + + pid = fork(); + if (pid < 0) + return -1; + else if (pid > 0) + return 0; + + umask(0); + sid = setsid(); + if (sid < 0) + return -1; + if ((chdir("/")) < 0) + return -1; + close(STDIN_FILENO); + close(STDOUT_FILENO); + close(STDERR_FILENO); + openlog("qcontrol", LOG_PID, LOG_DAEMON); + } + + print_log(LOG_INFO, "qcontrol " QCONTROL_VERSION " daemon starting."); + err = pic_lua_setup(&lua); + if (err != 0) + return -1; + err = network_listen(); + + if (daemon_mode == true) + closelog(); + return err; +} + +int main(int argc, char **argv) +{ + const char *help_arg = "--help"; + enum { + MODE_SERVER_DAEMON, + MODE_SERVER_FOREGROUND, + MODE_CLIENT, + MODE_DIRECT, + } mode = MODE_CLIENT; + bool help = false; + + while (1) { + struct option long_options[] = { + {"config", required_argument, 0, 'c' }, + {"daemon", no_argument, 0, 'd' }, + {"foreground", no_argument, 0, 'f' }, + {"direct", no_argument, 0, 1 }, + {"help", no_argument, 0, 'h' }, + {"version", no_argument, 0, 'V' }, + {0, 0, 0, 0} + }; + + int opt = getopt_long(argc, argv, "c:dfhV", + long_options, NULL); + + if (opt == -1) + break; + + switch (opt) { + case 1: mode = MODE_DIRECT; break; + case 'c': configfilename = optarg; break; + case 'd': mode = MODE_SERVER_DAEMON; break; + case 'f': mode = MODE_SERVER_FOREGROUND; break; + case 'h': help = 1; break; + case 'V': printf("%s", version); return 0; + case '?': break; + } + + } + + argc -= optind; + argv += optind; + + switch (mode) { + case MODE_SERVER_DAEMON: + case MODE_SERVER_FOREGROUND: + if (help) { + printf("%s", usage); + return 0; + } else { + return start_daemon(mode == MODE_SERVER_DAEMON); + } + + case MODE_CLIENT: + /* Send the command to the server */ + if (help || argc == 0) { + printf("%s", usage); + return network_send(1, &help_arg); + } else { + return network_send(argc, (const char **)argv); + } + + case MODE_DIRECT: + pic_lua_setup(&lua); + if (help || argc == 0) { + printf("%s", usage); + shorthelp_commands_direct(); + return 0; + } else { + /* Execute a single command and terminate */ + return run_command_direct(argv[0], argc - 1, (const char **)(argv + 1)); + } + } + + abort(); /* Not reached */ +} diff --git a/qnap-pic.c b/qnap-pic.c new file mode 100644 index 0000000..51bb132 --- /dev/null +++ b/qnap-pic.c @@ -0,0 +1,393 @@ +/* + * Copyright (C) 2007-2008 Byron Bradley (byron.bbradley@gmail.com) + * Copyright (C) 2008, 2009 Martin Michlmayr (tbm@cyrius.com) + * Copyright (C) 2013 Michael Stapelberg (michael+qnap@stapelberg.de) + * Copyright (C) 2013 Ian Campbell (ijc@hellion.org.uk) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <errno.h> +#include <fcntl.h> +#include <pthread.h> +#include <termios.h> +#include <string.h> +#include <syslog.h> +#include <unistd.h> + +#include <sys/select.h> + +#include "picmodule.h" +#include "qnap-pic.h" + +static int serial; +static struct termios oldtio, newtio; +static pthread_t qnap_thread; + +int qnap_serial_read(unsigned char *buf, int len) +{ + int err; + static int error_count = 0; + + err = read(serial, buf, len); + if (err != -1) { + buf[err] = 0; + error_count = 0; + } else if (errno == EAGAIN) { + error_count++; + if (error_count == 5) + print_log(LOG_WARNING, +"Contradicting information about data available to be read from /dev/ttyS1.\n" +"Please make sure nothing else is reading things there."); + } + + return err; +} + +int qnap_serial_write(unsigned char *buf, int len) +{ + int err; + + err = write(serial, buf, len); + + return err; +} + +static void *serial_poll(void *data) +{ + qnap_serial_cb cb = data; + int err; + fd_set rset; + + FD_ZERO(&rset); + FD_SET(serial, &rset); + + for (;;) { + err = select(serial + 1, &rset, NULL, NULL, NULL); + if (err <= 0) { + FD_SET(serial, &rset); + continue; + } + cb(); + FD_SET(serial, &rset); + } + + return NULL; +} + +int qnap_serial_poll(qnap_serial_cb cb) +{ + return pthread_create(&qnap_thread, NULL, serial_poll, cb); +} + +static int set_nonblock(int fd) +{ + int flags = fcntl(fd, F_GETFL, 0); + if (flags < 0) + flags = 0; + return fcntl(fd, F_SETFL, flags | O_NONBLOCK); +} + +int qnap_serial_open(char *device) +{ + int err; + + if ((serial = open(device , O_RDWR)) < 0) { + print_log(LOG_ERR, "Failed to open %s: %s", device, + strerror(errno)); + return -1; + } + err = set_nonblock(serial); + if (err < 0) { + print_log(LOG_ERR, "Error setting nonblock: %s", + strerror(errno)); + return -1; + } + + tcgetattr(serial, &oldtio); + memset(&newtio, 0, sizeof(newtio)); + + newtio.c_iflag |= IGNBRK; + newtio.c_lflag &= ~(ISIG | ICANON | ECHO); + newtio.c_cflag = B19200 | CS8 | CLOCAL | CREAD; + newtio.c_cc[VMIN] = 1; + newtio.c_cc[VTIME] = 0; + cfsetospeed(&newtio, B19200); + cfsetispeed(&newtio, B19200); + + err = tcsetattr(serial, TCSANOW, &newtio); + if (err < 0) { + print_log(LOG_ERR, "Failed to set attributes for %s: %s", + device, strerror(errno)); + return -1; + } + + return 0; +} + +void qnap_serial_close(void) +{ + tcsetattr(serial, TCSANOW, &oldtio); + close(serial); +} + +static int qnap_cmd_powerled(int argc, const char **argv) +{ + unsigned char code = 0; + + if (argc != 1) + return -1; + + if (strcmp(argv[0], "on") == 0) + code = QNAP_PICCMD_POWER_LED_ON; + else if (strcmp(argv[0], "1hz") == 0) + code = QNAP_PICCMD_POWER_LED_1HZ; + else if (strcmp(argv[0], "2hz") == 0) + code = QNAP_PICCMD_POWER_LED_2HZ; + else if (strcmp(argv[0], "off") == 0) + code = QNAP_PICCMD_POWER_LED_OFF; + else + return -1; + + return qnap_serial_write(&code, 1); +} + +static int qnap_cmd_statusled(int argc, const char **argv) +{ + unsigned char code = 0; + + if (argc != 1) + return -1; + + if (strcmp(argv[0], "red2hz") == 0) + code = QNAP_PICCMD_STATUS_RED_2HZ; + else if (strcmp(argv[0], "green2hz") == 0) + code = QNAP_PICCMD_STATUS_GREEN_2HZ; + else if (strcmp(argv[0], "greenon") == 0) + code = QNAP_PICCMD_STATUS_GREEN_ON; + else if (strcmp(argv[0], "redon") == 0) + code = QNAP_PICCMD_STATUS_RED_ON; + else if (strcmp(argv[0], "greenred2hz") == 0) + code = QNAP_PICCMD_STATUS_BOTH_2HZ; + else if (strcmp(argv[0], "off") == 0) + code = QNAP_PICCMD_STATUS_OFF; + else if (strcmp(argv[0], "green1hz") == 0) + code = QNAP_PICCMD_STATUS_GREEN_1HZ; + else if (strcmp(argv[0], "red1hz") == 0) + code = QNAP_PICCMD_STATUS_RED_1HZ; + else if (strcmp(argv[0], "greenred1hz") == 0) + code = QNAP_PICCMD_STATUS_BOTH_1HZ; + else + return -1; + + return qnap_serial_write(&code, 1); +} + +static int qnap_cmd_usbled(int argc, const char **argv) +{ + unsigned char code = 0; + + if (argc != 1) + return -1; + + if (strcmp(argv[0], "on") == 0) + code = QNAP_PICCMD_USB_LED_ON; + else if (strcmp(argv[0], "8hz") == 0) + code = QNAP_PICCMD_USB_LED_8HZ; + else if (strcmp(argv[0], "off") == 0) + code = QNAP_PICCMD_USB_LED_OFF; + else + return -1; + + return qnap_serial_write(&code, 1); +} + +static int qnap_cmd_autopower(int argc, const char **argv) +{ + unsigned char code = 0; + + if (argc != 1) + return -1; + + if (strcmp(argv[0], "on") == 0) + code = QNAP_PICCMD_AUTOPOWER_ON; + else if (strcmp(argv[0], "off") == 0) + code = QNAP_PICCMD_AUTOPOWER_OFF; + else + return -1; + + return qnap_serial_write(&code, 1); +} + +static int qnap_cmd_buzzer(int argc, const char **argv) +{ + unsigned char code = 0; + + if (argc != 1) + return -1; + + if (strcmp(argv[0], "short") == 0) + code = QNAP_PICCMD_BUZZER_SHORT; + else if (strcmp(argv[0], "long") == 0) + code = QNAP_PICCMD_BUZZER_LONG; + else + return -1; + + return qnap_serial_write(&code, 1); +} + +static int qnap_cmd_fanspeed(int argc, const char **argv) +{ + unsigned char code = 0; + + if (argc != 1) + return -1; + + if (strcmp(argv[0], "stop") == 0) + code = QNAP_PICCMD_FAN_STOP; + else if (strcmp(argv[0], "silence") == 0) + code = QNAP_PICCMD_FAN_SILENCE; + else if (strcmp(argv[0], "low") == 0) + code = QNAP_PICCMD_FAN_LOW; + else if (strcmp(argv[0], "medium") == 0) + code = QNAP_PICCMD_FAN_MEDIUM; + else if (strcmp(argv[0], "high") == 0) + code = QNAP_PICCMD_FAN_HIGH; + else if (strcmp(argv[0], "full") == 0) + code = QNAP_PICCMD_FAN_FULL; + else + return -1; + + return qnap_serial_write(&code, 1); +} + +static int qnap_cmd_watchdog(int argc, const char **argv) +{ + unsigned char code = 0; + + if (argc != 1) + return -1; + if (strcmp(argv[0], "off") == 0) + code = QNAP_PICCMD_WDT_OFF; + else + return -1; + + return qnap_serial_write(&code, 1); +} + +static int qnap_cmd_eup(int argc, const char **argv) +{ + unsigned char code = 0; + + if (argc != 1) + return -1; + + if (strcmp(argv[0], "on") == 0) + code = QNAP_PICCMD_EUP_ENABLE; + else if (strcmp(argv[0], "off") == 0) + code = QNAP_PICCMD_EUP_DISABLE; + else + return -1; + + return qnap_serial_write(&code, 1); +} + +static int qnap_cmd_rtc(int argc, const char **argv) +{ + unsigned char code = 0; + + if (argc != 1) + return -1; + + if (strcmp(argv[0], "on") == 0) + code = QNAP_PICCMD_RTC_ENABLE; + else if (strcmp(argv[0], "off") == 0) + code = QNAP_PICCMD_RTC_DISABLE; + else + return -1; + + return qnap_serial_write(&code, 1); +} + +static int qnap_cmd_wol(int argc, const char **argv) +{ + unsigned char code[2]; + int len; + + if (argc != 1) + return -1; + + if (strcmp(argv[0], "on") == 0) { + /* EUP turns the device in such a deep power-saving + * mode that WOL does not work. Therefore, in order + * to have WOL turned on, we also disable EUP. */ + code[0] = QNAP_PICCMD_WOL_ENABLE; + code[1] = QNAP_PICCMD_EUP_DISABLE; + len = 2; + } else if (strcmp(argv[0], "off") == 0) { + code[0] = QNAP_PICCMD_WOL_DISABLE; + len = 1; + } else + return -1; + + return qnap_serial_write(code, len); +} + + +int qnap_register_commands(unsigned long features) +{ +#define REGISTER1(feat,name,shorthelp,longhelp) do { \ + if (features & QNAP_PIC_FEATURE_##feat) { \ + int err = register_command(#name, shorthelp, longhelp, qnap_cmd_##name); \ + if (err < 0) return err; \ + } \ +} while(0) + + REGISTER1(POWERLED, powerled, "Change the power LED", + "Change the power LED, options are:\n" + "\ton\n\toff\n\t1hz\n\t2hz\n"); + REGISTER1(STATUSLED, statusled, "Change the status LED", + "Change the status LED, options are:\n" + "\tred2hz\n\tgreen2hz\n\tgreenon\n\tredon\n" + "\tgreenred2hz\n\toff\n\tgreen1hz\n\tred1hz\n"); + REGISTER1(USBLED, usbled, "Set the usbled", + "Set the usbled, options are:\n" + "\ton\n\t8hz\n\toff\n"); + REGISTER1(AUTOPOWER, autopower, "Control the automatic power mechanism", + "Control the automatic power mechanism, options are:\n" + "\ton\n\toff\n"); + REGISTER1(BUZZER, buzzer, "Buzz", + "Buzz, options are:\n" + "\tshort\n\tlong\n"); + REGISTER1(FANSPEED, fanspeed, "Set the fanspeed", + "Set the fanspeed, options are:\n" + "\tstop\n\tsilence\n\tlow\n\tmedium\n" + "\thigh\n\tfull\n"); + REGISTER1(WATCHDOG, watchdog, "Disable the PIC watchdog", + "Watchdog options are:\n" + "\toff"); + REGISTER1(WOL, wol, "Control Wake on LAN", + "Control Wake on LAN, options are:\n" + "\ton\n\toff"); + REGISTER1(EUP, eup, "Control EUP (Energy-using Products) power saving", + "Control EUP, options are:\n" + "\ton\n\toff"); + REGISTER1(RTC, rtc, "Control RTC (real time clock)", + "Control RTC, options are:\n" + "\ton\n\toff"); + +#undef REGISTER1 + return 0; +} + diff --git a/qnap-pic.h b/qnap-pic.h new file mode 100644 index 0000000..e6f980f --- /dev/null +++ b/qnap-pic.h @@ -0,0 +1,115 @@ +/* + * Copyright (C) 2014 Ian Campbell (ijc@hellion.org.uk) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef QNAP_PIC_H +#define QNAP_PIC_H + +typedef int (*qnap_serial_cb)(void); + +void qnap_serial_close(void); +int qnap_serial_open(char *device); +int qnap_serial_poll(qnap_serial_cb cb); +int qnap_serial_write(unsigned char *buf, int len); +int qnap_serial_read(unsigned char *buf, int len); + +enum { + QNAP_PIC_FEATURE_AUTOPOWER = (1U<<0), + + QNAP_PIC_FEATURE_BUZZER = (1U<<1), + + QNAP_PIC_FEATURE_FANSPEED = (1U<<2), + QNAP_PIC_FEATURE_POWERLED = (1U<<3), + QNAP_PIC_FEATURE_STATUSLED = (1U<<4), + QNAP_PIC_FEATURE_USBLED = (1U<<5), + + QNAP_PIC_FEATURE_WATCHDOG = (1U<<6), + + QNAP_PIC_FEATURE_EUP = (1U<<7), + QNAP_PIC_FEATURE_WOL = (1U<<8), + QNAP_PIC_FEATURE_RTC = (1U<<9), +}; + +int qnap_register_commands(unsigned long features); + +/* + * QNAP_PICCMD_* -- Command codes sent to PIC + * QNAP_PICSTS_* -- Status codes received from PIC + */ +#define QNAP_PICCMD_FAN_STOP 0x30 +#define QNAP_PICCMD_FAN_SILENCE 0x31 +#define QNAP_PICCMD_FAN_LOW 0x32 +#define QNAP_PICCMD_FAN_MEDIUM 0x33 +#define QNAP_PICCMD_FAN_HIGH 0x34 +#define QNAP_PICCMD_FAN_FULL 0x35 + +#define QNAP_PICSTS_SYS_TEMP_71_79 0x38 +#define QNAP_PICSTS_SYS_TEMP_80 0x39 +#define QNAP_PICSTS_TEMP_WARM_TO_HOT 0x3a +#define QNAP_PICSTS_TEMP_HOT_TO_WARM 0x3b +#define QNAP_PICSTS_TEMP_COLD_TO_WARM 0x3c +#define QNAP_PICSTS_TEMP_WARM_TO_COLD 0x3d + +#define QNAP_PICSTS_POWER_BUTTON 0x40 +#define QNAP_PICSTS_POWER_LOSS_POWER_OFF 0x43 + +#define QNAP_PICCMD_AUTOPOWER_ON 0x48 +#define QNAP_PICCMD_AUTOPOWER_OFF 0x49 + +#define QNAP_PICCMD_POWER_LED_OFF 0x4b +#define QNAP_PICCMD_POWER_LED_2HZ 0x4c +#define QNAP_PICCMD_POWER_LED_ON 0x4d +#define QNAP_PICCMD_POWER_LED_1HZ 0x4e + +#define QNAP_PICCMD_BUZZER_SHORT 0x50 +#define QNAP_PICCMD_BUZZER_LONG 0x51 + +#define QNAP_PICCMD_STATUS_RED_2HZ 0x54 +#define QNAP_PICCMD_STATUS_GREEN_2HZ 0x55 +#define QNAP_PICCMD_STATUS_GREEN_ON 0x56 +#define QNAP_PICCMD_STATUS_RED_ON 0x57 +#define QNAP_PICCMD_STATUS_BOTH_2HZ 0x58 +#define QNAP_PICCMD_STATUS_OFF 0x59 +#define QNAP_PICCMD_STATUS_GREEN_1HZ 0x5a +#define QNAP_PICCMD_STATUS_RED_1HZ 0x5b +#define QNAP_PICCMD_STATUS_BOTH_1HZ 0x5c + +#define QNAP_PICCMD_USB_LED_ON 0x60 +#define QNAP_PICCMD_USB_LED_8HZ 0x61 +#define QNAP_PICCMD_USB_LED_OFF 0x62 + +#define QNAP_PICCMD_WDT_OFF 0x67 + +#define QNAP_PICSTS_FAN1_ERROR 0x73 +#define QNAP_PICSTS_FAN1_NORMAL 0x74 +#define QNAP_PICSTS_FAN2_ERROR 0x75 +#define QNAP_PICSTS_FAN2_NORMAL 0x76 +#define QNAP_PICSTS_FAN3_ERROR 0x77 +#define QNAP_PICSTS_FAN3_NORMAL 0x78 +#define QNAP_PICSTS_FAN4_ERROR 0x79 +#define QNAP_PICSTS_FAN4_NORMAL 0x7a + +#define QNAP_PICSTS_SYS_TEMP_0 0x80 +#define QNAP_PICSTS_SYS_TEMP_70 0xc6 + +#define QNAP_PICCMD_RTC_ENABLE 0xf0 +#define QNAP_PICCMD_RTC_DISABLE 0xf1 +#define QNAP_PICCMD_WOL_ENABLE 0xf2 +#define QNAP_PICCMD_WOL_DISABLE 0xf3 +#define QNAP_PICCMD_EUP_DISABLE 0xf4 +#define QNAP_PICCMD_EUP_ENABLE 0xf5 + +#endif diff --git a/synology.c b/synology.c new file mode 100644 index 0000000..77fe7e9 --- /dev/null +++ b/synology.c @@ -0,0 +1,332 @@ +/* + * Copyright (C) 2007-2008 Byron Bradley (byron.bbradley@gmail.com) + * Copyright (C) 2008, 2009 Martin Michlmayr (tbm@cyrius.com) + * Copyright (C) 2013 Michael Stapelberg (michael+qnap@stapelberg.de) + * Copyright (C) 2013 Ian Campbell (ijc@hellion.org.uk) + * Copyright (C) 2014 Ben Peddell (klightspeed@killerwolves.net) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <errno.h> +#include <fcntl.h> +#include <pthread.h> +#include <termios.h> +#include <string.h> +#include <syslog.h> +#include <unistd.h> + +#include <sys/select.h> + +#include "picmodule.h" +#include "synology.h" + +static int serial; +static struct termios oldtio, newtio; +static pthread_t synology_thread; + +static int synology_serial_read(unsigned char *buf, int len) +{ + int err; + static int error_count = 0; + + err = read(serial, buf, len); + if (err != -1) { + buf[err] = 0; + error_count = 0; + } else if (errno == EAGAIN) { + error_count++; + if (error_count == 5) + print_log(LOG_WARNING, +"Contradicting information about data available to be read from /dev/ttyS1.\n" +"Please make sure nothing else is reading things there."); + } + + return err; +} + +static int synology_serial_write(unsigned char *buf, int len) +{ + int err; + + err = write(serial, buf, len); + + return err; +} + +static int synology_read_serial_events(void) +{ + unsigned char buf[100]; + int err = synology_serial_read(buf, 100); + if (err < 0) + return err; + switch (buf[0]) { + case SYNOLOGY_PICSTS_POWER_BUTTON: + call_function("power_button", "%d", 3); + break; + case SYNOLOGY_PICSTS_MEDIA_BUTTON: + call_function("media_button", "%d", 3); + break; + case SYNOLOGY_PICSTS_RESET_BUTTON: + call_function("restart_button", "%d", 3); + break; + default: + print_log(LOG_WARNING, "(PIC 0x%x) unknown command from PIC", + buf[0]); + } + + return -1; +} + +static void *serial_poll(void *data UNUSED) +{ + int err; + fd_set rset; + + FD_ZERO(&rset); + FD_SET(serial, &rset); + + for (;;) { + err = select(serial + 1, &rset, NULL, NULL, NULL); + if (err <= 0) { + FD_SET(serial, &rset); + continue; + } + synology_read_serial_events(); + FD_SET(serial, &rset); + } + + return NULL; +} + +static int synology_serial_poll(void) +{ + return pthread_create(&synology_thread, NULL, serial_poll, NULL); +} + +static int set_nonblock(int fd) +{ + int flags = fcntl(fd, F_GETFL, 0); + if (flags < 0) + flags = 0; + return fcntl(fd, F_SETFL, flags | O_NONBLOCK); +} + +static int synology_serial_open(char *device) +{ + int err; + + if ((serial = open(device , O_RDWR)) < 0) { + print_log(LOG_ERR, "Failed to open %s: %s", device, + strerror(errno)); + return -1; + } + err = set_nonblock(serial); + if (err < 0) { + print_log(LOG_ERR, "Error setting nonblock: %s", + strerror(errno)); + return -1; + } + + tcgetattr(serial, &oldtio); + memset(&newtio, 0, sizeof(newtio)); + + newtio.c_iflag |= IGNBRK; + newtio.c_lflag &= ~(ISIG | ICANON | ECHO); + newtio.c_cflag = B9600 | CS8 | CLOCAL | CREAD; + newtio.c_cc[VMIN] = 1; + newtio.c_cc[VTIME] = 0; + cfsetospeed(&newtio, B9600); + cfsetispeed(&newtio, B9600); + + err = tcsetattr(serial, TCSANOW, &newtio); + if (err < 0) { + print_log(LOG_ERR, "Failed to set attributes for %s: %s", + device, strerror(errno)); + return -1; + } + + return 0; +} + +static void synology_serial_close(void) +{ + tcsetattr(serial, TCSANOW, &oldtio); + close(serial); +} + +static int synology_cmd_powerled(int argc, const char **argv) +{ + unsigned char code = 0; + + if (argc != 1) + return -1; + + if (strcmp(argv[0], "on") == 0) + code = SYNOLOGY_PICCMD_POWER_LED_ON; + else if (strcmp(argv[0], "2hz") == 0) + code = SYNOLOGY_PICCMD_POWER_LED_2HZ; + else if (strcmp(argv[0], "off") == 0) + code = SYNOLOGY_PICCMD_POWER_LED_OFF; + else + return -1; + + return synology_serial_write(&code, 1); +} + +static int synology_cmd_statusled(int argc, const char **argv) +{ + unsigned char code = 0; + + if (argc != 1) + return -1; + + if (strcmp(argv[0], "orange2hz") == 0) + code = SYNOLOGY_PICCMD_STATUS_ORANGE_2HZ; + else if (strcmp(argv[0], "green2hz") == 0) + code = SYNOLOGY_PICCMD_STATUS_GREEN_2HZ; + else if (strcmp(argv[0], "orangeon") == 0) + code = SYNOLOGY_PICCMD_STATUS_ORANGE_ON; + else if (strcmp(argv[0], "greenon") == 0) + code = SYNOLOGY_PICCMD_STATUS_GREEN_ON; + else if (strcmp(argv[0], "off") == 0) + code = SYNOLOGY_PICCMD_STATUS_OFF; + else + return -1; + + return synology_serial_write(&code, 1); +} + +static int synology_cmd_usbled(int argc, const char **argv) +{ + unsigned char code = 0; + + if (argc != 1) + return -1; + + if (strcmp(argv[0], "on") == 0) + code = SYNOLOGY_PICCMD_USB_LED_ON; + else if (strcmp(argv[0], "2hz") == 0) + code = SYNOLOGY_PICCMD_USB_LED_2HZ; + else if (strcmp(argv[0], "off") == 0) + code = SYNOLOGY_PICCMD_USB_LED_OFF; + else + return -1; + + return synology_serial_write(&code, 1); +} + +static int synology_cmd_autopower(int argc, const char **argv) +{ + unsigned char code = 0; + + if (argc != 1) + return -1; + + if (strcmp(argv[0], "on") == 0) + code = SYNOLOGY_PICCMD_AUTOPOWER_ON; + else if (strcmp(argv[0], "off") == 0) + code = SYNOLOGY_PICCMD_AUTOPOWER_OFF; + else + return -1; + + return synology_serial_write(&code, 1); +} + +static int synology_cmd_buzzer(int argc, const char **argv) +{ + unsigned char code = 0; + + if (argc != 1) + return -1; + + if (strcmp(argv[0], "short") == 0) + code = SYNOLOGY_PICCMD_BUZZER_SHORT; + else if (strcmp(argv[0], "long") == 0) + code = SYNOLOGY_PICCMD_BUZZER_LONG; + else + return -1; + + return synology_serial_write(&code, 1); +} + +static int synology_cmd_rtc(int argc, const char **argv) +{ + unsigned char code = 0; + + if (argc != 1) + return -1; + + if (strcmp(argv[0], "on") == 0) + code = SYNOLOGY_PICCMD_RTC_ENABLE; + else if (strcmp(argv[0], "off") == 0) + code = SYNOLOGY_PICCMD_RTC_DISABLE; + else + return -1; + + return synology_serial_write(&code, 1); +} + +static int synology_init(int argc, const char **argv UNUSED) +{ + int err; + + if (argc > 0) { + print_log(LOG_ERR, "synology: module takes no arguments"); + return -1; + } + + err = synology_serial_open("/dev/ttyS1"); + if (err < 0) + return err; + +#define REGISTER1(name,shorthelp,longhelp) do { \ + int err = register_command(#name, shorthelp, longhelp, synology_cmd_##name); \ + if (err < 0) return err; \ +} while(0) + + REGISTER1(powerled, "Change the power LED", + "Change the power LED, options are:\n" + "\ton\n\toff\n\t2hz\n"); + REGISTER1(statusled, "Change the status LED", + "Change the status LED, options are:\n" + "\torange2hz\n\tgreen2hz\n" + "\torangeon\n\tgreenon\n\toff\n"); + REGISTER1(usbled, "Set the usbled", + "Set the usbled, options are:\n" + "\ton\n\t2hz\n\toff\n"); + REGISTER1(autopower, "Control the automatic power mechanism", + "Control the automatic power mechanism, options are:\n" + "\ton\n\toff\n"); + REGISTER1(buzzer, "Buzz", + "Buzz, options are:\n" + "\tshort\n\tlong\n"); + REGISTER1(rtc, "Control RTC (real time clock)", + "Control RTC, options are:\n" + "\ton\n\toff"); + +#undef REGISTER1 + return synology_serial_poll(); +} + +static void synology_exit(void) +{ + synology_serial_close(); +} + +struct picmodule synology_module = { + .name = "synology", + .init = synology_init, + .exit = synology_exit, +}; diff --git a/synology.h b/synology.h new file mode 100644 index 0000000..f5da61f --- /dev/null +++ b/synology.h @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2014 Ben Peddell (klightspeed@killerwolves.net) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef SYNOLOGY_PIC_H +#define SYNOLOGY_PIC_H + +/* + * SYNOLOGY_PICCMD_* -- Command codes sent to PIC + * SYNOLOGY_PICSTS_* -- Status codes received from PIC + */ +#define SYNOLOGY_PICSTS_POWER_BUTTON 0x30 +#define SYNOLOGY_PICSTS_MEDIA_BUTTON 0x60 +#define SYNOLOGY_PICSTS_RESET_BUTTON 0x61 + +#define SYNOLOGY_PICCMD_AUTOPOWER_ON 0x71 +#define SYNOLOGY_PICCMD_AUTOPOWER_OFF 0x70 + +#define SYNOLOGY_PICCMD_POWER_LED_ON 0x34 +#define SYNOLOGY_PICCMD_POWER_LED_2HZ 0x35 +#define SYNOLOGY_PICCMD_POWER_LED_OFF 0x36 + +#define SYNOLOGY_PICCMD_BUZZER_SHORT 0x32 +#define SYNOLOGY_PICCMD_BUZZER_LONG 0x33 + +#define SYNOLOGY_PICCMD_STATUS_GREEN_ON 0x38 +#define SYNOLOGY_PICCMD_STATUS_ORANGE_ON 0x3A +#define SYNOLOGY_PICCMD_STATUS_OFF 0x37 +#define SYNOLOGY_PICCMD_STATUS_GREEN_2HZ 0x39 +#define SYNOLOGY_PICCMD_STATUS_ORANGE_2HZ 0x3B + +#define SYNOLOGY_PICCMD_USB_LED_ON 0x40 +#define SYNOLOGY_PICCMD_USB_LED_2HZ 0x41 +#define SYNOLOGY_PICCMD_USB_LED_OFF 0x42 + +#define SYNOLOGY_PICCMD_RTC_ENABLE 0x73 +#define SYNOLOGY_PICCMD_RTC_DISABLE 0x72 + +#endif diff --git a/system.c b/system.c new file mode 100644 index 0000000..9a43d91 --- /dev/null +++ b/system.c @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2013 Ian Campbell (ijc@hellion.org.uk) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <syslog.h> +#include "picmodule.h" + +static int system_status(int argc, const char **argv) +{ + if (argc != 1) + return -1; + + call_function("system_status", "%s", argv[0]); + return 0; +} + +static int system_init(int argc, const char **argv UNUSED) +{ + if (argc > 0) { + print_log(LOG_ERR, "%s: module does not take any arguments\n", + __func__); + return -1; + } + + register_command("system-status", + "Signal system status", + "Signal system status, options are:\n" + "\tstart\n\tstop\n", + system_status); + return 0; +} + +struct picmodule system_module = { + .name = "system-status", + .init = system_init, +}; diff --git a/systemd/qcontrol.service b/systemd/qcontrol.service new file mode 100644 index 0000000..0fc515c --- /dev/null +++ b/systemd/qcontrol.service @@ -0,0 +1,19 @@ +[Unit] +Description=qcontrol +Requires=qcontrold.service +After=multi-user.target qcontrold.service +# If the config file is there, we assume qcontrol works on this machine. +ConditionPathExists=/etc/qcontrol.conf + +[Service] +Type=oneshot +RemainAfterExit=true +ExecStart=/usr/sbin/qcontrol statusled greenon +ExecStart=/usr/sbin/qcontrol powerled on +ExecStart=/usr/sbin/qcontrol buzzer short +ExecStop=/usr/sbin/qcontrol buzzer short +ExecStop=/usr/sbin/qcontrol statusled redon +ExecStop=/usr/sbin/qcontrol powerled 1hz + +[Install] +WantedBy=multi-user.target diff --git a/systemd/qcontrold.service b/systemd/qcontrold.service new file mode 100644 index 0000000..6e2164d --- /dev/null +++ b/systemd/qcontrold.service @@ -0,0 +1,12 @@ +[Unit] +Description=qcontrold +Requires=dev-input-by\x2dpath-platform\x2dgpio_keys\x2devent.device +After=dev-input-by\x2dpath-platform\x2dgpio_keys\x2devent.device +# If the config file is there, we assume qcontrol works on this machine. +ConditionPathExists=/etc/qcontrol.conf + +[Service] +ExecStart=/usr/sbin/qcontrol -f + +[Install] +WantedBy=multi-user.target diff --git a/systemd/qcontrold.socket b/systemd/qcontrold.socket new file mode 100644 index 0000000..983d5c2 --- /dev/null +++ b/systemd/qcontrold.socket @@ -0,0 +1,5 @@ +[Socket] +ListenStream=/run/qcontrol.sock + +[Install] +WantedBy=sockets.target @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2007-2008 Byron Bradley (byron.bbradley@gmail.com) + * Copyright (C) 2013 Ian Campbell (ijc@hellion.org.uk) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <lua.h> +#include <lualib.h> +#include <lauxlib.h> +#include <string.h> +#include <syslog.h> +#include <linux/input.h> + +#include "picmodule.h" +#include "qnap-pic.h" + +static int ts209_read_serial_events(void) +{ + unsigned char buf[100]; + int err = qnap_serial_read(buf, 100); + if (err < 0) + return err; + switch (buf[0]) { + case QNAP_PICSTS_POWER_BUTTON: + call_function("power_button", "%d", 3); + break; + case QNAP_PICSTS_POWER_LOSS_POWER_OFF: + /* RTC Wake-Up (ignored) */ + break; + case QNAP_PICSTS_FAN1_ERROR: + call_function("fan_error", ""); + break; + case QNAP_PICSTS_FAN1_NORMAL: + call_function("fan_normal", ""); + break; + case QNAP_PICSTS_TEMP_WARM_TO_HOT: + case QNAP_PICSTS_TEMP_COLD_TO_WARM: + call_function("temp_low", ""); + break; + case QNAP_PICSTS_TEMP_HOT_TO_WARM: + case QNAP_PICSTS_TEMP_WARM_TO_COLD: + call_function("temp_high", ""); + break; + default: + print_log(LOG_WARNING, "(PIC 0x%x) unknown command from PIC", + buf[0]); + } + + return -1; +} + +static int ts209_init(int argc, const char **argv UNUSED) +{ + int err; + + if (argc > 0) { + print_log(LOG_ERR, "ts209: module takes no arguments"); + return -1; + } + + err = qnap_serial_open("/dev/ttyS1"); + if (err < 0) + return err; + + err = qnap_register_commands(QNAP_PIC_FEATURE_POWERLED| + QNAP_PIC_FEATURE_STATUSLED| + QNAP_PIC_FEATURE_USBLED| + QNAP_PIC_FEATURE_AUTOPOWER| + QNAP_PIC_FEATURE_BUZZER| + QNAP_PIC_FEATURE_FANSPEED); + if (err < 0) + return err; + + return qnap_serial_poll(&ts209_read_serial_events); +} + +static void ts209_exit(void) +{ + qnap_serial_close(); +} + +struct picmodule ts209_module = { + .name = "ts209", + .init = ts209_init, + .exit = ts209_exit, +}; @@ -0,0 +1,111 @@ +/* + * Copyright (C) 2007-2008 Byron Bradley (byron.bbradley@gmail.com) + * Copyright (C) 2008, 2009 Martin Michlmayr (tbm@cyrius.com) + * Copyright (C) 2013 Michael Stapelberg (michael+qnap@stapelberg.de) + * Copyright (C) 2013 Ian Campbell (ijc@hellion.org.uk) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <lua.h> +#include <lualib.h> +#include <lauxlib.h> +#include <string.h> +#include <syslog.h> +#include <linux/input.h> + +#include "picmodule.h" +#include "qnap-pic.h" + +static int ts219_read_serial_events(void) +{ + unsigned char buf[100]; + int err = qnap_serial_read(buf, 100); + if (err < 0) + return err; + switch (buf[0]) { + case QNAP_PICSTS_POWER_BUTTON: + call_function("power_button", "%d", 3); + break; + case QNAP_PICSTS_POWER_LOSS_POWER_OFF: + /* RTC Wake-Up (ignored) */ + break; + case QNAP_PICSTS_FAN1_ERROR: + case QNAP_PICSTS_FAN2_ERROR: + case QNAP_PICSTS_FAN3_ERROR: + case QNAP_PICSTS_FAN4_ERROR: + call_function("fan_error", ""); + break; + case QNAP_PICSTS_FAN1_NORMAL: + case QNAP_PICSTS_FAN2_NORMAL: + case QNAP_PICSTS_FAN3_NORMAL: + case QNAP_PICSTS_FAN4_NORMAL: + call_function("fan_normal", ""); + break; + case QNAP_PICSTS_SYS_TEMP_0 ... QNAP_PICSTS_SYS_TEMP_70: + call_function("temp", "%d", buf[0] - QNAP_PICSTS_SYS_TEMP_0); + break; + case QNAP_PICSTS_SYS_TEMP_71_79: + call_function("temp", "%d", 75); + break; + case QNAP_PICSTS_SYS_TEMP_80: + call_function("temp", "%d", 80); + break; + default: + print_log(LOG_WARNING, "(PIC 0x%x) unknown command from PIC", + buf[0]); + } + + return -1; +} + +static int ts219_init(int argc, const char **argv UNUSED) +{ + int err; + + if (argc > 0) { + print_log(LOG_ERR, "ts219: module takes no arguments"); + return -1; + } + + err = qnap_serial_open("/dev/ttyS1"); + if (err < 0) + return err; + + err = qnap_register_commands(QNAP_PIC_FEATURE_POWERLED| + QNAP_PIC_FEATURE_STATUSLED| + QNAP_PIC_FEATURE_USBLED| + QNAP_PIC_FEATURE_AUTOPOWER| + QNAP_PIC_FEATURE_BUZZER| + QNAP_PIC_FEATURE_FANSPEED| + QNAP_PIC_FEATURE_WATCHDOG| + QNAP_PIC_FEATURE_WOL| + QNAP_PIC_FEATURE_EUP| + QNAP_PIC_FEATURE_RTC); + if (err < 0) + return err; + + return qnap_serial_poll(&ts219_read_serial_events); +} + +static void ts219_exit(void) +{ + qnap_serial_close(); +} + +struct picmodule ts219_module = { + .name = "ts219", + .init = ts219_init, + .exit = ts219_exit, +}; @@ -0,0 +1,105 @@ +/* + * Copyright (C) 2007-2008 Byron Bradley (byron.bbradley@gmail.com) + * Copyright (C) 2008 Martin Michlmayr (tbm@cyrius.com) + * Copyright (C) 2013 Ian Campbell (ijc@hellion.org.uk) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <lua.h> +#include <lualib.h> +#include <lauxlib.h> +#include <string.h> +#include <syslog.h> +#include <linux/input.h> + +#include "picmodule.h" +#include "qnap-pic.h" + +static int ts409_read_serial_events(void) +{ + unsigned char buf[100]; + int err = qnap_serial_read(buf, 100); + if (err < 0) + return err; + switch (buf[0]) { + case QNAP_PICSTS_POWER_BUTTON: + call_function("power_button", "%d", 3); + break; + case QNAP_PICSTS_POWER_LOSS_POWER_OFF: + /* RTC Wake-Up (ignored) */ + break; + case QNAP_PICSTS_FAN1_ERROR: + case QNAP_PICSTS_FAN2_ERROR: + case QNAP_PICSTS_FAN3_ERROR: + case QNAP_PICSTS_FAN4_ERROR: + call_function("fan_error", ""); + break; + case QNAP_PICSTS_FAN1_NORMAL: + case QNAP_PICSTS_FAN2_NORMAL: + case QNAP_PICSTS_FAN3_NORMAL: + case QNAP_PICSTS_FAN4_NORMAL: + call_function("fan_normal", ""); + break; + case QNAP_PICSTS_SYS_TEMP_0 ... QNAP_PICSTS_SYS_TEMP_70: + call_function("temp", "%d", buf[0] - QNAP_PICSTS_SYS_TEMP_0); + break; + case QNAP_PICSTS_SYS_TEMP_71_79: + call_function("temp", "%d", 75); + break; + case QNAP_PICSTS_SYS_TEMP_80: + call_function("temp", "%d", 80); + break; + default: + print_log(LOG_WARNING, "(PIC 0x%x) unknown command from PIC", + buf[0]); + } + + return -1; +} + +static int ts409_init(int argc, const char **argv UNUSED) +{ + int err; + + if (argc > 0) { + print_log(LOG_ERR, "ts409: module takes no arguments"); + return -1; + } + + err = qnap_serial_open("/dev/ttyS1"); + if (err < 0) + return err; + + err = qnap_register_commands(QNAP_PIC_FEATURE_STATUSLED| + QNAP_PIC_FEATURE_USBLED| + QNAP_PIC_FEATURE_AUTOPOWER| + QNAP_PIC_FEATURE_BUZZER| + QNAP_PIC_FEATURE_FANSPEED); + if (err < 0) + return err; + + return qnap_serial_poll(&ts409_read_serial_events); +} + +static void ts409_exit(void) +{ + qnap_serial_close(); +} + +struct picmodule ts409_module = { + .name = "ts409", + .init = ts409_init, + .exit = ts409_exit, +}; @@ -0,0 +1,111 @@ +/* + * Copyright (C) 2007-2008 Byron Bradley (byron.bbradley@gmail.com) + * Copyright (C) 2008, 2009 Martin Michlmayr (tbm@cyrius.com) + * Copyright (C) 2013 Michael Stapelberg (michael+qnap@stapelberg.de) + * Copyright (C) 2013 Ian Campbell (ijc@hellion.org.uk) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <lua.h> +#include <lualib.h> +#include <lauxlib.h> +#include <string.h> +#include <syslog.h> +#include <linux/input.h> + +#include "picmodule.h" +#include "qnap-pic.h" + +static int ts41x_read_serial_events(void) +{ + unsigned char buf[100]; + int err = qnap_serial_read(buf, 100); + if (err < 0) + return err; + switch (buf[0]) { + case QNAP_PICSTS_POWER_BUTTON: + call_function("power_button", "%d", 3); + break; + case QNAP_PICSTS_POWER_LOSS_POWER_OFF: + /* RTC Wake-Up (ignored) */ + break; + case QNAP_PICSTS_FAN1_ERROR: + case QNAP_PICSTS_FAN2_ERROR: + case QNAP_PICSTS_FAN3_ERROR: + case QNAP_PICSTS_FAN4_ERROR: + call_function("fan_error", ""); + break; + case QNAP_PICSTS_FAN1_NORMAL: + case QNAP_PICSTS_FAN2_NORMAL: + case QNAP_PICSTS_FAN3_NORMAL: + case QNAP_PICSTS_FAN4_NORMAL: + call_function("fan_normal", ""); + break; + case QNAP_PICSTS_SYS_TEMP_0 ... QNAP_PICSTS_SYS_TEMP_70: + call_function("temp", "%d", buf[0] - QNAP_PICSTS_SYS_TEMP_0); + break; + case QNAP_PICSTS_SYS_TEMP_71_79: + call_function("temp", "%d", 75); + break; + case QNAP_PICSTS_SYS_TEMP_80: + call_function("temp", "%d", 80); + break; + default: + print_log(LOG_WARNING, "(PIC 0x%x) unknown command from PIC", + buf[0]); + } + + return -1; +} + +static int ts41x_init(int argc, const char **argv UNUSED) +{ + int err; + + if (argc > 0) { + print_log(LOG_ERR, "ts41x: module takes no arguments"); + return -1; + } + + err = qnap_serial_open("/dev/ttyS1"); + if (err < 0) + return err; + + err = qnap_register_commands(QNAP_PIC_FEATURE_POWERLED| + QNAP_PIC_FEATURE_STATUSLED| + QNAP_PIC_FEATURE_USBLED| + QNAP_PIC_FEATURE_AUTOPOWER| + QNAP_PIC_FEATURE_BUZZER| + QNAP_PIC_FEATURE_FANSPEED| + QNAP_PIC_FEATURE_WATCHDOG| + QNAP_PIC_FEATURE_WOL| + QNAP_PIC_FEATURE_EUP| + QNAP_PIC_FEATURE_RTC); + if (err < 0) + return err; + + return qnap_serial_poll(&ts41x_read_serial_events); +} + +static void ts41x_exit(void) +{ + qnap_serial_close(); +} + +struct picmodule ts41x_module = { + .name = "ts41x", + .init = ts41x_init, + .exit = ts41x_exit, +}; |