diff --git a/OpenMQTTGateway.ino b/OpenMQTTGateway.ino index 5a2453a..9371b5b 100644 --- a/OpenMQTTGateway.ino +++ b/OpenMQTTGateway.ino @@ -61,7 +61,7 @@ #include // Modules config inclusion -#if defined(ZgatewayRF) || defined(ZgatewayRF2) +#if defined(ZgatewayRF) || defined(ZgatewayRF2) || defined(ZgatewayPilight) #include "config_RF.h" #endif #ifdef ZgatewayRF315 @@ -360,6 +360,9 @@ void setup() #ifdef ZgatewayRF2 setupRF2(); #endif + #ifdef ZgatewayPilight + setupPilight(); + #endif #ifdef ZgatewaySRFB setupSRFB(); #endif @@ -653,6 +656,13 @@ void loop() timer_led_receive = millis(); } #endif + #ifdef ZgatewayPilight + if(PilighttoMQTT()){ + trc(F("PilighttoMQTT OK")); + digitalWrite(led_receive, LOW); + timer_led_receive = millis(); + } + #endif #ifdef ZgatewaySRFB if(SRFBtoMQTT()) trc(F("SRFBtoMQTT OK")); @@ -739,6 +749,9 @@ void stateMeasures(){ #ifdef ZgatewayRF2 modules = modules + ZgatewayRF2; #endif + #ifdef ZgatewayPilight + modules = modules + ZgatewayPilight; + #endif #ifdef ZgatewaySRFB modules = modules + ZgatewaySRFB; #endif @@ -832,6 +845,9 @@ digitalWrite(led_send, LOW); #ifdef ZgatewayRF2 MQTTtoRF2(topicOri, datacallback); #endif +#ifdef ZgatewayPilight + MQTTtoPilight(topicOri, datacallback); +#endif #ifdef Zgateway2G MQTTto2G(topicOri, datacallback); #endif diff --git a/User_config.h b/User_config.h index 00d9005..5a01352 100644 --- a/User_config.h +++ b/User_config.h @@ -93,6 +93,7 @@ const byte subnet[] = { 255, 255, 255, 0 }; //ip adress #define ZgatewayIR "IR" //ESP8266, Arduino, Sonoff RF Bridge #define ZgatewayBT "BT" //ESP8266, Arduino, ESP32 //#define ZgatewayRF2 "RF2" //ESP8266, Arduino, ESP32 +//#define ZgatewayPilight "Pilight" //ESP8266 //#define ZgatewaySRFB "SRFB" // Sonoff RF Bridge //#define Zgateway2G "2G" //ESP8266, Arduino, ESP32 //#define ZactuatorONOFF "ONOFF" //ESP8266, Arduino, ESP32, Sonoff RF Bridge diff --git a/ZgatewayPilight.ino b/ZgatewayPilight.ino new file mode 100644 index 0000000..3e62daf --- /dev/null +++ b/ZgatewayPilight.ino @@ -0,0 +1,160 @@ +/* + OpenMQTTGateway - ESP8266 or Arduino program for home automation + + Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker + Send and receiving command by MQTT + + This gateway enables to: + - publish MQTT data to a different topic related to received 433Mhz signal using ESPiLight + + Copyright: (c)Florian ROBERT + Copyright: (c)Randy Simons http://randysimons.nl/ + + This file is part of OpenMQTTGateway. + + OpenMQTTGateway 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. + + OpenMQTTGateway 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 . +*/ + +#ifdef ZgatewayPilight + +#include + +ESPiLight rf(RF_EMITTER_PIN); // use -1 to disable transmitter + +void setupPilight(){ + #ifndef ZgatewayRF && ZgatewayRF2 //receiving with Pilight is not compatible with ZgatewayRF or RF2 as far as I can tell + rf.setCallback(pilightCallback); + rf.initReceiver(RF_RECEIVER_PIN); + trc(F("RF_EMITTER_PIN ")); + trc(String(RF_EMITTER_PIN)); + trc(F("RF_RECEIVER_PIN ")); + trc(String(RF_RECEIVER_PIN)); + trc(F("ZgatewayPilight setup done ")); + #endif +} + +boolean PilighttoMQTT(){ + rf.loop(); +} + +void pilightCallback(const String &protocol, const String &message, int status, + size_t repeats, const String &deviceID) { + + if (status == VALID) { + + trc(F("Rcv. Pilight")); + String MQTTprotocol; + String MQTTdeviceID; + String MQTTmessage; + + MQTTprotocol = String(protocol); + MQTTdeviceID = String(deviceID); + MQTTmessage = String(message); + String MQTTPilightstring; + MQTTPilightstring = subjectPilighttoMQTT+String("/")+MQTTprotocol+String("/")+MQTTdeviceID; + trc(F("Adv data PilighttoMQTT")); + client.publish((char *)MQTTPilightstring.c_str(),(char *)MQTTmessage.c_str()); + + } +} + +void MQTTtoPilight(char * topicOri, char * datacallback) { + + String topic = topicOri; + + int pos = topic.lastIndexOf(subjectMQTTtoPilight); + if (pos == -1){ + return; + } + + trc(F("Adv data PilighttoMQTT")); + + char *last = strrchr(topicOri, '/'); + String protocol = ""; + + if (last+1 != NULL) { + protocol = last+1; + } + + int result = 0; + + if (protocol == PilightRAW){ + trc(F("RAW:")); + trc(String(datacallback)); + + uint16_t rawpulses[MAXPULSESTREAMLENGTH]; + int rawlen = + rf.stringToPulseTrain(datacallback, rawpulses, MAXPULSESTREAMLENGTH); + if (rawlen > 0) { + rf.sendPulseTrain(rawpulses, rawlen); + result = rawlen; + } else { + result = -9999; + } + } else { + trc(F("PROTO:")); + trc(protocol); + result = rf.send(protocol, datacallback); + } + + String MQTTmessage; + String MQTTprotocol = String(protocol); + String MQTTPilightstring = subjectPilighttoMQTT+String("/")+MQTTprotocol+String("/"); + + if (result > 0) { + trc(F("Adv data MQTTtoPilight push state via PilighttoMQTT")); + + MQTTmessage = String(datacallback); + + char *deviceID = "0"; + + DynamicJsonBuffer jb; + JsonObject& rjson = jb.parseObject(MQTTmessage); + if (rjson.success()) { + strcpy(deviceID, rjson["id"]); + } + + MQTTmessage = String(datacallback); + MQTTPilightstring += String(deviceID); + + } else { + switch (result) { + case ESPiLight::ERROR_UNAVAILABLE_PROTOCOL: + MQTTmessage = "protocol is not avaiable"; + break; + case ESPiLight::ERROR_INVALID_PILIGHT_MSG: + MQTTmessage = "message is invalid"; + break; + case ESPiLight::ERROR_INVALID_JSON: + MQTTmessage = "message is not a proper json object"; + break; + case ESPiLight::ERROR_NO_OUTPUT_PIN: + MQTTmessage = "no transmitter pin"; + break; + case -9999: + MQTTmessage = "invalid pulse train message"; + break; + } + trc(F("ESPiLight Error: ")); + trc(String(MQTTmessage)); + MQTTPilightstring += String("error"); + + } + + client.publish((char *)MQTTPilightstring.c_str(),(char *)MQTTmessage.c_str()); + +} + +#endif + diff --git a/config_RF.h b/config_RF.h index 7835f65..6c4a2d3 100644 --- a/config_RF.h +++ b/config_RF.h @@ -60,6 +60,12 @@ RF supported protocols #define RF2groupKey "GROUP_" // number of your group value will be defined if a subject contains RF2groupKey followed by a value of 1 digit #define RF2dimKey "DIM" // number of your dim value will be defined if a subject contains RF2dimKey and the payload contains the dim value as digits +/*-------------------ESPPiLight topics & parameters----------------------*/ +//433Mhz Pilight MQTT Subjects and keys +#define subjectMQTTtoPilight Base_Topic Gateway_Name "/commands/MQTTtoPilight" +#define subjectPilighttoMQTT Base_Topic Gateway_Name "/PilighttoMQTT" +#define PilightRAW "RAW" + /*-------------------PIN DEFINITIONS----------------------*/ #ifdef ESP8266 #define RF_RECEIVER_PIN 0 // D3 on nodemcu diff --git a/lib/ESPiLight/.gitignore b/lib/ESPiLight/.gitignore new file mode 100755 index 0000000..b25c15b --- /dev/null +++ b/lib/ESPiLight/.gitignore @@ -0,0 +1 @@ +*~ diff --git a/lib/ESPiLight/.gitmodules b/lib/ESPiLight/.gitmodules new file mode 100755 index 0000000..dfdafd0 --- /dev/null +++ b/lib/ESPiLight/.gitmodules @@ -0,0 +1,4 @@ +[submodule "pilight"] + path = pilight + url = https://github.com/pilight/pilight + branch = development diff --git a/lib/ESPiLight/.travis.yml b/lib/ESPiLight/.travis.yml new file mode 100755 index 0000000..66d26e2 --- /dev/null +++ b/lib/ESPiLight/.travis.yml @@ -0,0 +1,26 @@ +language: python +python: + - "2.7" + +# Cache PlatformIO packages using Travis CI container-based infrastructure +sudo: false +cache: + directories: + - "~/.platformio" + +env: + - PLATFORMIO_CI_SRC=tests/test_parse + - PLATFORMIO_CI_SRC=tests/test_proto_limit + - PLATFORMIO_CI_SRC=tests/test_echo + - PLATFORMIO_CI_SRC=examples/Receive + - PLATFORMIO_CI_SRC=examples/Receive_Raw + - PLATFORMIO_CI_SRC=examples/Transmit + - PLATFORMIO_CI_SRC=examples/Transmit_Raw + +install: + # PlatformIO + - pip install -U platformio + +script: + - platformio ci --lib="." --board=huzzah --board=d1_mini --board=esp32dev + - make stylecheck diff --git a/lib/ESPiLight/LICENSE.txt b/lib/ESPiLight/LICENSE.txt new file mode 100755 index 0000000..94a9ed0 --- /dev/null +++ b/lib/ESPiLight/LICENSE.txt @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + 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. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 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 . + +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: + + Copyright (C) + 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 +. + + 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 +. diff --git a/lib/ESPiLight/Makefile b/lib/ESPiLight/Makefile new file mode 100755 index 0000000..d855a82 --- /dev/null +++ b/lib/ESPiLight/Makefile @@ -0,0 +1,91 @@ +# ESPiLight - pilight 433.92 MHz protocols library for Arduino +# Copyright (c) 2016 Puuu. All right reserved. +# +# Project home: https://github.com/puuu/espilight/ +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 3 of the License, or (at your option) any later version. +# This library 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 +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with library. If not, see + +SRC_DIR = pilight + +DST_DIR = src/pilight +PROTOCOL_DIR = libs/pilight/protocols/433.92 + +PROTOCOLS = $(patsubst $(SRC_DIR)/$(PROTOCOL_DIR)/%.h,%,$(sort $(wildcard $(SRC_DIR)/$(PROTOCOL_DIR)/*.h))) + +PILIGHT_FILES = libs/pilight/core/dso.h libs/pilight/core/mem.h \ + libs/pilight/core/json.h libs/pilight/core/json.c \ + libs/pilight/core/binary.h libs/pilight/core/binary.c \ + libs/pilight/protocols/protocol_header.h \ + libs/pilight/protocols/protocol_init.h +PROTOCOL_H_FILES = $(foreach protocol,$(PROTOCOLS),$(PROTOCOL_DIR)/$(protocol).h) +PROTOCOL_C_FILES = $(foreach protocol,$(PROTOCOLS),$(PROTOCOL_DIR)/$(protocol).c) +FILES = $(PILIGHT_FILES) $(PROTOCOL_H_FILES) $(PROTOCOL_C_FILES) + +DST_FILES = $(foreach file,$(FILES),$(DST_DIR)/$(file)) + +.PHONY: all clean copy update release + +all: $(SRC_DIR)/libs + $(MAKE) -e copy + +copy: $(DST_FILES) + +$(DST_DIR)/%: $(SRC_DIR)/% + @mkdir -p $(@D) + cp $< $@ + +$(DST_DIR)/libs/pilight/core/json.c: $(SRC_DIR)/libs/pilight/core/json.c + @mkdir -p $(@D) + cp $< $@ +# ESP8266 Android, sprintf not working with float. Patch: + sed 's/sprintf(buf, "%.*f", decimals, num);/dtostrf(num, 0, decimals, buf);/' -i $@ +# Arduino did not provide printf, fprintf + sed 's!#include !#include \n#include "../../../../tools/aprintf.h"!' -i $@ + +$(DST_DIR)/libs/pilight/protocols/protocol_header.h: + for protocol in $(PROTOCOLS); do\ + echo "#include \"433.92/$${protocol}.h\"" >> $@;\ + done + +$(DST_DIR)/libs/pilight/protocols/protocol_init.h: $(foreach file,$(PROTOCOL_C_FILES),$(DST_DIR)/$(file)) + for cfile in $^; do\ + grep 'void .*Init(' $$cfile | sed 's/void \(.*Init\)(.*/\1();/' >> $@;\ + done + +pilight/libs: + git submodule update --init pilight + +update: + $(MAKE) clean + git submodule update pilight + $(MAKE) copy + +release: $(SRC_DIR)/libs + git checkout release + git merge master + make update + git add src/pilight + git commit -m "integrate new pilight files" + @echo "change log:" + @git log --pretty="* %s" --no-merges HEAD...`git describe --abbrev=0 --tags` + @git submodule summary `git describe --abbrev=0 --tags` + @echo "run: git tag -a v"`grep version library.properties | sed 's/version=\(.*\)/\1/g'` + +clean: + -rm $(DST_FILES) + +stylecheck: + RESULT=0;\ + for file in src/*.h src/*.cpp src/tools/*.h src/tools/*.cpp tests/*/*.ino examples/*/*.ino; do\ + clang-format -style=google "$$file" | diff -u "$$file" - || RESULT=$$?;\ + done;\ + exit $$RESULT diff --git a/lib/ESPiLight/README.md b/lib/ESPiLight/README.md new file mode 100755 index 0000000..370c3fb --- /dev/null +++ b/lib/ESPiLight/README.md @@ -0,0 +1,113 @@ +[![Build Status](https://travis-ci.org/puuu/ESPiLight.svg?branch=master)](https://travis-ci.org/puuu/ESPiLight) + +# ESPiLight + +This Arduino library is a port of the [pilight](https://pilight.org/) +433.92MHz protocols to the Arduino platform. It was tested with a +ESP8266. The aim is to transmit, receive and parse many 433.92MHz +protocols, by providing a simple Arduino friendly API. This should +help to implement IoT bridges between the 434MHz-RF band and internet +protocols. + +By porting the C modules of the pilight protocols, allows to +participate on the excellent work of the pilight community. Thus, +supporting many protocols for rc switches and weather stations without +reimplementing them. + +A list of supported protocols can be found in the pilight manual: +https://manual.pilight.org/protocols/433.92/index.html + + +## Installation + +This library can be easily installed with the Arduino Library Manager. + + +## Usage + +Please have a look to the examples. + + +### Requirements + +This library was tested and developed for the +[ESP8266](https://github.com/esp8266/Arduino). It may not run on a +ATmega-based boards, because of memory usage. + +For transmitting and receiving you need 434MHz-RF modules. More +information can be found here: +- https://wiki.pilight.org/receivers +- https://wiki.pilight.org/senders +- https://github.com/sui77/rc-switch/wiki/List_TransmitterReceiverModules + + +## Contributing + +If you find any bug, feel free to open an issue at github. Also, pull +requests are welcome. The development takes place in the `master` +branch. The `release` is used to integrate the pilight files and for +version tagging, like necessary for the Arduino Library Manager +crawler. + +To prevent formating issues, please make sure that your code is proper +formatted. We use the `clang-format` tool with the `Google` style. +You can just format the code by calling +```console +$ clang-format -style=Google -i +``` + + +### Install from source + +If you are interested to install this library from source, you need to +integrate the pilight source files. Since Arduino build all cpp/c +files in the src directory and sub directories, only necessary files +from pilight will be integrated. This will be done with GNU make. + +On Linux you can run: +```console +$ git clone https://github.com/puuu/ESPiLight/ +$ cd ESPiLight +$ make +$ ln -s `pwd` ~/Arduino/libraries/ +``` + + +#### Update + +To update EPiLight from git run: +```console +$ git pull +$ make update +``` + + +#### New protocols + +ESPiLight only supports the 434MHz protocols supported by +[pilight](https://pilight.org/). If you are missing any protocol, +please report it directly to pilight. After the intergratrion into +pilight it can be merged into ESPILight. + +To report new protocols, please folow the +[rules](https://forum.pilight.org/showthread.php?tid=761) of the +[pilight forum](https://forum.pilight.org/). It is recommended to use +pilight directly. Alternativly, ESPiLight offers the +[`pilight_debug`](examples/pilight_debug/pilight_debug.ino) and +[`pilight_raw`](examples/pilight_raw/pilight_raw.ino) examples that +mimic tht pilight counterparts. + + +## Acknowledgement + +Big thanks goes to the pilight community, which implemented all the +434MHz protocols. If you want to integrate more protocols, please +contribute directly to [pilight](https://pilight.org/). + +[@janLo](https://github.com/janLo) [contributed](https://github.com/puuu/ESPiLight/graphs/contributors) some major cleanups and new functionalities to the library. + +## Other similar projects + +There are other, more lightweight 434Mhz implementations: +- https://github.com/sui77/rc-switch +- https://bitbucket.org/fuzzillogic/433mhzforarduino/wiki/Home diff --git a/lib/ESPiLight/examples/Receive/Receive.ino b/lib/ESPiLight/examples/Receive/Receive.ino new file mode 100755 index 0000000..5355772 --- /dev/null +++ b/lib/ESPiLight/examples/Receive/Receive.ino @@ -0,0 +1,55 @@ +/* + Basic ESPilight receive example + + https://github.com/puuu/espilight +*/ + +#include + +#define RECEIVER_PIN 4 // any intterupt able pin +#define TRANSMITTER_PIN 13 + +ESPiLight rf(TRANSMITTER_PIN); // use -1 to disable transmitter + +// callback function. It is called on successfully received and parsed rc signal +void rfCallback(const String &protocol, const String &message, int status, + size_t repeats, const String &deviceID) { + Serial.print("RF signal arrived ["); + Serial.print(protocol); // protocoll used to parse + Serial.print("]["); + Serial.print(deviceID); // value of id key in json message + Serial.print("] ("); + Serial.print(status); // status of message, depending on repeat, either: + // FIRST - first message of this protocoll within the + // last 0.5 s + // INVALID - message repeat is not equal to the + // previous message + // VALID - message is equal to the previous message + // KNOWN - repeat of a already valid message + Serial.print(") "); + Serial.print(message); // message in json format + Serial.println(); + + // check if message is valid and process it + if (status == VALID) { + Serial.print("Valid message: ["); + Serial.print(protocol); + Serial.print("] "); + Serial.print(message); + Serial.println(); + } +} + +void setup() { + Serial.begin(115200); + // set callback funktion + rf.setCallback(rfCallback); + // inittilize receiver + rf.initReceiver(RECEIVER_PIN); +} + +void loop() { + // process input queue and may fire calllback + rf.loop(); + delay(10); +} diff --git a/lib/ESPiLight/examples/Receive_Raw/Receive_Raw.ino b/lib/ESPiLight/examples/Receive_Raw/Receive_Raw.ino new file mode 100755 index 0000000..1454ea4 --- /dev/null +++ b/lib/ESPiLight/examples/Receive_Raw/Receive_Raw.ino @@ -0,0 +1,43 @@ +/* + Basic ESPiLight receive raw (pulse train) signal example + + https://github.com/puuu/espilight +*/ + +#include + +#define RECEIVER_PIN 4 // any intterupt able pin +#define TRANSMITTER_PIN 13 + +ESPiLight rf(TRANSMITTER_PIN); // use -1 to disable transmitter + +// callback function. It is called on successfully received and parsed rc signal +void rfRawCallback(const uint16_t* codes, size_t length) { + // print pulse lengths + Serial.print("RAW signal: "); + for (unsigned int i = 0; i < length; i++) { + Serial.print(codes[i]); + Serial.print(' '); + } + Serial.println(); + + // format of pilight USB Nano + String data = rf.pulseTrainToString(codes, length); + Serial.print("string format: "); + Serial.print(data); + Serial.println(); +} + +void setup() { + Serial.begin(115200); + // set callback funktion for raw messages + rf.setPulseTrainCallBack(rfRawCallback); + // inittilize receiver + rf.initReceiver(RECEIVER_PIN); +} + +void loop() { + // process input queue and may fire calllback + rf.loop(); + delay(10); +} diff --git a/lib/ESPiLight/examples/Transmit/Transmit.ino b/lib/ESPiLight/examples/Transmit/Transmit.ino new file mode 100755 index 0000000..6e3e4ad --- /dev/null +++ b/lib/ESPiLight/examples/Transmit/Transmit.ino @@ -0,0 +1,21 @@ +/* + Basic ESPiLight transmit example + + https://github.com/puuu/espilight +*/ + +#include + +#define TRANSMITTER_PIN 13 + +ESPiLight rf(TRANSMITTER_PIN); + +void setup() { Serial.begin(115200); } + +// Toggle state of elro 800 switch evrey 2 s +void loop() { + rf.send("elro_800_switch", "{\"systemcode\":17,\"unitcode\":1,\"on\":1}"); + delay(2000); + rf.send("elro_800_switch", "{\"systemcode\":17,\"unitcode\":1,\"off\":1}"); + delay(2000); +} diff --git a/lib/ESPiLight/examples/Transmit_Raw/Transmit_Raw.ino b/lib/ESPiLight/examples/Transmit_Raw/Transmit_Raw.ino new file mode 100755 index 0000000..c9064e1 --- /dev/null +++ b/lib/ESPiLight/examples/Transmit_Raw/Transmit_Raw.ino @@ -0,0 +1,32 @@ +/* + Basic ESPiLight transmit RAW signal example + + https://github.com/puuu/espilight +*/ + +#include + +#define TRANSMITTER_PIN 13 + +ESPiLight rf(TRANSMITTER_PIN); + +void setup() { + Serial.begin(115200); + + int length = 0; + uint16_t codes[MAXPULSESTREAMLENGTH]; + + // get pulse train from string (format see: pilight USB Nano) + length = rf.stringToPulseTrain( + "c:102020202020202020220202020020202200202200202020202020220020202203;p:" + "279,2511,1395,9486@", + codes, MAXPULSESTREAMLENGTH); + + // transmit the pulse train + rf.sendPulseTrain(codes, length); +} + +// Toggle state of elro 800 switch evrey 2 s +void loop() { + // stop +} diff --git a/lib/ESPiLight/examples/pilight_debug/pilight_debug.ino b/lib/ESPiLight/examples/pilight_debug/pilight_debug.ino new file mode 100755 index 0000000..1f5ce72 --- /dev/null +++ b/lib/ESPiLight/examples/pilight_debug/pilight_debug.ino @@ -0,0 +1,81 @@ +/* + Basic ESPiLight pilight_debug example + + This example mimic the output of the piligh_debug tool. + + https://github.com/puuu/espilight +*/ + +#include + +#define PULSE_DIV 34 + +#define RECEIVER_PIN 4 // any intterupt able pin +#define TRANSMITTER_PIN 13 + +ESPiLight rf(TRANSMITTER_PIN); // use -1 to disable transmitter + +unsigned int normalize(unsigned int i, unsigned int pulselen) { + double x; + x = (double)i / pulselen; + + return (unsigned int)(round(x)); +} + +// callback function. It is called on successfully received and parsed rc signal +void rfRawCallback(const uint16_t* pulses, size_t length) { + uint16_t pulse; + uint16_t pulselen = pulses[length - 1] / PULSE_DIV; + if (pulselen > 25) { + for (unsigned int i = 3; i < length; i++) { + if ((pulses[i] / pulselen) >= 2) { + pulse = pulses[i]; + break; + } + } + if (normalize(pulse, pulselen) > 0 && length > 25) { + /* Print everything */ + Serial.println("--[RESULTS]--"); + Serial.println(); + Serial.print("time:\t\t"); + Serial.print(millis()); + Serial.println(" ms"); + Serial.println("hardware:\tESPiLight"); + Serial.print("pulse:\t\t"); + Serial.println(normalize(pulse, pulselen)); + Serial.print("rawlen:\t\t"); + Serial.println(length); + Serial.printf("pulselen:\t"); + Serial.println(pulselen); + Serial.println(); + Serial.println("Raw code:"); + for (unsigned int i = 0; i < length; i++) { + Serial.print(pulses[i]); + Serial.print(" "); + } + Serial.println(); + } + } +} + +void setup() { + Serial.begin(115200); + // set callback funktion for raw messages + rf.setPulseTrainCallBack(rfRawCallback); + // inittilize receiver + rf.initReceiver(RECEIVER_PIN); + + Serial.println( + "Press and hold one of the buttons on your remote or wait until"); + Serial.println("another device such as a weather station has sent new codes"); + Serial.println( + "The debugger will automatically reset itself after one second of"); + Serial.println( + "failed leads. It will keep running until you explicitly stop it."); +} + +void loop() { + // process input queue and may fire calllback + rf.loop(); + delay(10); +} diff --git a/lib/ESPiLight/examples/pilight_raw/pilight_raw.ino b/lib/ESPiLight/examples/pilight_raw/pilight_raw.ino new file mode 100755 index 0000000..69f2009 --- /dev/null +++ b/lib/ESPiLight/examples/pilight_raw/pilight_raw.ino @@ -0,0 +1,41 @@ +/* + Basic ESPiLight pilight_raw example + + This example mimic the output of the piligh_raw tool. + + https://github.com/puuu/espilight +*/ + +#include + +#define RECEIVER_PIN 4 // any intterupt able pin +#define TRANSMITTER_PIN 13 + +ESPiLight rf(TRANSMITTER_PIN); // use -1 to disable transmitter + +// callback function. It is called on successfully received and parsed rc signal +void rfRawCallback(const uint16_t* pulses, size_t length) { + Serial.print("ESPiLight:"); + for (unsigned int i = 0; i < length; i++) { + Serial.print(" "); + Serial.print(pulses[i]); + if (pulses[i] > 5100) { + Serial.printf(" -# "); + Serial.println(i); + } + } +} + +void setup() { + Serial.begin(115200); + // set callback funktion for raw messages + rf.setPulseTrainCallBack(rfRawCallback); + // inittilize receiver + rf.initReceiver(RECEIVER_PIN); +} + +void loop() { + // process input queue and may fire calllback + rf.loop(); + delay(10); +} diff --git a/lib/ESPiLight/keywords.txt b/lib/ESPiLight/keywords.txt new file mode 100755 index 0000000..9d3bece --- /dev/null +++ b/lib/ESPiLight/keywords.txt @@ -0,0 +1,44 @@ +####################################### +# Syntax Coloring Map For ESPiLight +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +ESPiLight KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +send KEYWORD2 +loop KEYWORD2 + +initReceiver KEYWORD2 +setCallback KEYWORD2 +setPulseTrainCallBack KEYWORD2 +enableReceiver KEYWORD2 +disableReceiver KEYWORD2 + +pulseTrainToString KEYWORD2 +stringToPulseTrain KEYWORD2 +createPulseTrain KEYWORD2 +sendPulseTrain KEYWORD2 +parsePulseTrain KEYWORD2 +receivePulseTrain KEYWORD2 + +####################################### +# Instances (KEYWORD2) +####################################### + +####################################### +# Constants (LITERAL1) +####################################### + +MAXPULSESTREAMLENGTH LITERAL1 + +FIRST LITERAL1 +INVALID LITERAL1 +VALID LITERAL1 +KNOWN LITERAL1 diff --git a/lib/ESPiLight/library.json b/lib/ESPiLight/library.json new file mode 100755 index 0000000..2f7f69b --- /dev/null +++ b/lib/ESPiLight/library.json @@ -0,0 +1,18 @@ +{ + "name": "ESPiLight", + "keywords": "pilight, 433MHz, 434MHz, rf", + "description": "pilight 433.92 MHz protocols library for Arduino. With this port of pilight 433.92 MHz protocols, you can transmit, receive and parse all 434 MHz protocols (e.g., rc switches or weather stations) supported by pilight. This should help to implement IoT bridges between the 434MHz-RF band and internet protocols. It is developed and tested on ESP8266.", + "repository": + { + "type": "git", + "url": "https://github.com/puuu/ESPiLight.git" + }, + "version": "0.14.2", + "license": "GPL-3.0", + "frameworks": "arduino", + "platforms": + [ + "espressif8266", + "espressif32" + ] +} diff --git a/lib/ESPiLight/library.properties b/lib/ESPiLight/library.properties new file mode 100755 index 0000000..e036008 --- /dev/null +++ b/lib/ESPiLight/library.properties @@ -0,0 +1,9 @@ +name=ESPiLight +version=0.14.2 +author=Puuu +maintainer=Puuu +sentence=pilight 433.92 MHz protocols library for Arduino +paragraph=With this port of pilight 433.92 MHz protocols, you can transmit, receive and parse all 434 MHz protocols (e.g., rc switches or weather stations) supported by pilight. This should help to implement IoT bridges between the 434MHz-RF band and internet protocols. It is developed and tested on ESP8266. +category=Signal Input/Output +url=https://github.com/puuu/espilight +architectures=esp8266,esp32 diff --git a/lib/ESPiLight/src/ESPiLight.cpp b/lib/ESPiLight/src/ESPiLight.cpp new file mode 100755 index 0000000..f0ce326 --- /dev/null +++ b/lib/ESPiLight/src/ESPiLight.cpp @@ -0,0 +1,587 @@ +/* + ESPiLight - pilight 433.92 MHz protocols library for Arduino + Copyright (c) 2016 Puuu. All right reserved. + + Project home: https://github.com/puuu/espilight/ + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 3 of the License, or (at your option) any later version. + This library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU General Public License + along with library. If not, see +*/ + +#include +#include "tools/aprintf.h" + +// ESP32 doesn't define ICACHE_RAM_ATTR +#ifndef ICACHE_RAM_ATTR +#define ICACHE_RAM_ATTR IRAM_ATTR +#endif + +#ifdef DEBUG +#define Debug(x) Serial.print(x) +#define DebugLn(x) Serial.println(x) +#else +#define Debug(x) +#define DebugLn(x) +#endif + +extern "C" { +#include "pilight/libs/pilight/protocols/protocol.h" +} +protocols_t *protocols = nullptr; +protocols_t *used_protocols = nullptr; + +volatile PulseTrain_t ESPiLight::_pulseTrains[RECEIVER_BUFFER_SIZE]; +bool ESPiLight::_enabledReceiver; +volatile uint8_t ESPiLight::_actualPulseTrain = 0; +uint8_t ESPiLight::_avaiablePulseTrain = 0; +volatile unsigned long ESPiLight::_lastChange = + 0; // Timestamp of previous edge +volatile uint8_t ESPiLight::_nrpulses = 0; +int16_t ESPiLight::_interrupt = NOT_AN_INTERRUPT; + +uint8_t ESPiLight::minrawlen = 5; +uint8_t ESPiLight::maxrawlen = MAXPULSESTREAMLENGTH; +uint16_t ESPiLight::mingaplen = 5100; +uint16_t ESPiLight::maxgaplen = 10000; + +static void fire_callback(protocol_t *protocol, ESPiLightCallBack callback); + +static protocols_t *get_protocols() { + if (protocols == nullptr) { + ESPiLight::setErrorOutput(Serial); + protocol_init(); + } + return protocols; +} + +static protocols_t *get_used_protocols() { + if (used_protocols == nullptr) { + used_protocols = get_protocols(); + } + return used_protocols; +} + +void ESPiLight::initReceiver(byte inputPin) { + int16_t interrupt = digitalPinToInterrupt(inputPin); + if (_interrupt == interrupt) { + return; + } + if (_interrupt >= 0) { + detachInterrupt((uint8_t)_interrupt); + } + _interrupt = interrupt; + + resetReceiver(); + enableReceiver(); + + if (interrupt >= 0) { + attachInterrupt((uint8_t)interrupt, interruptHandler, CHANGE); + } +} + +uint8_t ESPiLight::receivePulseTrain(uint16_t *pulses) { + uint8_t length = nextPulseTrainLength(); + + if (length > 0) { + volatile PulseTrain_t &pulseTrain = _pulseTrains[_avaiablePulseTrain]; + _avaiablePulseTrain = (_avaiablePulseTrain + 1) % RECEIVER_BUFFER_SIZE; + for (uint8_t i = 0; i < length; i++) { + pulses[i] = pulseTrain.pulses[i]; + } + pulseTrain.length = 0; + } + return length; +} + +uint8_t ESPiLight::nextPulseTrainLength() { + return _pulseTrains[_avaiablePulseTrain].length; +} + +void ICACHE_RAM_ATTR ESPiLight::interruptHandler() { + if (!_enabledReceiver) { + return; + } + + unsigned long now = micros(); + unsigned int duration = 0; + + volatile PulseTrain_t &pulseTrain = _pulseTrains[_actualPulseTrain]; + volatile uint16_t *codes = pulseTrain.pulses; + + if (pulseTrain.length == 0) { + duration = now - _lastChange; + /* We first do some filtering (same as pilight BPF) */ + if (duration > MIN_PULSELENGTH) { + if (duration < MAX_PULSELENGTH) { + /* All codes are buffered */ + codes[_nrpulses] = (uint16_t)duration; + _nrpulses = (uint8_t)((_nrpulses + 1) % MAXPULSESTREAMLENGTH); + /* Let's match footers */ + if (duration > mingaplen) { + // Debug('g'); + /* Only match minimal length pulse streams */ + if (_nrpulses >= minrawlen && _nrpulses <= maxrawlen) { + // Debug(_nrpulses); + // Debug('l'); + pulseTrain.length = _nrpulses; + _actualPulseTrain = (_actualPulseTrain + 1) % RECEIVER_BUFFER_SIZE; + } + _nrpulses = 0; + } + } + _lastChange = now; + } + } else { + Debug("_!_"); + } +} + +void ESPiLight::resetReceiver() { + for (unsigned int i = 0; i < RECEIVER_BUFFER_SIZE; i++) { + _pulseTrains[i].length = 0; + } + _avaiablePulseTrain = 0; + _actualPulseTrain = 0; + _nrpulses = 0; +} + +void ESPiLight::enableReceiver() { _enabledReceiver = true; } + +void ESPiLight::disableReceiver() { _enabledReceiver = false; } + +void ESPiLight::loop() { + int length = 0; + uint16_t pulses[MAXPULSESTREAMLENGTH]; + + length = receivePulseTrain(pulses); + + if (length > 0) { + /* + Debug("RAW ("); + Debug(length); + Debug("): "); + for(int i=0;i= 0) { + pinMode((uint8_t)_outputPin, OUTPUT); + digitalWrite((uint8_t)_outputPin, LOW); + } + + get_protocols(); +} + +void ESPiLight::setCallback(ESPiLightCallBack callback) { + _callback = callback; +} + +void ESPiLight::setPulseTrainCallBack(PulseTrainCallBack rawCallback) { + _rawCallback = rawCallback; +} + +void ESPiLight::sendPulseTrain(const uint16_t *pulses, size_t length, + size_t repeats) { + if (_outputPin >= 0) { + bool receiverState = _enabledReceiver; + _enabledReceiver = (_echoEnabled && receiverState); + for (unsigned int r = 0; r < repeats; r++) { + for (unsigned int i = 0; i < length; i += 2) { + digitalWrite((uint8_t)_outputPin, HIGH); + delayMicroseconds(pulses[i]); + digitalWrite((uint8_t)_outputPin, LOW); + if (i + 1 < length) { + delayMicroseconds(pulses[i + 1]); + } + } + } + digitalWrite((uint8_t)_outputPin, LOW); + _enabledReceiver = receiverState; + } +} + +int ESPiLight::send(const String &protocol, const String &json, + size_t repeats) { + if (_outputPin < 0) { + DebugLn("No output pin set, cannot send"); + return ERROR_NO_OUTPUT_PIN; + } + int length = 0; + uint16_t pulses[MAXPULSESTREAMLENGTH]; + + length = createPulseTrain(pulses, protocol, json); + if (length > 0) { + /* + DebugLn(); + Debug("send: "); + Debug(length); + Debug(" pulses ("); + Debug(protocol); + Debug(", "); + Debug(content); + DebugLn(")"); + */ + sendPulseTrain(pulses, (unsigned)length, repeats); + } + return length; +} + +int ESPiLight::createPulseTrain(uint16_t *pulses, const String &protocol_id, + const String &content) { + protocol_t *protocol = nullptr; + protocols_t *pnode = get_used_protocols(); + JsonNode *message; + + Debug("piLightCreatePulseTrain: "); + + if (!json_validate(content.c_str())) { + Debug("invalid json: "); + DebugLn(content); + return ERROR_INVALID_JSON; + } + + while (pnode != nullptr) { + protocol = pnode->listener; + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wtype-limits" + if ((protocol->createCode != nullptr) && (protocol_id == protocol->id) && + (protocol->maxrawlen <= MAXPULSESTREAMLENGTH)) { +#pragma GCC diagnostic pop + Debug("protocol: "); + Debug(protocol->id); + + protocol->rawlen = 0; + protocol->raw = pulses; + message = json_decode(content.c_str()); + int return_value = protocol->createCode(message); + json_delete(message); + // delete message created by createCode() + json_delete(protocol->message); + protocol->message = nullptr; + + if (return_value == EXIT_SUCCESS) { + DebugLn(" create Code succeded."); + return protocol->rawlen; + } else { + DebugLn(" create Code failed."); + return ERROR_INVALID_PILIGHT_MSG; + } + } + pnode = pnode->next; + } + return ERROR_UNAVAILABLE_PROTOCOL; +} + +size_t ESPiLight::parsePulseTrain(uint16_t *pulses, uint8_t length) { + size_t matches = 0; + protocol_t *protocol = nullptr; + protocols_t *pnode = get_used_protocols(); + + // DebugLn("piLightParsePulseTrain start"); + while ((pnode != nullptr) && (_callback != nullptr)) { + protocol = pnode->listener; + + if (protocol->parseCode != nullptr && protocol->validate != nullptr) { + protocol->raw = pulses; + protocol->rawlen = length; + + if (protocol->validate() == 0) { + Debug("pulses: "); + Debug(length); + Debug(" possible protocol: "); + DebugLn(protocol->id); + + if (protocol->first > 0) { + protocol->first = protocol->second; + } + protocol->second = micros(); + if (protocol->first == 0) { + protocol->first = protocol->second; + } + + /* Reset # of repeats after a certain delay */ + if ((protocol->second - protocol->first) > 500000) { + protocol->repeats = 0; + } + + protocol->message = nullptr; + protocol->parseCode(); + if (protocol->message != nullptr) { + matches++; + protocol->repeats++; + + fire_callback(protocol, _callback); + + json_delete(protocol->message); + protocol->message = nullptr; + } + } + } + pnode = pnode->next; + } + if (_rawCallback != nullptr) { + (_rawCallback)(pulses, length); + } + + // Debug("piLightParsePulseTrain end. matches: "); + // DebugLn(matches); + return matches; +} + +static void fire_callback(protocol_t *protocol, ESPiLightCallBack callback) { + PilightRepeatStatus_t status = FIRST; + char *content = json_encode(protocol->message); + String deviceId = ""; + double itmp; + char *stmp; + + if ((protocol->repeats <= 1) || (protocol->old_content == nullptr)) { + status = FIRST; + json_free(protocol->old_content); + protocol->old_content = content; + } else if (protocol->repeats < 100) { + if (strcmp(content, protocol->old_content) == 0) { + protocol->repeats += 100; + status = VALID; + } else { + status = INVALID; + } + json_free(protocol->old_content); + protocol->old_content = content; + } else { + status = KNOWN; + json_free(content); + } + if (json_find_number(protocol->message, "id", &itmp) == 0) { + deviceId = String((int)round(itmp)); + } else if (json_find_string(protocol->message, "id", &stmp) == 0) { + deviceId = String(stmp); + }; + (callback)(String(protocol->id), String(protocol->old_content), status, + protocol->repeats, deviceId); +} + +String ESPiLight::pulseTrainToString(const uint16_t *codes, size_t length) { + bool match = false; + int diff = 0; + + uint8_t nrpulses = 0; // number of pulse types + uint16_t plstypes[MAX_PULSE_TYPES] = {}; + + String data(""); + data.reserve(6 + length); + data += "c:"; + for (unsigned int i = 0; i < length; i++) { + match = false; + for (uint8_t j = 0; j < MAX_PULSE_TYPES; j++) { + // We device these numbers by 10 to normalize them a bit + diff = (plstypes[j] / 50) - (codes[i] / 50); + if ((diff >= -2) && (diff <= 2)) { + // Write numbers + data += (char)('0' + ((char)j)); + match = true; + break; + } + } + if (!match) { + plstypes[nrpulses++] = codes[i]; + data += (char)('0' + ((char)(nrpulses - 1))); + if (nrpulses >= MAX_PULSE_TYPES) { + DebugLn("too many pulse types"); + return String(""); + } + } + } + data += ";p:"; + for (uint8_t i = 0; i < nrpulses; i++) { + data += plstypes[i]; + if (i + 1 < nrpulses) { + data += ','; + } + } + data += '@'; + return data; +} + +int ESPiLight::stringToPulseTrain(const String &data, uint16_t *codes, + size_t maxlength) { + unsigned int length = 0; // length of pulse train + unsigned int nrpulses = 0; // number of pulse types + uint16_t plstypes[MAX_PULSE_TYPES] = {}; + + // validate data string + int scode = data.indexOf('c') + 2; + if (scode < 0 || (unsigned)scode > data.length()) { + DebugLn("'c' not found in data string, or has no data"); + return ERROR_INVALID_PULSETRAIN_MSG_C; + } + int spulse = data.indexOf('p') + 2; + if (spulse < 0 || (unsigned)spulse > data.length()) { + DebugLn("'p' not found in data string, or has no data"); + return ERROR_INVALID_PULSETRAIN_MSG_P; + } + // parsing pulse types + unsigned int start = (unsigned)spulse; + int end = data.indexOf(',', start); + while (end > 0) { + plstypes[nrpulses++] = + (uint16_t)data.substring(start, (unsigned)end).toInt(); + start = (unsigned)end + 1; + end = data.indexOf(',', start); + } + end = data.indexOf(';', start); + if (end < 0) { + end = data.indexOf('@', start); + } + if (end < 0) { + DebugLn("';' or '@' not found in data string"); + return ERROR_INVALID_PULSETRAIN_MSG_END; + } + plstypes[nrpulses++] = (uint16_t)data.substring(start, (unsigned)end).toInt(); + // parsing pulses + int pulse_index = 0; + for (unsigned int i = (unsigned)scode; i < data.length(); i++) { + if ((data[i] == ';') || (data[i] == '@')) break; + if (i >= maxlength) break; + pulse_index = data[i] - '0'; + if ((pulse_index < 0) || ((unsigned)pulse_index >= nrpulses)) { + DebugLn("Pulse type not defined"); + return ERROR_INVALID_PULSETRAIN_MSG_TYPE; + } + codes[length++] = plstypes[pulse_index]; + } + return length; +} + +static protocols_t *find_proto(const char *name) { + protocols_t *pnode = get_protocols(); + while (pnode != nullptr) { + if (strcmp(name, pnode->listener->id) == 0) { + return pnode; + } + pnode = pnode->next; + } + return nullptr; +} + +void ESPiLight::limitProtocols(const String &protos) { + if (!json_validate(protos.c_str())) { + DebugLn("Protocol limit argument is not a valid json message!"); + return; + } + JsonNode *message = json_decode(protos.c_str()); + + if (message->tag != JSON_ARRAY) { + DebugLn("Protocol limit argument is not a json array!"); + json_delete(message); + return; + } + + if (get_used_protocols() != get_protocols()) { + protocols_t *pnode = get_used_protocols(); + while (pnode != nullptr) { + protocols_t *tmp = pnode; + pnode = pnode->next; + delete tmp; + } + } + + used_protocols = nullptr; + JsonNode *curr = message->children.head; + unsigned int proto_count = 0; + + while (curr != nullptr) { + if (!curr->tag == JSON_STRING) { + DebugLn("Element is not a String"); + curr = curr->next; + continue; + } + + protocols_t *templ = find_proto(curr->string_); + if (templ == nullptr) { + Debug("Protocol not found: "); + DebugLn(curr->string_); + curr = curr->next; + continue; + } + + protocols_t *new_node = new protocols_t; + new_node->listener = templ->listener; + new_node->next = used_protocols; + used_protocols = new_node; + + Debug("activated protocol "); + DebugLn(templ->listener->id); + proto_count++; + + if (curr == message->children.tail) { + break; + } + curr = curr->next; + } + + json_delete(message); +} + +static String protocols_to_array(protocols_t *pnode) { + protocols_t *tmp = pnode; + size_t needed_len = 2; // [] + while (tmp != nullptr) { + needed_len += strlen(tmp->listener->id) + 3; // "xx", + tmp = tmp->next; + } + + String ret; + ret.reserve(needed_len); + + ret += '['; + + bool first = true; + while (pnode != nullptr) { + if (first) { + first = false; + } else { + ret += ","; + } + ret += '"'; + ret += pnode->listener->id; + ret += '"'; + pnode = pnode->next; + } + ret += "]"; + + return ret; +} + +String ESPiLight::availableProtocols() { + return protocols_to_array(get_protocols()); +} + +String ESPiLight::enabledProtocols() { + return protocols_to_array(get_used_protocols()); +} + +void ESPiLight::setEchoEnabled(bool enabled) { _echoEnabled = enabled; } + +void ESPiLight::setErrorOutput(Print &output) { set_aprintf_output(&output); } diff --git a/lib/ESPiLight/src/ESPiLight.h b/lib/ESPiLight/src/ESPiLight.h new file mode 100755 index 0000000..5287866 --- /dev/null +++ b/lib/ESPiLight/src/ESPiLight.h @@ -0,0 +1,195 @@ +/* + ESPiLight - pilight 433.92 MHz protocols library for Arduino + Copyright (c) 2016 Puuu. All right reserved. + + Project home: https://github.com/puuu/espilight/ + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 3 of the License, or (at your option) any later version. + This library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU General Public License + along with library. If not, see +*/ + +#ifndef ESPILIGHT_H +#define ESPILIGHT_H + +#include +#include + +#define RECEIVER_BUFFER_SIZE 10 + +#define MIN_PULSELENGTH 80 +#define MAX_PULSELENGTH 16000 +#define MAXPULSESTREAMLENGTH 255 + +#define MAX_PULSE_TYPES 16 + +enum PilightRepeatStatus_t { FIRST, INVALID, VALID, KNOWN }; + +typedef struct PulseTrain_t { + uint16_t pulses[MAXPULSESTREAMLENGTH]; + uint8_t length; +} PulseTrain_t; + +typedef std::function + ESPiLightCallBack; +typedef std::function + PulseTrainCallBack; + +class ESPiLight { + public: + /** + * Constructor. + */ + ESPiLight(int8_t outputPin); + + /** + * Transmit pulse train + */ + void sendPulseTrain(const uint16_t *pulses, size_t length, + size_t repeats = 10); + + /** + * Transmit Pilight json message + */ + int send(const String &protocol, const String &json, size_t repeats = 10); + + /** + * Parse pulse train and fire callback + */ + size_t parsePulseTrain(uint16_t *pulses, uint8_t length); + + /** + * Process receiver queue and fire callback + */ + void loop(); + + void setCallback(ESPiLightCallBack callback); + void setPulseTrainCallBack(PulseTrainCallBack rawCallback); + + /** + * If set to true, the receiver will temporarely be disabled when sending. + */ + void setEchoEnabled(bool enabled); + + /** + * Initialise receiver + */ + static void initReceiver(byte inputPin); + + /** + * Get last received PulseTrain. + * Returns: length of PulseTrain or 0 if not avaiable + */ + static uint8_t receivePulseTrain(uint16_t *pulses); + + /** + * Check if new PulseTrain avaiable. + * Returns: 0 if no new PulseTrain avaiable + */ + static uint8_t nextPulseTrainLength(); + + /** + * Enable Receiver. No need to call enableReceiver() after initReceiver(). + */ + static void enableReceiver(); + + /** + * Disable decoding. You can re-enable decoding by calling enableReceiver(); + */ + static void disableReceiver(); + + /** + * interruptHandler is called on every change in the input + * signal. If RcPilight::initReceiver is called with interrupt <0, + * you have to call interruptHandler() yourself. (Or use + * InterruptChain) + */ + static void interruptHandler(); + + /** + * Limit the available protocols. + * + * This gets a json array of the protocol names that should be activated. + * If the array is empty, the filter gets reset. + */ + static void limitProtocols(const String &protos); + + /** + * Return a json array containing all the available protocols. + */ + static String availableProtocols(); + + /** + * Return an json array containing all the currently enabled protocols + */ + static String enabledProtocols(); + + /** + * Set pilight error output Print class (default is Serial) + */ + static void setErrorOutput(Print &output); + + static uint8_t minrawlen; + static uint8_t maxrawlen; + static uint16_t mingaplen; + static uint16_t maxgaplen; + + static String pulseTrainToString(const uint16_t *pulses, size_t length); + static int stringToPulseTrain(const String &data, uint16_t *pulses, + size_t maxlength); + + static int createPulseTrain(uint16_t *pulses, const String &protocol_id, + const String &json); + + /** + * Error return codes for send() and createPulseTrain() + */ + static const int ERROR_UNAVAILABLE_PROTOCOL = 0; + static const int ERROR_INVALID_PILIGHT_MSG = -1; + static const int ERROR_INVALID_JSON = -2; + static const int ERROR_NO_OUTPUT_PIN = -3; + + /** + * Error return codes for stringToPulseTrain() + */ + static const int ERROR_INVALID_PULSETRAIN_MSG_C = -1; + static const int ERROR_INVALID_PULSETRAIN_MSG_P = -2; + static const int ERROR_INVALID_PULSETRAIN_MSG_END = -3; + static const int ERROR_INVALID_PULSETRAIN_MSG_TYPE = -4; + + private: + ESPiLightCallBack _callback; + PulseTrainCallBack _rawCallback; + int8_t _outputPin; + bool _echoEnabled; + + /** + * Quasi-reset. Called when the current edge is too long or short. + * reset "promotes" the current edge as being the first edge of a new + * sequence. + */ + static void resetReceiver(); + + /** + * Internal functions + */ + static bool _enabledReceiver; // If true, monitoring and decoding is + // enabled. If false, interruptHandler will + // return immediately. + static volatile PulseTrain_t _pulseTrains[]; + static volatile uint8_t _actualPulseTrain; + static uint8_t _avaiablePulseTrain; + static volatile unsigned long _lastChange; // Timestamp of previous edge + static volatile uint8_t _nrpulses; + static int16_t _interrupt; +}; + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/core/binary.c b/lib/ESPiLight/src/pilight/libs/pilight/core/binary.c new file mode 100755 index 0000000..ca8affe --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/core/binary.c @@ -0,0 +1,119 @@ +/* + Copyright (C) 2013 - 2016 CurlyMo + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. +*/ + +#include +#include + +#include "binary.h" + +/* + * Macros to help convertion of a value from and to bits. + * The value-variable passed to VALUE_TO_BITS_MSB_FIRST() + * and VALUE_TO_BITS_LSB_FIRST() MUST be an unsigned type! + * Macros have side effects to almost all variables passed! + */ + +#define BITS_LSB_FIRST_TO_VALUE(bits, s, e, result) \ + unsigned long long mask = 1; \ + result = 0; \ + for(; s<=e; mask <<= 1) \ + if(bits[s++] != 0) \ + result |= mask + +#define BITS_MSB_FIRST_TO_VALUE(bits, s, e, result) \ + unsigned long long mask = 1; \ + result = 0; \ + e++; \ + for(; e > 0 && s>= 1); \ + int *start = bits; \ + do *--start = value & 1; while (value >>= 1); \ + length = bits - start + + +#define VALUE_TO_BITS_LSB_FIRST(value, bits, length) \ + int *start = bits; \ + do *bits++ = value & 1; while (value >>= 1); \ + length = bits - start + +/* + * Invocation of above macros for various types: + * Again: + * The value-variable passed to VALUE_TO_BITS_MSB_FIRST() + * and VALUE_TO_BITS_LSB_FIRST() MUST be an unsigned type! + */ +int binToDecRev(const int *binary, int s, int e) { // 0<=s<=e, binary[s(msb) .. e(lsb)] + int result; + BITS_MSB_FIRST_TO_VALUE(binary, s, e, result); + return result; +} + +int binToDec(const int *binary, int s, int e) { // 0<=s<=e, binary[s(lsb) .. e(msb)] + int result; + BITS_LSB_FIRST_TO_VALUE(binary, s, e, result); + return result; +} + +int decToBin(int dec, int *binary) { // stores dec as binary[msb .. lsb] and return index of lsb + unsigned int n = (unsigned int) dec; + int len; + VALUE_TO_BITS_MSB_FIRST(n, binary, len); + return len - 1; // return index, not count. +} + +int decToBinRev(int dec, int *binary) { // stores dec as binary[lsb .. msb] and return index of msb + unsigned int n = (unsigned int) dec; + int len; + VALUE_TO_BITS_LSB_FIRST(n, binary, len); + return len - 1; // return index, not count. +} + +unsigned long long binToDecRevUl(const int *binary, unsigned int s, unsigned int e) { + unsigned long long result = 0; + BITS_MSB_FIRST_TO_VALUE(binary, s, e, result); + return result; +} + +unsigned long long binToDecUl(const int *binary, unsigned int s, unsigned int e) { + unsigned long long result; + BITS_LSB_FIRST_TO_VALUE(binary, s, e, result); + return result; +} + +int decToBinUl(unsigned long long n, int *binary) { + int len; + VALUE_TO_BITS_MSB_FIRST(n, binary, len); + return len - 1; // return index, not count. +} + +int decToBinRevUl(unsigned long long n, int *binary) { + int len; + VALUE_TO_BITS_LSB_FIRST(n, binary, len); + return len - 1; // return index, not count. +} + +int binToSignedRev(const int *binary, int s, int e) { // 0<=s<=e, binary[s(msb) .. e(lsb)] + int result = binToDecRev(binary, s, e); + if (binary[s]) { + result -= 1<<(e-s+1); + } + return result; +} + +int binToSigned(const int *binary, int s, int e) { // 0<=s<=e, binary[s(lsb) .. e(msb)] + int result = binToDec(binary, s, e); + if (binary[e]) { + result -= 1<<(e-s+1); + } + return result; +} \ No newline at end of file diff --git a/lib/ESPiLight/src/pilight/libs/pilight/core/binary.h b/lib/ESPiLight/src/pilight/libs/pilight/core/binary.h new file mode 100755 index 0000000..64af2ba --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/core/binary.h @@ -0,0 +1,91 @@ +/* + Copyright (C) 2013 - 2014 CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#ifndef _BINARY_H_ +#define _BINARY_H_ + +/* + * Convert "bits" to the corresponding integer value. + * The difference between binToDecRev[Ul]() and binToDec[Ul]() is where the most and where the least + * significant bit is located (at index position s or e). + * @param binary The bits to convert, each represented by an int in a buffer (non-zero = "1"). + * @param s, e Start+End index in the buffer of "bits". 0<=s<=e. e-s < sizeof(int)*8 + * @return int The converted value. + */ +int binToDecRev(const int *binary, int s, int e); // 0<=s<=e, binary[s(msb) .. e(lsb)] +int binToDec(const int *binary, int s, int e); // 0<=s<=e, binary[s(lsb) .. e(msb)] + +/* + * Convert an integer value to its bits, stored in a buffer of ints. + * The difference of decToBinRev() and decToBin() is if Most or Least Significant bit is generated first. + * IMPORTANT: binToDec() devToBin() use the "Rev"/"non-Rev" in opposit meaning: A buffer generated + * using the "Rev" function must be read by the "non-Rev" function to produce the original value. :-( + * @param dec The number to convert. + * @param binary The buffer of int where to store the "bits" (0 and 1). + * @return int The index of the last bit generated (0 if one bit was generated). + */ +int decToBinRev(int dec, int *binary); // stores dec as binary[lsb .. msb] and return index of msb +int decToBin(int dec, int *binary); // stores dec as binary[msb .. lsb] and return index of lsb + +/* + * Dito for unsigned long long values. + */ +unsigned long long binToDecRevUl(const int *binary, unsigned int s, unsigned int e); +unsigned long long binToDecUl(const int *binary, unsigned int s, unsigned int e); +int decToBinUl(unsigned long long n, int *binary); +int decToBinRevUl(unsigned long long n, int *binary); + +/* + * Convert "bits" to the corresponding signed integer value. + * The difference between binToSignedRev() and binToSigned() is where the most and where the least + * significant bit is located (at index position s or e). + * @param binary The bits to convert, each represented by an int in a buffer (non-zero = "1"). + * @param s, e Start+End index in the buffer of "bits". 0<=s<=e. e-s < sizeof(int)*8 + * @return signed int The converted value. + * + * Examples: + * binToSignedRev((int[]){0,1,1,1, 1,1,1,1}, 0, 7) == 127 + * binToSignedRev((int[]){0,1,1,1, 1,1,1,0}, 0, 7) == 126 + * binToSignedRev((int[]){0,0,0,0, 0,0,1,0}, 0, 7) == 2 + * binToSignedRev((int[]){0,0,0,0, 0,0,0,1}, 0, 7) == 1 + * binToSignedRev((int[]){0,0,0,0, 0,0,0,0}, 0, 7) == 0 + * binToSignedRev((int[]){1,1,1,1, 1,1,1,1}, 0, 7) == -1 + * binToSignedRev((int[]){1,1,1,1, 1,1,1,0}, 0, 7) == -2 + * binToSignedRev((int[]){1,0,0,0, 0,0,1,0}, 0, 7) == -126 + * binToSignedRev((int[]){1,0,0,0, 0,0,0,1}, 0, 7) == -127 + * binToSignedRev((int[]){1,0,0,0, 0,0,0,0}, 0, 7) == -128 + * binToSignedRev((int[]){1,0, 0,1,1,1, 0,0}, 2, 5) == 7 + * binToSignedRev((int[]){1,0, 1,1,1,0, 0,0}, 2, 5) == -2 + * + * binToSigned((int[]){1,1,1,1, 1,1,1,0}, 0, 7) == 127 + * binToSigned((int[]){0,1,1,1, 1,1,1,0}, 0, 7) == 126 + * binToSigned((int[]){0,1,0,0, 0,0,0,0}, 0, 7) == 2 + * binToSigned((int[]){1,0,0,0, 0,0,0,0}, 0, 7) == 1 + * binToSigned((int[]){0,0,0,0, 0,0,0,0}, 0, 7) == 0 + * binToSigned((int[]){1,1,1,1, 1,1,1,1}, 0, 7) == -1 + * binToSigned((int[]){0,1,1,1, 1,1,1,1}, 0, 7) == -2 + * binToSigned((int[]){0,1,0,0, 0,0,0,1}, 0, 7) == -126 + * binToSigned((int[]){1,0,0,0, 0,0,0,1}, 0, 7) == -127 + * binToSigned((int[]){0,0,0,0, 0,0,0,1}, 0, 7) == -128 + * binToSigned((int[]){1,0, 1,1,1,0, 0,0}, 2, 5) == 7 + * binToSigned((int[]){1,0, 0,1,1,1, 0,0}, 2, 5) == -2 + */ +int binToSignedRev(const int *binary, int s, int e); // 0<=s<=e, binary[s(msb) .. e(lsb)] +int binToSigned(const int *binary, int s, int e); // 0<=s<=e, binary[s(lsb) .. e(msb)] + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/core/common.h b/lib/ESPiLight/src/pilight/libs/pilight/core/common.h new file mode 100755 index 0000000..c65491e --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/core/common.h @@ -0,0 +1 @@ +/*dummy*/ diff --git a/lib/ESPiLight/src/pilight/libs/pilight/core/dso.h b/lib/ESPiLight/src/pilight/libs/pilight/core/dso.h new file mode 100755 index 0000000..c32739a --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/core/dso.h @@ -0,0 +1,42 @@ +/* + Copyright (C) 2013 CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#ifndef _DSO_H_ +#define _DSO_H_ + +typedef struct dso_t { + void *handle; + char *name; + ssize_t size; + struct dso_t *next; +} dso_t; + +typedef struct module_t { + const char *name; + const char *version; + const char *reqversion; + const char *reqcommit; +} module_t; + +struct dso_t *dso; + +void *dso_load(char *object); +int dso_gc(void); +void *dso_function(void *handle, const char *name); + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/core/gc.h b/lib/ESPiLight/src/pilight/libs/pilight/core/gc.h new file mode 100755 index 0000000..c65491e --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/core/gc.h @@ -0,0 +1 @@ +/*dummy*/ diff --git a/lib/ESPiLight/src/pilight/libs/pilight/core/json.c b/lib/ESPiLight/src/pilight/libs/pilight/core/json.c new file mode 100755 index 0000000..b034da6 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/core/json.c @@ -0,0 +1,1439 @@ +/* + Copyright (C) 2011 Joseph A. Adams (joeyadams3.14159@gmail.com) + All rights reserved. + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include +#include +#include +#include "../../../../tools/aprintf.h" +#include +#include + +#include "json.h" +#include "mem.h" + +#define out_of_memory() do { \ + fprintf(stderr, "Out of memory.\n"); \ + exit(EXIT_FAILURE); \ + } while (0) + +/* Sadly, strdup is not portable. */ +static char *json_strdup(const char *str) +{ + char *ret = (char*) malloc(strlen(str) + 1); + if (ret == NULL) + out_of_memory(); + memset(ret, 0, strlen(str) + 1); + strcpy(ret, str); + return ret; +} + +/* String buffer */ + +typedef struct +{ + char *cur; + char *end; + char *start; +} SB; + +static void sb_init(SB *sb) +{ + sb->start = (char*) malloc(17); + memset(sb->start, 0, 17); + if (sb->start == NULL) + out_of_memory(); + sb->cur = sb->start; + sb->end = sb->start + 16; +} + +/* sb and need may be evaluated multiple times. */ +#define sb_need(sb, need) do { \ + if ((sb)->end - (sb)->cur < (need)) \ + sb_grow(sb, need); \ + } while (0) + +static void sb_grow(SB *sb, int need) +{ + size_t length = sb->cur - sb->start; + size_t alloc = sb->end - sb->start; + + do { + alloc *= 2; + } while (alloc < length + need); + + sb->start = (char*) realloc(sb->start, alloc + 1); + if (sb->start == NULL) + out_of_memory(); + sb->cur = sb->start + length; + sb->end = sb->start + alloc; +} + +static void sb_put(SB *sb, const char *bytes, int count) +{ + sb_need(sb, count); + memcpy(sb->cur, bytes, count); + sb->cur += count; +} + +#define sb_putc(sb, c) do { \ + if ((sb)->cur >= (sb)->end) \ + sb_grow(sb, 1); \ + *(sb)->cur++ = (c); \ + } while (0) + +static void sb_puts(SB *sb, const char *str) +{ + sb_put(sb, str, strlen(str)); +} + +static char *sb_finish(SB *sb) +{ + *sb->cur = 0; + assert(sb->start <= sb->cur && strlen(sb->start) == (size_t)(sb->cur - sb->start)); + return sb->start; +} + +static void sb_free(SB *sb) +{ + free(sb->start); +} + +/* + * Unicode helper functions + * + * These are taken from the ccan/charset module and customized a bit. + * Putting them here means the compiler can (choose to) inline them, + * and it keeps ccan/json from having a dependency. + */ + +/* + * Type for Unicode codepoints. + * We need our own because wchar_t might be 16 bits. + */ +typedef uint32_t uchar_t; + +/* + * Validate a single UTF-8 character starting at @s. + * The string must be null-terminated. + * + * If it's valid, return its length (1 thru 4). + * If it's invalid or clipped, return 0. + * + * This function implements the syntax given in RFC3629, which is + * the same as that given in The Unicode Standard, Version 6.0. + * + * It has the following properties: + * + * * All codepoints U+0000..U+10FFFF may be encoded, + * except for U+D800..U+DFFF, which are reserved + * for UTF-16 surrogate pair encoding. + * * UTF-8 byte sequences longer than 4 bytes are not permitted, + * as they exceed the range of Unicode. + * * The sixty-six Unicode "non-characters" are permitted + * (namely, U+FDD0..U+FDEF, U+xxFFFE, and U+xxFFFF). + */ +static int utf8_validate_cz(const char *s) +{ + unsigned char c = *s++; + + if (c <= 0x7F) { /* 00..7F */ + return 1; + } else if (c <= 0xC1) { /* 80..C1 */ + /* Disallow overlong 2-byte sequence. */ + return 0; + } else if (c <= 0xDF) { /* C2..DF */ + /* Make sure subsequent byte is in the range 0x80..0xBF. */ + if (((unsigned char)*s++ & 0xC0) != 0x80) + return 0; + + return 2; + } else if (c <= 0xEF) { /* E0..EF */ + /* Disallow overlong 3-byte sequence. */ + if (c == 0xE0 && (unsigned char)*s < 0xA0) + return 0; + + /* Disallow U+D800..U+DFFF. */ + if (c == 0xED && (unsigned char)*s > 0x9F) + return 0; + + /* Make sure subsequent bytes are in the range 0x80..0xBF. */ + if (((unsigned char)*s++ & 0xC0) != 0x80) + return 0; + if (((unsigned char)*s++ & 0xC0) != 0x80) + return 0; + + return 3; + } else if (c <= 0xF4) { /* F0..F4 */ + /* Disallow overlong 4-byte sequence. */ + if (c == 0xF0 && (unsigned char)*s < 0x90) + return 0; + + /* Disallow codepoints beyond U+10FFFF. */ + if (c == 0xF4 && (unsigned char)*s > 0x8F) + return 0; + + /* Make sure subsequent bytes are in the range 0x80..0xBF. */ + if (((unsigned char)*s++ & 0xC0) != 0x80) + return 0; + if (((unsigned char)*s++ & 0xC0) != 0x80) + return 0; + if (((unsigned char)*s++ & 0xC0) != 0x80) + return 0; + + return 4; + } else { /* F5..FF */ + return 0; + } +} + +/* Validate a null-terminated UTF-8 string. */ +static bool utf8_validate(const char *s) +{ + int len; + + for (; *s != 0; s += len) { + len = utf8_validate_cz(s); + if (len == 0) + return false; + } + + return true; +} + +/* + * Read a single UTF-8 character starting at @s, + * returning the length, in bytes, of the character read. + * + * This function assumes input is valid UTF-8, + * and that there are enough characters in front of @s. + */ +static int utf8_read_char(const char *s, uchar_t *out) +{ + const unsigned char *c = (const unsigned char*) s; + + assert(utf8_validate_cz(s)); + + if (c[0] <= 0x7F) { + /* 00..7F */ + *out = c[0]; + return 1; + } else if (c[0] <= 0xDF) { + /* C2..DF (unless input is invalid) */ + *out = ((uchar_t)c[0] & 0x1F) << 6 | + ((uchar_t)c[1] & 0x3F); + return 2; + } else if (c[0] <= 0xEF) { + /* E0..EF */ + *out = ((uchar_t)c[0] & 0xF) << 12 | + ((uchar_t)c[1] & 0x3F) << 6 | + ((uchar_t)c[2] & 0x3F); + return 3; + } else { + /* F0..F4 (unless input is invalid) */ + *out = ((uchar_t)c[0] & 0x7) << 18 | + ((uchar_t)c[1] & 0x3F) << 12 | + ((uchar_t)c[2] & 0x3F) << 6 | + ((uchar_t)c[3] & 0x3F); + return 4; + } +} + +/* + * Write a single UTF-8 character to @s, + * returning the length, in bytes, of the character written. + * + * @unicode must be U+0000..U+10FFFF, but not U+D800..U+DFFF. + * + * This function will write up to 4 bytes to @out. + */ +static int utf8_write_char(uchar_t unicode, char *out) +{ + unsigned char *o = (unsigned char*) out; + + assert(unicode <= 0x10FFFF && !(unicode >= 0xD800 && unicode <= 0xDFFF)); + + if (unicode <= 0x7F) { + /* U+0000..U+007F */ + *o++ = unicode; + return 1; + } else if (unicode <= 0x7FF) { + /* U+0080..U+07FF */ + *o++ = 0xC0 | unicode >> 6; + *o++ = 0x80 | (unicode & 0x3F); + return 2; + } else if (unicode <= 0xFFFF) { + /* U+0800..U+FFFF */ + *o++ = 0xE0 | unicode >> 12; + *o++ = 0x80 | (unicode >> 6 & 0x3F); + *o++ = 0x80 | (unicode & 0x3F); + return 3; + } else { + /* U+10000..U+10FFFF */ + *o++ = 0xF0 | unicode >> 18; + *o++ = 0x80 | (unicode >> 12 & 0x3F); + *o++ = 0x80 | (unicode >> 6 & 0x3F); + *o++ = 0x80 | (unicode & 0x3F); + return 4; + } +} + +/* + * Compute the Unicode codepoint of a UTF-16 surrogate pair. + * + * @uc should be 0xD800..0xDBFF, and @lc should be 0xDC00..0xDFFF. + * If they aren't, this function returns false. + */ +static bool from_surrogate_pair(uint16_t uc, uint16_t lc, uchar_t *unicode) +{ + if (uc >= 0xD800 && uc <= 0xDBFF && lc >= 0xDC00 && lc <= 0xDFFF) { + *unicode = 0x10000 + ((((uchar_t)uc & 0x3FF) << 10) | (lc & 0x3FF)); + return true; + } else { + return false; + } +} + +/* + * Construct a UTF-16 surrogate pair given a Unicode codepoint. + * + * @unicode must be U+10000..U+10FFFF. + */ +static void to_surrogate_pair(uchar_t unicode, uint16_t *uc, uint16_t *lc) +{ + uchar_t n; + + assert(unicode >= 0x10000 && unicode <= 0x10FFFF); + + n = unicode - 0x10000; + *uc = ((n >> 10) & 0x3FF) | 0xD800; + *lc = (n & 0x3FF) | 0xDC00; +} + +#define is_space(c) ((c) == '\t' || (c) == '\n' || (c) == '\r' || (c) == ' ') +#define is_digit(c) ((c) >= '0' && (c) <= '9') + +static bool parse_value (const char **sp, JsonNode **out); +static bool parse_string (const char **sp, char **out); +static bool parse_number (const char **sp, double *out, int *decimals); +static bool parse_array (const char **sp, JsonNode **out); +static bool parse_object (const char **sp, JsonNode **out); +static bool parse_hex16 (const char **sp, uint16_t *out); + +static bool expect_literal (const char **sp, const char *str); +static void skip_space (const char **sp); + +static void emit_value (SB *out, const JsonNode *node); +static void emit_value_indented (SB *out, const JsonNode *node, const char *space, int indent_level); +static void emit_string (SB *out, const char *str); +static void emit_number (SB *out, double num, int decimals); +static void emit_array (SB *out, const JsonNode *array); +static void emit_array_indented (SB *out, const JsonNode *array, const char *space, int indent_level); +static void emit_object (SB *out, const JsonNode *object); +static void emit_object_indented (SB *out, const JsonNode *object, const char *space, int indent_level); + +static int write_hex16(char *out, uint16_t val); + +static JsonNode *mknode(JsonTag tag); +static void append_node(JsonNode *parent, JsonNode *child); +static void prepend_node(JsonNode *parent, JsonNode *child); +static void append_member(JsonNode *object, char *key, JsonNode *value); + +/* Assertion-friendly validity checks */ +static bool tag_is_valid(unsigned int tag); +static bool number_is_valid(const char *num); + +JsonNode *json_decode(const char *json) +{ + const char *s = json; + JsonNode *ret; + + skip_space(&s); + if (!parse_value(&s, &ret)) + return NULL; + + skip_space(&s); + if (*s != 0) { + json_delete(ret); + return NULL; + } + + return ret; +} + +char *json_encode(const JsonNode *node) +{ + return json_stringify(node, NULL); +} + +char *json_encode_string(const char *str) +{ + SB sb; + sb_init(&sb); + + emit_string(&sb, str); + + return sb_finish(&sb); +} + +char *json_stringify(const JsonNode *node, const char *space) +{ + SB sb; + sb_init(&sb); + + if (space != NULL) + emit_value_indented(&sb, node, space, 0); + else + emit_value(&sb, node); + + return sb_finish(&sb); +} + +void json_delete(JsonNode *node) +{ + if (node != NULL) { + json_remove_from_parent(node); + + switch (node->tag) { + case JSON_STRING: + free(node->string_); + break; + case JSON_ARRAY: + case JSON_OBJECT: + { + JsonNode *child, *next; + for (child = node->children.head; child != NULL; child = next) { + next = child->next; + json_delete(child); + } + break; + } + default:; + } + + free(node); + } +} + +bool json_validate(const char *json) +{ + const char *s = json; + + skip_space(&s); + if (!parse_value(&s, NULL)) + return false; + + skip_space(&s); + if (*s != 0) + return false; + + return true; +} + +JsonNode *json_find_element(JsonNode *array, int index) +{ + JsonNode *element; + int i = 0; + + if (array == NULL || array->tag != JSON_ARRAY) + return NULL; + + json_foreach(element, array) { + if (i == index) + return element; + i++; + } + + return NULL; +} + +JsonNode *json_find_member(JsonNode *object, const char *name) +{ + JsonNode *member; + + if (object == NULL || object->tag != JSON_OBJECT) + return NULL; + + json_foreach(member, object) + if (strcmp(member->key, name) == 0) + return member; + + return NULL; +} + +JsonNode *json_first_child(const JsonNode *node) +{ + if (node != NULL && (node->tag == JSON_ARRAY || node->tag == JSON_OBJECT)) + return node->children.head; + return NULL; +} + +static JsonNode *mknode(JsonTag tag) +{ + JsonNode *ret = (JsonNode*) calloc(1, sizeof(JsonNode)); + if (ret == NULL) + out_of_memory(); + ret->tag = tag; + return ret; +} + +JsonNode *json_mknull(void) +{ + return mknode(JSON_NULL); +} + +JsonNode *json_mkbool(bool b) +{ + JsonNode *ret = mknode(JSON_BOOL); + ret->bool_ = b; + return ret; +} + +static JsonNode *mkstring(char *s) +{ + JsonNode *ret = mknode(JSON_STRING); + ret->string_ = s; + return ret; +} + +JsonNode *json_mkstring(const char *s) +{ + return mkstring(json_strdup(s)); +} + +JsonNode *json_mknumber(double n, int decimals) +{ + JsonNode *node = mknode(JSON_NUMBER); + node->number_ = n; + node->decimals_ = decimals; + return node; +} + +JsonNode *json_mkarray(void) +{ + return mknode(JSON_ARRAY); +} + +JsonNode *json_mkobject(void) +{ + return mknode(JSON_OBJECT); +} + +static void append_node(JsonNode *parent, JsonNode *child) +{ + child->parent = parent; + child->prev = parent->children.tail; + child->next = NULL; + + if (parent->children.tail != NULL) + parent->children.tail->next = child; + else + parent->children.head = child; + parent->children.tail = child; +} + +static void prepend_node(JsonNode *parent, JsonNode *child) +{ + child->parent = parent; + child->prev = NULL; + child->next = parent->children.head; + + if (parent->children.head != NULL) + parent->children.head->prev = child; + else + parent->children.tail = child; + parent->children.head = child; +} + +static void append_member(JsonNode *object, char *key, JsonNode *value) +{ + value->key = key; + append_node(object, value); +} + +void json_append_element(JsonNode *array, JsonNode *element) +{ + assert(array->tag == JSON_ARRAY); + assert(element->parent == NULL); + + append_node(array, element); +} + +void json_prepend_element(JsonNode *array, JsonNode *element) +{ + assert(array->tag == JSON_ARRAY); + assert(element->parent == NULL); + + prepend_node(array, element); +} + +void json_append_member(JsonNode *object, const char *key, JsonNode *value) +{ + assert(object->tag == JSON_OBJECT); + assert(value->parent == NULL); + + append_member(object, json_strdup(key), value); +} + +void json_prepend_member(JsonNode *object, const char *key, JsonNode *value) +{ + assert(object->tag == JSON_OBJECT); + assert(value->parent == NULL); + + value->key = json_strdup(key); + prepend_node(object, value); +} + +void json_remove_from_parent(JsonNode *node) +{ + JsonNode *parent = node->parent; + + if (parent != NULL) { + if (node->prev != NULL) + node->prev->next = node->next; + else + parent->children.head = node->next; + if (node->next != NULL) + node->next->prev = node->prev; + else + parent->children.tail = node->prev; + + free(node->key); + + node->parent = NULL; + node->prev = node->next = NULL; + node->key = NULL; + } +} + +static bool parse_value(const char **sp, JsonNode **out) +{ + const char *s = *sp; + + switch (*s) { + case 'n': + if (expect_literal(&s, "null")) { + if (out) + *out = json_mknull(); + *sp = s; + return true; + } + return false; + + case 'f': + if (expect_literal(&s, "false")) { + if (out) + *out = json_mkbool(false); + *sp = s; + return true; + } + return false; + + case 't': + if (expect_literal(&s, "true")) { + if (out) + *out = json_mkbool(true); + *sp = s; + return true; + } + return false; + + case '"': { + char *str; + if (parse_string(&s, out ? &str : NULL)) { + if (out) + *out = mkstring(str); + *sp = s; + return true; + } + return false; + } + + case '[': + if (parse_array(&s, out)) { + *sp = s; + return true; + } + return false; + + case '{': + if (parse_object(&s, out)) { + *sp = s; + return true; + } + return false; + + default: { + double num; + int decimals = 0; + if (parse_number(&s, out ? &num : NULL, &decimals)) { + if (out) + *out = json_mknumber(num, decimals); + *sp = s; + return true; + } + return false; + } + } +} + +static bool parse_array(const char **sp, JsonNode **out) +{ + const char *s = *sp; + JsonNode *ret = out ? json_mkarray() : NULL; + JsonNode *element; + + if (*s++ != '[') + goto failure; + skip_space(&s); + + if (*s == ']') { + s++; + goto success; + } + + for (;;) { + if (!parse_value(&s, out ? &element : NULL)) + goto failure; + skip_space(&s); + + if (out) + json_append_element(ret, element); + + if (*s == ']') { + s++; + goto success; + } + + if (*s++ != ',') + goto failure; + skip_space(&s); + } + +success: + *sp = s; + if (out) + *out = ret; + return true; + +failure: + json_delete(ret); + return false; +} + +static bool parse_object(const char **sp, JsonNode **out) +{ + const char *s = *sp; + JsonNode *ret = out ? json_mkobject() : NULL; + char *key; + JsonNode *value; + + if (*s++ != '{') + goto failure; + skip_space(&s); + + if (*s == '}') { + s++; + goto success; + } + + for (;;) { + if (!parse_string(&s, out ? &key : NULL)) + goto failure; + skip_space(&s); + + if (*s++ != ':') + goto failure_free_key; + skip_space(&s); + + if (!parse_value(&s, out ? &value : NULL)) + goto failure_free_key; + skip_space(&s); + + if (out) + append_member(ret, key, value); + + if (*s == '}') { + s++; + goto success; + } + + if (*s++ != ',') + goto failure; + skip_space(&s); + } + +success: + *sp = s; + if (out) + *out = ret; + return true; + +failure_free_key: + if (out) + free(key); +failure: + json_delete(ret); + return false; +} + +bool parse_string(const char **sp, char **out) +{ + const char *s = *sp; + SB sb; + char throwaway_buffer[4]; + /* enough space for a UTF-8 character */ + char *b; + + if (*s++ != '"') + return false; + + if (out) { + sb_init(&sb); + sb_need(&sb, 4); + b = sb.cur; + } else { + b = throwaway_buffer; + } + + while (*s != '"') { + unsigned char c = *s++; + + /* Parse next character, and write it to b. */ + if (c == '\\') { + c = *s++; + switch (c) { + case '"': + case '\\': + case '/': + *b++ = c; + break; + case 'b': + *b++ = '\b'; + break; + case 'f': + *b++ = '\f'; + break; + case 'n': + *b++ = '\n'; + break; + case 'r': + *b++ = '\r'; + break; + case 't': + *b++ = '\t'; + break; + case 'u': + { + uint16_t uc, lc; + uchar_t unicode; + + if (!parse_hex16(&s, &uc)) + goto failed; + + if (uc >= 0xD800 && uc <= 0xDFFF) { + /* Handle UTF-16 surrogate pair. */ + if (*s++ != '\\' || *s++ != 'u' || !parse_hex16(&s, &lc)) + goto failed; /* Incomplete surrogate pair. */ + if (!from_surrogate_pair(uc, lc, &unicode)) + goto failed; /* Invalid surrogate pair. */ + } else if (uc == 0) { + /* Disallow "\u0000". */ + goto failed; + } else { + unicode = uc; + } + + b += utf8_write_char(unicode, b); + break; + } + default: + /* Invalid escape */ + goto failed; + } + } else if (c <= 0x1F) { + /* Control characters are not allowed in string literals. */ + goto failed; + } else { + /* Validate and echo a UTF-8 character. */ + int len; + + s--; + len = utf8_validate_cz(s); + if (len == 0) + goto failed; /* Invalid UTF-8 character. */ + + while (len--) + *b++ = *s++; + } + + /* + * Update sb to know about the new bytes, + * and set up b to write another character. + */ + if (out) { + sb.cur = b; + sb_need(&sb, 4); + b = sb.cur; + } else { + b = throwaway_buffer; + } + } + s++; + + if (out) + *out = sb_finish(&sb); + *sp = s; + return true; + +failed: + if (out) + sb_free(&sb); + return false; +} + +/* + * The JSON spec says that a number shall follow this precise pattern + * (spaces and quotes added for readability): + * '-'? (0 | [1-9][0-9]*) ('.' [0-9]+)? ([Ee] [+-]? [0-9]+)? + * + * However, some JSON parsers are more liberal. For instance, PHP accepts + * '.5' and '1.'. JSON.parse accepts '+3'. + * + * This function takes the strict approach. + */ +bool parse_number(const char **sp, double *out, int *decimals) +{ + const char *s = *sp; + if(decimals != NULL) { + (*decimals) = 0; + } + + /* '-'? */ + if (*s == '-') + s++; + + /* (0 | [1-9][0-9]*) */ + if (*s == '0') { + s++; + } else { + if (!is_digit(*s)) + return false; + do { + s++; + } while (is_digit(*s)); + } + + /* ('.' [0-9]+)? */ + if (*s == '.') { + s++; + if (!is_digit(*s)) + return false; + do { + s++; + if(decimals != NULL) { + (*decimals)++; + } + } while (is_digit(*s)); + } + + /* ([Ee] [+-]? [0-9]+)? */ + if (*s == 'E' || *s == 'e') { + s++; + if (*s == '+' || *s == '-') + s++; + if (!is_digit(*s)) + return false; + do { + s++; + } while (is_digit(*s)); + } + + if (out) + *out = strtod(*sp, NULL); + + *sp = s; + return true; +} + +static void skip_space(const char **sp) +{ + const char *s = *sp; + while (is_space(*s)) + s++; + *sp = s; +} + +static void emit_value(SB *out, const JsonNode *node) +{ + assert(tag_is_valid(node->tag)); + switch (node->tag) { + case JSON_NULL: + sb_puts(out, "null"); + break; + case JSON_BOOL: + sb_puts(out, node->bool_ ? "true" : "false"); + break; + case JSON_STRING: + emit_string(out, node->string_); + break; + case JSON_NUMBER: + emit_number(out, node->number_, node->decimals_); + break; + case JSON_ARRAY: + emit_array(out, node); + break; + case JSON_OBJECT: + emit_object(out, node); + break; + default: + assert(false); + } +} + +void emit_value_indented(SB *out, const JsonNode *node, const char *space, int indent_level) +{ + assert(tag_is_valid(node->tag)); + switch (node->tag) { + case JSON_NULL: + sb_puts(out, "null"); + break; + case JSON_BOOL: + sb_puts(out, node->bool_ ? "true" : "false"); + break; + case JSON_STRING: + emit_string(out, node->string_); + break; + case JSON_NUMBER: + emit_number(out, node->number_, node->decimals_); + break; + case JSON_ARRAY: + emit_array_indented(out, node, space, indent_level); + break; + case JSON_OBJECT: + emit_object_indented(out, node, space, indent_level); + break; + default: + assert(false); + } +} + +static void emit_array_indented(SB *out, const JsonNode *array, const char *space, int indent_level) +{ + const JsonNode *element = array->children.head; + const JsonNode *tmp = json_first_child(array); + int i, x; + + if(tmp && (tmp->tag == JSON_STRING || tmp->tag == JSON_NUMBER)) { + const JsonNode *element1 = array->children.head; + + if (element1 == NULL) { + sb_puts(out, "[]"); + return; + } + + sb_puts(out, "[ "); + while (element1 != NULL) { + emit_value(out, element1); + element1 = element1->next; + sb_puts(out, element1 != NULL ? ", " : ""); + } + sb_puts(out, " ]"); + } else { + if (element == NULL) { + sb_puts(out, "[]"); + return; + } + + sb_puts(out, "["); + x = 0; + while (element != NULL) { + x++; + if(x > 1) { + for (i = 0; i < indent_level; i++) + sb_puts(out, space); + } + emit_value_indented(out, element, space, indent_level); + element = element->next; + sb_puts(out, element != NULL ? ",\n" : ""); + } + sb_puts(out, "]"); + } +} + +static void emit_array(SB *out, const JsonNode *array) +{ + const JsonNode *element = array->children.head; + + if (element == NULL) { + sb_puts(out, "[]"); + return; + } + + sb_puts(out, "["); + while (element != NULL) { + emit_value(out, element); + element = element->next; + sb_puts(out, element != NULL ? "," : ""); + } + sb_puts(out, "]"); +} + +static void emit_object(SB *out, const JsonNode *object) +{ + const JsonNode *member; + + sb_putc(out, '{'); + json_foreach(member, object) { + emit_string(out, member->key); + sb_putc(out, ':'); + emit_value(out, member); + if (member->next != NULL) + sb_putc(out, ','); + } + sb_putc(out, '}'); +} + +static void emit_object_indented(SB *out, const JsonNode *object, const char *space, int indent_level) +{ + const JsonNode *member = object->children.head; + int i; + + if (member == NULL) { + sb_puts(out, "{}"); + return; + } + + sb_puts(out, "{\n"); + while (member != NULL) { + for (i = 0; i < indent_level + 1; i++) + sb_puts(out, space); + emit_string(out, member->key); + sb_puts(out, ": "); + emit_value_indented(out, member, space, indent_level + 1); + + member = member->next; + sb_puts(out, member != NULL ? ",\n" : "\n"); + } + for (i = 0; i < indent_level; i++) + sb_puts(out, space); + sb_putc(out, '}'); +} + +void emit_string(SB *out, const char *str) +{ + bool escape_unicode = false; + const char *s = str; + char *b; + + assert(utf8_validate(str)); + + /* + * 14 bytes is enough space to write up to two + * \uXXXX escapes and two quotation marks. + */ + sb_need(out, 14); + b = out->cur; + + *b++ = '"'; + while (*s != 0) { + unsigned char c = *s++; + + /* Encode the next character, and write it to b. */ + switch (c) { + case '"': + *b++ = '\\'; + *b++ = '"'; + break; + case '\\': + *b++ = '\\'; + *b++ = '\\'; + break; + case '\b': + *b++ = '\\'; + *b++ = 'b'; + break; + case '\f': + *b++ = '\\'; + *b++ = 'f'; + break; + case '\n': + *b++ = '\\'; + *b++ = 'n'; + break; + case '\r': + *b++ = '\\'; + *b++ = 'r'; + break; + case '\t': + *b++ = '\\'; + *b++ = 't'; + break; + default: { + int len; + + s--; + len = utf8_validate_cz(s); + + if (len == 0) { + /* + * Handle invalid UTF-8 character gracefully in production + * by writing a replacement character (U+FFFD) + * and skipping a single byte. + * + * This should never happen when assertions are enabled + * due to the assertion at the beginning of this function. + */ + assert(false); + if (escape_unicode) { + strcpy(b, "\\uFFFD"); + b += 6; + } else { + *b++ = 0xEF; + *b++ = 0xBF; + *b++ = 0xBD; + } + s++; + } else if (c < 0x1F || (c >= 0x80 && escape_unicode)) { + /* Encode using \u.... */ + uint32_t unicode; + + s += utf8_read_char(s, &unicode); + + if (unicode <= 0xFFFF) { + *b++ = '\\'; + *b++ = 'u'; + b += write_hex16(b, unicode); + } else { + /* Produce a surrogate pair. */ + uint16_t uc, lc; + assert(unicode <= 0x10FFFF); + to_surrogate_pair(unicode, &uc, &lc); + *b++ = '\\'; + *b++ = 'u'; + b += write_hex16(b, uc); + *b++ = '\\'; + *b++ = 'u'; + b += write_hex16(b, lc); + } + } else { + /* Write the character directly. */ + while (len--) + *b++ = *s++; + } + + break; + } + } + + /* + * Update *out to know about the new bytes, + * and set up b to write another encoded character. + */ + out->cur = b; + sb_need(out, 14); + b = out->cur; + } + *b++ = '"'; + + out->cur = b; +} + +static void emit_number(SB *out, double num, int decimals) +{ + /* + * This isn't exactly how JavaScript renders numbers, + * but it should produce valid JSON for reasonable numbers + * preserve precision well enough, and avoid some oddities + * like 0.3 -> 0.299999999999999988898 . + */ + char buf[64]; + dtostrf(num, 0, decimals, buf); + + if (number_is_valid(buf)) + sb_puts(out, buf); + else + sb_puts(out, "null"); +} + +static bool tag_is_valid(unsigned int tag) +{ + return (/* tag >= JSON_NULL && */ tag <= JSON_OBJECT); +} + +static bool number_is_valid(const char *num) +{ + return (parse_number(&num, NULL, NULL) && *num == '\0'); +} + +static bool expect_literal(const char **sp, const char *str) +{ + const char *s = *sp; + + while (*str != '\0') + if (*s++ != *str++) + return false; + + *sp = s; + return true; +} + +/* + * Parses exactly 4 hex characters (capital or lowercase). + * Fails if any input chars are not [0-9A-Fa-f]. + */ +static bool parse_hex16(const char **sp, uint16_t *out) +{ + const char *s = *sp; + uint16_t ret = 0; + uint16_t i; + uint16_t tmp; + char c; + + for (i = 0; i < 4; i++) { + c = *s++; + if (c >= '0' && c <= '9') + tmp = c - '0'; + else if (c >= 'A' && c <= 'F') + tmp = c - 'A' + 10; + else if (c >= 'a' && c <= 'f') + tmp = c - 'a' + 10; + else + return false; + + ret <<= 4; + ret += tmp; + } + + if (out) + *out = ret; + *sp = s; + return true; +} + +/* + * Encodes a 16-bit number into hexadecimal, + * writing exactly 4 hex chars. + */ +static int write_hex16(char *out, uint16_t val) +{ + const char *hex = "0123456789ABCDEF"; + + *out++ = hex[(val >> 12) & 0xF]; + *out++ = hex[(val >> 8) & 0xF]; + *out++ = hex[(val >> 4) & 0xF]; + *out++ = hex[ val & 0xF]; + + return 4; +} + +bool json_check(const JsonNode *node, char errmsg[256]) +{ + #define problem(...) do { \ + if (errmsg != NULL) \ + snprintf(errmsg, 256, __VA_ARGS__); \ + return false; \ + } while (0) + + if (node->key != NULL && !utf8_validate(node->key)) + problem("key contains invalid UTF-8"); + + if (!tag_is_valid(node->tag)) + problem("tag is invalid (%u)", node->tag); + + if (node->tag == JSON_BOOL) { + if (node->bool_ != false && node->bool_ != true) + problem("bool_ is neither false (%d) nor true (%d)", (int)false, (int)true); + } else if (node->tag == JSON_STRING) { + if (node->string_ == NULL) + problem("string_ is NULL"); + if (!utf8_validate(node->string_)) + problem("string_ contains invalid UTF-8"); + } else if (node->tag == JSON_ARRAY || node->tag == JSON_OBJECT) { + JsonNode *head = node->children.head; + JsonNode *tail = node->children.tail; + + if (head == NULL || tail == NULL) { + if (head != NULL) + problem("tail is NULL, but head is not"); + if (tail != NULL) + problem("head is NULL, but tail is not"); + } else { + JsonNode *child; + JsonNode *last = NULL; + + if (head->prev != NULL) + problem("First child's prev pointer is not NULL"); + + for (child = head; child != NULL; last = child, child = child->next) { + if (child == node) + problem("node is its own child"); + if (child->next == child) + problem("child->next == child (cycle)"); + if (child->next == head) + problem("child->next == head (cycle)"); + + if (child->parent != node) + problem("child does not point back to parent"); + if (child->next != NULL && child->next->prev != child) + problem("child->next does not point back to child"); + + if (node->tag == JSON_ARRAY && child->key != NULL) + problem("Array element's key is not NULL"); + if (node->tag == JSON_OBJECT && child->key == NULL) + problem("Object member's key is NULL"); + + if (!json_check(child, errmsg)) + return false; + } + + if (last != tail) + problem("tail does not match pointer found by starting at head and following next links"); + } + } + + return true; + + #undef problem +} + +int json_find_number(JsonNode *object, const char *name, double *out) { + JsonNode *node = json_find_member(object, name); + if (node && node->tag == JSON_NUMBER) { + *out = node->number_; + return 0; + } + return 1; +} + +int json_find_string(JsonNode *object, const char *name, char **out) { + JsonNode *node = json_find_member(object, name); + if (node && node->tag == JSON_STRING) { + *out = node->string_; + return 0; + } + return 1; +} + +void json_free(void *a) { + free(a); +} diff --git a/lib/ESPiLight/src/pilight/libs/pilight/core/json.h b/lib/ESPiLight/src/pilight/libs/pilight/core/json.h new file mode 100755 index 0000000..b6526d9 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/core/json.h @@ -0,0 +1,124 @@ +/* + Copyright (C) 2013 - 2014 CurlyMo (curlymoo1@gmail.com) + 2011 Joseph A. Adams (joeyadams3.14159@gmail.com) + All rights reserved. + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#ifndef CCAN_JSON_H +#define CCAN_JSON_H + +#include +#include + +#define JSON_NULL 0x01 +#define JSON_BOOL 0x01 << 1 +#define JSON_STRING 0x01 << 2 +#define JSON_NUMBER 0x01 << 3 +#define JSON_ARRAY 0x01 << 4 +#define JSON_OBJECT 0x01 << 5 + +#define JsonTag int + +typedef struct JsonNode JsonNode; + +struct JsonNode +{ + /* only if parent is an object or array (NULL otherwise) */ + JsonNode *parent; + JsonNode *prev, *next; + + /* only if parent is an object (NULL otherwise) */ + char *key; /* Must be valid UTF-8. */ + + JsonTag tag; + union { + /* JSON_BOOL */ + bool bool_; + + /* JSON_STRING */ + char *string_; /* Must be valid UTF-8. */ + + /* JSON_NUMBER */ + double number_; + + /* JSON_ARRAY */ + /* JSON_OBJECT */ + struct { + JsonNode *head, *tail; + } children; + }; + int decimals_; +}; + +/*** Encoding, decoding, and validation ***/ + +JsonNode *json_decode (const char *json); +char *json_encode (const JsonNode *node); +char *json_encode_string (const char *str); +char *json_stringify (const JsonNode *node, const char *space); +void json_delete (JsonNode *node); + +bool json_validate (const char *json); + +/*** Lookup and traversal ***/ + +JsonNode *json_find_element (JsonNode *array, int index); +JsonNode *json_find_member (JsonNode *object, const char *key); + +JsonNode *json_first_child (const JsonNode *node); + +#define json_foreach(i, object_or_array) \ + for ((i) = json_first_child(object_or_array); \ + (i) != NULL; \ + (i) = (i)->next) + +/*** Construction and manipulation ***/ + +JsonNode *json_mknull(void); +JsonNode *json_mkbool(bool b); +JsonNode *json_mkstring(const char *s); +JsonNode *json_mknumber(double n, int decimals); +JsonNode *json_mkarray(void); +JsonNode *json_mkobject(void); + +void json_append_element(JsonNode *array, JsonNode *element); +void json_prepend_element(JsonNode *array, JsonNode *element); +void json_append_member(JsonNode *object, const char *key, JsonNode *value); +void json_prepend_member(JsonNode *object, const char *key, JsonNode *value); + +void json_remove_from_parent(JsonNode *node); + +void json_free(void *a); + +/*** Debugging ***/ + +/* + * Look for structure and encoding problems in a JsonNode or its descendents. + * + * If a problem is detected, return false, writing a description of the problem + * to errmsg (unless errmsg is NULL). + */ +bool json_check(const JsonNode *node, char errmsg[256]); + +int json_find_number(JsonNode *object, const char *name, double *out); +int json_find_string(JsonNode *object, const char *name, char **out); + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/core/log.h b/lib/ESPiLight/src/pilight/libs/pilight/core/log.h new file mode 100755 index 0000000..71da84f --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/core/log.h @@ -0,0 +1,37 @@ +/* + ESPiLight - pilight 433.92 MHz protocols library for Arduino + Copyright (c) 2016 Puuu. All right reserved. + + Project home: https://github.com/puuu/espilight/ + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 3 of the License, or (at your option) any later version. + This library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU General Public License + along with library. If not, see +*/ + +#ifndef _LOG_H_ +#define _LOG_H_ + +#define LOG_EMERG 0 +#define LOG_ALERT 1 +#define LOG_CRIT 2 +#define LOG_ERR 3 +#define LOG_WARNING 4 +#define LOG_NOTICE 5 +#define LOG_INFO 6 +#define LOG_DEBUG 7 + +#define LOG_STACK 255 + +#include + +#define logprintf(prio, args...) {printf("pilight(%d): ", prio);printf(args);printf("\n");} + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/core/mem.h b/lib/ESPiLight/src/pilight/libs/pilight/core/mem.h new file mode 100755 index 0000000..7715d2a --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/core/mem.h @@ -0,0 +1,44 @@ +/* + Copyright (C) 2013 - 2016 CurlyMo + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. +*/ + +#ifndef _MEM_H_ +#define _MEM_H_ + +#define OUT_OF_MEMORY fprintf(stderr, "out of memory in %s #%d\n", __FILE__, __LINE__),exit(EXIT_FAILURE); + +int xfree(void); +void memtrack(void); + +void *__malloc(unsigned long, const char *, int); +void *__realloc(void *, unsigned long, const char *, int); +void *__calloc(unsigned long a, unsigned long b, const char *, int); +void __free(void *, const char *, int); +/* + * Windows already has a _strdup, libpcap uses __strdup + */ +char *___strdup(char *, const char *, int); + +// #define MALLOC(a) __malloc(a, __FILE__, __LINE__) +// #define REALLOC(a, b) __realloc(a, b, __FILE__, __LINE__) +// #define CALLOC(a, b) __calloc(a, b, __FILE__, __LINE__) +// #define STRDUP(a) ___strdup(a, __FILE__, __LINE__) +// #define FREE(a) __free((void *)(a), __FILE__, __LINE__),(a)=NULL + +#define MALLOC malloc +#define REALLOC realloc +#define CALLOC calloc +#define STRDUP strdup +#define FREE(a) free((void *)(a)),(a)=NULL + +#define _MALLOC malloc +#define _REALLOC realloc +#define _CALLOC calloc +#define _STRDUP strdup +#define _FREE free + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/core/pilight.h b/lib/ESPiLight/src/pilight/libs/pilight/core/pilight.h new file mode 100755 index 0000000..e10475f --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/core/pilight.h @@ -0,0 +1,29 @@ +/* + ESPiLight - pilight 433.92 MHz protocols library for Arduino + Copyright (c) 2016 Puuu. All right reserved. + + Project home: https://github.com/puuu/espilight/ + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 3 of the License, or (at your option) any later version. + This library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU General Public License + along with library. If not, see +*/ + +#ifndef _PILIGHT_H_ +#define _PILIGHT_H_ + +//#include "defines.h" +#define PULSE_DIV 34 +#define EPSILON 0.00001 + +#include "mem.h" +#include "../../../../tools/aprintf.h" + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/alecto_ws1700.c b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/alecto_ws1700.c new file mode 100755 index 0000000..1b74493 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/alecto_ws1700.c @@ -0,0 +1,218 @@ +/* + Copyright (C) 2014 CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#include +#include +#include +#include + +#include "../../core/pilight.h" +#include "../../core/common.h" +#include "../../core/dso.h" +#include "../../core/log.h" +#include "../protocol.h" +#include "../../core/binary.h" +#include "../../core/gc.h" +#include "alecto_ws1700.h" + +#define PULSE_MULTIPLIER 20 +#define MIN_PULSE_LENGTH 261 +#define AVG_PULSE_LENGTH 266 +#define MAX_PULSE_LENGTH 280 +#define RAW_LENGTH 74 + +typedef struct settings_t { + double id; + double temp; + double humi; + struct settings_t *next; +} settings_t; + +static struct settings_t *settings = NULL; + +static int validate(void) { + if(alecto_ws1700->rawlen == RAW_LENGTH) { + if(alecto_ws1700->raw[alecto_ws1700->rawlen-1] >= (MIN_PULSE_LENGTH*PULSE_DIV) && + alecto_ws1700->raw[alecto_ws1700->rawlen-1] <= (MAX_PULSE_LENGTH*PULSE_DIV)) { + return 0; + } + } + + return -1; +} + +static void parseCode(void) { + int i = 0, x = 0, binary[RAW_LENGTH/2]; + int id = 0, battery = 0, header = 0; + double humi_offset = 0.0, temp_offset = 0.0; + double temperature = 0.0, humidity = 0.0; + + if(alecto_ws1700->rawlen>RAW_LENGTH) { + logprintf(LOG_ERR, "alecto_ws1700: parsecode - invalid parameter passed %d", alecto_ws1700->rawlen); + return; + } + + for(x=1;xrawlen-1;x+=2) { + if(alecto_ws1700->raw[x] > (int)((double)AVG_PULSE_LENGTH*((double)PULSE_MULTIPLIER/2))) { + binary[i++] = 1; + } else { + binary[i++] = 0; + } + } + + header = binToDecRev(binary, 0, 3); + if (header != 5) { + return; + } + id = binToDecRev(binary, 4, 11); + battery = binary[12]; + temperature = (double)binToSignedRev(binary, 16, 27); + humidity = (double)binToDecRev(binary, 28, 35); + + temperature /= 10; + + struct settings_t *tmp = settings; + while(tmp) { + if(fabs(tmp->id-id) < EPSILON) { + humi_offset = tmp->humi; + temp_offset = tmp->temp; + break; + } + tmp = tmp->next; + } + + temperature += temp_offset; + humidity += humi_offset; + + alecto_ws1700->message = json_mkobject(); + json_append_member(alecto_ws1700->message, "id", json_mknumber(id, 0)); + json_append_member(alecto_ws1700->message, "temperature", json_mknumber(temperature, 1)); + json_append_member(alecto_ws1700->message, "humidity", json_mknumber(humidity, 1)); + json_append_member(alecto_ws1700->message, "battery", json_mknumber(battery, 0)); +} + +static int checkValues(struct JsonNode *jvalues) { + struct JsonNode *jid = NULL; + + if((jid = json_find_member(jvalues, "id"))) { + struct settings_t *snode = NULL; + struct JsonNode *jchild = NULL; + struct JsonNode *jchild1 = NULL; + double id = -1; + int match = 0; + + jchild = json_first_child(jid); + while(jchild) { + jchild1 = json_first_child(jchild); + while(jchild1) { + if(strcmp(jchild1->key, "id") == 0) { + id = jchild1->number_; + } + jchild1 = jchild1->next; + } + jchild = jchild->next; + } + + struct settings_t *tmp = settings; + while(tmp) { + if(fabs(tmp->id-id) < EPSILON) { + match = 1; + break; + } + tmp = tmp->next; + } + + if(match == 0) { + if((snode = MALLOC(sizeof(struct settings_t))) == NULL) { + fprintf(stderr, "out of memory\n"); + exit(EXIT_FAILURE); + } + snode->id = id; + snode->temp = 0; + snode->humi = 0; + + json_find_number(jvalues, "temperature-offset", &snode->temp); + json_find_number(jvalues, "humidity-offset", &snode->humi); + + snode->next = settings; + settings = snode; + } + } + return 0; +} + +static void gc(void) { + struct settings_t *tmp = NULL; + while(settings) { + tmp = settings; + settings = settings->next; + FREE(tmp); + } + if(settings != NULL) { + FREE(settings); + } +} + +#if !defined(MODULE) && !defined(_WIN32) +__attribute__((weak)) +#endif +void alectoWS1700Init(void) { + + protocol_register(&alecto_ws1700); + protocol_set_id(alecto_ws1700, "alecto_ws1700"); + protocol_device_add(alecto_ws1700, "alecto_ws1700", "Alecto WS1700 Weather Stations"); + protocol_device_add(alecto_ws1700, "iboutique", "iBoutique Weather Stations"); + alecto_ws1700->devtype = WEATHER; + alecto_ws1700->hwtype = RF433; + alecto_ws1700->minrawlen = RAW_LENGTH; + alecto_ws1700->maxrawlen = RAW_LENGTH; + alecto_ws1700->maxgaplen = MAX_PULSE_LENGTH*PULSE_DIV; + alecto_ws1700->mingaplen = MIN_PULSE_LENGTH*PULSE_DIV; + + options_add(&alecto_ws1700->options, 't', "temperature", OPTION_HAS_VALUE, DEVICES_VALUE, JSON_NUMBER, NULL, "^[0-9]{1,3}$"); + options_add(&alecto_ws1700->options, 'i', "id", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "[0-9]"); + options_add(&alecto_ws1700->options, 'h', "humidity", OPTION_HAS_VALUE, DEVICES_VALUE, JSON_NUMBER, NULL, "[0-9]"); + options_add(&alecto_ws1700->options, 'b', "battery", OPTION_HAS_VALUE, DEVICES_VALUE, JSON_NUMBER, NULL, "^[01]$"); + + // options_add(&alecto_ws1700->options, 0, "decimals", OPTION_HAS_VALUE, DEVICES_SETTING, JSON_NUMBER, (void *)1, "[0-9]"); + options_add(&alecto_ws1700->options, 0, "temperature-offset", OPTION_HAS_VALUE, DEVICES_SETTING, JSON_NUMBER, (void *)0, "[0-9]"); + options_add(&alecto_ws1700->options, 0, "humidity-offset", OPTION_HAS_VALUE, DEVICES_SETTING, JSON_NUMBER, (void *)0, "[0-9]"); + options_add(&alecto_ws1700->options, 0, "temperature-decimals", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)1, "[0-9]"); + options_add(&alecto_ws1700->options, 0, "humidity-decimals", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)1, "[0-9]"); + options_add(&alecto_ws1700->options, 0, "show-humidity", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)1, "^[10]{1}$"); + options_add(&alecto_ws1700->options, 0, "show-temperature", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)1, "^[10]{1}$"); + options_add(&alecto_ws1700->options, 0, "show-battery", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)1, "^[10]{1}$"); + + alecto_ws1700->parseCode=&parseCode; + alecto_ws1700->checkValues=&checkValues; + alecto_ws1700->validate=&validate; + alecto_ws1700->gc=&gc; +} + +#if defined(MODULE) && !defined(_WIN32) +void compatibility(struct module_t *module) { + module->name = "alecto_ws1700"; + module->version = "2.5"; + module->reqversion = "6.0"; + module->reqcommit = "84"; +} + +void init(void) { + alectoWS1700Init(); +} +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/alecto_ws1700.h b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/alecto_ws1700.h new file mode 100755 index 0000000..10bc16b --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/alecto_ws1700.h @@ -0,0 +1,27 @@ +/* + Copyright (C) 2013 CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#ifndef _PROTOCOL_WS1700_H_ +#define _PROTOCOL_WS1700_H_ + +#include "../protocol.h" + +struct protocol_t *alecto_ws1700; +void alectoWS1700Init(void); + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/alecto_wsd17.c b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/alecto_wsd17.c new file mode 100755 index 0000000..d595470 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/alecto_wsd17.c @@ -0,0 +1,193 @@ +/* + Copyright (C) 2014 CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#include +#include +#include +#include + +#include "../../core/pilight.h" +#include "../../core/common.h" +#include "../../core/dso.h" +#include "../../core/log.h" +#include "../protocol.h" +#include "../../core/binary.h" +#include "../../core/gc.h" +#include "alecto_wsd17.h" + +#define PULSE_MULTIPLIER 14 +#define MIN_PULSE_LENGTH 265 +#define AVG_PULSE_LENGTH 270 +#define MAX_PULSE_LENGTH 275 +#define RAW_LENGTH 74 + +typedef struct settings_t { + double id; + double temp; + struct settings_t *next; +} settings_t; + +static struct settings_t *settings = NULL; + +static int validate(void) { + if(alecto_wsd17->rawlen == RAW_LENGTH) { + if(alecto_wsd17->raw[alecto_wsd17->rawlen-1] >= (MIN_PULSE_LENGTH*PULSE_DIV) && + alecto_wsd17->raw[alecto_wsd17->rawlen-1] <= (MAX_PULSE_LENGTH*PULSE_DIV)) { + return 0; + } + } + + return -1; +} + +static void parseCode(void) { + int i = 0, x = 0, id = 0, binary[RAW_LENGTH/2]; + double temp_offset = 0.0, temperature = 0.0; + + if(alecto_wsd17->rawlen>RAW_LENGTH) { + logprintf(LOG_ERR, "alecto_wsd17: parsecode - invalid parameter passed %d", alecto_wsd17->rawlen); + return; + } + + for(x=1;xrawlen-1;x+=2) { + if(alecto_wsd17->raw[x] > (int)((double)AVG_PULSE_LENGTH*((double)PULSE_MULTIPLIER/2))) { + binary[i++] = 1; + } else { + binary[i++] = 0; + } + } + + id = binToDecRev(binary, 0, 11); + temperature = binToDecRev(binary, 16, 27); + + struct settings_t *tmp = settings; + while(tmp) { + if(fabs(tmp->id-id) < EPSILON) { + temp_offset = tmp->temp; + break; + } + tmp = tmp->next; + } + + temperature += temp_offset; + + alecto_wsd17->message = json_mkobject(); + json_append_member(alecto_wsd17->message, "id", json_mknumber(id, 0)); + json_append_member(alecto_wsd17->message, "temperature", json_mknumber(temperature/10, 1)); +} + +static int checkValues(struct JsonNode *jvalues) { + struct JsonNode *jid = NULL; + + if((jid = json_find_member(jvalues, "id"))) { + struct settings_t *snode = NULL; + struct JsonNode *jchild = NULL; + struct JsonNode *jchild1 = NULL; + double id = -1; + int match = 0; + + jchild = json_first_child(jid); + while(jchild) { + jchild1 = json_first_child(jchild); + while(jchild1) { + if(strcmp(jchild1->key, "id") == 0) { + id = jchild1->number_; + } + jchild1 = jchild1->next; + } + jchild = jchild->next; + } + + struct settings_t *tmp = settings; + while(tmp) { + if(fabs(tmp->id-id) < EPSILON) { + match = 1; + break; + } + tmp = tmp->next; + } + + if(match == 0) { + if((snode = MALLOC(sizeof(struct settings_t))) == NULL) { + fprintf(stderr, "out of memory\n"); + exit(EXIT_FAILURE); + } + snode->id = id; + snode->temp = 0; + + json_find_number(jvalues, "temperature-offset", &snode->temp); + + snode->next = settings; + settings = snode; + } + } + return 0; +} + +static void gc(void) { + struct settings_t *tmp = NULL; + while(settings) { + tmp = settings; + settings = settings->next; + FREE(tmp); + } + if(settings != NULL) { + FREE(settings); + } +} + +#if !defined(MODULE) && !defined(_WIN32) +__attribute__((weak)) +#endif +void alectoWSD17Init(void) { + protocol_register(&alecto_wsd17); + protocol_set_id(alecto_wsd17, "alecto_wsd17"); + protocol_device_add(alecto_wsd17, "alecto_wsd17", "Alecto WSD-17 Weather Stations"); + alecto_wsd17->devtype = WEATHER; + alecto_wsd17->hwtype = RF433; + alecto_wsd17->minrawlen = RAW_LENGTH; + alecto_wsd17->maxrawlen = RAW_LENGTH; + alecto_wsd17->maxgaplen = MAX_PULSE_LENGTH*PULSE_DIV; + alecto_wsd17->mingaplen = MIN_PULSE_LENGTH*PULSE_DIV; + + options_add(&alecto_wsd17->options, 't', "temperature", OPTION_HAS_VALUE, DEVICES_VALUE, JSON_NUMBER, NULL, "^[0-9]{1,3}$"); + options_add(&alecto_wsd17->options, 'i', "id", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "[0-9]"); + + // options_add(&alecto_wsd17->options, 0, "decimals", OPTION_HAS_VALUE, DEVICES_SETTING, JSON_NUMBER, (void *)1, "[0-9]"); + options_add(&alecto_wsd17->options, 0, "temperature-decimals", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)1, "[0-9]"); + options_add(&alecto_wsd17->options, 0, "temperature-offset", OPTION_HAS_VALUE, DEVICES_SETTING, JSON_NUMBER, (void *)0, "[0-9]"); + options_add(&alecto_wsd17->options, 0, "show-temperature", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)1, "^[10]{1}$"); + + alecto_wsd17->parseCode=&parseCode; + alecto_wsd17->checkValues=&checkValues; + alecto_wsd17->validate=&validate; + alecto_wsd17->gc=&gc; +} + +#if defined(MODULE) && !defined(_WIN32) +void compatibility(struct module_t *module) { + module->name = "alecto_wsd17"; + module->version = "1.1"; + module->reqversion = "6.0"; + module->reqcommit = "84"; +} + +void init(void) { + alectoWSD17Init(); +} +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/alecto_wsd17.h b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/alecto_wsd17.h new file mode 100755 index 0000000..3233e44 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/alecto_wsd17.h @@ -0,0 +1,27 @@ +/* + Copyright (C) 2013 CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#ifndef _PROTOCOL_ALECTO_WSD17_H_ +#define _PROTOCOL_ALECTO_WSD17_H_ + +#include "../protocol.h" + +struct protocol_t *alecto_wsd17; +void alectoWSD17Init(void); + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/alecto_wx500.c b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/alecto_wx500.c new file mode 100755 index 0000000..ff8dac0 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/alecto_wx500.c @@ -0,0 +1,316 @@ +/* + Copyright (C) 2014 CurlyMo & Tommybear1979 + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#include +#include +#include +#include + +#include "../../core/pilight.h" +#include "../../core/common.h" +#include "../../core/dso.h" +#include "../../core/log.h" +#include "../protocol.h" +#include "../../core/binary.h" +#include "../../core/gc.h" +#include "alecto_wx500.h" +// +// Protocol characteristics: SYNC bit: 526/8942, Logical 0: 526/1578, Logical 1: 526/3419 +// +#define PULSE_MULTIPLIER 16 +#define MIN_PULSE_LENGTH 235 +#define AVG_PULSE_LENGTH 255 +#define MAX_PULSE_LENGTH 275 +#define ZERO_PULSE 2104 +#define ONE_PULSE 3945 +#define AVG_PULSE (ZERO_PULSE+ONE_PULSE)/2 +#define RAW_LENGTH 74 + +typedef struct settings_t { + double id; + double temp; + double humi; + struct settings_t *next; +} settings_t; + +static struct settings_t *settings = NULL; + +static int validate(void) { + if(alecto_wx500->rawlen == RAW_LENGTH) { + if(alecto_wx500->raw[alecto_wx500->rawlen-1] >= (MIN_PULSE_LENGTH*PULSE_DIV) && + alecto_wx500->raw[alecto_wx500->rawlen-1] <= (MAX_PULSE_LENGTH*PULSE_DIV)) { + return 0; + } + } + return -1; +} + +static void parseCode(void) { + int i = 0, x = 0, type = 0, id = 0, binary[RAW_LENGTH/2]; + double temp_offset = 0.0, humi_offset = 0.0; + double humidity = 0.0, temperature = 0.0; + int winddir = 0, windavg = 0, windgust = 0; + int /*rain = 0, */battery = 0; + int n0 = 0, n1 = 0, n2 = 0, n3 = 0; + int n4 = 0, n5 = 0, n6 = 0, n7 = 0, n8 = 0; + int checksum = 1; + + if(alecto_wx500->rawlen>RAW_LENGTH) { + logprintf(LOG_ERR, "alecto_wx500: parsecode - invalid parameter passed %d", alecto_wx500->rawlen); + return; + } + + for(x=1;xrawlen;x+=2) { + if(alecto_wx500->raw[x] > AVG_PULSE) { + binary[i++] = 1; + } else { + binary[i++] = 0; + } + } + + n8=binToDec(binary, 32, 36); + n7=binToDec(binary, 28, 31); + n6=binToDec(binary, 24, 27); + n5=binToDec(binary, 20, 23); + n4=binToDec(binary, 16, 19); + n3=binToDec(binary, 12, 15); + n2=binToDec(binary, 8, 11); + n1=binToDec(binary, 4, 7); + n0=binToDec(binary, 0, 3); + + struct settings_t *tmp = settings; + while(tmp) { + if(fabs(tmp->id-id) < EPSILON){ + humi_offset = tmp->humi; + temp_offset = tmp->temp; + break; + } + tmp = tmp->next; + } + + if((n2 & 0x6) != 0x6) { + type = 0x1; + checksum = (0xf-n0-n1-n2-n3-n4-n5-n6-n7) & 0xf; + if(n8 != checksum) { + type=0x5; + return; + } + //Wind average * 0.2 + } else if(n3 == 0x1) { + type = 0x2; + checksum = (0xf-n0-n1-n2-n3-n4-n5-n6-n7) & 0xf; + if(n8 != checksum){ + type=0x5; + return; + } + //Wind direction & gust + } else if((n3 & 0x7) == 0x7) { + type = 0x3; + checksum = (0xf-n0-n1-n2-n3-n4-n5-n6-n7) & 0xf; + if(n8 != checksum) { + type=0x5; + return; + } + //Rain + } else if(n3 == 0x3) { + type = 0x4; + checksum = (0x7+n0+n1+n2+n3+n4+n5+n6+n7) & 0xf; + if(n8 != checksum){ + type = 0x5; + return; + } + //Catch + } else { + type = 0x5; + return; + } + + alecto_wx500->message = json_mkobject(); + switch(type) { + case 1: + id = binToDec(binary, 0, 7); + temperature = (double)(binToSigned(binary, 12, 23)) / 10.0; + humidity = (binToDec(binary, 28, 31) * 10) + binToDec(binary, 24,27); + battery = !binary[8]; + + temperature += temp_offset; + humidity += humi_offset; + + json_append_member(alecto_wx500->message, "id", json_mknumber(id, 0)); + json_append_member(alecto_wx500->message, "temperature", json_mknumber(temperature, 1)); + json_append_member(alecto_wx500->message, "humidity", json_mknumber(humidity, 1)); + json_append_member(alecto_wx500->message, "battery", json_mknumber(battery, 0)); + break; + case 2: + id = binToDec(binary, 0, 7); + windavg = binToDec(binary, 24, 31) * 2; + battery = !binary[8]; + + json_append_member(alecto_wx500->message, "id", json_mknumber(id, 0)); + json_append_member(alecto_wx500->message, "windavg", json_mknumber((double)windavg/10, 1)); + json_append_member(alecto_wx500->message, "battery", json_mknumber(battery, 0)); + break; + case 3: + id = binToDec(binary, 0, 7); + winddir = binToDec(binary, 15, 23); + windgust = binToDec(binary, 24, 31) * 2; + battery = !binary[8]; + + json_append_member(alecto_wx500->message, "id", json_mknumber(id, 0)); + json_append_member(alecto_wx500->message, "winddir", json_mknumber((double)winddir, 0)); + json_append_member(alecto_wx500->message, "windgust", json_mknumber((double)windgust/10, 1)); + json_append_member(alecto_wx500->message, "battery", json_mknumber(battery, 0)); + break; + case 4: + id = binToDec(binary, 0, 7); + /*rain = binToDec(binary, 16, 30) * 5;*/ + battery = !binary[8]; + //json_append_member(alecto_wx500->message, "rain", json_mknumber((double)rain/10, 1)); + json_append_member(alecto_wx500->message, "id", json_mknumber(id, 0)); + json_append_member(alecto_wx500->message, "battery", json_mknumber(battery, 0)); + break; + default: + type=0x5; + json_delete(alecto_wx500->message); + alecto_wx500->message = NULL; + return; + break; + } +} + +static int checkValues(struct JsonNode *jvalues) { + struct JsonNode *jid = NULL; + + if((jid = json_find_member(jvalues, "id"))) { + struct settings_t *snode = NULL; + struct JsonNode *jchild = NULL; + struct JsonNode *jchild1 = NULL; + double id = -1; + int match = 0; + + jchild = json_first_child(jid); + while(jchild) { + jchild1 = json_first_child(jchild); + while(jchild1) { + if(strcmp(jchild1->key, "id") == 0) { + id = jchild1->number_; + } + jchild1 = jchild1->next; + } + jchild = jchild->next; + } + + struct settings_t *tmp = settings; + while(tmp) { + if(fabs(tmp->id-id) < EPSILON) { + match = 1; + break; + } + tmp = tmp->next; + } + + if(match == 0) { + if((snode = MALLOC(sizeof(struct settings_t))) == NULL) { + fprintf(stderr, "out of memory\n"); + exit(EXIT_FAILURE); + } + snode->id = id; + snode->temp = 0; + snode->humi = 0; + + json_find_number(jvalues, "temperature-offset", &snode->temp); + json_find_number(jvalues, "humidity-offset", &snode->humi); + + snode->next = settings; + settings = snode; + } + } + return 0; +} + +static void gc(void) { + struct settings_t *tmp = NULL; + while(settings) { + tmp = settings; + settings = settings->next; + FREE(tmp); + } + if(settings != NULL) { + FREE(settings); + } +} + +#if !defined(MODULE) && !defined(_WIN32) +__attribute__((weak)) +#endif +void alectoWX500Init(void) { + + protocol_register(&alecto_wx500); + protocol_set_id(alecto_wx500, "alecto_wx500"); + protocol_device_add(alecto_wx500, "alecto_wx500", "Alecto WX500 Weather Stations"); + protocol_device_add(alecto_wx500, "auriol_h13726", "Auriol H13726 Weather Stations"); + protocol_device_add(alecto_wx500, "ventus_wsxxx", "Ventus WSXXX Weather Stations"); + protocol_device_add(alecto_wx500, "hama_ews1500", "Hama EWS1500 Weather Stations"); + protocol_device_add(alecto_wx500, "meteoscan_w1XX", "Meteoscan W1XXX Weather Stations"); + protocol_device_add(alecto_wx500, "balance_rf_ws105", "Balance RF-WS105 Weather Stations"); + alecto_wx500->devtype = WEATHER; + alecto_wx500->hwtype = RF433; + alecto_wx500->minrawlen = RAW_LENGTH; + alecto_wx500->maxrawlen = RAW_LENGTH; + alecto_wx500->maxgaplen = MAX_PULSE_LENGTH*PULSE_DIV; + alecto_wx500->mingaplen = MIN_PULSE_LENGTH*PULSE_DIV; + + options_add(&alecto_wx500->options, 't', "temperature", OPTION_HAS_VALUE, DEVICES_VALUE, JSON_NUMBER, NULL, "^[0-9]{1,3}$"); + options_add(&alecto_wx500->options, 'i', "id", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "[0-9]"); + options_add(&alecto_wx500->options, 'b', "battery", OPTION_HAS_VALUE, DEVICES_VALUE, JSON_NUMBER, NULL, "[0-9]"); + options_add(&alecto_wx500->options, 'h', "humidity", OPTION_HAS_VALUE, DEVICES_VALUE, JSON_NUMBER, NULL, "^[0-9]{1,3}$"); + options_add(&alecto_wx500->options, 'w', "windavg", OPTION_HAS_VALUE, DEVICES_VALUE, JSON_NUMBER, NULL, "^[0-9]{1,3}$"); + options_add(&alecto_wx500->options, 'd', "winddir", OPTION_HAS_VALUE, DEVICES_VALUE, JSON_NUMBER, NULL, "^[0-9]{1,3}$"); + options_add(&alecto_wx500->options, 'g', "windgust", OPTION_HAS_VALUE, DEVICES_VALUE, JSON_NUMBER, NULL, "^[0-9]{1,3}$"); + //options_add(&alecto_wx500->options, 'r', "rain", OPTION_HAS_VALUE, DEVICES_VALUE, JSON_NUMBER, NULL, "^[0-9]{1,3}$"); + + // options_add(&alecto_wx500->options, 0, "decimals", OPTION_HAS_VALUE, DEVICES_SETTING, JSON_NUMBER, (void *)1, "[0-9]"); + options_add(&alecto_wx500->options, 0, "temperature-decimals", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)1, "[0-9]"); + options_add(&alecto_wx500->options, 0, "humidity-decimals", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)1, "[0-9]"); + options_add(&alecto_wx500->options, 0, "wind-decimals", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)1, "[0-9]"); + options_add(&alecto_wx500->options, 0, "humidity-offset", OPTION_HAS_VALUE, DEVICES_SETTING, JSON_NUMBER, (void *)0, "[0-9]"); + options_add(&alecto_wx500->options, 0, "temperature-offset", OPTION_HAS_VALUE, DEVICES_SETTING, JSON_NUMBER, (void *)0, "[0-9]"); + options_add(&alecto_wx500->options, 0, "show-humidity", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)1, "^[10]{1}$"); + options_add(&alecto_wx500->options, 0, "show-temperature", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)1, "^[10]{1}$"); + options_add(&alecto_wx500->options, 0, "show-battery", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)1, "^[10]{1}$"); + options_add(&alecto_wx500->options, 0, "show-wind", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)1, "^[10]{1}$"); + //options_add(&alecto_wx500->options, 0, "show-rain", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + + alecto_wx500->parseCode=&parseCode; + alecto_wx500->checkValues=&checkValues; + alecto_wx500->validate=&validate; + alecto_wx500->gc=&gc; +} + +#ifdef MODULAR +void compatibility(const char **version, const char **commit) { + module->name = "alecto_wx500"; + module->version = "1.2"; + module->reqversion = "6.0"; + module->reqcommit = "84"; +} + +void init(void) { + alectoWX500Init(); +} +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/alecto_wx500.h b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/alecto_wx500.h new file mode 100755 index 0000000..096f112 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/alecto_wx500.h @@ -0,0 +1,27 @@ +/* + Copyright (C) 2014 CurlyMo & Tommybear1979 + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#ifndef _PROTOCOL_ALECTO_WX500_H_ +#define _PROTOCOL_ALECTO_WX500_H_ + +#include "../protocol.h" + +struct protocol_t *alecto_wx500; +void alectoWX500Init(void); + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_contact.c b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_contact.c new file mode 100755 index 0000000..bd034d6 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_contact.c @@ -0,0 +1,132 @@ +/* + Copyright (C) 2014 CurlyMo & lvdp + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#include +#include +#include +#include + +#include "../../core/pilight.h" +#include "../../core/common.h" +#include "../../core/dso.h" +#include "../../core/log.h" +#include "../protocol.h" +#include "../../core/binary.h" +#include "../../core/gc.h" +#include "arctech_contact.h" + +#define PULSE_MULTIPLIER 4 +#define MIN_PULSE_LENGTH 250 +#define MAX_PULSE_LENGTH 320 +#define AVG_PULSE_LENGTH 300 +#define MIN_RAW_LENGTH 132 +#define MAX_RAW_LENGTH 148 +#define RAW_LENGTH 148 + +static int validate(void) { + if(arctech_contact->rawlen == MIN_RAW_LENGTH || arctech_contact->rawlen == MAX_RAW_LENGTH) { + if(arctech_contact->raw[arctech_contact->rawlen-1] >= (MIN_PULSE_LENGTH*PULSE_DIV) && + arctech_contact->raw[arctech_contact->rawlen-1] <= (MAX_PULSE_LENGTH*PULSE_DIV) && + arctech_contact->raw[1] >= AVG_PULSE_LENGTH*(PULSE_MULTIPLIER*2)) { + return 0; + } + } + + return -1; +} + +static void createMessage(int id, int unit, int state, int all) { + arctech_contact->message = json_mkobject(); + json_append_member(arctech_contact->message, "id", json_mknumber(id, 0)); + if(all == 1) { + json_append_member(arctech_contact->message, "all", json_mknumber(all, 0)); + } else { + json_append_member(arctech_contact->message, "unit", json_mknumber(unit, 0)); + } + + if(state == 1) { + json_append_member(arctech_contact->message, "state", json_mkstring("opened")); + } else { + json_append_member(arctech_contact->message, "state", json_mkstring("closed")); + } +} + +static void parseCode(void) { + int binary[MAX_RAW_LENGTH/4], x = 0, i = 0; + + if(arctech_contact->rawlen>MAX_RAW_LENGTH) { + logprintf(LOG_ERR, "arctech_contact: parsecode - invalid parameter passed %d", arctech_contact->rawlen); + return; + } + + for(x=0;xrawlen;x+=4) { + if(arctech_contact->raw[x+3] > AVG_PULSE_LENGTH*PULSE_MULTIPLIER) { + binary[i++] = 1; + } else { + binary[i++] = 0; + } + } + + int unit = binToDecRev(binary, 28, 31); + int state = binary[27]; + int all = binary[26]; + int id = binToDecRev(binary, 0, 25); + + createMessage(id, unit, state, all); +} + +#if !defined(MODULE) && !defined(_WIN32) +__attribute__((weak)) +#endif +void arctechContactInit(void) { + + protocol_register(&arctech_contact); + protocol_set_id(arctech_contact, "arctech_contact"); + protocol_device_add(arctech_contact, "kaku_contact", "KlikAanKlikUit Contact Sensor"); + protocol_device_add(arctech_contact, "dio_contact", "D-IO Contact Sensor"); + + arctech_contact->devtype = CONTACT; + arctech_contact->hwtype = RF433; + arctech_contact->minrawlen = MIN_RAW_LENGTH; + arctech_contact->maxrawlen = MAX_RAW_LENGTH; + arctech_contact->maxgaplen = MAX_PULSE_LENGTH*PULSE_DIV; + arctech_contact->mingaplen = MIN_PULSE_LENGTH*PULSE_DIV; + + options_add(&arctech_contact->options, 'u', "unit", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^([0-9]{1}|[1][0-5])$"); + options_add(&arctech_contact->options, 'i', "id", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^([0-9]{1,7}|[1-5][0-9]{7}|6([0-6][0-9]{6}|7(0[0-9]{5}|10([0-7][0-9]{3}|8([0-7][0-9]{2}|8([0-5][0-9]|6[0-3]))))))$"); + options_add(&arctech_contact->options, 't', "opened", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&arctech_contact->options, 'f', "closed", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + + options_add(&arctech_contact->options, 'a', "all", OPTION_HAS_VALUE, DEVICES_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + + arctech_contact->parseCode=&parseCode; + arctech_contact->validate=&validate; +} + +#if defined(MODULE) && !defined(_WIN32) +void compatibility(struct module_t *module) { + module->name = "arctech_contact"; + module->version = "2.2"; + module->reqversion = "6.0"; + module->reqcommit = "38"; +} + +void init(void) { + arctechContactInit(); +} +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_contact.h b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_contact.h new file mode 100755 index 0000000..f37d739 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_contact.h @@ -0,0 +1,27 @@ +/* + Copyright (C) 2013 CurlyMo & ldvp + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#ifndef _PROTOCOL_ARCTECH_CONTACT_H_ +#define _PROTOCOL_ARCTECH_CONTACT_H_ + +#include "../protocol.h" + +struct protocol_t *arctech_contact; +void arctechContactInit(void); + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_dimmer.c b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_dimmer.c new file mode 100755 index 0000000..d1d55fb --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_dimmer.c @@ -0,0 +1,357 @@ +/* + Copyright (C) 2013 CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#include +#include +#include +#include + +#include "../../core/pilight.h" +#include "../../core/common.h" +#include "../../core/dso.h" +#include "../../core/log.h" +#include "../protocol.h" +#include "../../core/binary.h" +#include "../../core/gc.h" +#include "arctech_dimmer.h" + +#define LEARN_REPEATS 40 +#define NORMAL_REPEATS 10 +#define PULSE_MULTIPLIER 4 +#define MIN_PULSE_LENGTH 250 +#define MAX_PULSE_LENGTH 320 +#define AVG_PULSE_LENGTH 300 +#define RAW_LENGTH 148 + +static int validate(void) { + if(arctech_dimmer->rawlen == RAW_LENGTH) { + if(arctech_dimmer->raw[arctech_dimmer->rawlen-1] >= (MIN_PULSE_LENGTH*PULSE_DIV) && + arctech_dimmer->raw[arctech_dimmer->rawlen-1] <= (MAX_PULSE_LENGTH*PULSE_DIV) && + arctech_dimmer->raw[1] >= AVG_PULSE_LENGTH*(PULSE_MULTIPLIER*2)) { + return 0; + } + } + + return -1; +} + +static void createMessage(int id, int unit, int state, int all, int dimlevel, int learn) { + arctech_dimmer->message = json_mkobject(); + json_append_member(arctech_dimmer->message, "id", json_mknumber(id, 0)); + + if(all == 1) { + json_append_member(arctech_dimmer->message, "all", json_mknumber(all, 0)); + } else { + json_append_member(arctech_dimmer->message, "unit", json_mknumber(unit, 0)); + } + + if(dimlevel == 0) { + state = 0; + } else if(dimlevel > 0) { + state = 1; + json_append_member(arctech_dimmer->message, "dimlevel", json_mknumber(dimlevel, 0)); + } + + if(state == 1) { + json_append_member(arctech_dimmer->message, "state", json_mkstring("on")); + } else { + json_append_member(arctech_dimmer->message, "state", json_mkstring("off")); + } + + if(learn == 1) { + arctech_dimmer->txrpt = LEARN_REPEATS; + } else { + arctech_dimmer->txrpt = NORMAL_REPEATS; + } +} + +static void parseCode(void) { + int binary[RAW_LENGTH/4], x = 0, i = 0; + + if(arctech_dimmer->rawlen>RAW_LENGTH) { + logprintf(LOG_ERR, "arctech_dimmer: parsecode - invalid parameter passed %d", arctech_dimmer->rawlen); + return; + } + + for(x=0;xrawlen;x+=4) { + if(arctech_dimmer->raw[x+3] > (int)((double)AVG_PULSE_LENGTH*((double)PULSE_MULTIPLIER/2))) { + binary[i++] = 1; + } else { + binary[i++] = 0; + } + } + + int dimlevel = binToDecRev(binary, 32, 35); + int unit = binToDecRev(binary, 28, 31); + int state = binary[27]; + int all = binary[26]; + int id = binToDecRev(binary, 0, 25); + + createMessage(id, unit, state, all, dimlevel, 0); +} + +static void createLow(int s, int e) { + int i; + + for(i=s;i<=e;i+=4) { + arctech_dimmer->raw[i]=(AVG_PULSE_LENGTH); + arctech_dimmer->raw[i+1]=(AVG_PULSE_LENGTH); + arctech_dimmer->raw[i+2]=(AVG_PULSE_LENGTH); + arctech_dimmer->raw[i+3]=(AVG_PULSE_LENGTH*PULSE_MULTIPLIER); + } +} + +static void createHigh(int s, int e) { + int i; + + for(i=s;i<=e;i+=4) { + arctech_dimmer->raw[i]=(AVG_PULSE_LENGTH); + arctech_dimmer->raw[i+1]=(AVG_PULSE_LENGTH*PULSE_MULTIPLIER); + arctech_dimmer->raw[i+2]=(AVG_PULSE_LENGTH); + arctech_dimmer->raw[i+3]=(AVG_PULSE_LENGTH); + } +} + +static void clearCode(void) { + createLow(2,147); +} + +static void createStart(void) { + arctech_dimmer->raw[0]=AVG_PULSE_LENGTH; + arctech_dimmer->raw[1]=(10*AVG_PULSE_LENGTH); +} + +static void createId(int id) { + int binary[255]; + int length = 0; + int i=0, x=0; + + length = decToBin(id, binary); + for(i=0;i<=length;i++) { + if(binary[i]==1) { + x=((length-i)+1)*4; + createHigh(106-x, 106-(x-3)); + } + } +} + +static void createAll(int all) { + if(all == 1) { + createHigh(106, 109); + } +} + +static void createState(int state) { + if(state == 1) { + createHigh(110, 113); + } else if(state == -1) { + arctech_dimmer->raw[110]=(AVG_PULSE_LENGTH); + arctech_dimmer->raw[111]=(AVG_PULSE_LENGTH); + arctech_dimmer->raw[112]=(AVG_PULSE_LENGTH); + arctech_dimmer->raw[113]=(AVG_PULSE_LENGTH); + } +} + +static void createUnit(int unit) { + int binary[255]; + int length = 0; + int i=0, x=0; + + length = decToBin(unit, binary); + for(i=0;i<=length;i++) { + if(binary[i]==1) { + x=((length-i)+1)*4; + createHigh(130-x, 130-(x-3)); + } + } +} + +static void createDimlevel(int dimlevel) { + int binary[255]; + int length = 0; + int i=0, x=0; + + length = decToBin(dimlevel, binary); + for(i=0;i<=length;i++) { + if(binary[i]==1) { + x=((length-i)+1)*4; + createHigh(146-x, 146-(x-3)); + } + } +} + +static void createFooter(void) { + arctech_dimmer->raw[147]=(PULSE_DIV*AVG_PULSE_LENGTH); +} + +static int checkValues(struct JsonNode *code) { + int dimlevel = -1; + int max = 15; + int min = 0; + double itmp = -1; + + if(json_find_number(code, "dimlevel-maximum", &itmp) == 0) + max = (int)round(itmp); + if(json_find_number(code, "dimlevel-minimum", &itmp) == 0) + min = (int)round(itmp); + if(json_find_number(code, "dimlevel", &itmp) == 0) + dimlevel = (int)round(itmp); + + if(min > max) { + return 1; + } + + if(dimlevel != -1) { + if(dimlevel < min || dimlevel > max) { + return 1; + } else { + return 0; + } + } + return 0; +} + +static int createCode(struct JsonNode *code) { + int id = -1; + int unit = -1; + int state = -1; + int all = 0; + int dimlevel = -1; + int learn = -1; + int max = 15; + int min = 0; + double itmp = -1; + + if(json_find_number(code, "dimlevel-maximum", &itmp) == 0) + max = (int)round(itmp); + if(json_find_number(code, "dimlevel-minimum", &itmp) == 0) + min = (int)round(itmp); + + if(json_find_number(code, "id", &itmp) == 0) + id = (int)round(itmp); + if(json_find_number(code, "unit", &itmp) == 0) + unit = (int)round(itmp); + if(json_find_number(code, "dimlevel", &itmp) == 0) + dimlevel = (int)round(itmp); + if(json_find_number(code, "all", &itmp) == 0) + all = (int)round(itmp); + if(json_find_number(code, "learn", &itmp) == 0) + learn = 1; + + if(json_find_number(code, "off", &itmp) == 0) + state=0; + else if(json_find_number(code, "on", &itmp) == 0) + state=1; + + if(all > 0 && learn > -1) { + logprintf(LOG_ERR, "arctech_dimmer: all and learn cannot be combined"); + return EXIT_FAILURE; + } else if(id == -1 || (unit == -1 && all == 0) || (dimlevel == -1 && state == -1)) { + logprintf(LOG_ERR, "arctech_dimmer: insufficient number of arguments"); + return EXIT_FAILURE; + } else if(id > 67108863 || id < 1) { + logprintf(LOG_ERR, "arctech_dimmer: invalid id range"); + return EXIT_FAILURE; + } else if((unit > 15 || unit < 0) && all == 0) { + logprintf(LOG_ERR, "arctech_dimmer: invalid unit range"); + return EXIT_FAILURE; + } else if(dimlevel != -1 && (dimlevel > max || dimlevel < min) ) { + logprintf(LOG_ERR, "arctech_dimmer: invalid dimlevel range"); + return EXIT_FAILURE; + } else if(dimlevel >= 0 && state == 0) { + logprintf(LOG_ERR, "arctech_dimmer: dimlevel and off state cannot be combined"); + return EXIT_FAILURE; + } else { + if(unit == -1 && all == 1) { + unit = 0; + } + if(dimlevel >= 0) { + state = -1; + } + createMessage(id, unit, state, all, dimlevel, learn); + createStart(); + clearCode(); + createId(id); + createAll(all); + createState(state); + createUnit(unit); + if(dimlevel > -1) { + createDimlevel(dimlevel); + } + createFooter(); + arctech_dimmer->rawlen = RAW_LENGTH; + } + return EXIT_SUCCESS; +} + +static void printHelp(void) { + printf("\t -u --unit=unit\t\t\tcontrol a device with this unit code\n"); + printf("\t -i --id=id\t\t\tcontrol a device with this id\n"); + printf("\t -a --all\t\t\tsend command to all devices with this id\n"); + printf("\t -d --dimlevel=dimlevel\t\tsend a specific dimlevel\n"); + printf("\t -l --learn\t\t\tsend multiple streams so dimmer can learn\n"); +} + +#if !defined(MODULE) && !defined(_WIN32) +__attribute__((weak)) +#endif +void arctechDimmerInit(void) { + + protocol_register(&arctech_dimmer); + protocol_set_id(arctech_dimmer, "arctech_dimmer"); + protocol_device_add(arctech_dimmer, "kaku_dimmer", "KlikAanKlikUit Dimmers"); + arctech_dimmer->devtype = DIMMER; + arctech_dimmer->hwtype = RF433; + arctech_dimmer->txrpt = NORMAL_REPEATS; + arctech_dimmer->minrawlen = RAW_LENGTH; + arctech_dimmer->maxrawlen = RAW_LENGTH; + arctech_dimmer->maxgaplen = MAX_PULSE_LENGTH*PULSE_DIV; + arctech_dimmer->mingaplen = MIN_PULSE_LENGTH*PULSE_DIV; + + options_add(&arctech_dimmer->options, 'd', "dimlevel", OPTION_HAS_VALUE, DEVICES_VALUE, JSON_NUMBER, NULL, "^([0-9]{1}|[1][0-5])$"); + options_add(&arctech_dimmer->options, 'u', "unit", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^([0-9]{1}|[1][0-5])$"); + options_add(&arctech_dimmer->options, 'i', "id", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^([0-9]{1,7}|[1-5][0-9]{7}|6([0-6][0-9]{6}|7(0[0-9]{5}|10([0-7][0-9]{3}|8([0-7][0-9]{2}|8([0-5][0-9]|6[0-3]))))))$"); + options_add(&arctech_dimmer->options, 't', "on", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&arctech_dimmer->options, 'f', "off", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&arctech_dimmer->options, 'a', "all", OPTION_OPT_VALUE, DEVICES_OPTIONAL, JSON_NUMBER, NULL, NULL); + options_add(&arctech_dimmer->options, 'l', "learn", OPTION_NO_VALUE, DEVICES_OPTIONAL, JSON_NUMBER, NULL, NULL); + + options_add(&arctech_dimmer->options, 0, "dimlevel-minimum", OPTION_HAS_VALUE, DEVICES_SETTING, JSON_NUMBER, (void *)0, "^([0-9]{1}|[1][0-5])$"); + options_add(&arctech_dimmer->options, 0, "dimlevel-maximum", OPTION_HAS_VALUE, DEVICES_SETTING, JSON_NUMBER, (void *)15, "^([0-9]{1}|[1][0-5])$"); + options_add(&arctech_dimmer->options, 0, "readonly", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + options_add(&arctech_dimmer->options, 0, "confirm", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + + arctech_dimmer->parseCode=&parseCode; + arctech_dimmer->createCode=&createCode; + arctech_dimmer->printHelp=&printHelp; + arctech_dimmer->checkValues=&checkValues; + arctech_dimmer->validate=&validate; +} + +#if defined(MODULE) && !defined(_WIN32) +void compatibility(struct module_t *module) { + module->name = "arctech_dimmer"; + module->version = "3.4"; + module->reqversion = "6.0"; + module->reqcommit = "84"; +} + +void init(void) { + arctechDimmerInit(); +} +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_dimmer.h b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_dimmer.h new file mode 100755 index 0000000..8d10bda --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_dimmer.h @@ -0,0 +1,27 @@ +/* + Copyright (C) 2013 CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#ifndef _PROTOCOL_ARCTECH_DIMMER_H_ +#define _PROTOCOL_ARCTECH_DIMMER_H_ + +#include "../protocol.h" + +struct protocol_t *arctech_dimmer; +void arctechDimmerInit(void); + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_dusk.c b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_dusk.c new file mode 100755 index 0000000..42797d8 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_dusk.c @@ -0,0 +1,125 @@ +/* + Copyright (C) 2014 CurlyMo & lvdp + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#include +#include +#include + +#include "../../core/pilight.h" +#include "../../core/common.h" +#include "../../core/dso.h" +#include "../../core/log.h" +#include "../protocol.h" +#include "../../core/binary.h" +#include "../../core/gc.h" +#include "arctech_dusk.h" + +#define PULSE_MULTIPLIER 3 +#define MIN_PULSE_LENGTH 250 +#define MAX_PULSE_LENGTH 282 +#define AVG_PULSE_LENGTH 277 +#define RAW_LENGTH 132 + +static int validate(void) { + if(arctech_dusk->rawlen == RAW_LENGTH) { + if(arctech_dusk->raw[arctech_dusk->rawlen-1] >= (MIN_PULSE_LENGTH*PULSE_DIV) && + arctech_dusk->raw[arctech_dusk->rawlen-1] <= (MAX_PULSE_LENGTH*PULSE_DIV) && + arctech_dusk->raw[1] >= AVG_PULSE_LENGTH*(PULSE_MULTIPLIER*3)) { + return 0; + } + } + + return -1; +} + +static void createMessage(int id, int unit, int state, int all) { + arctech_dusk->message = json_mkobject(); + json_append_member(arctech_dusk->message, "id", json_mknumber(id, 0)); + if(all == 1) { + json_append_member(arctech_dusk->message, "all", json_mknumber(all, 0)); + } else { + json_append_member(arctech_dusk->message, "unit", json_mknumber(unit, 0)); + } + + if(state == 1) { + json_append_member(arctech_dusk->message, "state", json_mkstring("dusk")); + } else { + json_append_member(arctech_dusk->message, "state", json_mkstring("dawn")); + } +} + +static void parseCode(void) { + int binary[RAW_LENGTH/4], x = 0, i = 0; + + if(arctech_dusk->rawlen>RAW_LENGTH) { + logprintf(LOG_ERR, "arctech_dusk: parsecode - invalid parameter passed %d", arctech_dusk->rawlen); + return; + } + + for(x=0;xrawlen;x+=4) { + if(arctech_dusk->raw[x+3] > AVG_PULSE_LENGTH*PULSE_MULTIPLIER) { + binary[i++] = 1; + } else { + binary[i++] = 0; + } + } + + int unit = binToDecRev(binary, 28, 31); + int state = binary[27]; + int all = binary[26]; + int id = binToDecRev(binary, 0, 25); + + createMessage(id, unit, state, all); +} + +#if !defined(MODULE) && !defined(_WIN32) +__attribute__((weak)) +#endif +void arctechDuskInit(void) { + + protocol_register(&arctech_dusk); + protocol_set_id(arctech_dusk, "arctech_dusk"); + protocol_device_add(arctech_dusk, "kaku_dusk", "KlikAanKlikUit Dusk Sensor"); + arctech_dusk->devtype = DUSK; + arctech_dusk->hwtype = RF433; + arctech_dusk->minrawlen = RAW_LENGTH; + arctech_dusk->maxrawlen = RAW_LENGTH; + arctech_dusk->maxgaplen = MAX_PULSE_LENGTH*PULSE_DIV; + arctech_dusk->mingaplen = MIN_PULSE_LENGTH*PULSE_DIV; + + options_add(&arctech_dusk->options, 'u', "unit", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^([0-9]{1}|[1][0-5])$"); + options_add(&arctech_dusk->options, 'i', "id", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^([0-9]{1,7}|[1-5][0-9]{7}|6([0-6][0-9]{6}|7(0[0-9]{5}|10([0-7][0-9]{3}|8([0-7][0-9]{2}|8([0-5][0-9]|6[0-3]))))))$"); + options_add(&arctech_dusk->options, 't', "dusk", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&arctech_dusk->options, 'f', "dawn", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + + arctech_dusk->parseCode=&parseCode; + arctech_dusk->validate=&validate; +} + +#if defined(MODULE) && !defined(_WIN32) +void compatibility(struct module_t *module) { + module->name = "arctech_dusk"; + module->version = "2.2"; + module->reqversion = "6.0"; + module->reqcommit = "84"; +} + +void init(void) { + arctechDuskInit(); +} +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_dusk.h b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_dusk.h new file mode 100755 index 0000000..5fa3275 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_dusk.h @@ -0,0 +1,27 @@ +/* + Copyright (C) 2013 CurlyMo & ldvp + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#ifndef _PROTOCOL_ARCTECH_DUSK_H_ +#define _PROTOCOL_ARCTECH_DUSK_H_ + +#include "../protocol.h" + +struct protocol_t *arctech_dusk; +void arctechDuskInit(void); + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_motion.c b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_motion.c new file mode 100755 index 0000000..37cc6b1 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_motion.c @@ -0,0 +1,125 @@ +/* + Copyright (C) 2014 CurlyMo & lvdp + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#include +#include +#include + +#include "../../core/pilight.h" +#include "../../core/common.h" +#include "../../core/dso.h" +#include "../../core/log.h" +#include "../protocol.h" +#include "../../core/binary.h" +#include "../../core/gc.h" +#include "arctech_motion.h" + +#define PULSE_MULTIPLIER 3 +#define MIN_PULSE_LENGTH 250 +#define MAX_PULSE_LENGTH 284 +#define AVG_PULSE_LENGTH 279 +#define RAW_LENGTH 132 + +static int validate(void) { + if(arctech_motion->rawlen == RAW_LENGTH) { + if(arctech_motion->raw[arctech_motion->rawlen-1] >= (MIN_PULSE_LENGTH*PULSE_DIV) && + arctech_motion->raw[arctech_motion->rawlen-1] <= (MAX_PULSE_LENGTH*PULSE_DIV) && + arctech_motion->raw[1] >= AVG_PULSE_LENGTH*(PULSE_MULTIPLIER*3)) { + return 0; + } + } + + return -1; +} + +static void createMessage(int id, int unit, int state, int all) { + arctech_motion->message = json_mkobject(); + json_append_member(arctech_motion->message, "id", json_mknumber(id, 0)); + if(all == 1) { + json_append_member(arctech_motion->message, "all", json_mknumber(all, 0)); + } else { + json_append_member(arctech_motion->message, "unit", json_mknumber(unit, 0)); + } + + if(state == 1) { + json_append_member(arctech_motion->message, "state", json_mkstring("on")); + } else { + json_append_member(arctech_motion->message, "state", json_mkstring("off")); + } +} + +static void parseCode(void) { + int binary[RAW_LENGTH/4], x = 0, i = 0; + + if(arctech_motion->rawlen>RAW_LENGTH) { + logprintf(LOG_ERR, "arctech_motion: parsecode - invalid parameter passed %d", arctech_motion->rawlen); + return; + } + + for(x=0;xrawlen;x+=4) { + if(arctech_motion->raw[x+3] > AVG_PULSE_LENGTH*PULSE_MULTIPLIER) { + binary[i++] = 1; + } else { + binary[i++] = 0; + } + } + + int unit = binToDecRev(binary, 28, 31); + int state = binary[27]; + int all = binary[26]; + int id = binToDecRev(binary, 0, 25); + + createMessage(id, unit, state, all); +} + +#if !defined(MODULE) && !defined(_WIN32) +__attribute__((weak)) +#endif +void arctechMotionInit(void) { + + protocol_register(&arctech_motion); + protocol_set_id(arctech_motion, "arctech_motion"); + protocol_device_add(arctech_motion, "kaku_motion", "KlikAanKlikUit Motion Sensor"); + arctech_motion->devtype = MOTION; + arctech_motion->hwtype = RF433; + arctech_motion->minrawlen = RAW_LENGTH; + arctech_motion->maxrawlen = RAW_LENGTH; + arctech_motion->maxgaplen = MAX_PULSE_LENGTH*PULSE_DIV; + arctech_motion->mingaplen = MIN_PULSE_LENGTH*PULSE_DIV; + + options_add(&arctech_motion->options, 'u', "unit", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^([0-9]{1}|[1][0-5])$"); + options_add(&arctech_motion->options, 'i', "id", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^([0-9]{1,7}|[1-5][0-9]{7}|6([0-6][0-9]{6}|7(0[0-9]{5}|10([0-7][0-9]{3}|8([0-7][0-9]{2}|8([0-5][0-9]|6[0-3]))))))$"); + options_add(&arctech_motion->options, 't', "on", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&arctech_motion->options, 'f', "off", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + + arctech_motion->parseCode=&parseCode; + arctech_motion->validate=&validate; +} + +#if defined(MODULE) && !defined(_WIN32) +void compatibility(struct module_t *module) { + module->name = "arctech_motion"; + module->version = "2.2"; + module->reqversion = "6.0"; + module->reqcommit = "84"; +} + +void init(void) { + arctechMotionInit(); +} +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_motion.h b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_motion.h new file mode 100755 index 0000000..67c59e5 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_motion.h @@ -0,0 +1,27 @@ +/* + Copyright (C) 2013 CurlyMo & lvdp + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#ifndef _PROTOCOL_ARCTECH_MOTION_H_ +#define _PROTOCOL_ARCTECH_MOTION_H_ + +#include "../protocol.h" + +struct protocol_t *arctech_motion; +void arctechMotionInit(void); + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_screen.c b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_screen.c new file mode 100755 index 0000000..89c25c4 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_screen.c @@ -0,0 +1,277 @@ +/* + Copyright (C) 2013 CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#include +#include +#include +#include + +#include "../../core/pilight.h" +#include "../../core/common.h" +#include "../../core/dso.h" +#include "../../core/log.h" +#include "../protocol.h" +#include "../../core/binary.h" +#include "../../core/gc.h" +#include "arctech_screen.h" + +#define LEARN_REPEATS 40 +#define NORMAL_REPEATS 10 +#define PULSE_MULTIPLIER 5 +#define MIN_PULSE_LENGTH 250 +#define MAX_PULSE_LENGTH 320 +#define AVG_PULSE_LENGTH 300 +#define RAW_LENGTH 132 + +static int validate(void) { + if(arctech_screen->rawlen == RAW_LENGTH) { + if(arctech_screen->raw[arctech_screen->rawlen-1] >= (MIN_PULSE_LENGTH*PULSE_DIV) && + arctech_screen->raw[arctech_screen->rawlen-1] <= (MAX_PULSE_LENGTH*PULSE_DIV) && + arctech_screen->raw[1] >= AVG_PULSE_LENGTH*(PULSE_MULTIPLIER*1.5)) { + return 0; + } + } + + return -1; +} + +static void createMessage(int id, int unit, int state, int all, int learn) { + arctech_screen->message = json_mkobject(); + json_append_member(arctech_screen->message, "id", json_mknumber(id, 0)); + if(all == 1) { + json_append_member(arctech_screen->message, "all", json_mknumber(all, 0)); + } else { + json_append_member(arctech_screen->message, "unit", json_mknumber(unit, 0)); + } + + if(state == 1) { + json_append_member(arctech_screen->message, "state", json_mkstring("up")); + } else { + json_append_member(arctech_screen->message, "state", json_mkstring("down")); + } + + if(learn == 1) { + arctech_screen->txrpt = LEARN_REPEATS; + } else { + arctech_screen->txrpt = NORMAL_REPEATS; + } +} + +static void parseCode(void) { + int binary[RAW_LENGTH/4], x = 0, i = 0; + + if(arctech_screen->rawlen>RAW_LENGTH) { + logprintf(LOG_ERR, "arctech_screen: parsecode - invalid parameter passed %d", arctech_screen->rawlen); + return; + } + + for(x=0;xrawlen;x+=4) { + if(arctech_screen->raw[x+3] > (int)((double)AVG_PULSE_LENGTH*((double)PULSE_MULTIPLIER/2))) { + binary[i++] = 1; + } else { + binary[i++] = 0; + } + } + + int unit = binToDecRev(binary, 28, 31); + int state = binary[27]; + int all = binary[26]; + int id = binToDecRev(binary, 0, 25); + + createMessage(id, unit, state, all, 0); +} + +static void createLow(int s, int e) { + int i; + + for(i=s;i<=e;i+=4) { + arctech_screen->raw[i]=(AVG_PULSE_LENGTH); + arctech_screen->raw[i+1]=(AVG_PULSE_LENGTH); + arctech_screen->raw[i+2]=(AVG_PULSE_LENGTH); + arctech_screen->raw[i+3]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + } +} + +static void createHigh(int s, int e) { + int i; + + for(i=s;i<=e;i+=4) { + arctech_screen->raw[i]=(AVG_PULSE_LENGTH); + arctech_screen->raw[i+1]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + arctech_screen->raw[i+2]=(AVG_PULSE_LENGTH); + arctech_screen->raw[i+3]=(AVG_PULSE_LENGTH); + } +} + +static void clearCode(void) { + createLow(2, 132); +} + +static void createStart(void) { + arctech_screen->raw[0]=(AVG_PULSE_LENGTH); + arctech_screen->raw[1]=(9*AVG_PULSE_LENGTH); +} + +static void createId(int id) { + int binary[255]; + int length = 0; + int i=0, x=0; + + length = decToBin(id, binary); + for(i=0;i<=length;i++) { + if(binary[i]==1) { + x=((length-i)+1)*4; + createHigh(106-x, 106-(x-3)); + } + } +} + +static void createAll(int all) { + if(all == 1) { + createHigh(106, 109); + } +} + +static void createState(int state) { + if(state == 1) { + createHigh(110, 113); + } +} + +static void createUnit(int unit) { + int binary[255]; + int length = 0; + int i=0, x=0; + + length = decToBin(unit, binary); + for(i=0;i<=length;i++) { + if(binary[i]==1) { + x=((length-i)+1)*4; + createHigh(130-x, 130-(x-3)); + } + } +} + +static void createFooter(void) { + arctech_screen->raw[131]=(PULSE_DIV*AVG_PULSE_LENGTH); +} + +static int createCode(struct JsonNode *code) { + int id = -1; + int unit = -1; + int state = -1; + int all = 0; + int learn = -1; + double itmp = -1; + + if(json_find_number(code, "id", &itmp) == 0) + id = (int)round(itmp); + if(json_find_number(code, "unit", &itmp) == 0) + unit = (int)round(itmp); + if(json_find_number(code, "all", &itmp) == 0) + all = (int)round(itmp); + if(json_find_number(code, "down", &itmp) == 0) + state=0; + else if(json_find_number(code, "up", &itmp) == 0) + state=1; + if(json_find_number(code, "learn", &itmp) == 0) + learn = 1; + + if(all > 0 && learn > -1) { + logprintf(LOG_ERR, "arctech_screen: all and learn cannot be combined"); + return EXIT_FAILURE; + } else if(id == -1 || (unit == -1 && all == 0) || state == -1) { + logprintf(LOG_ERR, "arctech_screen: insufficient number of arguments"); + return EXIT_FAILURE; + } else if(id > 67108863 || id < 1) { + logprintf(LOG_ERR, "arctech_screen: invalid id range"); + return EXIT_FAILURE; + } else if((unit > 15 || unit < 0) && all == 0) { + logprintf(LOG_ERR, "arctech_screen: invalid unit range"); + return EXIT_FAILURE; + } else { + if(unit == -1 && all == 1) { + unit = 0; + } + createMessage(id, unit, state, all, learn); + createStart(); + clearCode(); + createId(id); + createAll(all); + createState(state); + createUnit(unit); + createFooter(); + arctech_screen->rawlen = RAW_LENGTH; + } + return EXIT_SUCCESS; +} + +static void printHelp(void) { + printf("\t -t --up\t\t\tsend an up signal\n"); + printf("\t -f --down\t\t\tsend an down signal\n"); + printf("\t -u --unit=unit\t\t\tcontrol a device with this unit code\n"); + printf("\t -i --id=id\t\t\tcontrol a device with this id\n"); + printf("\t -a --all\t\t\tsend command to all devices with this id\n"); + printf("\t -l --learn\t\t\tsend multiple streams so screen can learn\n"); +} + +#if !defined(MODULE) && !defined(_WIN32) +__attribute__((weak)) +#endif +void arctechScreenInit(void) { + + protocol_register(&arctech_screen); + protocol_set_id(arctech_screen, "arctech_screen"); + protocol_device_add(arctech_screen, "kaku_screen", "KlikAanKlikUit Screens"); + protocol_device_add(arctech_screen, "dio_screen", "DI-O Screens"); + arctech_screen->devtype = SCREEN; + arctech_screen->hwtype = RF433; + arctech_screen->txrpt = NORMAL_REPEATS; + arctech_screen->minrawlen = RAW_LENGTH; + arctech_screen->maxrawlen = RAW_LENGTH; + arctech_screen->maxgaplen = MAX_PULSE_LENGTH*PULSE_DIV; + arctech_screen->mingaplen = MIN_PULSE_LENGTH*PULSE_DIV; + + options_add(&arctech_screen->options, 't', "up", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&arctech_screen->options, 'f', "down", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&arctech_screen->options, 'u', "unit", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^([0-9]{1}|[1][0-5])$"); + options_add(&arctech_screen->options, 'i', "id", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^([0-9]{1,7}|[1-5][0-9]{7}|6([0-6][0-9]{6}|7(0[0-9]{5}|10([0-7][0-9]{3}|8([0-7][0-9]{2}|8([0-5][0-9]|6[0-3]))))))$"); + options_add(&arctech_screen->options, 'a', "all", OPTION_OPT_VALUE, DEVICES_OPTIONAL, JSON_NUMBER, NULL, NULL); + options_add(&arctech_screen->options, 'l', "learn", OPTION_NO_VALUE, DEVICES_OPTIONAL, JSON_NUMBER, NULL, NULL); + + options_add(&arctech_screen->options, 0, "readonly", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + options_add(&arctech_screen->options, 0, "confirm", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + + arctech_screen->parseCode=&parseCode; + arctech_screen->createCode=&createCode; + arctech_screen->printHelp=&printHelp; + arctech_screen->validate=&validate; +} + +#if defined(MODULE) && !defined(_WIN32) +void compatibility(struct module_t *module) { + module->name = "arctech_screen"; + module->version = "3.4"; + module->reqversion = "6.0"; + module->reqcommit = "84"; +} + +void init(void) { + arctechScreenInit(); +} +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_screen.h b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_screen.h new file mode 100755 index 0000000..c9b790a --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_screen.h @@ -0,0 +1,27 @@ +/* + Copyright (C) 2013 CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#ifndef _PROTOCOL_ARCTECH_SCREEN_H_ +#define _PROTOCOL_ARCTECH_SCREEN_H_ + +#include "../protocol.h" + +struct protocol_t *arctech_screen; +void arctechScreenInit(void); + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_screen_old.c b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_screen_old.c new file mode 100755 index 0000000..e82ea00 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_screen_old.c @@ -0,0 +1,232 @@ +/* + Copyright (C) 2013 CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#include +#include +#include +#include + +#include "../../core/pilight.h" +#include "../../core/common.h" +#include "../../core/dso.h" +#include "../../core/log.h" +#include "../protocol.h" +#include "../../core/binary.h" +#include "../../core/gc.h" +#include "arctech_screen_old.h" + +#define PULSE_MULTIPLIER 3 +#define MIN_PULSE_LENGTH 310 +#define MAX_PULSE_LENGTH 350 +#define AVG_PULSE_LENGTH 335 +#define RAW_LENGTH 50 + +static int validate(void) { + if(arctech_screen_old->rawlen == RAW_LENGTH) { + if(arctech_screen_old->raw[arctech_screen_old->rawlen-1] >= (MIN_PULSE_LENGTH*PULSE_DIV) && + arctech_screen_old->raw[arctech_screen_old->rawlen-1] <= (MAX_PULSE_LENGTH*PULSE_DIV)) { + return 0; + } + } + + return -1; +} + +static void createMessage(int id, int unit, int state) { + arctech_screen_old->message = json_mkobject(); + json_append_member(arctech_screen_old->message, "id", json_mknumber(id, 0)); + json_append_member(arctech_screen_old->message, "unit", json_mknumber(unit, 0)); + if(state == 1) + json_append_member(arctech_screen_old->message, "state", json_mkstring("up")); + else + json_append_member(arctech_screen_old->message, "state", json_mkstring("down")); +} + +static void parseCode(void) { + int binary[RAW_LENGTH/4], x = 0, i = 0; + int len = (int)((double)AVG_PULSE_LENGTH*((double)PULSE_MULTIPLIER/2)); + + if(arctech_screen_old->rawlen>RAW_LENGTH) { + logprintf(LOG_ERR, "arctech_screen_old: parsecode - invalid parameter passed %d", arctech_screen_old->rawlen); + return; + } + + for(x=0;xrawlen-2;x+=4) { + if(arctech_screen_old->raw[x+3] > len) { + binary[i++] = 0; + } else { + binary[i++] = 1; + } + } + + int unit = binToDec(binary, 0, 3); + int state = binary[11]; + int id = binToDec(binary, 4, 8); + createMessage(id, unit, state); +} + +static void createLow(int s, int e) { + int i; + + for(i=s;i<=e;i+=4) { + arctech_screen_old->raw[i]=(AVG_PULSE_LENGTH); + arctech_screen_old->raw[i+1]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + arctech_screen_old->raw[i+2]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + arctech_screen_old->raw[i+3]=(AVG_PULSE_LENGTH); + } +} + +static void createHigh(int s, int e) { + int i; + + for(i=s;i<=e;i+=4) { + arctech_screen_old->raw[i]=(AVG_PULSE_LENGTH); + arctech_screen_old->raw[i+1]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + arctech_screen_old->raw[i+2]=(AVG_PULSE_LENGTH); + arctech_screen_old->raw[i+3]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + } +} + +static void clearCode(void) { + createHigh(0,35); + createLow(36,47); +} + +static void createUnit(int unit) { + int binary[255]; + int length = 0; + int i=0, x=0; + + length = decToBinRev(unit, binary); + for(i=0;i<=length;i++) { + if(binary[i]==1) { + x=i*4; + createLow(x, x+3); + } + } +} + +static void createId(int id) { + int binary[255]; + int length = 0; + int i=0, x=0; + + length = decToBinRev(id, binary); + for(i=0;i<=length;i++) { + if(binary[i]==1) { + x=i*4; + createLow(16+x, 16+x+3); + } + } +} + +static void createState(int state) { + if(state == 0) { + createHigh(44,47); + } +} + +static void createFooter(void) { + arctech_screen_old->raw[48]=(AVG_PULSE_LENGTH); + arctech_screen_old->raw[49]=(PULSE_DIV*AVG_PULSE_LENGTH); +} + +static int createCode(struct JsonNode *code) { + int id = -1; + int unit = -1; + int state = -1; + double itmp = -1; + + if(json_find_number(code, "id", &itmp) == 0) + id = (int)round(itmp); + if(json_find_number(code, "unit", &itmp) == 0) + unit = (int)round(itmp); + if(json_find_number(code, "down", &itmp) == 0) + state=0; + else if(json_find_number(code, "up", &itmp) == 0) + state=1; + + if(id == -1 || unit == -1 || state == -1) { + logprintf(LOG_ERR, "arctech_screen_old: insufficient number of arguments"); + return EXIT_FAILURE; + } else if(id > 31 || id < 0) { + logprintf(LOG_ERR, "arctech_screen_old: invalid id range"); + return EXIT_FAILURE; + } else if(unit > 15 || unit < 0) { + logprintf(LOG_ERR, "arctech_screen_old: invalid unit range"); + return EXIT_FAILURE; + } else { + createMessage(id, unit, state); + clearCode(); + createUnit(unit); + createId(id); + createState(state); + createFooter(); + arctech_screen_old->rawlen = RAW_LENGTH; + } + return EXIT_SUCCESS; +} + +static void printHelp(void) { + printf("\t -t --up\t\t\tsend an up signal\n"); + printf("\t -f --down\t\t\tsend an down signal\n"); + printf("\t -u --unit=unit\t\t\tcontrol a device with this unit code\n"); + printf("\t -i --id=id\t\t\tcontrol a device with this id\n"); +} + +#if !defined(MODULE) && !defined(_WIN32) +__attribute__((weak)) +#endif +void arctechScreenOldInit(void) { + + protocol_register(&arctech_screen_old); + protocol_set_id(arctech_screen_old, "arctech_screen_old"); + protocol_device_add(arctech_screen_old, "kaku_screen_old", "Old KlikAanKlikUit Screens"); + arctech_screen_old->devtype = SCREEN; + arctech_screen_old->hwtype = RF433; + arctech_screen_old->minrawlen = RAW_LENGTH; + arctech_screen_old->maxrawlen = RAW_LENGTH; + arctech_screen_old->maxgaplen = MAX_PULSE_LENGTH*PULSE_DIV; + arctech_screen_old->mingaplen = MIN_PULSE_LENGTH*PULSE_DIV; + + options_add(&arctech_screen_old->options, 't', "up", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&arctech_screen_old->options, 'f', "down", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&arctech_screen_old->options, 'u', "unit", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^([0-9]{1}|[1][0-5])$"); + options_add(&arctech_screen_old->options, 'i', "id", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^(3[012]?|[012][0-9]|[0-9]{1})$"); + + options_add(&arctech_screen_old->options, 0, "readonly", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + options_add(&arctech_screen_old->options, 0, "confirm", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + + arctech_screen_old->parseCode=&parseCode; + arctech_screen_old->createCode=&createCode; + arctech_screen_old->printHelp=&printHelp; + arctech_screen_old->validate=&validate; +} + +#if defined(MODULE) && !defined(_WIN32) +void compatibility(struct module_t *module) { + module->name = "arctech_screen_old"; + module->version = "2.5"; + module->reqversion = "6.0"; + module->reqcommit = "84"; +} + +void init(void) { + arctechScreenOldInit(); +} +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_screen_old.h b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_screen_old.h new file mode 100755 index 0000000..09080dc --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_screen_old.h @@ -0,0 +1,27 @@ +/* + Copyright (C) 2013 CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#ifndef _PROTOCOL_ARCTECH_SCREEN_OLD_H_ +#define _PROTOCOL_ARCTECH_SCREEN_OLD_H_ + +#include "../protocol.h" + +struct protocol_t *arctech_screen_old; +void arctechScreenOldInit(void); + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_switch.c b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_switch.c new file mode 100755 index 0000000..bcc0ad0 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_switch.c @@ -0,0 +1,281 @@ +/* + Copyright (C) 2013 CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#include +#include +#include +#include + +#include "../../core/pilight.h" +#include "../../core/common.h" +#include "../../core/dso.h" +#include "../../core/log.h" +#include "../protocol.h" +#include "../../core/binary.h" +#include "../../core/gc.h" +#include "arctech_switch.h" + +#define LEARN_REPEATS 40 +#define NORMAL_REPEATS 10 +#define PULSE_MULTIPLIER 4 +#define MIN_PULSE_LENGTH 250 +#define MAX_PULSE_LENGTH 320 +#define AVG_PULSE_LENGTH 315 +#define RAW_LENGTH 132 + +static int validate(void) { + if(arctech_switch->rawlen == RAW_LENGTH) { + if(arctech_switch->raw[arctech_switch->rawlen-1] >= (MIN_PULSE_LENGTH*PULSE_DIV) && + arctech_switch->raw[arctech_switch->rawlen-1] <= (MAX_PULSE_LENGTH*PULSE_DIV) && + arctech_switch->raw[1] >= AVG_PULSE_LENGTH*(PULSE_MULTIPLIER*1.5)) { + return 0; + } + } + + return -1; +} + +static void createMessage(int id, int unit, int state, int all, int learn) { + arctech_switch->message = json_mkobject(); + + json_append_member(arctech_switch->message, "id", json_mknumber(id, 0)); + + if(all == 1) { + json_append_member(arctech_switch->message, "all", json_mknumber(all, 0)); + } else { + json_append_member(arctech_switch->message, "unit", json_mknumber(unit, 0)); + } + + if(state == 1) { + json_append_member(arctech_switch->message, "state", json_mkstring("on")); + } else { + json_append_member(arctech_switch->message, "state", json_mkstring("off")); + } + + if(learn == 1) { + arctech_switch->txrpt = LEARN_REPEATS; + } else { + arctech_switch->txrpt = NORMAL_REPEATS; + } +} + +static void parseCode(void) { + int binary[RAW_LENGTH/4], x = 0, i = 0; + + if(arctech_switch->rawlen>RAW_LENGTH) { + logprintf(LOG_ERR, "arctech_switch: parsecode - invalid parameter passed %d", arctech_switch->rawlen); + return; + } + + for(x=0;xrawlen;x+=4) { + if(arctech_switch->raw[x+3] > (int)((double)AVG_PULSE_LENGTH*((double)PULSE_MULTIPLIER/2))) { + binary[i++] = 1; + } else { + binary[i++] = 0; + } + } + + int unit = binToDecRev(binary, 28, 31); + int state = binary[27]; + int all = binary[26]; + int id = binToDecRev(binary, 0, 25); + + createMessage(id, unit, state, all, 0); +} + +static void createLow(int s, int e) { + int i; + + for(i=s;i<=e;i+=4) { + arctech_switch->raw[i]=(AVG_PULSE_LENGTH); + arctech_switch->raw[i+1]=(AVG_PULSE_LENGTH); + arctech_switch->raw[i+2]=(AVG_PULSE_LENGTH); + arctech_switch->raw[i+3]=(AVG_PULSE_LENGTH*PULSE_MULTIPLIER); + } +} + +static void createHigh(int s, int e) { + int i; + + for(i=s;i<=e;i+=4) { + arctech_switch->raw[i]=(AVG_PULSE_LENGTH); + arctech_switch->raw[i+1]=(AVG_PULSE_LENGTH*PULSE_MULTIPLIER); + arctech_switch->raw[i+2]=(AVG_PULSE_LENGTH); + arctech_switch->raw[i+3]=(AVG_PULSE_LENGTH); + } +} + +static void clearCode(void) { + createLow(2, 131); +} + +static void createStart(void) { + arctech_switch->raw[0]=(AVG_PULSE_LENGTH); + arctech_switch->raw[1]=(9*AVG_PULSE_LENGTH); +} + +static void createId(int id) { + int binary[255]; + int length = 0; + int i=0, x=0; + + length = decToBin(id, binary); + for(i=0;i<=length;i++) { + if(binary[i]==1) { + x=((length-i)+1)*4; + createHigh(106-x, 106-(x-3)); + } + } +} + +static void createAll(int all) { + if(all == 1) { + createHigh(106, 109); + } +} + +static void createState(int state) { + if(state == 1) { + createHigh(110, 113); + } +} + +static void createUnit(int unit) { + int binary[255]; + int length = 0; + int i=0, x=0; + + length = decToBin(unit, binary); + for(i=0;i<=length;i++) { + if(binary[i]==1) { + x=((length-i)+1)*4; + createHigh(130-x, 130-(x-3)); + } + } +} + +static void createFooter(void) { + arctech_switch->raw[131]=(PULSE_DIV*AVG_PULSE_LENGTH); +} + +static int createCode(struct JsonNode *code) { + int id = -1; + int unit = -1; + int state = -1; + int all = 0; + int learn = -1; + double itmp = -1; + + if(json_find_number(code, "id", &itmp) == 0) + id = (int)round(itmp); + if(json_find_number(code, "unit", &itmp) == 0) + unit = (int)round(itmp); + if(json_find_number(code, "all", &itmp) == 0) + all = (int)round(itmp); + if(json_find_number(code, "off", &itmp) == 0) + state=0; + else if(json_find_number(code, "on", &itmp) == 0) + state=1; + if(json_find_number(code, "learn", &itmp) == 0) + learn = 1; + + if(all > 0 && learn > -1) { + logprintf(LOG_ERR, "arctech_switch: all and learn cannot be combined"); + return EXIT_FAILURE; + } else if(id == -1 || (unit == -1 && all == 0) || state == -1) { + logprintf(LOG_ERR, "arctech_switch: insufficient number of arguments"); + return EXIT_FAILURE; + } else if(id > 67108863 || id < 1) { + logprintf(LOG_ERR, "arctech_switch: invalid id range"); + return EXIT_FAILURE; + } else if((unit > 15 || unit < 0) && all == 0) { + logprintf(LOG_ERR, "arctech_switch: invalid unit range"); + return EXIT_FAILURE; + } else { + if(unit == -1 && all == 1) { + unit = 0; + } + createMessage(id, unit, state, all, learn); + createStart(); + clearCode(); + createId(id); + createAll(all); + createState(state); + createUnit(unit); + createFooter(); + arctech_switch->rawlen = RAW_LENGTH; + } + return EXIT_SUCCESS; +} + +static void printHelp(void) { + printf("\t -t --on\t\t\tsend an on signal\n"); + printf("\t -f --off\t\t\tsend an off signal\n"); + printf("\t -u --unit=unit\t\t\tcontrol a device with this unit code\n"); + printf("\t -i --id=id\t\t\tcontrol a device with this id\n"); + printf("\t -a --all\t\t\tsend command to all devices with this id\n"); + printf("\t -l --learn\t\t\tsend multiple streams so switch can learn\n"); +} + +#if !defined(MODULE) && !defined(_WIN32) +__attribute__((weak)) +#endif +void arctechSwitchInit(void) { + protocol_register(&arctech_switch); + protocol_set_id(arctech_switch, "arctech_switch"); + protocol_device_add(arctech_switch, "kaku_switch", "KlikAanKlikUit Switches"); + protocol_device_add(arctech_switch, "dio_switch", "D-IO Switches"); + protocol_device_add(arctech_switch, "nexa_switch", "Nexa Switches"); + protocol_device_add(arctech_switch, "coco_switch", "CoCo Technologies Switches"); + protocol_device_add(arctech_switch, "intertechno_switch", "Intertechno Switches"); + arctech_switch->devtype = SWITCH; + arctech_switch->hwtype = RF433; + arctech_switch->txrpt = NORMAL_REPEATS; + arctech_switch->minrawlen = RAW_LENGTH; + arctech_switch->maxrawlen = RAW_LENGTH; + arctech_switch->maxgaplen = MAX_PULSE_LENGTH*PULSE_DIV; + arctech_switch->mingaplen = MIN_PULSE_LENGTH*PULSE_DIV; + + options_add(&arctech_switch->options, 't', "on", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&arctech_switch->options, 'f', "off", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&arctech_switch->options, 'u', "unit", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^([0-9]{1}|[1][0-5])$"); + options_add(&arctech_switch->options, 'i', "id", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^([0-9]{1,7}|[1-5][0-9]{7}|6([0-6][0-9]{6}|7(0[0-9]{5}|10([0-7][0-9]{3}|8([0-7][0-9]{2}|8([0-5][0-9]|6[0-3]))))))$"); + options_add(&arctech_switch->options, 'a', "all", OPTION_OPT_VALUE, DEVICES_OPTIONAL, JSON_NUMBER, NULL, NULL); + options_add(&arctech_switch->options, 'l', "learn", OPTION_NO_VALUE, DEVICES_OPTIONAL, JSON_NUMBER, NULL, NULL); + + options_add(&arctech_switch->options, 0, "readonly", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + options_add(&arctech_switch->options, 0, "confirm", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + + arctech_switch->parseCode=&parseCode; + arctech_switch->createCode=&createCode; + arctech_switch->printHelp=&printHelp; + arctech_switch->validate=&validate; +} + +#if defined(MODULE) && !defined(_WIN32) +void compatibility(struct module_t *module) { + module->name = "arctech_switch"; + module->version = "3.4"; + module->reqversion = "6.0"; + module->reqcommit = "84"; +} + +void init(void) { + arctechSwitchInit(); +} +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_switch.h b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_switch.h new file mode 100755 index 0000000..2bffaee --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_switch.h @@ -0,0 +1,27 @@ +/* + Copyright (C) 2013 CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#ifndef _PROTOCOL_ARCTECH_SWITCH_H_ +#define _PROTOCOL_ARCTECH_SWITCH_H_ + +#include "../protocol.h" + +struct protocol_t *arctech_switch; +void arctechSwitchInit(void); + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_switch_old.c b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_switch_old.c new file mode 100755 index 0000000..b57d09e --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_switch_old.c @@ -0,0 +1,255 @@ +/* + Copyright (C) 2013 CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#include +#include +#include +#include + +#include "../../core/pilight.h" +#include "../../core/common.h" +#include "../../core/dso.h" +#include "../../core/log.h" +#include "../protocol.h" +#include "../../core/binary.h" +#include "../../core/gc.h" +#include "arctech_switch_old.h" + +#define PULSE_MULTIPLIER 3 +#define MIN_PULSE_LENGTH 310 +#define MAX_PULSE_LENGTH 405 +#define AVG_PULSE_LENGTH 335 +#define RAW_LENGTH 50 + +static int validate(void) { + if(arctech_switch_old->rawlen == RAW_LENGTH) { + if(arctech_switch_old->raw[arctech_switch_old->rawlen-1] >= (MIN_PULSE_LENGTH*PULSE_DIV) && + arctech_switch_old->raw[arctech_switch_old->rawlen-1] <= (MAX_PULSE_LENGTH*PULSE_DIV)) { + return 0; + } + } + + return -1; +} + +static void createMessage(int id, int unit, int state) { + arctech_switch_old->message = json_mkobject(); + json_append_member(arctech_switch_old->message, "id", json_mknumber(id, 0)); + json_append_member(arctech_switch_old->message, "unit", json_mknumber(unit, 0)); + if(state == 1) + json_append_member(arctech_switch_old->message, "state", json_mkstring("on")); + else + json_append_member(arctech_switch_old->message, "state", json_mkstring("off")); +} + +static void parseCode(void) { + int binary[RAW_LENGTH/4], x = 0, i = 0; + int len = (int)((double)AVG_PULSE_LENGTH*((double)PULSE_MULTIPLIER/2)); + + if(arctech_switch_old->rawlen>RAW_LENGTH) { + logprintf(LOG_ERR, "arctech_switch_old: parsecode - invalid parameter passed %d", arctech_switch_old->rawlen); + return; + } + + for(x=0;xrawlen-3;x+=4) { + // valid telegrams must consist of 0110 and 1001 blocks + int low_high = 0; + if(arctech_switch_old->raw[x] > len) { + low_high |= 1; + } + if(arctech_switch_old->raw[x+1] > len) { + low_high |= 2; + } + if(arctech_switch_old->raw[x+2] > len) { + low_high |= 4; + } + if(arctech_switch_old->raw[x+3] > len) { + low_high |= 8; + } + switch(low_high) { + case 6: + binary[i++] = 1; + break; + case 10: + binary[i++] = 0; + break; + default: + return; // invalid telegram + } + } + + int unit = binToDec(binary, 0, 3); + int state = binary[11]; + int id = binToDec(binary, 4, 8); + createMessage(id, unit, state); +} + +static void createLow(int s, int e) { + int i; + + for(i=s;i<=e;i+=4) { + arctech_switch_old->raw[i]=(AVG_PULSE_LENGTH); + arctech_switch_old->raw[i+1]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + arctech_switch_old->raw[i+2]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + arctech_switch_old->raw[i+3]=(AVG_PULSE_LENGTH); + } +} + +static void createHigh(int s, int e) { + int i; + + for(i=s;i<=e;i+=4) { + arctech_switch_old->raw[i]=(AVG_PULSE_LENGTH); + arctech_switch_old->raw[i+1]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + arctech_switch_old->raw[i+2]=(AVG_PULSE_LENGTH); + arctech_switch_old->raw[i+3]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + } +} + +static void clearCode(void) { + createHigh(0,35); + createLow(36,47); +} + +static void createUnit(int unit) { + int binary[255]; + int length = 0; + int i=0, x=0; + + length = decToBinRev(unit, binary); + for(i=0;i<=length;i++) { + if(binary[i]==1) { + x=i*4; + createLow(x, x+3); + } + } +} + +static void createId(int id) { + int binary[255]; + int length = 0; + int i=0, x=0; + + length = decToBinRev(id, binary); + for(i=0;i<=length;i++) { + if(binary[i]==1) { + x=i*4; + createLow(16+x, 16+x+3); + } + } +} + +static void createState(int state) { + if(state == 0) { + createHigh(44,47); + } +} + +static void createFooter(void) { + arctech_switch_old->raw[48]=(AVG_PULSE_LENGTH); + arctech_switch_old->raw[49]=(PULSE_DIV*AVG_PULSE_LENGTH); +} + +static int createCode(struct JsonNode *code) { + int id = -1; + int unit = -1; + int state = -1; + double itmp = -1; + + if(json_find_number(code, "id", &itmp) == 0) + id = (int)round(itmp); + if(json_find_number(code, "unit", &itmp) == 0) + unit = (int)round(itmp); + if(json_find_number(code, "off", &itmp) == 0) + state=0; + else if(json_find_number(code, "on", &itmp) == 0) + state=1; + + if(id == -1 || unit == -1 || state == -1) { + logprintf(LOG_ERR, "arctech_switch_old: insufficient number of arguments"); + return EXIT_FAILURE; + } else if(id > 31 || id < 0) { + logprintf(LOG_ERR, "arctech_switch_old: invalid id range"); + return EXIT_FAILURE; + } else if(unit > 15 || unit < 0) { + logprintf(LOG_ERR, "arctech_switch_old: invalid unit range"); + return EXIT_FAILURE; + } else { + createMessage(id, unit, state); + clearCode(); + createUnit(unit); + createId(id); + createState(state); + createFooter(); + arctech_switch_old->rawlen = RAW_LENGTH; + } + return EXIT_SUCCESS; +} + +static void printHelp(void) { + printf("\t -t --on\t\t\tsend an on signal\n"); + printf("\t -f --off\t\t\tsend an off signal\n"); + printf("\t -u --unit=unit\t\t\tcontrol a device with this unit code\n"); + printf("\t -i --id=id\t\t\tcontrol a device with this id\n"); +} + +#if !defined(MODULE) && !defined(_WIN32) +__attribute__((weak)) +#endif +void arctechSwitchOldInit(void) { + + protocol_register(&arctech_switch_old); + protocol_set_id(arctech_switch_old, "arctech_switch_old"); + protocol_device_add(arctech_switch_old, "kaku_switch_old", "Old KlikAanKlikUit Switches"); + protocol_device_add(arctech_switch_old, "cogex", "Cogex Switches"); + protocol_device_add(arctech_switch_old, "intertechno_old", "Old Intertechno Switches"); + protocol_device_add(arctech_switch_old, "byebyestandby", "Bye Bye Standby Switches"); + protocol_device_add(arctech_switch_old, "duwi", "Düwi Terminal Switches"); + arctech_switch_old->devtype = SWITCH; + arctech_switch_old->hwtype = RF433; + arctech_switch_old->minrawlen = RAW_LENGTH; + arctech_switch_old->maxrawlen = RAW_LENGTH; + arctech_switch_old->maxgaplen = MAX_PULSE_LENGTH*PULSE_DIV; + arctech_switch_old->mingaplen = MIN_PULSE_LENGTH*PULSE_DIV; + + options_add(&arctech_switch_old->options, 't', "on", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&arctech_switch_old->options, 'f', "off", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&arctech_switch_old->options, 'u', "unit", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^([0-9]{1}|[1][0-5])$"); + options_add(&arctech_switch_old->options, 'i', "id", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^(3[012]?|[012][0-9]|[0-9]{1})$"); + + options_add(&arctech_switch_old->options, 0, "readonly", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + options_add(&arctech_switch_old->options, 0, "confirm", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + + arctech_switch_old->parseCode=&parseCode; + arctech_switch_old->createCode=&createCode; + arctech_switch_old->printHelp=&printHelp; + arctech_switch_old->validate=&validate; +} + +#if defined(MODULE) && !defined(_WIN32) +void compatibility(struct module_t *module) { + module->name = "arctech_switch_old"; + module->version = "2.5"; + module->reqversion = "6.0"; + module->reqcommit = "84"; +} + +void init(void) { + arctechSwitchOldInit(); +} +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_switch_old.h b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_switch_old.h new file mode 100755 index 0000000..6ee2228 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/arctech_switch_old.h @@ -0,0 +1,27 @@ +/* + Copyright (C) 2013 CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#ifndef _PROTOCOL_ARCTECH_SWITCH_OLD_H_ +#define _PROTOCOL_ARCTECH_SWITCH_OLD_H_ + +#include "../protocol.h" + +struct protocol_t *arctech_switch_old; +void arctechSwitchOldInit(void); + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/auriol.c b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/auriol.c new file mode 100755 index 0000000..78cbb05 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/auriol.c @@ -0,0 +1,204 @@ +/* + Copyright (C) 2014 Bram1337 & CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#include +#include +#include +#include + +#include "../../core/pilight.h" +#include "../../core/common.h" +#include "../../core/dso.h" +#include "../../core/log.h" +#include "../protocol.h" +#include "../../core/binary.h" +#include "../../core/gc.h" +#include "auriol.h" + +#define PULSE_MULTIPLIER 13 +#define MIN_PULSE_LENGTH 264 +#define MAX_PULSE_LENGTH 274 +#define AVG_PULSE_LENGTH 269 +#define RAW_LENGTH 66 + +typedef struct settings_t { + double id; + double temp; + struct settings_t *next; +} settings_t; + +static struct settings_t *settings = NULL; + +static int validate(void) { + if(auriol->rawlen == RAW_LENGTH) { + if(auriol->raw[auriol->rawlen-1] >= (MIN_PULSE_LENGTH*PULSE_DIV) && + auriol->raw[auriol->rawlen-1] <= (MAX_PULSE_LENGTH*PULSE_DIV)) { + return 0; + } + } + + return -1; +} + +static void parseCode(void) { + int i = 0, x = 0, binary[RAW_LENGTH/2]; + int channel = 0, id = 0, battery = 0; + double temp_offset = 0.0, temperature = 0.0; + + if(auriol->rawlen>RAW_LENGTH) { + logprintf(LOG_ERR, "auriol: parsecode - invalid parameter passed %d", auriol->rawlen); + return; + } + + for(x=1;xrawlen-2;x+=2) { + if(auriol->raw[x] > AVG_PULSE_LENGTH*PULSE_MULTIPLIER) { + binary[i++] = 1; + } else { + binary[i++] = 0; + } + } + + id = binToDecRev(binary, 0, 7); + battery = binary[8]; + channel = 1 + binToDecRev(binary, 10, 11); // channel as id + temperature = (double)binToSignedRev(binary, 12, 23)/10; + // checksum = (double)binToDecRev(binary, 24, 31); been unable to deciper it + struct settings_t *tmp = settings; + while(tmp) { + if(fabs(tmp->id-id) < EPSILON) { + temp_offset = tmp->temp; + break; + } + tmp = tmp->next; + } + + temperature += temp_offset; + + if(channel != 4) { + auriol->message = json_mkobject(); + json_append_member(auriol->message, "id", json_mknumber(id, 0)); + json_append_member(auriol->message, "temperature", json_mknumber(temperature, 1)); + json_append_member(auriol->message, "battery", json_mknumber(battery, 0)); + json_append_member(auriol->message, "channel", json_mknumber(channel, 0)); + } +} + +static int checkValues(struct JsonNode *jvalues) { + struct JsonNode *jid = NULL; + + if((jid = json_find_member(jvalues, "id"))) { + struct settings_t *snode = NULL; + struct JsonNode *jchild = NULL; + struct JsonNode *jchild1 = NULL; + double id = -1; + int match = 0; + + jchild = json_first_child(jid); + while(jchild) { + jchild1 = json_first_child(jchild); + while(jchild1) { + if(strcmp(jchild1->key, "id") == 0) { + id = jchild1->number_; + } + jchild1 = jchild1->next; + } + jchild = jchild->next; + } + + struct settings_t *tmp = settings; + while(tmp) { + if(fabs(tmp->id-id) < EPSILON) { + match = 1; + break; + } + tmp = tmp->next; + } + + if(match == 0) { + if((snode = MALLOC(sizeof(struct settings_t))) == NULL) { + fprintf(stderr, "out of memory\n"); + exit(EXIT_FAILURE); + } + snode->id = id; + snode->temp = 0; + + json_find_number(jvalues, "temperature-offset", &snode->temp); + + snode->next = settings; + settings = snode; + } + } + return 0; +} + +static void gc(void) { + struct settings_t *tmp = NULL; + while(settings) { + tmp = settings; + settings = settings->next; + FREE(tmp); + } + if(settings != NULL) { + FREE(settings); + } +} + +#if !defined(MODULE) && !defined(_WIN32) +__attribute__((weak)) +#endif +void auriolInit(void) { + + protocol_register(&auriol); + protocol_set_id(auriol, "auriol"); + protocol_device_add(auriol, "auriol", "Auriol Weather Stations"); + auriol->devtype = WEATHER; + auriol->hwtype = RF433; + auriol->minrawlen = RAW_LENGTH; + auriol->maxrawlen = RAW_LENGTH; + auriol->maxgaplen = MAX_PULSE_LENGTH*PULSE_DIV; + auriol->mingaplen = MIN_PULSE_LENGTH*PULSE_DIV; + + options_add(&auriol->options, 'i', "id", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "[0-9]"); + options_add(&auriol->options, 'c', "channel", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "[1-3]"); + options_add(&auriol->options, 't', "temperature", OPTION_HAS_VALUE, DEVICES_VALUE, JSON_NUMBER, NULL, "^[0-9]{1,3}$"); + options_add(&auriol->options, 'b', "battery", OPTION_HAS_VALUE, DEVICES_VALUE, JSON_NUMBER, NULL, "^[01]$"); + + // options_add(&auriol->options, 0, "decimals", OPTION_HAS_VALUE, DEVICES_SETTING, JSON_NUMBER, (void *)1, "[0-9]"); + options_add(&auriol->options, 0, "temperature-offset", OPTION_HAS_VALUE, DEVICES_SETTING, JSON_NUMBER, (void *)0, "[0-9]"); + options_add(&auriol->options, 0, "temperature-decimals", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)1, "[0-9]"); + options_add(&auriol->options, 0, "show-temperature", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)1, "^[10]{1}$"); + options_add(&auriol->options, 0, "show-battery", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)1, "^[10]{1}$"); + + auriol->parseCode=&parseCode; + auriol->checkValues=&checkValues; + auriol->validate=&validate; + auriol->gc=&gc; +} + +#if defined(MODULE) && !defined(_WIN32) +void compatibility(struct module_t *module) { + module->name = "auriol"; + module->version = "2.3"; + module->reqversion = "6.0"; + module->reqcommit = "84"; +} + +void init(void) { + auriolInit(); +} +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/auriol.h b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/auriol.h new file mode 100755 index 0000000..96bf3e3 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/auriol.h @@ -0,0 +1,27 @@ +/* + Copyright (C) 2014 Bram1337 & CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#ifndef _PROTOCOL_AURIOL_H_ +#define _PROTOCOL_AURIOL_H_ + +#include "../protocol.h" + +struct protocol_t *auriol; +void auriolInit(void); + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/beamish_switch.c b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/beamish_switch.c new file mode 100755 index 0000000..a5fe845 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/beamish_switch.c @@ -0,0 +1,259 @@ +/* + Copyright (C) 2014 CurlyMo & wo-rasp + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#include +#include +#include +#include + +#include "../../core/pilight.h" +#include "../../core/common.h" +#include "../../core/dso.h" +#include "../../core/log.h" +#include "../protocol.h" +#include "../../core/binary.h" +#include "../../core/gc.h" +#include "beamish_switch.h" + +#define PULSE_MULTIPLIER 4 +#define NORMAL_REPEATS 10 +#define MIN_PULSE_LENGTH 318 +#define MAX_PULSE_LENGTH 328 +#define AVG_PULSE_LENGTH 323 +#define RAW_LENGTH 50 + +static int map[7] = {0, 192, 48, 12, 3, 15, 195}; + +static int validate(void) { + if(beamish_switch->rawlen == RAW_LENGTH) { + if(beamish_switch->raw[beamish_switch->rawlen-1] >= (MIN_PULSE_LENGTH*PULSE_DIV) && + beamish_switch->raw[beamish_switch->rawlen-1] <= (MAX_PULSE_LENGTH*PULSE_DIV)) { + return 0; + } + } + + return -1; +} + +static void createMessage(int id, int unit, int state, int all) { + beamish_switch->message = json_mkobject(); + json_append_member(beamish_switch->message, "id", json_mknumber(id, 0)); + if(all == 1) { + json_append_member(beamish_switch->message, "all", json_mknumber(1, 0)); + } else { + json_append_member(beamish_switch->message, "unit", json_mknumber(unit, 0)); + } + if(state == 0) { + json_append_member(beamish_switch->message, "state", json_mkstring("off")); + } + if(state == 1) { + json_append_member(beamish_switch->message, "state", json_mkstring("on")); + } +} + +static void parseCode(void) { + int i = 0, x = 0, y = 0, binary[RAW_LENGTH/2]; + int id = -1, state = -1, unit = -1, all = 0, code = 0; + + if(beamish_switch->rawlen>RAW_LENGTH) { + logprintf(LOG_ERR, "beamish_switch: parsecode - invalid parameter passed %d", beamish_switch->rawlen); + return; + } + + for(x=0;xrawlen;x+=2) { + if(beamish_switch->raw[x] > (int)((double)AVG_PULSE_LENGTH*((double)PULSE_MULTIPLIER/2))) { + binary[i++] = 1; + } else { + binary[i++] = 0; + } + } + + id = binToDecRev(binary, 0, 15); + code = binToDecRev(binary, 16, 23); + + for(y=0;y<7;y++) { + if(map[y] == code) { + unit = y; + break; + } + } + + if(unit == 5) { + state = 1; + all = 1; + } + if(unit == 6) { + state = 0; + all = 1; + } + + createMessage(id, unit, state, all); +} + +static void createHigh(int s, int e) { + int i; + + for(i=s;i<=e;i+=2) { + beamish_switch->raw[i]=(AVG_PULSE_LENGTH); + beamish_switch->raw[i+1]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + } +} + +static void createLow(int s, int e) { + int i; + + for(i=s;i<=e;i+=2) { + beamish_switch->raw[i]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + beamish_switch->raw[i+1]=(AVG_PULSE_LENGTH); + } +} + +static void clearCode(void) { + createHigh(0,47); +} + +static void createId(int id) { + int binary[255]; + int length = 0; + int i=0, x=0; + + length = decToBinRev(id, binary); + for(i=0;i<=length;i++) { + if(binary[i]==1) { + x=i*2; + createLow(31-(x+1), 31-x); + } + } +} + +static void createUnit(int unit) { + int binary[255]; + int length = 0; + int i=0, x=0; + + length = decToBinRev(unit, binary); + for(i=0;i<=length;i++) { + if(binary[i]==1) { + x=i*2; + createLow(47-(x+1), 47-x); + } + } +} + +static void createFooter(void) { + beamish_switch->raw[48]=(AVG_PULSE_LENGTH); + beamish_switch->raw[49]=(PULSE_DIV*AVG_PULSE_LENGTH); +} + +static int createCode(struct JsonNode *code) { + int id = -1; + int unit = -1; + int state = -1; + int all = 0; + double itmp = -1; + + if(json_find_number(code, "id", &itmp) == 0) + id = (int)round(itmp); + if(json_find_number(code, "unit", &itmp) == 0) + unit = (int)round(itmp); + if(json_find_number(code, "all", &itmp) == 0) + all = 1; + if(json_find_number(code, "off", &itmp) == 0) + state=0; + if(json_find_number(code, "on", &itmp) == 0) + state=1; + + if(id == -1 || (unit == -1 && all == 0) || (state == -1 && all == 1)) { + logprintf(LOG_ERR, "beamish_switch: insufficient number of arguments"); + return EXIT_FAILURE; + } else if(id > 65535 || id < 1) { + logprintf(LOG_ERR, "beamish_switch: invalid id range"); + return EXIT_FAILURE; + } else if((unit > 4 || unit < 1) && all == 0) { + logprintf(LOG_ERR, "beamish_switch: invalid unit range"); + return EXIT_FAILURE; + } else { + if(all == 1 && state == 1) + unit = 5; + if(all == 1 && state == 0) + unit = 6; + + createMessage(id, unit, state, all); + clearCode(); + createId(id); + unit = map[unit]; + createUnit(unit); + createFooter(); + beamish_switch->rawlen = RAW_LENGTH; + } + return EXIT_SUCCESS; +} + +static void printHelp(void) { + printf("\t -t --on\t\t\tsend an on signal\n"); + printf("\t -f --off\t\t\tsend an off signal\n"); + printf("\t -u --unit=unit\t\t\tcontrol a device with this unit code\n"); + printf("\t -i --id=id\t\t\tcontrol a device with this id\n"); + printf("\t -a --all\t\t\tsend command to all devices with this id\n"); +} + +#if !defined(MODULE) && !defined(_WIN32) +__attribute__((weak)) +#endif +void beamishSwitchInit(void) { + + protocol_register(&beamish_switch); + protocol_set_id(beamish_switch, "beamish_switch"); + protocol_device_add(beamish_switch, "beamish_switch", "beamish_switch Switches"); + beamish_switch->devtype = SWITCH; + beamish_switch->hwtype = RF433; + beamish_switch->txrpt = NORMAL_REPEATS; + beamish_switch->minrawlen = RAW_LENGTH; + beamish_switch->maxrawlen = RAW_LENGTH; + beamish_switch->maxgaplen = MAX_PULSE_LENGTH*PULSE_DIV; + beamish_switch->mingaplen = MIN_PULSE_LENGTH*PULSE_DIV; + + options_add(&beamish_switch->options, 't', "on", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&beamish_switch->options, 'f', "off", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&beamish_switch->options, 'u', "unit", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^([1-4])$"); + options_add(&beamish_switch->options, 'i', "id", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$"); + options_add(&beamish_switch->options, 'a', "all", OPTION_NO_VALUE, 0, JSON_NUMBER, NULL, NULL); + + options_add(&beamish_switch->options, 0, "readonly", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + options_add(&beamish_switch->options, 0, "confirm", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + + beamish_switch->parseCode=&parseCode; + beamish_switch->createCode=&createCode; + beamish_switch->printHelp=&printHelp; + beamish_switch->validate=&validate; +} + +#if defined(MODULE) && !defined(_WIN32) +void compatibility(struct module_t *module) { + module->name = "beamish_switch"; + module->version = "1.2"; + module->reqversion = "6.0"; + module->reqcommit = "84"; +} + +void init(void) { + beamishSwitchInit(); +} +#endif + + diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/beamish_switch.h b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/beamish_switch.h new file mode 100755 index 0000000..a8b3628 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/beamish_switch.h @@ -0,0 +1,28 @@ +/* + Copyright (C) 2014 CurlyMo & wo-rasp + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#ifndef _PROTOCOL_BEAMISH_SWITCH_H_ +#define _PROTOCOL_BEAMISH_SWITCH_H_ + +#include "../protocol.h" + +struct protocol_t *beamish_switch; +void beamishSwitchInit(void); + +#endif + diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/clarus.c b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/clarus.c new file mode 100755 index 0000000..6a3467f --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/clarus.c @@ -0,0 +1,266 @@ +/* + Copyright (C) 2013 CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#include +#include +#include +#include + +#include "../../core/pilight.h" +#include "../../core/common.h" +#include "../../core/dso.h" +#include "../../core/log.h" +#include "../protocol.h" +#include "../../core/binary.h" +#include "../../core/gc.h" +#include "clarus.h" + +#define PULSE_MULTIPLIER 3 +#define MIN_PULSE_LENGTH 160 +#define MAX_PULSE_LENGTH 200 +#define AVG_PULSE_LENGTH 180 +#define RAW_LENGTH 50 + +static int validate(void) { + if(clarus_switch->rawlen == RAW_LENGTH) { + if(clarus_switch->raw[clarus_switch->rawlen-1] >= (MIN_PULSE_LENGTH*PULSE_DIV) && + clarus_switch->raw[clarus_switch->rawlen-1] <= (MAX_PULSE_LENGTH*PULSE_DIV)) { + return 0; + } + } + + return -1; +} + +static void createMessage(const char *id, int unit, int state) { + clarus_switch->message = json_mkobject(); + json_append_member(clarus_switch->message, "id", json_mkstring(id)); + json_append_member(clarus_switch->message, "unit", json_mknumber(unit, 0)); + if(state == 2) + json_append_member(clarus_switch->message, "state", json_mkstring("on")); + else + json_append_member(clarus_switch->message, "state", json_mkstring("off")); +} + +static void parseCode(void) { + int x = 0, z = 65, binary[RAW_LENGTH/4]; + char id[3]; + + if(clarus_switch->rawlen>RAW_LENGTH) { + logprintf(LOG_ERR, "clarus_switch: parsecode - invalid parameter passed %d", clarus_switch->rawlen); + return; + } + + /* Convert the one's and zero's into binary */ + for(x=0;xrawlen-2;x+=4) { + if(clarus_switch->raw[x+3] > (int)((double)AVG_PULSE_LENGTH*((double)PULSE_MULTIPLIER/2))) { + binary[x/4]=1; + } else if(clarus_switch->raw[x+0] > (int)((double)AVG_PULSE_LENGTH*((double)PULSE_MULTIPLIER/2))) { + binary[x/4]=2; + } else { + binary[x/4]=0; + } + } + + for(x=9;x>=5;--x) { + if(binary[x] == 2) { + break; + } + z++; + } + + int unit = binToDecRev(binary, 0, 5); + int state = binary[11]; + int y = binToDecRev(binary, 6, 9); + sprintf(&id[0], "%c%d", z, y); + + createMessage(id, unit, state); +} + +static void createLow(int s, int e) { + int i; + + for(i=s;i<=e;i+=4) { + clarus_switch->raw[i]=(AVG_PULSE_LENGTH); + clarus_switch->raw[i+1]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + clarus_switch->raw[i+2]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + clarus_switch->raw[i+3]=(AVG_PULSE_LENGTH); + } +} + +static void createMed(int s, int e) { + int i; + + for(i=s;i<=e;i+=4) { + clarus_switch->raw[i]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + clarus_switch->raw[i+1]=(AVG_PULSE_LENGTH); + clarus_switch->raw[i+2]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + clarus_switch->raw[i+3]=(AVG_PULSE_LENGTH); + } +} + +static void createHigh(int s, int e) { + int i; + + for(i=s;i<=e;i+=4) { + clarus_switch->raw[i]=(AVG_PULSE_LENGTH); + clarus_switch->raw[i+1]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + clarus_switch->raw[i+2]=(AVG_PULSE_LENGTH); + clarus_switch->raw[i+3]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + } +} + +static void clearCode(void) { + createLow(0,47); +} + +static void createUnit(int unit) { + int binary[255]; + int length = 0; + int i=0, x=0; + + length = decToBinRev(unit, binary); + for(i=0;i<=length;i++) { + if(binary[i]==1) { + x=i*4; + createHigh(23-(x+3), 23-x); + } + } +} + +static void createId(const char *id) { + int l = ((int)(id[0]))-65; + int y = atoi(&id[1]); + int binary[255]; + int length = 0; + int i=0, x=0; + + length = decToBinRev(y, binary); + for(i=0;i<=length;i++) { + x=i*4; + if(binary[i]==1) { + createHigh(39-(x+3), 39-x); + } + } + x=(l*4); + createMed(39-(x+3), 39-x); +} + +static void createState(int state) { + if(state == 0) { + createMed(40,43); + createHigh(44,47); + } else { + createHigh(40,43); + createMed(44,47); + } +} + +static void createFooter(void) { + clarus_switch->raw[48]=(AVG_PULSE_LENGTH); + clarus_switch->raw[49]=(PULSE_DIV*AVG_PULSE_LENGTH); +} + +static int createCode(struct JsonNode *code) { + const char *id = NULL; + int unit = -1; + int state = -1; + double itmp; + char *stmp; + + if(json_find_string(code, "id", &stmp) == 0) + id = stmp; + if(json_find_number(code, "off", &itmp) == 0) + state=0; + else if(json_find_number(code, "on", &itmp) == 0) + state=1; + if(json_find_number(code, "unit", &itmp) == 0) + unit = (int)round(itmp); + + if(id == NULL || unit == -1 || state == -1) { + logprintf(LOG_ERR, "clarus_switch: insufficient number of arguments"); + return EXIT_FAILURE; + } else if((int)(id[0]) < 65 || (int)(id[0]) > 70) { + logprintf(LOG_ERR, "clarus_switch: invalid id range"); + return EXIT_FAILURE; + } else if(atoi(&id[1]) < 0 || atoi(&id[1]) > 31) { + logprintf(LOG_ERR, "clarus_switch: invalid id range"); + return EXIT_FAILURE; + } else if(unit > 63 || unit < 0) { + logprintf(LOG_ERR, "clarus_switch: invalid unit range"); + return EXIT_FAILURE; + } else { + createMessage(id, unit, ((state == 2 || state == 1) ? 2 : 0)); + clearCode(); + createUnit(unit); + createId(id); + createState(state); + createFooter(); + clarus_switch->rawlen = RAW_LENGTH; + } + return EXIT_SUCCESS; +} + +static void printHelp(void) { + printf("\t -t --on\t\t\tsend an on signal\n"); + printf("\t -f --off\t\t\tsend an off signal\n"); + printf("\t -u --unit=unit\t\t\tcontrol a device with this unit code\n"); + printf("\t -i --id=id\t\t\tcontrol a device with this id\n"); +} + +#if !defined(MODULE) && !defined(_WIN32) +__attribute__((weak)) +#endif +void clarusSwitchInit(void) { + + protocol_register(&clarus_switch); + protocol_set_id(clarus_switch, "clarus_switch"); + protocol_device_add(clarus_switch, "clarus_switch", "Clarus Switches"); + clarus_switch->devtype = SWITCH; + clarus_switch->hwtype = RF433; + clarus_switch->minrawlen = RAW_LENGTH; + clarus_switch->maxrawlen = RAW_LENGTH; + clarus_switch->maxgaplen = MAX_PULSE_LENGTH*PULSE_DIV; + clarus_switch->mingaplen = MIN_PULSE_LENGTH*PULSE_DIV; + + options_add(&clarus_switch->options, 't', "on", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&clarus_switch->options, 'f', "off", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&clarus_switch->options, 'u', "unit", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^([0-9]|[1-5][0-9]|6[0-3])$"); + options_add(&clarus_switch->options, 'i', "id", OPTION_HAS_VALUE, DEVICES_ID, JSON_STRING, NULL, "^[ABCDEF](3[012]?|[012][0-9]|[0-9]{1})$"); + + options_add(&clarus_switch->options, 0, "readonly", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + options_add(&clarus_switch->options, 0, "confirm", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + + clarus_switch->parseCode=&parseCode; + clarus_switch->createCode=&createCode; + clarus_switch->printHelp=&printHelp; + clarus_switch->validate=&validate; +} + +#if defined(MODULE) && !defined(_WIN32) +void compatibility(struct module_t *module) { + module->name = "clarus_switch"; + module->version = "2.4"; + module->reqversion = "6.0"; + module->reqcommit = "84"; +} + +void init(void) { + clarusSwitchInit(); +} +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/clarus.h b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/clarus.h new file mode 100755 index 0000000..93ed23f --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/clarus.h @@ -0,0 +1,27 @@ +/* + Copyright (C) 2013 CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#ifndef _PROTOCOL_CLARUS_H_ +#define _PROTOCOL_CLARUS_H_ + +#include "../protocol.h" + +struct protocol_t *clarus_switch; +void clarusSwitchInit(void); + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/cleverwatts.c b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/cleverwatts.c new file mode 100755 index 0000000..fbb6668 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/cleverwatts.c @@ -0,0 +1,248 @@ +/* + Copyright (C) 2014 CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#include +#include +#include +#include + +#include "../../core/pilight.h" +#include "../../core/common.h" +#include "../../core/dso.h" +#include "../../core/log.h" +#include "../protocol.h" +#include "../../core/binary.h" +#include "../../core/gc.h" +#include "cleverwatts.h" + +#define PULSE_MULTIPLIER 4 +#define MIN_PULSE_LENGTH 265 +#define MAX_PULSE_LENGTH 274 +#define AVG_PULSE_LENGTH 269 +#define RAW_LENGTH 50 + +static int validate(void) { + if(cleverwatts->rawlen == RAW_LENGTH) { + if(cleverwatts->raw[cleverwatts->rawlen-1] >= (MIN_PULSE_LENGTH*PULSE_DIV) && + cleverwatts->raw[cleverwatts->rawlen-1] <= (MAX_PULSE_LENGTH*PULSE_DIV)) { + return 0; + } + } + + return -1; +} + +static void createMessage(int id, int unit, int state, int all) { + cleverwatts->message = json_mkobject(); + json_append_member(cleverwatts->message, "id", json_mknumber(id, 0)); + if(all == 0) { + json_append_member(cleverwatts->message, "all", json_mknumber(1, 0)); + } else { + json_append_member(cleverwatts->message, "unit", json_mknumber(unit, 0)); + } + if(state == 0) + json_append_member(cleverwatts->message, "state", json_mkstring("on")); + else + json_append_member(cleverwatts->message, "state", json_mkstring("off")); +} + +static void parseCode(void) { + int i = 0, x = 0, binary[RAW_LENGTH/2]; + int id = 0, state = 0, unit = 0, all = 0; + + if(cleverwatts->rawlen>RAW_LENGTH) { + logprintf(LOG_ERR, "cleverwatts: parsecode - invalid parameter passed %d", cleverwatts->rawlen); + return; + } + + for(x=1;xrawlen-1;x+=2) { + if(cleverwatts->raw[x] > (int)((double)AVG_PULSE_LENGTH*((double)PULSE_MULTIPLIER/2))) { + binary[i++] = 1; + } else { + binary[i++] = 0; + } + } + + id = binToDecRev(binary, 0, 19); + state = binary[20]; + unit = binToDecRev(binary, 21, 22); + all = binary[23]; + + createMessage(id, unit, state, all); +} + +static void createLow(int s, int e) { + int i; + + for(i=s;i<=e;i+=2) { + cleverwatts->raw[i]=(AVG_PULSE_LENGTH); + cleverwatts->raw[i+1]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + } +} + +static void createHigh(int s, int e) { + int i; + + for(i=s;i<=e;i+=2) { + cleverwatts->raw[i]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + cleverwatts->raw[i+1]=(AVG_PULSE_LENGTH); + } +} + +static void clearCode(void) { + createHigh(0,47); +} + +static void createId(int id) { + int binary[255]; + int length = 0; + int i=0, x=0; + + length = decToBinRev(id, binary); + for(i=0;i<=length;i++) { + if(binary[i]==1) { + x=i*2; + createLow(39-(x+1), 39-x); + } + } +} + +static void createAll(int all) { + if(all == 0) { + createLow(46, 47); + } +} + +static void createState(int state) { + if(state == 1) { + createLow(40, 41); + } +} + +static void createUnit(int unit) { + int binary[255]; + int length = 0; + int i=0, x=0; + + length = decToBinRev(unit, binary); + for(i=0;i<=length;i++) { + if(binary[i]==1) { + x=i*2; + createLow(45-(x+1), 45-x); + } + } +} + +static void createFooter(void) { + cleverwatts->raw[48]=(AVG_PULSE_LENGTH); + cleverwatts->raw[49]=(PULSE_DIV*AVG_PULSE_LENGTH); +} + +static int createCode(struct JsonNode *code) { + int id = -1; + int unit = -1; + int state = -1; + int all = 0; + double itmp = -1; + + if(json_find_number(code, "id", &itmp) == 0) + id = (int)round(itmp); + if(json_find_number(code, "unit", &itmp) == 0) + unit = (int)round(itmp); + if(json_find_number(code, "all", &itmp) == 0) + all = (int)round(itmp); + if(json_find_number(code, "off", &itmp) == 0) + state=1; + else if(json_find_number(code, "on", &itmp) == 0) + state=0; + + if(id == -1 || (unit == -1 && all == 0) || state == -1) { + logprintf(LOG_ERR, "cleverwatts: insufficient number of arguments"); + return EXIT_FAILURE; + } else if(id > 1048575 || id < 1) { + logprintf(LOG_ERR, "cleverwatts: invalid id range"); + return EXIT_FAILURE; + } else if((unit > 3 || unit < 0) && all == 0) { + logprintf(LOG_ERR, "cleverwatts: invalid unit range"); + return EXIT_FAILURE; + } else { + if(unit == -1 && all == 1) { + unit = 3; + } + createMessage(id, unit, state, all ^ 1); + clearCode(); + createId(id); + createState(state); + createUnit(unit); + createAll(all); + createFooter(); + cleverwatts->rawlen = RAW_LENGTH; + } + return EXIT_SUCCESS; +} + +static void printHelp(void) { + printf("\t -t --on\t\t\tsend an on signal\n"); + printf("\t -f --off\t\t\tsend an off signal\n"); + printf("\t -u --unit=unit\t\t\tcontrol a device with this unit code\n"); + printf("\t -i --id=id\t\t\tcontrol a device with this id\n"); + printf("\t -a --all\t\t\tsend command to all devices with this id\n"); +} + +#if !defined(MODULE) && !defined(_WIN32) +__attribute__((weak)) +#endif +void cleverwattsInit(void) { + + protocol_register(&cleverwatts); + protocol_set_id(cleverwatts, "cleverwatts"); + protocol_device_add(cleverwatts, "cleverwatts", "Cleverwatts Switches"); + cleverwatts->devtype = SWITCH; + cleverwatts->hwtype = RF433; + cleverwatts->minrawlen = RAW_LENGTH; + cleverwatts->maxrawlen = RAW_LENGTH; + cleverwatts->maxgaplen = MAX_PULSE_LENGTH*PULSE_DIV; + cleverwatts->mingaplen = MIN_PULSE_LENGTH*PULSE_DIV; + + options_add(&cleverwatts->options, 't', "on", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&cleverwatts->options, 'f', "off", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&cleverwatts->options, 'u', "unit", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^([0-3])$"); + options_add(&cleverwatts->options, 'i', "id", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^([0-9]{1,6}|10([0-3][0-9]{4}|4([0-7][0-9]{3}|8([0-4][0-9]{2}|5([0-6][0-9]|7[0-5])))))$"); + options_add(&cleverwatts->options, 'a', "all", OPTION_OPT_VALUE, DEVICES_OPTIONAL, JSON_NUMBER, NULL, NULL); + + options_add(&cleverwatts->options, 0, "readonly", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + options_add(&cleverwatts->options, 0, "confirm", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + + cleverwatts->parseCode=&parseCode; + cleverwatts->createCode=&createCode; + cleverwatts->printHelp=&printHelp; + cleverwatts->validate=&validate; +} + +#if defined(MODULE) && !defined(_WIN32) +void compatibility(struct module_t *module) { + module->name = "cleverwatts"; + module->version = "1.1"; + module->reqversion = "6.0"; + module->reqcommit = "84"; +} + +void init(void) { + cleverwattsInit(); +} +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/cleverwatts.h b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/cleverwatts.h new file mode 100755 index 0000000..f2ddcc8 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/cleverwatts.h @@ -0,0 +1,27 @@ +/* + Copyright (C) 2014 CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#ifndef _PROTOCOL_CLEVERWATTS_H_ +#define _PROTOCOL_CLEVERWATTS_H_ + +#include "../protocol.h" + +struct protocol_t *cleverwatts; +void cleverwattsInit(void); + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/conrad_rsl_contact.c b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/conrad_rsl_contact.c new file mode 100755 index 0000000..3397da4 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/conrad_rsl_contact.c @@ -0,0 +1,120 @@ +/* + Copyright (C) 2013 CurlyMo & Bram1337 + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#include +#include +#include + +#include "../../core/pilight.h" +#include "../../core/common.h" +#include "../../core/dso.h" +#include "../../core/log.h" +#include "../protocol.h" +#include "../../core/binary.h" +#include "../../core/gc.h" +#include "conrad_rsl_contact.h" + +#define PULSE_MULTIPLIER 5 +#define MIN_PULSE_LENGTH 185 +#define MAX_PULSE_LENGTH 195 +#define AVG_PULSE_LENGTH 190 +#define RAW_LENGTH 66 + +static int validate(void) { + if(conrad_rsl_contact->rawlen == RAW_LENGTH) { + if(conrad_rsl_contact->raw[conrad_rsl_contact->rawlen-1] >= (MIN_PULSE_LENGTH*PULSE_DIV) && + conrad_rsl_contact->raw[conrad_rsl_contact->rawlen-1] <= (MAX_PULSE_LENGTH*PULSE_DIV)) { + return 0; + } + } + + return -1; +} + +static void createMessage(int id, int state) { + conrad_rsl_contact->message = json_mkobject(); + json_append_member(conrad_rsl_contact->message, "id", json_mknumber(id, 0)); + if(state == 1) { + json_append_member(conrad_rsl_contact->message, "state", json_mkstring("opened")); + } else { + json_append_member(conrad_rsl_contact->message, "state", json_mkstring("closed")); + } +} + +static void parseCode(void) { + int x = 0, binary[RAW_LENGTH/2]; + + if(conrad_rsl_contact->rawlen>RAW_LENGTH) { + logprintf(LOG_ERR, "conrad_rsl_contact: parsecode - invalid parameter passed %d", conrad_rsl_contact->rawlen); + return; + } + + /* Convert the one's and zero's into binary */ + for(x=0; xrawlen; x+=2) { + if(conrad_rsl_contact->raw[x+1] > (int)((double)AVG_PULSE_LENGTH*((double)PULSE_MULTIPLIER/2))) { + binary[x/2]=1; + } else { + binary[x/2]=0; + } + } + + int id = binToDecRev(binary, 6, 31); + int check = binToDecRev(binary, 0, 3); + int check1 = binary[32]; + int state = binary[4]; + + if(check == 5 && check1 == 1) { + createMessage(id, state); + } +} + +#if !defined(MODULE) && !defined(_WIN32) +__attribute__((weak)) +#endif +void conradRSLContactInit(void) { + + protocol_register(&conrad_rsl_contact); + protocol_set_id(conrad_rsl_contact, "conrad_rsl_contact"); + protocol_device_add(conrad_rsl_contact, "conrad_rsl_contact", "Conrad RSL Contact Sensor"); + conrad_rsl_contact->devtype = CONTACT; + conrad_rsl_contact->hwtype = RF433; + conrad_rsl_contact->minrawlen = RAW_LENGTH; + conrad_rsl_contact->maxrawlen = RAW_LENGTH; + conrad_rsl_contact->maxgaplen = MAX_PULSE_LENGTH*PULSE_DIV; + conrad_rsl_contact->mingaplen = MIN_PULSE_LENGTH*PULSE_DIV; + + options_add(&conrad_rsl_contact->options, 'i', "id", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^(([0-9]|([1-9][0-9])|([1-9][0-9]{2})|([1-9][0-9]{3})|([1-9][0-9]{4})|([1-9][0-9]{5})|([1-9][0-9]{6})|((6710886[0-3])|(671088[0-5][0-9])|(67108[0-7][0-9]{2})|(6710[0-7][0-9]{3})|(671[0--1][0-9]{4})|(670[0-9]{5})|(6[0-6][0-9]{6})|(0[0-5][0-9]{7}))))$"); + options_add(&conrad_rsl_contact->options, 't', "opened", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&conrad_rsl_contact->options, 'f', "closed", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + + conrad_rsl_contact->parseCode=&parseCode; + conrad_rsl_contact->validate=&validate; +} + +#if defined(MODULE) && !defined(_WIN32) +void compatibility(struct module_t *module) { + module->name = "conrad_rsl_contact"; + module->version = "2.3"; + module->reqversion = "6.0"; + module->reqcommit = "84"; +} + +void init(void) { + conradRSLContactInit(); +} +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/conrad_rsl_contact.h b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/conrad_rsl_contact.h new file mode 100755 index 0000000..5835bb0 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/conrad_rsl_contact.h @@ -0,0 +1,27 @@ +/* + Copyright (C) 2013 CurlyMo & Bram1337 + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#ifndef _PROTOCOL_CONRAD_RSL_CN_H_ +#define _PROTOCOL_CONRAD_RSL_CN_H_ + +#include "../protocol.h" + +struct protocol_t *conrad_rsl_contact; +void conradRSLContactInit(void); + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/conrad_rsl_switch.c b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/conrad_rsl_switch.c new file mode 100755 index 0000000..67206c6 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/conrad_rsl_switch.c @@ -0,0 +1,316 @@ +/* + Copyright (C) 2013 CurlyMo & Bram1337 + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#include +#include +#include +#include + +#include "../../core/pilight.h" +#include "../../core/common.h" +#include "../../core/dso.h" +#include "../../core/log.h" +#include "../protocol.h" +#include "../../core/binary.h" +#include "../../core/gc.h" +#include "conrad_rsl_switch.h" + +#define LEARN_REPEATS 40 +#define NORMAL_REPEATS 10 +#define PULSE_MULTIPLIER 6 +#define MIN_PULSE_LENGTH 190 +#define MAX_PULSE_LENGTH 210 +#define AVG_PULSE_LENGTH 200 +#define RAW_LENGTH 66 + +static int codes[5][4][2]; + +static int validate(void) { + if(conrad_rsl_switch->rawlen == RAW_LENGTH) { + if(conrad_rsl_switch->raw[conrad_rsl_switch->rawlen-1] >= (MIN_PULSE_LENGTH*PULSE_DIV) && + conrad_rsl_switch->raw[conrad_rsl_switch->rawlen-1] <= (MAX_PULSE_LENGTH*PULSE_DIV)) { + return 0; + } + } + + return -1; +} + +static void createMessage(int id, int unit, int state, int learn) { + conrad_rsl_switch->message = json_mkobject(); + + if(id == 4) { + json_append_member(conrad_rsl_switch->message, "all", json_mknumber(1, 0)); + } else { + json_append_member(conrad_rsl_switch->message, "id", json_mknumber(id+1, 0)); + } + json_append_member(conrad_rsl_switch->message, "unit", json_mknumber(unit+1, 0)); + if(state == 1) { + json_append_member(conrad_rsl_switch->message, "state", json_mkstring("on")); + } else { + json_append_member(conrad_rsl_switch->message, "state", json_mkstring("off")); + } + if(learn == 1) { + conrad_rsl_switch->txrpt = LEARN_REPEATS; + } else { + conrad_rsl_switch->txrpt = NORMAL_REPEATS; + } +} + +static void parseCode(void) { + int x = 0, binary[RAW_LENGTH/2]; + int id = 0, unit = 0, state = 0; + + if(conrad_rsl_switch->rawlen>RAW_LENGTH) { + logprintf(LOG_ERR, "conrad_rsl_switch: parsecode - invalid parameter passed %d", conrad_rsl_switch->rawlen); + return; + } + + /* Convert the one's and zero's into binary */ + for(x=0;xrawlen;x+=2) { + if(conrad_rsl_switch->raw[x+1] > (int)((double)AVG_PULSE_LENGTH*((double)PULSE_MULTIPLIER/2))) { + binary[x/2]=0; + } else { + binary[x/2]=1; + } + } + + int check = binToDecRev(binary, 0, 7); + int match = 0; + for(id=0;id<5;id++) { + for(unit=0;unit<4;unit++) { + if(codes[id][unit][0] == check) { + state = 0; + match = 1; + } + if(codes[id][unit][1] == check) { + state = 1; + match = 1; + } + if(match) { + break; + } + } + if(match) { + break; + } + } + createMessage(id, unit, state, 0); +} + +static void createLow(int s, int e) { + int i; + + for(i=s;i<=e;i+=2) { + conrad_rsl_switch->raw[i]=((PULSE_MULTIPLIER+1)*AVG_PULSE_LENGTH); + conrad_rsl_switch->raw[i+1]=AVG_PULSE_LENGTH*3; + } +} + +static void createHigh(int s, int e) { + int i; + + for(i=s;i<=e;i+=2) { + conrad_rsl_switch->raw[i]=AVG_PULSE_LENGTH*3; + conrad_rsl_switch->raw[i+1]=((PULSE_MULTIPLIER+1)*AVG_PULSE_LENGTH); + } +} + +static void clearCode(void) { + int i = 0, x = 0; + createHigh(0,65); + // for(i=0;i<65;i+=2) { + // x=i*2; + // createHigh(x,x+1); + // } + + int binary[255]; + int length = 0; + + length = decToBinRev(23876, binary); + for(i=0;i<=length;i++) { + x=i*2; + if(binary[i]==1) { + createLow(x+16, x+16+1); + } else { + createHigh(x+16, x+16+1); + } + } +} + +static void createId(int id, int unit, int state) { + int binary[255]; + int length = 0; + int i=0, x=0; + + int code = codes[id][unit][state]; + + length = decToBin(code, binary); + for(i=0;i<=length;i++) { + x=i*2; + if(binary[i]==1) { + createLow(x, x+1); + } else { + createHigh(x, x+1); + } + } +} + +static void createFooter(void) { + conrad_rsl_switch->raw[64]=(AVG_PULSE_LENGTH*3); + conrad_rsl_switch->raw[65]=(PULSE_DIV*AVG_PULSE_LENGTH); +} + +static int createCode(struct JsonNode *code) { + int id = -1; + int state = -1; + int unit = -1; + int all = 0; + int learn = -1; + double itmp = 0; + + if(json_find_number(code, "id", &itmp) == 0) + id = (int)round(itmp); + if(json_find_number(code, "unit", &itmp) == 0) + unit = (int)round(itmp); + if(json_find_number(code, "all", &itmp) == 0) + all = 1; + if(json_find_number(code, "off", &itmp) == 0) + state=0; + else if(json_find_number(code, "on", &itmp) == 0) + state=1; + if(json_find_number(code, "learn", &itmp) == 0) + learn = 1; + + if(unit == -1 || (id == -1 && all == 0) || state == -1) { + logprintf(LOG_ERR, "conrad_rsl_switch: insufficient number of arguments"); + return EXIT_FAILURE; + } else if((id > 5 || id < 0) && all == 0) { + logprintf(LOG_ERR, "conrad_rsl_switch: invalid id range"); + return EXIT_FAILURE; + } else if(unit > 5 || unit < 0) { + logprintf(LOG_ERR, "conrad_rsl_switch: invalid unit range"); + return EXIT_FAILURE; + } else { + if(all == 1) { + id = 5; + } + id -= 1; + unit -= 1; + createMessage(id, unit, state, learn); + clearCode(); + createId(id, unit, state); + createFooter(); + conrad_rsl_switch->rawlen = RAW_LENGTH; + } + return EXIT_SUCCESS; +} + +static void printHelp(void) { + printf("\t -i --id=id\tcontrol a device with this id\n"); + printf("\t -i --unit=unit\tcontrol a device with this unit\n"); + printf("\t -t --on\t\t\tsend an on signal\n"); + printf("\t -f --off\t\t\tsend an off signal\n"); + printf("\t -a --all\t\t\tsend an all signal\n"); + printf("\t -l --learn\t\t\tsend multiple streams so switch can learn\n"); +} + +#if !defined(MODULE) && !defined(_WIN32) +__attribute__((weak)) +#endif +void conradRSLSwitchInit(void) { + + memset(codes, 0, 33); + codes[0][0][0] = 190; + codes[0][0][1] = 182; + codes[0][1][0] = 129; + codes[0][1][1] = 142; + codes[0][2][0] = 174; + codes[0][2][1] = 166; + codes[0][3][0] = 158; + codes[0][3][1] = 150; + + codes[1][0][0] = 181; + codes[1][0][1] = 185; + codes[1][1][0] = 141; + codes[1][1][1] = 133; + codes[1][2][0] = 165; + codes[1][2][1] = 169; + codes[1][3][0] = 149; + codes[1][3][1] = 153; + + codes[2][0][0] = 184; + codes[2][0][1] = 176; + codes[2][1][0] = 132; + codes[2][1][1] = 136; + codes[2][2][0] = 168; + codes[2][2][1] = 160; + codes[2][3][0] = 152; + codes[2][3][1] = 144; + + codes[3][0][0] = 178; + codes[3][0][1] = 188; + codes[3][1][0] = 138; + codes[3][1][1] = 130; + codes[3][2][0] = 162; + codes[3][2][1] = 172; + codes[3][3][0] = 146; + codes[3][3][1] = 156; + + codes[4][0][0] = 163; + codes[4][0][1] = 147; + + protocol_register(&conrad_rsl_switch); + protocol_set_id(conrad_rsl_switch, "conrad_rsl_switch"); + protocol_device_add(conrad_rsl_switch, "conrad_rsl_switch", "Conrad RSL Switches"); + conrad_rsl_switch->devtype = SWITCH; + conrad_rsl_switch->hwtype = RF433; + conrad_rsl_switch->txrpt = NORMAL_REPEATS; + conrad_rsl_switch->minrawlen = RAW_LENGTH; + conrad_rsl_switch->maxrawlen = RAW_LENGTH; + conrad_rsl_switch->maxgaplen = MAX_PULSE_LENGTH*PULSE_DIV; + conrad_rsl_switch->mingaplen = MIN_PULSE_LENGTH*PULSE_DIV; + + options_add(&conrad_rsl_switch->options, 'i', "id", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^[1-4]$"); + options_add(&conrad_rsl_switch->options, 'u', "unit", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^[1-4]$"); + options_add(&conrad_rsl_switch->options, 't', "on", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&conrad_rsl_switch->options, 'f', "off", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&conrad_rsl_switch->options, 'a', "all", OPTION_OPT_VALUE, DEVICES_OPTIONAL, JSON_NUMBER, NULL, NULL); + options_add(&conrad_rsl_switch->options, 'l', "learn", OPTION_NO_VALUE, DEVICES_OPTIONAL, JSON_NUMBER, NULL, NULL); + + options_add(&conrad_rsl_switch->options, 0, "readonly", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + options_add(&conrad_rsl_switch->options, 0, "confirm", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + + conrad_rsl_switch->parseCode=&parseCode; + conrad_rsl_switch->createCode=&createCode; + conrad_rsl_switch->printHelp=&printHelp; + conrad_rsl_switch->validate=&validate; +} + +#if defined(MODULE) && !defined(_WIN32) +void compatibility(struct module_t *module) { + module->name = "conrad_rsl_switch"; + module->version = "2.3"; + module->reqversion = "6.0"; + module->reqcommit = "84"; +} + +void init(void) { + conradRSLSwitchInit(); +} +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/conrad_rsl_switch.h b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/conrad_rsl_switch.h new file mode 100755 index 0000000..21e2748 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/conrad_rsl_switch.h @@ -0,0 +1,27 @@ +/* + Copyright (C) 2013 CurlyMo & Bram1337 + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#ifndef _PROTOCOL_CONRAD_RSL_SW_H_ +#define _PROTOCOL_CONRAD_RSL_SW_H_ + +#include "../protocol.h" + +struct protocol_t *conrad_rsl_switch; +void conradRSLSwitchInit(void); + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/daycom.c b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/daycom.c new file mode 100755 index 0000000..7f846c3 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/daycom.c @@ -0,0 +1,251 @@ +/* + Copyright (C) 2015 CurlyMo & Rafael + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#include +#include +#include +#include + +#include "../../core/pilight.h" +#include "../../core/common.h" +#include "../../core/dso.h" +#include "../../core/log.h" +#include "../../core/binary.h" +#include "../../core/gc.h" +#include "../protocol.h" +#include "daycom.h" + +#define PULSE_MULTIPLIER 4 +#define MIN_PULSE_LENGTH 280 +#define MAX_PULSE_LENGTH 296 +#define AVG_PULSE_LENGTH 284 +#define RAW_LENGTH 50 + +static int validate(void) { + if(daycom->rawlen == RAW_LENGTH) { + if(daycom->raw[daycom->rawlen-1] >= (MIN_PULSE_LENGTH*PULSE_DIV) && + daycom->raw[daycom->rawlen-1] <= (MAX_PULSE_LENGTH*PULSE_DIV)) { + return 0; + } + } + + return -1; +} + +static void createMessage(int id, int systemcode, int unit, int state) { + daycom->message = json_mkobject(); + json_append_member(daycom->message, "id", json_mknumber(id, 0)); + json_append_member(daycom->message, "systemcode", json_mknumber(systemcode, 0)); + json_append_member(daycom->message, "unit", json_mknumber(unit, 0)); + if(state == 0) { + json_append_member(daycom->message, "state", json_mkstring("on")); + } else { + json_append_member(daycom->message, "state", json_mkstring("off")); + } +} + +static void parseCode(void) { + int binary[RAW_LENGTH/2], x = 0, i = 0; + int id = -1, state = -1, unit = -1, systemcode = -1; + + if(daycom->rawlen>RAW_LENGTH) { + logprintf(LOG_ERR, "daycom: parsecode - invalid parameter passed %d", daycom->rawlen); + return; + } + + for(x=0;xrawlen;x+=2) { + if(daycom->raw[x] > AVG_PULSE_LENGTH*(PULSE_MULTIPLIER/2)) { + binary[i++] = 1; + } else { + binary[i++] = 0; + } + } + + id = binToDecRev(binary, 0, 5); + systemcode = binToDecRev(binary, 6, 19); + unit = binToDecRev(binary, 21, 23 ); + state = binary[20]; + createMessage(id, systemcode, unit, state); +} + +static void createLow(int s, int e) { + int i; + for(i=s;i<=e;i+=2) { + daycom->raw[i]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + daycom->raw[i+1]=(AVG_PULSE_LENGTH); + } +} +static void createHigh(int s, int e) { + int i; + for(i=s;i<=e;i+=2) { + daycom->raw[i]=(AVG_PULSE_LENGTH); + daycom->raw[i+1]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + } +} + +static void clearCode(void) { + createHigh(0,47); +} + +static void createId(int id) { + int binary[255]; + int length = 0; + int i=0, x=0; + + length = decToBinRev(id, binary); + for(i=0;i<=length;i++) { + if(binary[i]==1) { + x=i*2; + createLow(11-(x+1), 11-x); + } + } +} + + +static void createSystemCode(int systemcode) { + int binary[255]; + int length = 0; + int i=0, x=0; + + length = decToBinRev(systemcode, binary); + for(i=0;i<=length;i++) { + if(binary[i]==1) { + x=i*2; + createLow(39-(x+1), 39-x); + } + } +} + +static void createUnit(int unit) { + int binary[255]; + int length = 0; + int i=0, x=0; + + length = decToBinRev(unit, binary); + for(i=0;i<=length;i++) { + if(binary[i]==1) { + x=i*2; + createLow(47-(x+1), 47-x); + } + } +} + +static void createState(int state) { + if(state == 0) { + createLow(40, 41); + } +} + + +static void createFooter(void) { + daycom->raw[48]=(AVG_PULSE_LENGTH); + daycom->raw[49]=(PULSE_DIV*AVG_PULSE_LENGTH); +} + +static int createCode(JsonNode *code) { + int id = -1; + int systemcode = -1; + int unit = -1; + int state = -1; + double itmp = -1; + + if(json_find_number(code, "id", &itmp) == 0) + id = (int)round(itmp); + if(json_find_number(code, "systemcode", &itmp) == 0) + systemcode = (int)round(itmp); + if(json_find_number(code, "unit", &itmp) == 0) + unit = (int)round(itmp); + if(json_find_number(code, "off", &itmp) == 0) + state=1; + if(json_find_number(code, "on", &itmp) == 0) + state=0; + + if(id == -1 || systemcode == -1 || unit == -1 || state == -1) { + logprintf(LOG_ERR, "daycom: insufficient number of arguments"); + return EXIT_FAILURE; + } else if(id > 63 || id < 0) { + logprintf(LOG_ERR, "daycom: invalid id range"); + return EXIT_FAILURE; + } else if(systemcode > 16999 || systemcode < 0) { + logprintf(LOG_ERR, "daycom: invalid systemcode range"); + return EXIT_FAILURE; + } else if(unit > 7 || unit < 0) { + logprintf(LOG_ERR, "daycom: invalid unit range"); + return EXIT_FAILURE; + } else { + createMessage(id, systemcode, unit, state); + clearCode(); + createId(id); + createSystemCode(systemcode); + createState(state); + createUnit(unit); + createFooter(); + daycom->rawlen = RAW_LENGTH; + state = 0; + } + return EXIT_SUCCESS; +} + +static void printHelp(void) { + printf("\t -i --id=id\t\t\tcontrol a device with this id\n"); + printf("\t -s --systemcode=systemcode\t\t\tcontrol a device with this systemcode\n"); + printf("\t -u --unitcode=unitcode\t\t\tcontrol a device with this unitcode\n"); + printf("\t -t --on\t\t\tsend an on signal\n"); + printf("\t -f --off\t\t\tsend an off signal\n"); +} + +#if !defined(MODULE) && !defined(_WIN32) +__attribute__((weak)) +#endif + +void daycomInit(void) { + protocol_register(&daycom); + protocol_set_id(daycom, "daycom"); + protocol_device_add(daycom, "daycom", "Daycom switches"); + daycom->devtype = SWITCH; + daycom->hwtype = RF433; + daycom->minrawlen = RAW_LENGTH; + daycom->maxrawlen = RAW_LENGTH; + daycom->maxgaplen = MAX_PULSE_LENGTH*PULSE_DIV; + daycom->mingaplen = MIN_PULSE_LENGTH*PULSE_DIV; + + options_add(&daycom->options, 't', "on", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&daycom->options, 'f', "off", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&daycom->options, 'u', "unit", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^([0-7])$"); + options_add(&daycom->options, 's', "systemcode", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^([0-9]{1,4}|1[0-6][0-9]{3})$"); + options_add(&daycom->options, 'i', "id", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^[0-6]?[0-9]$"); + + options_add(&daycom->options, 0, "readonly", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + + daycom->parseCode=&parseCode; + daycom->createCode=&createCode; + daycom->printHelp=&printHelp; + daycom->validate=&validate; +} +#if defined(MODULE) && !defined(_WIN32) +void compatibility(struct module_t *module) { + module->name = "daycom"; + module->version = "1.1"; + module->reqversion = "6.0"; + module->reqcommit = "187"; +} + +void init(void) { + daycomInit(); +} +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/daycom.h b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/daycom.h new file mode 100755 index 0000000..8005a16 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/daycom.h @@ -0,0 +1,27 @@ +/* + Copyright (C) 2015 CurlyMo & Rafael + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#ifndef _PROTOCOL_DAYCOM_H_ +#define _PROTOCOL_DAYCOM_H_ + +#include "../protocol.h" + +struct protocol_t *daycom; +void daycomInit(void); + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/ehome.c b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/ehome.c new file mode 100755 index 0000000..71564c8 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/ehome.c @@ -0,0 +1,220 @@ +/* + Copyright (C) 2014 CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#include +#include +#include +#include + +#include "../../core/pilight.h" +#include "../../core/common.h" +#include "../../core/dso.h" +#include "../../core/log.h" +#include "../protocol.h" +#include "../../core/binary.h" +#include "../../core/gc.h" +#include "ehome.h" + +#define PULSE_MULTIPLIER 3 +#define MIN_PULSE_LENGTH 277 +#define MAX_PULSE_LENGTH 287 +#define AVG_PULSE_LENGTH 282 +#define RAW_LENGTH 50 + +static int validate(void) { + if(ehome->rawlen == RAW_LENGTH) { + if(ehome->raw[ehome->rawlen-1] >= (MIN_PULSE_LENGTH*PULSE_DIV) && + ehome->raw[ehome->rawlen-1] <= (MAX_PULSE_LENGTH*PULSE_DIV)) { + return 0; + } + } + + return -1; +} + +static void createMessage(int id, int state) { + ehome->message = json_mkobject(); + json_append_member(ehome->message, "id", json_mknumber(id, 0)); + if(state == 1) { + json_append_member(ehome->message, "state", json_mkstring("on")); + } else { + json_append_member(ehome->message, "state", json_mkstring("off")); + } +} + +static void parseCode(void) { + int i = 0, binary[RAW_LENGTH/4]; + + if(ehome->rawlen>RAW_LENGTH) { + logprintf(LOG_ERR, "ehome: parsecode - invalid parameter passed %d", ehome->rawlen); + return; + } + + for(i=0;irawlen-2;i+=4) { + if(ehome->raw[i+3] > (int)((double)AVG_PULSE_LENGTH*((double)PULSE_MULTIPLIER/2))) { + binary[i/4]=1; + } else { + binary[i/4]=0; + } + } + + int id = binToDec(binary, 1, 3); + int state = binary[0]; + + createMessage(id, state); +} + +static void createLow(int s, int e) { + int i; + + for(i=s;i<=e;i+=4) { + ehome->raw[i]=AVG_PULSE_LENGTH; + ehome->raw[i+1]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + ehome->raw[i+2]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + ehome->raw[i+3]=AVG_PULSE_LENGTH; + } +} + +static void createMed(int s, int e) { + int i; + + for(i=s;i<=e;i+=4) { + ehome->raw[i]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + ehome->raw[i+1]=AVG_PULSE_LENGTH; + ehome->raw[i+2]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + ehome->raw[i+3]=AVG_PULSE_LENGTH; + } +} + +static void createHigh(int s, int e) { + int i; + + for(i=s;i<=e;i+=4) { + ehome->raw[i]=AVG_PULSE_LENGTH; + ehome->raw[i+1]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + ehome->raw[i+2]=AVG_PULSE_LENGTH; + ehome->raw[i+3]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + } +} + +static void clearCode(void) { + createLow(0,47); +} + +static void createId(int id) { + int binary[255]; + int length = 0; + int i=0, x=0; + + length = decToBinRev(id, binary); + for(i=0;i<=length;i++) { + if(binary[i]==1) { + x=i*4; + createHigh(4+x, 4+(x+3)); + } + } +} + +static void createState(int state) { + if(state == 0) { + createMed(0, 3); + } else { + createHigh(0, 3); + } +} + +static void createFooter(void) { + ehome->raw[48]=(AVG_PULSE_LENGTH); + ehome->raw[49]=(PULSE_DIV*AVG_PULSE_LENGTH); +} + +static int createCode(struct JsonNode *code) { + int id = -1; + int state = -1; + double itmp = 0; + + if(json_find_number(code, "id", &itmp) == 0) + id = (int)round(itmp); + if(json_find_number(code, "off", &itmp) == 0) + state=0; + else if(json_find_number(code, "on", &itmp) == 0) + state=1; + + if(id == -1 || state == -1) { + logprintf(LOG_ERR, "ehome: insufficient number of arguments"); + return EXIT_FAILURE; + } else if(id > 7 || id < 0) { + logprintf(LOG_ERR, "ehome: invalid id range"); + return EXIT_FAILURE; + } else { + createMessage(id, state); + clearCode(); + createId(id); + createState(state); + createFooter(); + ehome->rawlen = RAW_LENGTH; + } + return EXIT_SUCCESS; +} + +static void printHelp(void) { + printf("\t -i --id=id\t\t\tcontrol a device with this id\n"); + printf("\t -t --on\t\t\tsend an on signal\n"); + printf("\t -f --off\t\t\tsend an off signal\n"); +} + +#if !defined(MODULE) && !defined(_WIN32) +__attribute__((weak)) +#endif +void ehomeInit(void) { + + protocol_register(&ehome); + protocol_set_id(ehome, "ehome"); + protocol_device_add(ehome, "ehome", "eHome Switches"); + ehome->devtype = SWITCH; + ehome->hwtype = RF433; + ehome->minrawlen = RAW_LENGTH; + ehome->maxrawlen = RAW_LENGTH; + ehome->maxgaplen = MAX_PULSE_LENGTH*PULSE_DIV; + ehome->mingaplen = MIN_PULSE_LENGTH*PULSE_DIV; + + options_add(&ehome->options, 'i', "id", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^([0-4])$"); + options_add(&ehome->options, 't', "on", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&ehome->options, 'f', "off", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + + options_add(&ehome->options, 0, "readonly", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + options_add(&ehome->options, 0, "confirm", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + + ehome->parseCode=&parseCode; + ehome->createCode=&createCode; + ehome->printHelp=&printHelp; + ehome->validate=&validate; +} + +#if defined(MODULE) && !defined(_WIN32) +void compatibility(struct module_t *module) { + module->name = "ehome"; + module->version = "1.1"; + module->reqversion = "6.0"; + module->reqcommit = "84"; +} + +void init(void) { + ehomeInit(); +} +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/ehome.h b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/ehome.h new file mode 100755 index 0000000..ca50757 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/ehome.h @@ -0,0 +1,27 @@ +/* + Copyright (C) 2014 CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#ifndef _PROTOCOL_EHOME_H_ +#define _PROTOCOL_EHOME_H_ + +#include "../protocol.h" + +struct protocol_t *ehome; +void ehomeInit(void); + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/elro_300_switch.c b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/elro_300_switch.c new file mode 100755 index 0000000..0de1d53 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/elro_300_switch.c @@ -0,0 +1,367 @@ +/* + Copyright (C) 2014 CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#include +#include +#include +#include + +#include "../../core/pilight.h" +#include "../../core/common.h" +#include "../../core/dso.h" +#include "../../core/log.h" +#include "../protocol.h" +#include "../../core/binary.h" +#include "../../core/gc.h" +#include "elro_300_switch.h" + +#define PULSE_MULTIPLIER 4 +#define MIN_PULSE_LENGTH 297 +#define MAX_PULSE_LENGTH 307 +#define AVG_PULSE_LENGTH 302 +#define RAW_LENGTH 116 + +static int validate(void) { + if(elro_300_switch->rawlen == RAW_LENGTH) { + if(elro_300_switch->raw[elro_300_switch->rawlen-1] >= (MIN_PULSE_LENGTH*PULSE_DIV) && + elro_300_switch->raw[elro_300_switch->rawlen-1] <= (MAX_PULSE_LENGTH*PULSE_DIV)) { + return 0; + } + } + + return -1; +} + +/** + * Creates as System message informing the daemon about a received or created message + * + * systemcode : integer number, the 32 bit system code + * unitcode : unit being adressed, integer number + * state : either 2 (off) or 1 (on) + * group : if 1 this affects a whole group of devices + */ +static void createMessage(unsigned long long systemcode, int unitcode, int state, int group) { + elro_300_switch->message = json_mkobject(); + //aka address + json_append_member(elro_300_switch->message, "systemcode", json_mknumber((double)systemcode, 0)); + //toggle all or just one unit + if(group == 1) { + json_append_member(elro_300_switch->message, "all", json_mknumber(group, 0)); + } else { + json_append_member(elro_300_switch->message, "unitcode", json_mknumber(unitcode, 0)); + } + //aka command + if(state == 1) { + json_append_member(elro_300_switch->message, "state", json_mkstring("on")); + } + else if(state == 2) { + json_append_member(elro_300_switch->message, "state", json_mkstring("off")); + } +} + +/** + * This is the main method when reading a received code + * Decodes the received stream + * + */ +static void parseCode(void) { + int i = 0, x = 0, binary[RAW_LENGTH/2]; + + if(elro_300_switch->rawlen>RAW_LENGTH) { + logprintf(LOG_ERR, "elro_300_switch: parsecode - invalid parameter passed %d", elro_300_switch->rawlen); + return; + } + + //utilize the "code" field + //at this point the code field holds translated "0" and "1" codes from the received pulses + //this means that we have to combine these ourselves into meaningful values in groups of 2 + + for(i=0; i < elro_300_switch->rawlen; i++) { + if(elro_300_switch->raw[i] > (int)((double)AVG_PULSE_LENGTH*((double)PULSE_MULTIPLIER/2))) { + if(i&1) { + binary[x++] = 1; + } else { + return; // even pulse lengths must be low + } + } else if(i&1) { + binary[x++] = 0; + } + } + + //chunked code now contains "groups of 2" codes for us to handle. + unsigned long long systemcode = binToDecRevUl(binary, 11, 42); + int groupcode = binToDec(binary, 43, 46); + int groupcode2 = binToDec(binary, 49, 50); + int unitcode = binToDec(binary, 51, 56); + int state = binToDec(binary, 47, 48); + int groupRes = 0; + + if(groupcode == 13 && groupcode2 == 2) { + groupRes = 0; + } else if(groupcode == 3 && groupcode2 == 3) { + groupRes = 1; + } else { + return; + } + if(state < 1 || state > 2) { + return; + } else { + createMessage(systemcode, unitcode, state, groupRes); + } +} + +/** + * Creates a number of "low" entries (302 302). Note that each entry requires 2 raw positions + * so e-s should be a multiple of 2 + * s : start position in the raw code (inclusive) + * e : end position in the raw code (inclusive) + */ +static void createLow(int s, int e) { + int i; + + for(i=s;i<=e;i+=2) { + elro_300_switch->raw[i]=(AVG_PULSE_LENGTH); + elro_300_switch->raw[i+1]=(AVG_PULSE_LENGTH); + } +} + +/** + * Creates a number of "high" entries (302 1028). Note that each entry requires 2 raw positions + * so e-s should be a multiple of 2 + * s : start position in the raw code (inclusive) + * e : end position in the raw code (inclusive) + */ +static void createHigh(int s, int e) { + int i; + + for(i=s;i<=e;i+=2) { + elro_300_switch->raw[i]=(AVG_PULSE_LENGTH); + elro_300_switch->raw[i+1]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + } +} + +/** + * This simply clears the full length of the code to be all "zeroes" (LOW entries) + */ +static void elro300ClearCode(void) { + createLow(0,116); +} + +/** + * Takes the passed number + * converts it into raw and inserts them into the raw code at the appropriate position + * + * systemcode : unsigned integer number, the 32 bit system code + */ +static void createSystemCode(unsigned long long systemcode) { + int binary[255]; + int length = 0; + int i=0, x=0; + length = decToBinRevUl(systemcode, binary); + for(i=0;i<=length;i++) { + if(binary[(length)-i]==1) { + x=i*2; + createHigh(22+x, 22+x+1); + } + } +} + +/** + * Takes the passed number converts it into raw and inserts it into the raw code at the appropriate position + * + * unitcode : integer number, id of the unit to control + */ +static void createUnitCode(int unitcode) { + int binary[255]; + int length = 0; + int i=0, x=0; + + length = decToBinRev(unitcode, binary); + for(i=0;i<=length;i++) { + if(binary[i]==1) { + x=i*2; + createHigh(102+x, 102+x+1); + } + } +} + +/** + * Takes the passed number converts it into raw and inserts it into the raw code at the appropriate position + * + * state : integer number, state value to set. can be either 1 (on) or 2 (off) + */ +static void createState(int state) { + if(state == 1) { + createHigh(94, 95); + createLow(96, 97); + } + else { + createLow(94, 95); + createHigh(96, 97); + } +} + +/** + * sets the first group code portions to the appropriate raw values. + * Fro grouped mode this is the equivalent to 1100 and 11, for non-grouped mode 1011 and 01 + * + * group : integer value, 1 means grouped enabled, 0 means disabled + */ +static void createGroupCode(int group) { + if(group == 1) { + createHigh(86, 89); + createLow(90, 93); + createHigh(98, 101); + } else { + createHigh(86, 87); + createLow(88, 89); + createHigh(90, 93); + createLow(98, 99); + createHigh(100, 101); + } +} + +/** + * Inserts the (as far as is known) fixed message preamble + * First eleven words are the preamble + */ +static void createPreamble(void) { + createHigh(0,3); + createLow(4,9); + createHigh(10,17); + createLow(18,21); +} + +/** + * Inserts the message trailer (one HIGH) into the raw message + */ +static void createFooter(void) { + elro_300_switch->raw[114]=(AVG_PULSE_LENGTH); + elro_300_switch->raw[115]=(PULSE_DIV*AVG_PULSE_LENGTH); +} + + +/** + * Main method for creating a message based on daemon-passed values in the elro_300_switch protocol. + * code : JSON Message containing the received parameters to use for message creation + * + * returns : EXIT_SUCCESS or EXIT_FAILURE on obvious occasions + */ +static int createCode(struct JsonNode *code) { + unsigned long long systemcode = 0; + int unitcode = -1; + int group = 0; + int state = -1; + double itmp; + + if(json_find_number(code, "systemcode", &itmp) == 0) { + systemcode = (unsigned long long)itmp; + } + if(json_find_number(code, "unitcode", &itmp) == 0) { + unitcode = (int)round(itmp); + } + if(json_find_number(code, "all", &itmp) == 0) { + group = 1; + //on the reference remote, group toggles always used a unit code of 56. + //for that reason we are enforcing that here + unitcode = 56; + } + + if(json_find_number(code, "off", &itmp) == 0) { + state=2; + } + else if(json_find_number(code, "on", &itmp) == 0) { + state=1; + } + + if(systemcode == 0 || unitcode == -1 || state == -1) { + logprintf(LOG_ERR, "elro_300_switch: insufficient number of arguments"); + return EXIT_FAILURE; + } else if(systemcode > 4294967295u || unitcode > 99 || unitcode < 0) { + logprintf(LOG_ERR, "elro_300_switch: values out of valid range"); + } else { + createMessage(systemcode, unitcode, state, group); + elro300ClearCode(); + createPreamble(); + createSystemCode(systemcode); + createGroupCode(group); + createState(state); + createUnitCode(unitcode); + createFooter(); + elro_300_switch->rawlen = RAW_LENGTH; + } + return EXIT_SUCCESS; +} + +/** + * Outputs help messages directly to the current output target (probably the console) + */ +static void printHelp(void) { + printf("\t -s --systemcode=systemcode\tcontrol a device with this systemcode\n"); + printf("\t -a --all\t\t\ttoggle switching all devices on or off\n"); + printf("\t -u --unitcode=unitcode\t\tcontrol a device with this unitcode\n"); + printf("\t -t --on\t\t\tsend an on signal\n"); + printf("\t -f --off\t\t\tsend an off signal\n"); +} + +/** + * Main Init method called to init the protocol and register its functions with pilight + */ +#if !defined(MODULE) && !defined(_WIN32) +__attribute__((weak)) +#endif +void elro300SwitchInit(void) { + + protocol_register(&elro_300_switch); + protocol_set_id(elro_300_switch, "elro_300_switch"); + protocol_device_add(elro_300_switch, "elro_300_switch", "Elro 300 Series Switches"); + elro_300_switch->devtype = SWITCH; + elro_300_switch->hwtype = RF433; + elro_300_switch->minrawlen = RAW_LENGTH; + elro_300_switch->maxrawlen = RAW_LENGTH; + elro_300_switch->maxgaplen = MAX_PULSE_LENGTH*PULSE_DIV; + elro_300_switch->mingaplen = MIN_PULSE_LENGTH*PULSE_DIV; + + options_add(&elro_300_switch->options, 's', "systemcode", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^([0-9]{1,9}|[1-3][0-9]{9}|4([01][0-9]{8}|2([0-8][0-9]{7}|9([0-3][0-9]{6}|4([0-8][0-9]{5}|9([0-5][0-9]{4}|6([0-6][0-9]{3}|7([01][0-9]{2}|2([0-8][0-9]|9[0-4])))))))))$"); + options_add(&elro_300_switch->options, 'u', "unitcode", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^[0-9]{1,2}$"); + options_add(&elro_300_switch->options, 't', "on", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&elro_300_switch->options, 'f', "off", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&elro_300_switch->options, 'a', "all", OPTION_OPT_VALUE, DEVICES_OPTIONAL, JSON_NUMBER, NULL, NULL); + + options_add(&elro_300_switch->options, 0, "readonly", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + options_add(&elro_300_switch->options, 0, "confirm", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + + + elro_300_switch->parseCode=&parseCode; + elro_300_switch->createCode=&createCode; + elro_300_switch->printHelp=&printHelp; + elro_300_switch->validate=&validate; +} + +#if defined(MODULE) && !defined(_WIN32) +void compatibility(struct module_t *module) { + module->name = "elro_300_switch"; + module->version = "2.3"; + module->reqversion = "6.0"; + module->reqcommit = "84"; +} + +void init(void) { + elro300SwitchInit(); +} +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/elro_300_switch.h b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/elro_300_switch.h new file mode 100755 index 0000000..464903a --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/elro_300_switch.h @@ -0,0 +1,27 @@ +/* + Copyright (C) 2013 CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#ifndef _PROTOCOL_ELRO_300_SWITCH_H_ +#define _PROTOCOL_ELRO_300_SWITCH_H_ + +#include "../protocol.h" + +struct protocol_t *elro_300_switch; +void elro300SwitchInit(void); + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/elro_400_switch.c b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/elro_400_switch.c new file mode 100755 index 0000000..6fea134 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/elro_400_switch.c @@ -0,0 +1,233 @@ +/* + Copyright (C) 2013 CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#include +#include +#include +#include + +#include "../../core/pilight.h" +#include "../../core/common.h" +#include "../../core/dso.h" +#include "../../core/log.h" +#include "../protocol.h" +#include "../../core/binary.h" +#include "../../core/gc.h" +#include "elro_400_switch.h" + +#define PULSE_MULTIPLIER 3 +#define MIN_PULSE_LENGTH 291 +#define MAX_PULSE_LENGTH 301 +#define AVG_PULSE_LENGTH 296 +#define RAW_LENGTH 50 + +static int validate(void) { + if(elro_400_switch->rawlen == RAW_LENGTH) { + if(elro_400_switch->raw[elro_400_switch->rawlen-1] >= (MIN_PULSE_LENGTH*PULSE_DIV) && + elro_400_switch->raw[elro_400_switch->rawlen-1] <= (MAX_PULSE_LENGTH*PULSE_DIV)) { + return 0; + } + } + + return -1; +} + +static void createMessage(int systemcode, int unitcode, int state) { + elro_400_switch->message = json_mkobject(); + json_append_member(elro_400_switch->message, "systemcode", json_mknumber(systemcode, 0)); + json_append_member(elro_400_switch->message, "unitcode", json_mknumber(unitcode, 0)); + if(state == 1) { + json_append_member(elro_400_switch->message, "state", json_mkstring("on")); + } else { + json_append_member(elro_400_switch->message, "state", json_mkstring("off")); + } +} + +static void parseCode(void) { + int x = 0, i = 0, binary[RAW_LENGTH/4]; + + if(elro_400_switch->rawlen>RAW_LENGTH) { + logprintf(LOG_ERR, "elro_400_switch: parsecode - invalid parameter passed %d", elro_400_switch->rawlen); + return; + } + + for(x=0;xrawlen-2;x+=4) { + if(elro_400_switch->raw[x+3] > (int)((double)AVG_PULSE_LENGTH*((double)PULSE_MULTIPLIER/2))) { + binary[i++] = 0; + } else { + binary[i++] = 1; + } + } + + int systemcode = binToDecRev(binary, 0, 4); + int unitcode = binToDecRev(binary, 5, 9); + int state = binary[11]; + createMessage(systemcode, unitcode, state); +} + +static void createLow(int s, int e) { + int i; + + for(i=s;i<=e;i+=4) { + elro_400_switch->raw[i]=(AVG_PULSE_LENGTH); + elro_400_switch->raw[i+1]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + elro_400_switch->raw[i+2]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + elro_400_switch->raw[i+3]=(AVG_PULSE_LENGTH); + } +} + +static void createHigh(int s, int e) { + int i; + + for(i=s;i<=e;i+=4) { + elro_400_switch->raw[i]=(AVG_PULSE_LENGTH); + elro_400_switch->raw[i+1]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + elro_400_switch->raw[i+2]=(AVG_PULSE_LENGTH); + elro_400_switch->raw[i+3]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + } +} +static void clearCode(void) { + createHigh(0,47); +} + +static void createSystemCode(int systemcode) { + int binary[255]; + int length = 0; + int i=0, x=0; + + length = decToBinRev(systemcode, binary); + for(i=0;i<=length;i++) { + if(binary[i]==1) { + x=i*4; + createLow(19-(x+3), 19-x); + } + } +} + +static void createUnitCode(int unitcode) { + int binary[255]; + int length = 0; + int i=0, x=0; + + length = decToBinRev(unitcode, binary); + for(i=0;i<=length;i++) { + if(binary[i]==1) { + x=i*4; + createLow(39-(x+3), 39-x); + } + } +} + +static void createState(int state) { + if(state == 1) { + createLow(44, 47); + createLow(40, 43); + } else { + createLow(40, 43); + } +} + +static void createFooter(void) { + elro_400_switch->raw[48]=(AVG_PULSE_LENGTH); + elro_400_switch->raw[49]=(PULSE_DIV*AVG_PULSE_LENGTH); +} + +static int createCode(struct JsonNode *code) { + int systemcode = -1; + int unitcode = -1; + int state = -1; + double itmp = 0; + + if(json_find_number(code, "systemcode", &itmp) == 0) + systemcode = (int)round(itmp); + if(json_find_number(code, "unitcode", &itmp) == 0) + unitcode = (int)round(itmp); + if(json_find_number(code, "off", &itmp) == 0) + state=0; + else if(json_find_number(code, "on", &itmp) == 0) + state=1; + + if(systemcode == -1 || unitcode == -1 || state == -1) { + logprintf(LOG_ERR, "elro_400_switch: insufficient number of arguments"); + return EXIT_FAILURE; + } else if(systemcode > 31 || systemcode < 0) { + logprintf(LOG_ERR, "elro_400_switch: invalid systemcode range"); + return EXIT_FAILURE; + } else if(unitcode > 31 || unitcode < 0) { + logprintf(LOG_ERR, "elro_400_switch: invalid unitcode range"); + return EXIT_FAILURE; + } else { + createMessage(systemcode, unitcode, state); + clearCode(); + createSystemCode(systemcode); + createUnitCode(unitcode); + createState(state); + createFooter(); + elro_400_switch->rawlen = RAW_LENGTH; + } + return EXIT_SUCCESS; +} + +static void printHelp(void) { + printf("\t -s --systemcode=systemcode\tcontrol a device with this systemcode\n"); + printf("\t -u --unitcode=unitcode\t\tcontrol a device with this unitcode\n"); + printf("\t -t --on\t\t\tsend an on signal\n"); + printf("\t -f --off\t\t\tsend an off signal\n"); +} + +#if !defined(MODULE) && !defined(_WIN32) +__attribute__((weak)) +#endif +void elro400SwitchInit(void) { + + protocol_register(&elro_400_switch); + protocol_set_id(elro_400_switch, "elro_400_switch"); + protocol_device_add(elro_400_switch, "elro_400_switch", "Elro 400 Series Switches"); + elro_400_switch->devtype = SWITCH; + elro_400_switch->hwtype = RF433; + elro_400_switch->minrawlen = RAW_LENGTH; + elro_400_switch->maxrawlen = RAW_LENGTH; + elro_400_switch->maxgaplen = MAX_PULSE_LENGTH*PULSE_DIV; + elro_400_switch->mingaplen = MIN_PULSE_LENGTH*PULSE_DIV; + + options_add(&elro_400_switch->options, 's', "systemcode", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^(3[012]?|[012][0-9]|[0-9]{1})$"); + options_add(&elro_400_switch->options, 'u', "unitcode", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^(3[012]?|[012][0-9]|[0-9]{1})$"); + options_add(&elro_400_switch->options, 't', "on", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&elro_400_switch->options, 'f', "off", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + + options_add(&elro_400_switch->options, 0, "readonly", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + options_add(&elro_400_switch->options, 0, "confirm", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + + elro_400_switch->parseCode=&parseCode; + elro_400_switch->createCode=&createCode; + elro_400_switch->printHelp=&printHelp; + elro_400_switch->validate=&validate; +} + +#if defined(MODULE) && !defined(_WIN32) +void compatibility(struct module_t *module) { + module->name = "elro_400_switch"; + module->version = "2.4"; + module->reqversion = "6.0"; + module->reqcommit = "84"; +} + +void init(void) { + elro400SwitchInit(); +} +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/elro_400_switch.h b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/elro_400_switch.h new file mode 100755 index 0000000..82679c9 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/elro_400_switch.h @@ -0,0 +1,27 @@ +/* + Copyright (C) 2013 CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#ifndef _PROTOCOL_ELRO_400_SWITCH_H_ +#define _PROTOCOL_ELRO_400_SWITCH_H_ + +#include "../protocol.h" + +struct protocol_t *elro_400_switch; +void elro400SwitchInit(void); + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/elro_800_contact.c b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/elro_800_contact.c new file mode 100755 index 0000000..b4a1dc6 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/elro_800_contact.c @@ -0,0 +1,118 @@ +/* + Copyright (C) 2014 CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#include +#include +#include +#include + +#include "../../core/pilight.h" +#include "../../core/common.h" +#include "../../core/dso.h" +#include "../../core/log.h" +#include "../protocol.h" +#include "../../core/binary.h" +#include "../../core/gc.h" +#include "elro_800_contact.h" + +#define PULSE_MULTIPLIER 3 +#define MIN_PULSE_LENGTH 283 +#define MAX_PULSE_LENGTH 305 +#define AVG_PULSE_LENGTH 300 +#define RAW_LENGTH 50 + +static int validate(void) { + if(elro_800_contact->rawlen == RAW_LENGTH) { + if(elro_800_contact->raw[elro_800_contact->rawlen-1] >= (MIN_PULSE_LENGTH*PULSE_DIV) && + elro_800_contact->raw[elro_800_contact->rawlen-1] <= (MAX_PULSE_LENGTH*PULSE_DIV)) { + return 0; + } + } + + return -1; +} + +static void createMessage(int systemcode, int unitcode, int state) { + elro_800_contact->message = json_mkobject(); + json_append_member(elro_800_contact->message, "systemcode", json_mknumber(systemcode, 0)); + json_append_member(elro_800_contact->message, "unitcode", json_mknumber(unitcode, 0)); + if(state == 0) { + json_append_member(elro_800_contact->message, "state", json_mkstring("opened")); + } else { + json_append_member(elro_800_contact->message, "state", json_mkstring("closed")); + } +} + +static void parseCode(void) { + int binary[RAW_LENGTH/4], x = 0, i = 0; + + if(elro_800_contact->rawlen>RAW_LENGTH) { + logprintf(LOG_ERR, "elro_800_contact: parsecode - invalid parameter passed %d", elro_800_contact->rawlen); + return; + } + + for(x=0;xrawlen-2;x+=4) { + if(elro_800_contact->raw[x+3] > (int)((double)AVG_PULSE_LENGTH*((double)PULSE_MULTIPLIER/2))) { + binary[i++] = 1; + } else { + binary[i++] = 0; + } + } + + int systemcode = binToDec(binary, 0, 4); + int unitcode = binToDec(binary, 5, 9); + int state = binary[11]; + createMessage(systemcode, unitcode, state); +} + +#if !defined(MODULE) && !defined(_WIN32) +__attribute__((weak)) +#endif +void elro800ContactInit(void) { + + protocol_register(&elro_800_contact); + protocol_set_id(elro_800_contact, "elro_800_contact"); + protocol_device_add(elro_800_contact, "elro_800_contact", "Elro Series 800 Contact"); + elro_800_contact->devtype = CONTACT; + elro_800_contact->hwtype = RF433; + elro_800_contact->minrawlen = RAW_LENGTH; + elro_800_contact->maxrawlen = RAW_LENGTH; + elro_800_contact->maxgaplen = MAX_PULSE_LENGTH*PULSE_DIV; + elro_800_contact->mingaplen = MIN_PULSE_LENGTH*PULSE_DIV; + + options_add(&elro_800_contact->options, 's', "systemcode", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^(3[012]?|[012][0-9]|[0-9]{1})$"); + options_add(&elro_800_contact->options, 'u', "unitcode", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^(3[012]?|[012][0-9]|[0-9]{1})$"); + options_add(&elro_800_contact->options, 't', "opened", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&elro_800_contact->options, 'f', "closed", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + + elro_800_contact->parseCode=&parseCode; + elro_800_contact->validate=&validate; +} + +#if defined(MODULE) && !defined(_WIN32) +void compatibility(struct module_t *module) { + module->name = "elro_800_contact"; + module->version = "2.4"; + module->reqversion = "6.0"; + module->reqcommit = "84"; +} + +void init(void) { + elro800ContactInit(); +} +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/elro_800_contact.h b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/elro_800_contact.h new file mode 100755 index 0000000..841986a --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/elro_800_contact.h @@ -0,0 +1,27 @@ +/* + Copyright (C) 2013 CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#ifndef _PROTOCOL_ELRO_800_CONTACT_H_ +#define _PROTOCOL_ELRO_800_CONTACT_H_ + +#include "../protocol.h" + +struct protocol_t *elro_800_contact; +void elro800ContactInit(void); + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/elro_800_switch.c b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/elro_800_switch.c new file mode 100755 index 0000000..dfb3021 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/elro_800_switch.c @@ -0,0 +1,269 @@ +/* + Copyright (C) 2014 CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#include +#include +#include +#include + +#include "../../core/pilight.h" +#include "../../core/common.h" +#include "../../core/dso.h" +#include "../../core/log.h" +#include "../protocol.h" +#include "../../core/binary.h" +#include "../../core/gc.h" +#include "elro_800_switch.h" + +#define PULSE_MULTIPLIER 3 +#define MIN_PULSE_LENGTH 274 +#define MAX_PULSE_LENGTH 320 +#define AVG_PULSE_LENGTH 300 +#define RAW_LENGTH 50 + +static int validate(void) { + if(elro_800_switch->rawlen == RAW_LENGTH) { + if(elro_800_switch->raw[elro_800_switch->rawlen-1] >= (MIN_PULSE_LENGTH*PULSE_DIV) && + elro_800_switch->raw[elro_800_switch->rawlen-1] <= (MAX_PULSE_LENGTH*PULSE_DIV)) { + return 0; + } + } + + return -1; +} + +static void createMessage(int systemcode, int unitcode, int state) { + elro_800_switch->message = json_mkobject(); + json_append_member(elro_800_switch->message, "systemcode", json_mknumber(systemcode, 0)); + json_append_member(elro_800_switch->message, "unitcode", json_mknumber(unitcode, 0)); + if(state == 0) { + json_append_member(elro_800_switch->message, "state", json_mkstring("on")); + } else { + json_append_member(elro_800_switch->message, "state", json_mkstring("off")); + } +} + +static void parseCode(void) { + int binary[RAW_LENGTH/4], x = 0; + + if(elro_800_switch->rawlen>RAW_LENGTH) { + logprintf(LOG_ERR, "elro_800_switch: parsecode - invalid parameter passed %d", elro_800_switch->rawlen); + return; + } + + for(x=0;xrawlen-2;x+=4) { + if(elro_800_switch->raw[x+3] > (int)((double)AVG_PULSE_LENGTH*((double)PULSE_MULTIPLIER/2))) { + binary[x/4] = 1; + } else { + binary[x/4] = 0; + } + } + + int systemcode = binToDec(binary, 0, 4); + int unitcode = binToDec(binary, 5, 9); + int check = binary[10]; + int state = binary[11]; + + // second part of systemcode based on Med + for(x=0;x<=16;x+=4) { + if(elro_800_switch->raw[x+0] > (int)((double)AVG_PULSE_LENGTH*((double)PULSE_MULTIPLIER/2))) { + binary[x/4] = 1; + } else { + binary[x/4] = 0; + } + } + int systemcode2 = binToDec(binary, 0, 4); + + systemcode |= (systemcode2<<5); + + if(check != state) { + createMessage(systemcode, unitcode, state); + } +} + +static void createLow(int s, int e) { + int i; + + for(i=s;i<=e;i+=4) { + elro_800_switch->raw[i]=(AVG_PULSE_LENGTH); + elro_800_switch->raw[i+1]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + elro_800_switch->raw[i+2]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + elro_800_switch->raw[i+3]=(AVG_PULSE_LENGTH); + } +} + +static void createMed(int s, int e) { + int i; + + for(i=s;i<=e;i+=4) { + elro_800_switch->raw[i]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + elro_800_switch->raw[i+1]=(AVG_PULSE_LENGTH); + elro_800_switch->raw[i+2]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + elro_800_switch->raw[i+3]=(AVG_PULSE_LENGTH); + } +} + +static void createHigh(int s, int e) { + int i; + + for(i=s;i<=e;i+=4) { + elro_800_switch->raw[i]=(AVG_PULSE_LENGTH); + elro_800_switch->raw[i+1]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + elro_800_switch->raw[i+2]=(AVG_PULSE_LENGTH); + elro_800_switch->raw[i+3]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + } +} +static void clearCode(void) { + createLow(0,47); +} + +static void createSystemCode(int systemcode) { + int binary[255]; + int length = 0; + int i=0, x=0; + + length = decToBinRev(systemcode & 0x1F, binary); + for(i=0;i<=length;i++) { + if(binary[i]==1) { + x=i*4; + createHigh(x, x+3); + } + } + + length = decToBinRev((systemcode>>5) & 0x1F, binary); + for(i=0;i<=length;i++) { + if(binary[i]==1) { + x=i*4; + createMed(x, x+3); + } + } +} + +static void createUnitCode(int unitcode) { + int binary[255]; + int length = 0; + int i=0, x=0; + + length = decToBinRev(unitcode, binary); + for(i=0;i<=length;i++) { + if(binary[i]==1) { + x=i*4; + createHigh(20+x, 20+x+3); + } + } +} + +static void createState(int state) { + if(state == 1) { + createHigh(44, 47); + } else { + createHigh(40, 43); + } +} + +static void createFooter(void) { + elro_800_switch->raw[48]=(AVG_PULSE_LENGTH); + elro_800_switch->raw[49]=(PULSE_DIV*AVG_PULSE_LENGTH); +} + +static int createCode(struct JsonNode *code) { + int systemcode = -1; + int unitcode = -1; + int state = -1; + double itmp = 0; + + if(json_find_number(code, "systemcode", &itmp) == 0) + systemcode = (int)round(itmp); + if(json_find_number(code, "unitcode", &itmp) == 0) + unitcode = (int)round(itmp); + if(json_find_number(code, "off", &itmp) == 0) + state=1; + else if(json_find_number(code, "on", &itmp) == 0) + state=0; + + if(systemcode == -1 || unitcode == -1 || state == -1) { + logprintf(LOG_ERR, "elro_800_switch: insufficient number of arguments"); + return EXIT_FAILURE; + } else if(systemcode > 1023 || systemcode < 0) { + logprintf(LOG_ERR, "elro_800_switch: invalid systemcode range"); + return EXIT_FAILURE; + } else if(unitcode > 31 || unitcode < 0) { + logprintf(LOG_ERR, "elro_800_switch: invalid unitcode range"); + return EXIT_FAILURE; + } else { + createMessage(systemcode, unitcode, state); + clearCode(); + createSystemCode(systemcode); + createUnitCode(unitcode); + createState(state); + createFooter(); + elro_800_switch->rawlen = RAW_LENGTH; + } + return EXIT_SUCCESS; +} + +static void printHelp(void) { + printf("\t -s --systemcode=systemcode\tcontrol a device with this systemcode\n"); + printf("\t -u --unitcode=unitcode\t\tcontrol a device with this unitcode\n"); + printf("\t -t --on\t\t\tsend an on signal\n"); + printf("\t -f --off\t\t\tsend an off signal\n"); +} + +#if !defined(MODULE) && !defined(_WIN32) +__attribute__((weak)) +#endif +void elro800SwitchInit(void) { + + protocol_register(&elro_800_switch); + protocol_set_id(elro_800_switch, "elro_800_switch"); + protocol_device_add(elro_800_switch, "elro_800_switch", "Elro 800 series Switches"); + protocol_device_add(elro_800_switch, "brennenstuhl", "Brennenstuhl Comfort"); + protocol_device_add(elro_800_switch, "maxitronic", "Maxi-Tronic FUNK-LIGHT switches"); + elro_800_switch->devtype = SWITCH; + elro_800_switch->hwtype = RF433; + elro_800_switch->minrawlen = RAW_LENGTH; + elro_800_switch->maxrawlen = RAW_LENGTH; + elro_800_switch->maxgaplen = MAX_PULSE_LENGTH*PULSE_DIV; + elro_800_switch->mingaplen = MIN_PULSE_LENGTH*PULSE_DIV; + + options_add(&elro_800_switch->options, 's', "systemcode", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^102[0-3]|10[01][0-9]|[0-9]{1,3}$"); + options_add(&elro_800_switch->options, 'u', "unitcode", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^(3[012]?|[012][0-9]|[0-9]{1})$"); + options_add(&elro_800_switch->options, 't', "on", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&elro_800_switch->options, 'f', "off", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + + options_add(&elro_800_switch->options, 0, "readonly", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + options_add(&elro_800_switch->options, 0, "confirm", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + + elro_800_switch->parseCode=&parseCode; + elro_800_switch->createCode=&createCode; + elro_800_switch->printHelp=&printHelp; + elro_800_switch->validate=&validate; +} + +#if defined(MODULE) && !defined(_WIN32) +void compatibility(struct module_t *module) { + module->name = "elro_800_switch"; + module->version = "2.5"; + module->reqversion = "6.0"; + module->reqcommit = "84"; +} + +void init(void) { + elro800SwitchInit(); +} +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/elro_800_switch.h b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/elro_800_switch.h new file mode 100755 index 0000000..b31eb90 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/elro_800_switch.h @@ -0,0 +1,27 @@ +/* + Copyright (C) 2013 CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#ifndef _PROTOCOL_ELRO_800_SWITCH_H_ +#define _PROTOCOL_ELRO_800_SWITCH_H_ + +#include "../protocol.h" + +struct protocol_t *elro_800_switch; +void elro800SwitchInit(void); + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/eurodomest_switch.c b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/eurodomest_switch.c new file mode 100755 index 0000000..9182089 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/eurodomest_switch.c @@ -0,0 +1,368 @@ +/* + Copyright (C) 2015 woutput + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#include +#include +#include +#include + +#include "../../core/pilight.h" +#include "../../core/common.h" +#include "../../core/dso.h" +#include "../../core/log.h" +#include "../protocol.h" +#include "../../core/binary.h" +#include "../../core/gc.h" +#include "eurodomest_switch.h" + +// Timing from http://forum.pilight.org/attachment.php?aid=595 + +// Increase this value to be more robust, but also create more false positives +#define PEAK_TO_PEAK_JITTER 200 + +// About 2/3 of the jitter appears to make the pulses longer instead of shorter +// The same jitter appears on every pulse type (as may be expected) + +// Short pulse timing +#define MIN_SHORT_PULSE_LENGTH (AVG_SHORT_PULSE_LENGTH - 0.333 * PEAK_TO_PEAK_JITTER) +#define AVG_SHORT_PULSE_LENGTH 280 +#define MAX_SHORT_PULSE_LENGTH (AVG_SHORT_PULSE_LENGTH + 0.667 * PEAK_TO_PEAK_JITTER) + +// Medium pulse timing +#define MIN_MEDIUM_PULSE_LENGTH (AVG_MEDIUM_PULSE_LENGTH - 0.333 * PEAK_TO_PEAK_JITTER) +#define AVG_MEDIUM_PULSE_LENGTH 868 +#define MAX_MEDIUM_PULSE_LENGTH (AVG_MEDIUM_PULSE_LENGTH + 0.667 * PEAK_TO_PEAK_JITTER) + +// Long pulse timing +#define MIN_LONG_PULSE_LENGTH (AVG_LONG_PULSE_LENGTH - 0.333 * PEAK_TO_PEAK_JITTER) +#define AVG_LONG_PULSE_LENGTH 9660 +#define MAX_LONG_PULSE_LENGTH (AVG_LONG_PULSE_LENGTH + 0.667 * PEAK_TO_PEAK_JITTER) + +#define RAW_LENGTH 50 +// Two pulses per bit, last two pulses are footer +#define BINARY_LENGTH 24 + +#define LEARN_REPEATS 40 +#define NORMAL_REPEATS 10 + +static int validate(void) { + if (eurodomest_switch->rawlen == RAW_LENGTH) { + if (eurodomest_switch->raw[eurodomest_switch->rawlen - 1] >= MIN_LONG_PULSE_LENGTH && + eurodomest_switch->raw[eurodomest_switch->rawlen - 1] <= MAX_LONG_PULSE_LENGTH) { + return 0; + } + } + + return -1; +} + +static void createMessage(int id, int unit, int state, int all, int learn) { + eurodomest_switch->message = json_mkobject(); + + json_append_member(eurodomest_switch->message, "id", json_mknumber(id, 0)); + + if (all == 1) { + json_append_member(eurodomest_switch->message, "all", json_mknumber(all, 0)); + } else { + json_append_member(eurodomest_switch->message, "unit", json_mknumber(unit, 0)); + } + + if (state == 1) { + json_append_member(eurodomest_switch->message, "state", json_mkstring("on")); + } else { + json_append_member(eurodomest_switch->message, "state", json_mkstring("off")); + } + + if (learn == 1) { + eurodomest_switch->txrpt = LEARN_REPEATS; + } else { + eurodomest_switch->txrpt = NORMAL_REPEATS; + } +} + +static void parseCode(void) { + int binary[BINARY_LENGTH], x = 0, i = 0; + + for (x = 0; x < eurodomest_switch->rawlen - 2; x += 2) { + if ((eurodomest_switch->raw[x] >= MIN_MEDIUM_PULSE_LENGTH) && + (eurodomest_switch->raw[x] <= MAX_MEDIUM_PULSE_LENGTH) && + (eurodomest_switch->raw[x + 1] >= MIN_SHORT_PULSE_LENGTH) && + (eurodomest_switch->raw[x + 1] <= MAX_SHORT_PULSE_LENGTH)) { + binary[i++] = 0; + } else if ((eurodomest_switch->raw[x] >= MIN_SHORT_PULSE_LENGTH) && + (eurodomest_switch->raw[x] <= MAX_SHORT_PULSE_LENGTH) && + (eurodomest_switch->raw[x + 1] >= MIN_MEDIUM_PULSE_LENGTH) && + (eurodomest_switch->raw[x + 1] <= MAX_MEDIUM_PULSE_LENGTH)) { + binary[i++] = 1; + } else { + return; // decoding failed, return without creating message + } + } + + // The last 4 bits contain the unit, state and all information + + int unit = 0; + int all = 0; + int state = 0; + + if (binary[20] == 0 && binary[21] == 0 && binary[22] == 0 && binary[23] == 0) { + unit = 1; + all = 0; + state = 1; // on + } else if (binary[20] == 0 && binary[21] == 0 && binary[22] == 0 && binary[23] == 1) { + unit = 1; + all = 0; + state = 0; // off + } else if (binary[20] == 0 && binary[21] == 0 && binary[22] == 1 && binary[23] == 1) { + unit = 2; + all = 0; + state = 0; // off + } else if (binary[20] == 0 && binary[21] == 0 && binary[22] == 1 && binary[23] == 0) { + unit = 2; + all = 0; + state = 1; // on + } else if (binary[20] == 0 && binary[21] == 1 && binary[22] == 0 && binary[23] == 1) { + unit = 3; + all = 0; + state = 0; // off + } else if (binary[20] == 0 && binary[21] == 1 && binary[22] == 0 && binary[23] == 0) { + unit = 3; + all = 0; + state = 1; // on + } else if (binary[20] == 1 && binary[21] == 0 && binary[22] == 0 && binary[23] == 1) { + unit = 4; + all = 0; + state = 0; // off + } else if (binary[20] == 1 && binary[21] == 0 && binary[22] == 0 && binary[23] == 0) { + unit = 4; + all = 0; + state = 1; // on + } else if (binary[20] == 1 && binary[21] == 1 && binary[22] == 1 && binary[23] == 0) { + unit = 0; // not used, all = 1 + all = 1; + state = 0; // off + } else if (binary[20] == 1 && binary[21] == 1 && binary[22] == 0 && binary[23] == 1) { + unit = 0; // not used, all = 1 + all = 1; + state = 1; // on + } else { + return; // decoding failed, return without creating message + } + + int id = binToDec(binary, 0, 19); + createMessage(id, unit, state, all, 0); +} + +static void createLow(int s, int e) { + int i; + + for (i = s; i <= e; i += 2) { // medium - short + eurodomest_switch->raw[i] = AVG_MEDIUM_PULSE_LENGTH; + eurodomest_switch->raw[i + 1] = AVG_SHORT_PULSE_LENGTH; + } +} + +static void createHigh(int s, int e) { + int i; + + for (i = s; i <= e; i += 2) { // short - medium + eurodomest_switch->raw[i] = AVG_SHORT_PULSE_LENGTH; + eurodomest_switch->raw[i + 1] = AVG_MEDIUM_PULSE_LENGTH; + } +} + +static void createId(int id) { + int binary[255]; + int length = 0; + int i = 0, x = 0; + + length = decToBinRev(id, binary); + for (i = 0; i <= length; i++) { + if (binary[i] == 0) { + x = i * 2; + createLow(x, x+1); + } else { //so binary[i] == 1 + x = i * 2; + createHigh(x, x + 1); + } + } +} + +static int createUnitAndStateAndAll(int unit, int state, int all) { + if (unit == 1 && state == 0 && all == 0) { + createLow(40, 41); + createLow(42, 43); + createLow(44, 45); + createHigh(46, 47); + } else if (unit == 1 && state == 1 && all == 0) { + createLow(40, 41); + createLow(42, 43); + createLow(44, 45); + createLow(46, 47); + } else if (unit == 2 && state == 0 && all == 0) { + createLow(40, 41); + createLow(42, 43); + createHigh(44, 45); + createHigh(46, 47); + } else if (unit == 2 && state == 1 && all == 0) { + createLow(40, 41); + createLow(42, 43); + createHigh(44, 45); + createLow(46, 47); + } else if (unit == 3 && state == 0 && all == 0) { + createLow(40, 41); + createHigh(42, 43); + createLow(44, 45); + createHigh(46, 47); + } else if (unit == 3 && state == 1 && all == 0) { + createLow(40, 41); + createHigh(42, 43); + createLow(44, 45); + createLow(46, 47); + } else if (unit == 4 && state == 0 && all == 0) { + createHigh(40, 41); + createLow(42, 43); + createLow(44, 45); + createHigh(46, 47); + } else if (unit == 4 && state == 1 && all == 0) { + createHigh(40, 41); + createLow(42, 43); + createLow(44, 45); + createLow(46, 47); + } else if (unit == 0 && state == 0 && all == 1) { + createHigh(40, 41); + createHigh(42, 43); + createHigh(44, 45); + createLow(46, 47); + } else if (unit == 0 && state == 1 && all == 1) { + createHigh(40, 41); + createHigh(42, 43); + createLow(44, 45); + createHigh(46, 47); + } else { + logprintf(LOG_ERR, "eurodomest_switch: incorrect combination of arguments"); + return EXIT_FAILURE; + } + return EXIT_SUCCESS; +} + +static void createFooter(void) { + eurodomest_switch->raw[48] = AVG_SHORT_PULSE_LENGTH; + eurodomest_switch->raw[49] = AVG_LONG_PULSE_LENGTH; +} + +static int createCode(struct JsonNode *code) { + int id = -1; + int unit = -1; + int state = -1; + int all = 0; + int learn = -1; + double itmp = -1; + + if (json_find_number(code, "id", &itmp) == 0) + id = (int)round(itmp); + if (json_find_number(code, "unit", &itmp) == 0) + unit = (int)round(itmp); + if (json_find_number(code, "all", &itmp) == 0) + all = (int)round(itmp); + if (json_find_number(code, "off", &itmp) == 0) + state = 0; + else if (json_find_number(code, "on", &itmp) == 0) + state = 1; + if (json_find_number(code, "learn", &itmp) == 0) + learn = 1; + + if (all > 0 && learn > -1) { + logprintf(LOG_ERR, "eurodomest_switch: all and learn cannot be combined"); + return EXIT_FAILURE; + } else if (id == -1 || (unit == -1 && all == 0) || state == -1) { + logprintf(LOG_ERR, "eurodomest_switch: insufficient number of arguments"); + return EXIT_FAILURE; + } else if (id > 1048575 || id < 0) { + logprintf(LOG_ERR, "eurodomest_switch: invalid id range"); + return EXIT_FAILURE; + } else if ((unit > 4 || unit < 1) && all == 0) { + logprintf(LOG_ERR, "eurodomest_switch: invalid unit range"); + return EXIT_FAILURE; + } else { + if (unit == -1 && all == 1) { + unit = 0; + } + createMessage(id, unit, state, all, learn); + createId(id); + if (createUnitAndStateAndAll(unit, state, all) == EXIT_FAILURE) + return EXIT_FAILURE; + createFooter(); + eurodomest_switch->rawlen = RAW_LENGTH; + } + return EXIT_SUCCESS; +} + + +static void printHelp(void) { + printf("\t -t --on\t\t\tsend an on signal\n"); + printf("\t -f --off\t\t\tsend an off signal\n"); + printf("\t -u --unit=unit\t\t\tcontrol a device with this unit code\n"); + printf("\t -i --id=id\t\t\tcontrol a device with this id\n"); + printf("\t -a --all\t\t\tsend command to all devices with this id\n"); + printf("\t -l --learn\t\t\tsend multiple streams so switch can learn\n"); +} + +#if !defined(MODULE) && !defined(_WIN32) +__attribute__((weak)) +#endif +void eurodomestSwitchInit(void) { + + protocol_register(&eurodomest_switch); + protocol_set_id(eurodomest_switch, "eurodomest_switch"); + protocol_device_add(eurodomest_switch, "eurodomest_switch", "Eurodomest Switches"); + eurodomest_switch->devtype = SWITCH; + eurodomest_switch->hwtype = RF433; + eurodomest_switch->minrawlen = RAW_LENGTH; + eurodomest_switch->maxrawlen = RAW_LENGTH; + eurodomest_switch->maxgaplen = MAX_LONG_PULSE_LENGTH; + eurodomest_switch->mingaplen = MIN_LONG_PULSE_LENGTH; + + options_add(&eurodomest_switch->options, 't', "on", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&eurodomest_switch->options, 'f', "off", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&eurodomest_switch->options, 'u', "unit", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^([1-4])$"); + options_add(&eurodomest_switch->options, 'i', "id", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^([0-9]{1}|[0-9]{2}|[0-9]{3}|[0-9]{4}|[0-9]{5}|[0-9]{6}|[0-9]{7})$"); + options_add(&eurodomest_switch->options, 'a', "all", OPTION_OPT_VALUE, DEVICES_OPTIONAL, JSON_NUMBER, NULL, NULL); + options_add(&eurodomest_switch->options, 'l', "learn", OPTION_NO_VALUE, DEVICES_OPTIONAL, JSON_NUMBER, NULL, NULL); + + options_add(&eurodomest_switch->options, 0, "readonly", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]$"); + options_add(&eurodomest_switch->options, 0, "confirm", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]$"); + + eurodomest_switch->parseCode = &parseCode; + eurodomest_switch->createCode = &createCode; + eurodomest_switch->printHelp = &printHelp; + eurodomest_switch->validate = &validate; +} + +#if defined(MODULE) && !defined(_WIN32) +void compatibility(struct module_t *module) { + module->name = "eurodomest_switch"; + module->version = "1.0"; + module->reqversion = "6.0"; + module->reqcommit = "84"; +} + +void init(void) { + eurodomestSwitchInit(); +} +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/eurodomest_switch.h b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/eurodomest_switch.h new file mode 100755 index 0000000..9af3243 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/eurodomest_switch.h @@ -0,0 +1,27 @@ +/* + Copyright (C) 2015 woutput + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#ifndef _PROTOCOL_EURODOMEST_SWITCH_H_ +#define _PROTOCOL_EURODOMEST_SWITCH_H_ + +#include "../protocol.h" + +struct protocol_t *eurodomest_switch; +void eurodomestSwitchInit(void); + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/ev1527.c b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/ev1527.c new file mode 100755 index 0000000..c4d11fa --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/ev1527.c @@ -0,0 +1,115 @@ +/* + Copyright (C) 2015 CurlyMo and Meloen + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#include +#include +#include +#include + +#include "../../core/pilight.h" +#include "../../core/common.h" +#include "../../core/dso.h" +#include "../../core/log.h" +#include "../protocol.h" +#include "../../core/binary.h" +#include "../../core/gc.h" +#include "ev1527.h" + +#define PULSE_MULTIPLIER 5 +#define MIN_PULSE_LENGTH 251 +#define MAX_PULSE_LENGTH 311 +#define AVG_PULSE_LENGTH 256 +#define RAW_LENGTH 50 + +static int validate(void) { + if(ev1527->rawlen == RAW_LENGTH) { + if(ev1527->raw[ev1527->rawlen-1] >= (MIN_PULSE_LENGTH*PULSE_DIV) && + ev1527->raw[ev1527->rawlen-1] <= (MAX_PULSE_LENGTH*PULSE_DIV)) { + return 0; + } + } + + return -1; +} + +static void createMessage(int unitcode, int state) { + ev1527->message = json_mkobject(); + json_append_member(ev1527->message, "unitcode", json_mknumber(unitcode, 0)); + if(state == 0) { + json_append_member(ev1527->message, "state", json_mkstring("opened")); + } else { + json_append_member(ev1527->message, "state", json_mkstring("closed")); + } +} + +static void parseCode(void) { + int binary[RAW_LENGTH/2], x = 0, i = 0; + + if(ev1527->rawlen>RAW_LENGTH) { + logprintf(LOG_ERR, "ev1527: parsecode - invalid parameter passed %d", ev1527->rawlen); + return; + } + + for(x=0;xrawlen-2;x+=2) { + if(ev1527->raw[x+3] > (int)((double)AVG_PULSE_LENGTH*((double)PULSE_MULTIPLIER/2))) { + binary[i++] = 1; + } else { + binary[i++] = 0; + } + } + + int unitcode = binToDec(binary, 0, 19); + int state = binary[20]; + createMessage(unitcode, state); +} + +#if !defined(MODULE) && !defined(_WIN32) +__attribute__((weak)) +#endif +void ev1527Init(void) { + + protocol_register(&ev1527); + protocol_set_id(ev1527, "ev1527"); + protocol_device_add(ev1527, "ev1527", "ev1527 contact sensor"); + ev1527->devtype = CONTACT; + ev1527->hwtype = RF433; + ev1527->minrawlen = RAW_LENGTH; + ev1527->maxrawlen = RAW_LENGTH; + ev1527->maxgaplen = MAX_PULSE_LENGTH*PULSE_DIV; + ev1527->mingaplen = MIN_PULSE_LENGTH*PULSE_DIV; + + options_add(&ev1527->options, 'u', "unitcode", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^(104857[0-5]|10485[0-6][0-9]|1048[0-4][0-9][0-9]|104[0-7][0-9]{3}|10[0-3][0-9]{4}|0?[0-9]{1,6})$"); + options_add(&ev1527->options, 't', "opened", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&ev1527->options, 'f', "closed", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + + ev1527->parseCode=&parseCode; + ev1527->validate=&validate; +} + +#if defined(MODULE) && !defined(_WIN32) +void compatibility(struct module_t *module) { + module->name = "ev1527"; + module->version = "1.2"; + module->reqversion = "6.0"; + module->reqcommit = "84"; +} + +void init(void) { + ev1527Init(); +} +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/ev1527.h b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/ev1527.h new file mode 100755 index 0000000..ec40573 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/ev1527.h @@ -0,0 +1,27 @@ +/* + Copyright (C) 2014 CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#ifndef _PROTOCOL_EV1527_H_ +#define _PROTOCOL_EV1527_H_ + +#include "../protocol.h" + +struct protocol_t *ev1527; +void ev1527Init(void); + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/heitech.c b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/heitech.c new file mode 100755 index 0000000..1713839 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/heitech.c @@ -0,0 +1,238 @@ +/* + + Copyright (C) 2015 CurlyMo & Meloen + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#include +#include +#include +#include + +#include "../../core/pilight.h" +#include "../../core/common.h" +#include "../../core/dso.h" +#include "../../core/log.h" +#include "../../core/binary.h" +#include "../../core/gc.h" +#include "../protocol.h" +#include "heitech.h" + +#define PULSE_MULTIPLIER 3 +#define MIN_PULSE_LENGTH 270 +#define MAX_PULSE_LENGTH 290 +#define AVG_PULSE_LENGTH 280 +#define RAW_LENGTH 50 + +static int validate(void) { + if(heitech->rawlen == RAW_LENGTH) { + if(heitech->raw[heitech->rawlen-1] >= (MIN_PULSE_LENGTH*PULSE_DIV) && + heitech->raw[heitech->rawlen-1] <= (MAX_PULSE_LENGTH*PULSE_DIV)) { + return 0; + } + } + + return -1; +} + +static void createMessage(int systemcode, int unitcode, int state) { + heitech->message = json_mkobject(); + json_append_member(heitech->message, "systemcode", json_mknumber(systemcode, 0)); + json_append_member(heitech->message, "unitcode", json_mknumber(unitcode, 0)); + + if(state == 0) { + json_append_member(heitech->message, "state", json_mkstring("on")); + } else { + json_append_member(heitech->message, "state", json_mkstring("off")); + } +} + +static void parseCode(void) { + int x = 0, binary[RAW_LENGTH/4]; + + if(heitech->rawlen>RAW_LENGTH) { + logprintf(LOG_ERR, "heitech: parsecode - invalid parameter passed %d", heitech->rawlen); + return; + } + + for(x=0;xrawlen-2;x+=4) { + if(heitech->raw[x+3] > (int)((double)AVG_PULSE_LENGTH*((double)PULSE_MULTIPLIER/2))) { + binary[x/4]=1; + } else { + binary[x/4]=0; + } + } + + int systemcode = binToDec(binary, 0, 4); + int unitcode = binToDec(binary, 5, 9); + int check = binary[10]; + int state = binary[11]; + + if(check != state) { + createMessage(systemcode, unitcode, state); + } +} + +static void createLow(int s, int e) { + int i; + + for(i=s;i<=e;i+=4) { + heitech->raw[i]=AVG_PULSE_LENGTH; + heitech->raw[i+1]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + heitech->raw[i+2]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + heitech->raw[i+3]=AVG_PULSE_LENGTH; + } +} + +static void createHigh(int s, int e) { + int i; + + for(i=s;i<=e;i+=4) { + heitech->raw[i]=AVG_PULSE_LENGTH; + heitech->raw[i+1]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + heitech->raw[i+2]=AVG_PULSE_LENGTH; + heitech->raw[i+3]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + } +} + +static void clearCode(void) { + createLow(0,47); +} + +static void createSystemCode(int systemcode) { + int binary[255]; + int length = 0; + int i=0, x=0; + + length = decToBinRev(systemcode, binary); + for(i=0;i<=length;i++) { + if(binary[i]==1) { + x=i*4; + createHigh(x, x+3); + } + } +} + +static void createUnitCode(int unitcode) { + int binary[255]; + int length = 0; + int i=0, x=0; + + length = decToBinRev(unitcode, binary); + for(i=0;i<=length;i++) { + if(binary[i]==1) { + x=i*4; + createHigh(20+x, 20+x+3); + } + } +} + +static void createState(int state) { + if(state == 1) { + createHigh(44, 47); + } else { + createHigh(40, 43); + } +} + +static void createFooter(void) { + heitech->raw[48]=(AVG_PULSE_LENGTH); + heitech->raw[49]=(PULSE_DIV*AVG_PULSE_LENGTH); +} +static int createCode(JsonNode *code) { + int systemcode = -1; + int unitcode = -1; + int state = -1; + double itmp = 0; + + if(json_find_number(code, "systemcode", &itmp) == 0) + systemcode = (int)round(itmp); + if(json_find_number(code, "unitcode", &itmp) == 0) + unitcode = (int)round(itmp); + if(json_find_number(code, "off", &itmp) == 0) + state=1; + else if(json_find_number(code, "on", &itmp) == 0) + state=0; + + if(systemcode == -1 || unitcode == -1 || state == -1) { + logprintf(LOG_ERR, "heitech: insufficient number of arguments"); + return EXIT_FAILURE; + } else if(systemcode > 31 || systemcode < 0) { + logprintf(LOG_ERR, "heitech: invalid systemcode range"); + return EXIT_FAILURE; + } else if(unitcode > 31 || unitcode < 0) { + logprintf(LOG_ERR, "heitech: invalid unitcode range"); + return EXIT_FAILURE; + } else { + createMessage(systemcode, unitcode, state); + clearCode(); + createSystemCode(systemcode); + createUnitCode(unitcode); + createState(state); + createFooter(); + heitech->rawlen = RAW_LENGTH; + } + return EXIT_SUCCESS; +} + +static void printHelp(void) { + printf("\t -s --systemcode=systemcode\tcontrol a device with this systemcode\n"); + printf("\t -u --unitcode=unitcode\t\tcontrol a device with this unitcode\n"); + printf("\t -t --on\t\t\tsend an on signal\n"); + printf("\t -f --off\t\t\tsend an off signal\n"); +} + +#if !defined(MODULE) && !defined(_WIN32) +__attribute__((weak)) +#endif +void heitechInit(void) { + + protocol_register(&heitech); + protocol_set_id(heitech, "heitech"); + protocol_device_add(heitech, "heitech", "Heitech series Switches"); + heitech->devtype = SWITCH; + heitech->hwtype = RF433; + heitech->minrawlen = RAW_LENGTH; + heitech->maxrawlen = RAW_LENGTH; + heitech->maxgaplen = MAX_PULSE_LENGTH*PULSE_DIV; + heitech->mingaplen = MIN_PULSE_LENGTH*PULSE_DIV; + + options_add(&heitech->options, 's', "systemcode", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^(3[012]?|[012][0-9]|[0-9]{1})$"); + options_add(&heitech->options, 'u', "unitcode", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^(3[012]?|[012][0-9]|[0-9]{1})$"); + options_add(&heitech->options, 't', "on", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&heitech->options, 'f', "off", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + + options_add(&heitech->options, 0, "readonly", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + + heitech->parseCode=&parseCode; + heitech->createCode=&createCode; + heitech->printHelp=&printHelp; + heitech->validate=&validate; +} + +#if defined(MODULE) && !defined(_WIN32) +void compatibility(struct module_t *module) { + module->name = "heitech"; + module->version = "1.1"; + module->reqversion = "6.0"; + module->reqcommit = "187"; +} + +void init(void) { + heitechInit(); +} + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/heitech.h b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/heitech.h new file mode 100755 index 0000000..f04eae0 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/heitech.h @@ -0,0 +1,27 @@ +/* + Copyright (C) 2015 CurlyMo & meloen + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#ifndef _PROTOCOL_HEITECH_H_ +#define _PROTOCOL_HEITECH_H_ + +#include "../protocol.h" + +struct protocol_t *heitech; +void heitechInit(void); + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/impuls.c b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/impuls.c new file mode 100755 index 0000000..481601f --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/impuls.c @@ -0,0 +1,250 @@ +/* + Copyright (C) 2013 Bram1337 & CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#include +#include +#include +#include + +#include "../../core/pilight.h" +#include "../../core/common.h" +#include "../../core/dso.h" +#include "../../core/log.h" +#include "../protocol.h" +#include "../../core/binary.h" +#include "../../core/gc.h" +#include "impuls.h" + +#define PULSE_MULTIPLIER 3 +#define MIN_PULSE_LENGTH 130 +#define MAX_PULSE_LENGTH 170 +#define AVG_PULSE_LENGTH 150 +#define RAW_LENGTH 50 + +static int validate(void) { + if(impuls->rawlen == RAW_LENGTH) { + if(impuls->raw[impuls->rawlen-1] >= (MIN_PULSE_LENGTH*PULSE_DIV) && + impuls->raw[impuls->rawlen-1] <= (MAX_PULSE_LENGTH*PULSE_DIV)) { + return 0; + } + } + + return -1; +} + +static void createMessage(int systemcode, int programcode, int state) { + impuls->message = json_mkobject(); + json_append_member(impuls->message, "systemcode", json_mknumber(systemcode, 0)); + json_append_member(impuls->message, "programcode", json_mknumber(programcode, 0)); + if(state == 1) { + json_append_member(impuls->message, "state", json_mkstring("on")); + } else { + json_append_member(impuls->message, "state", json_mkstring("off")); + } +} + +static void parseCode(void) { + int x = 0, binary[RAW_LENGTH/4]; + + if(impuls->rawlen>RAW_LENGTH) { + logprintf(LOG_ERR, "impuls: parsecode - invalid parameter passed %d", impuls->rawlen); + return; + } + + /* Convert the one's and zero's into binary */ + for(x=0;xrawlen-2;x+=4) { + if(impuls->raw[x+3] > (int)((double)AVG_PULSE_LENGTH*((double)PULSE_MULTIPLIER/2)) || + impuls->raw[x+0] > (int)((double)AVG_PULSE_LENGTH*((double)PULSE_MULTIPLIER/2))) { + binary[x/4]=1; + } else { + binary[x/4]=0; + } + } + + int systemcode = binToDec(binary, 0, 4); + int programcode = binToDec(binary, 5, 9); + int check = binary[10]; + int state = binary[11]; + + if(check != state) { + createMessage(systemcode, programcode, state); + } +} + +static void createLow(int s, int e) { + int i; + + for(i=s;i<=e;i+=4) { + impuls->raw[i]=AVG_PULSE_LENGTH; + impuls->raw[i+1]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + impuls->raw[i+2]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + impuls->raw[i+3]=AVG_PULSE_LENGTH; + } +} + +static void createMed(int s, int e) { + int i; + + for(i=s;i<=e;i+=4) { + impuls->raw[i]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + impuls->raw[i+1]=AVG_PULSE_LENGTH; + impuls->raw[i+2]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + impuls->raw[i+3]=AVG_PULSE_LENGTH; + } +} + +static void createHigh(int s, int e) { + int i; + + for(i=s;i<=e;i+=4) { + impuls->raw[i]=AVG_PULSE_LENGTH; + impuls->raw[i+1]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + impuls->raw[i+2]=AVG_PULSE_LENGTH; + impuls->raw[i+3]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + } +} + +static void clearCode(void) { + createLow(0,47); +} + +static void createSystemCode(int systemcode) { + int binary[255]; + int length = 0; + int i=0, x=0; + + length = decToBinRev(systemcode, binary); + for(i=0;i<=length;i++) { + if(binary[i]==1) { + x=i*4; + createMed(x, x+3); + } + } +} + +static void createProgramCode(int programcode) { + int binary[255]; + int length = 0; + int i=0, x=0; + + length = decToBinRev(programcode, binary); + for(i=0;i<=length;i++) { + if(binary[i]==1) { + x=i*4; + createHigh(20+x, 20+x+3); + } + } +} + +static void createState(int state) { + if(state == 0) { + createHigh(40, 43); + } else { + createHigh(44, 47); + } +} + +static void createFooter(void) { + impuls->raw[48]=(AVG_PULSE_LENGTH); + impuls->raw[49]=(PULSE_DIV*AVG_PULSE_LENGTH); +} + +static int createCode(struct JsonNode *code) { + int systemcode = -1; + int programcode = -1; + int state = -1; + double itmp = 0; + + if(json_find_number(code, "systemcode", &itmp) == 0) + systemcode = (int)round(itmp); + if(json_find_number(code, "programcode", &itmp) == 0) + programcode = (int)round(itmp); + if(json_find_number(code, "off", &itmp) == 0) + state=0; + else if(json_find_number(code, "on", &itmp) == 0) + state=1; + + if(systemcode == -1 || programcode == -1 || state == -1) { + logprintf(LOG_ERR, "impuls: insufficient number of arguments"); + return EXIT_FAILURE; + } else if(systemcode > 31 || systemcode < 0) { + logprintf(LOG_ERR, "impuls: invalid systemcode range"); + return EXIT_FAILURE; + } else if(programcode > 31 || programcode < 0) { + logprintf(LOG_ERR, "impuls: invalid programcode range"); + return EXIT_FAILURE; + } else { + createMessage(systemcode, programcode, state); + clearCode(); + createSystemCode(systemcode); + createProgramCode(programcode); + createState(state); + createFooter(); + impuls->rawlen = RAW_LENGTH; + } + return EXIT_SUCCESS; +} + +static void printHelp(void) { + printf("\t -s --systemcode=systemcode\tcontrol a device with this systemcode\n"); + printf("\t -u --programcode=programcode\tcontrol a device with this programcode\n"); + printf("\t -t --on\t\t\tsend an on signal\n"); + printf("\t -f --off\t\t\tsend an off signal\n"); +} + +#if !defined(MODULE) && !defined(_WIN32) +__attribute__((weak)) +#endif +void impulsInit(void) { + + protocol_register(&impuls); + protocol_set_id(impuls, "impuls"); + protocol_device_add(impuls, "impuls", "Impuls Switches"); + impuls->devtype = SWITCH; + impuls->hwtype = RF433; + impuls->minrawlen = RAW_LENGTH; + impuls->maxrawlen = RAW_LENGTH; + impuls->maxgaplen = MAX_PULSE_LENGTH*PULSE_DIV; + impuls->mingaplen = MIN_PULSE_LENGTH*PULSE_DIV; + + options_add(&impuls->options, 's', "systemcode", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^(3[012]?|[012][0-9]|[0-9]{1})$"); + options_add(&impuls->options, 'u', "programcode", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^(3[012]?|[012][0-9]|[0-9]{1})$"); + options_add(&impuls->options, 't', "on", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&impuls->options, 'f', "off", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + + options_add(&impuls->options, 0, "readonly", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + options_add(&impuls->options, 0, "confirm", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + + impuls->parseCode=&parseCode; + impuls->createCode=&createCode; + impuls->printHelp=&printHelp; + impuls->validate=&validate; +} + +#if defined(MODULE) && !defined(_WIN32) +void compatibility(struct module_t *module) { + module->name = "impuls"; + module->version = "2.4"; + module->reqversion = "6.0"; + module->reqcommit = "84"; +} + +void init(void) { + impulsInit(); +} +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/impuls.h b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/impuls.h new file mode 100755 index 0000000..bc8aa25 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/impuls.h @@ -0,0 +1,27 @@ +/* + Copyright (C) 2013 Bram1337 & CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#ifndef _PROTOCOL_IMPULS_H_ +#define _PROTOCOL_IMPULS_H_ + +#include "../protocol.h" + +struct protocol_t *impuls; +void impulsInit(void); + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/livolo_switch.c b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/livolo_switch.c new file mode 100755 index 0000000..a7a7208 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/livolo_switch.c @@ -0,0 +1,207 @@ +/* + Copyright (C) 2017 Oleksii Serdiuk + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#include "livolo_switch.h" + +#include "../../core/binary.h" +#include "../../core/log.h" + +#if defined(MODULE) && !defined(_WIN32) +# include "../../core/dso.h" +#endif + +#include +#include +#include + +// Based on the protocol description from +// http://platenspeler.github.io/DeveloperGuide/433Transmitters/433messaging.html +// with timing adjustments, based on sniffing the remote I own + +#define REPEATS 120 +#define PULSE_LENGTH 170 +#define MIN_RAW_LENGTH 24 +#define MAX_RAW_LENGTH 47 + +#define ID_BITS 16 +#define UNIT_BITS 7 + +static int createHeader() { + livolo_switch->raw[0] = PULSE_LENGTH*3.5; + return 1; +} + +static int createLow(int i) { + livolo_switch->raw[i] = PULSE_LENGTH; + livolo_switch->raw[i+1] = PULSE_LENGTH; + return 2; +} + +static int createHigh(int i) { + livolo_switch->raw[i] = PULSE_LENGTH*2; + return 1; +} + +static int createBits(int value, int start, int length) { + int binary[255]; + int l = 0; + int i = 0, x = start; + + l = decToBin(value, binary); + // Pad with zeroes + for(i=0;imessage = json_mkobject(); + + json_append_member(livolo_switch->message, "id", json_mknumber(id, 0)); + + if(off == 1) { + json_append_member(livolo_switch->message, "off", json_mknumber(off, 0)); + } else { + json_append_member(livolo_switch->message, "key", json_mknumber(key, 0)); + } +} + +static int createCode(JsonNode *code) { + int id = -1; + int key = -1; + int off = 0; + int x = 0; + double itmp = -1; + + if(json_find_number(code, "id", &itmp) == 0) + id = (int)round(itmp); + if(json_find_number(code, "key", &itmp) == 0) + key = (int)round(itmp); + if(json_find_number(code, "off", &itmp) == 0) { + off = 1; + key = 0; + } + + if(off > 0 && key > 0) { + logprintf(LOG_ERR, "livolo_switch: off and key cannot be combined"); + return EXIT_FAILURE; + } else if(id == -1 || (key == -1 && off == 0)) { + logprintf(LOG_ERR, "livolo_switch: insufficient number of arguments"); + return EXIT_FAILURE; + } else if(id < 0 || id > 65535) { + logprintf(LOG_ERR, "livolo_switch: invalid id range"); + return EXIT_FAILURE; + } else if((key < 1 || key > 10) && off == 0) { + logprintf(LOG_ERR, "livolo_switch: invalid key range"); + return EXIT_FAILURE; + } else { + createMessage(id, key, off); + + x = createHeader(); + x = createBits(id, x, ID_BITS); + x = createBits(createKeycode(key), x, UNIT_BITS); + livolo_switch->rawlen = x; + } + + return EXIT_SUCCESS; +} + +static void printHelp(void) { + printf("\t -i --id=id\t\t\tsend with id of the remote (0..65535)\n"); + printf("\t -n --key=number\t\t\tsend with key number on the remote (1..10)\n"); + printf("\t -f --off\t\t\tsend turn off code for the remote\n"); +} + +#if !defined(MODULE) && !defined(_WIN32) +__attribute__((weak)) +#endif +void livoloSwitchInit(void) { + protocol_register(&livolo_switch); + protocol_set_id(livolo_switch, "livolo_switch"); + protocol_device_add(livolo_switch, "livolo_switch", "Livolo Switches"); + livolo_switch->devtype = SWITCH; + livolo_switch->hwtype = RF433; + livolo_switch->txrpt = REPEATS; + livolo_switch->minrawlen = MIN_RAW_LENGTH; + livolo_switch->maxrawlen = MAX_RAW_LENGTH; + + options_add(&livolo_switch->options, 'i', "id", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, + // 16 bits: 0..65535 + "^([0-5]?[0-9]{1,4}|(6[0-4][0-9]{3})|(65[0-4][0-9]{2})|(655[0-2][0-9])|(6553[0-5]))$"); + options_add(&livolo_switch->options, 'n', "key", OPTION_OPT_VALUE, DEVICES_ID, JSON_NUMBER, NULL, + // 7 its: 0..127 + "^(([0-9]{1,2})|(1[0-1][0-9])|(12[0-7]))$"); + options_add(&livolo_switch->options, 'f', "off", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + + options_add(&livolo_switch->options, 0, "readonly", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, NULL, "^[01]$"); + options_add(&livolo_switch->options, 0, "confirm", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, NULL, "^[01]$"); + + livolo_switch->createCode=&createCode; + livolo_switch->printHelp=&printHelp; +} + +#if defined(MODULE) && !defined(_WIN32) +void compatibility(struct module_t *module) { + module->name = "livolo_switch"; + module->version = "0.1"; + module->reqversion = "7.0"; + module->reqcommit = NULL; +} + +void init(void) { + livoloSwitchInit(); +} +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/livolo_switch.h b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/livolo_switch.h new file mode 100755 index 0000000..f379df2 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/livolo_switch.h @@ -0,0 +1,27 @@ +/* + Copyright (C) 2017 Oleksii Serdiuk + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#ifndef _PROTOCOL_LIVOLO_SWITCH_H_ +#define _PROTOCOL_LIVOLO_SWITCH_H_ + +#include "../protocol.h" + +struct protocol_t *livolo_switch; +void livoloSwitchInit(void); + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/logilink_switch.c b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/logilink_switch.c new file mode 100755 index 0000000..96c53ac --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/logilink_switch.c @@ -0,0 +1,243 @@ +/* + Copyright (C) 2015 CurlyMo, Meloen and Scarala + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#include +#include +#include +#include + +#include "../../core/pilight.h" +#include "../../core/common.h" +#include "../../core/dso.h" +#include "../../core/log.h" +#include "../protocol.h" +#include "../../core/binary.h" +#include "../../core/gc.h" +#include "logilink_switch.h" + +#define PULSE_MULTIPLIER 3 +#define MIN_PULSE_LENGTH 279 +#define MAX_PULSE_LENGTH 289 +#define AVG_PULSE_LENGTH 284 +#define RAW_LENGTH 50 + +static int validate(void) { + if(logilink_switch->rawlen == RAW_LENGTH) { + if(logilink_switch->raw[logilink_switch->rawlen-1] >= (MIN_PULSE_LENGTH*PULSE_DIV) && + logilink_switch->raw[logilink_switch->rawlen-1] <= (MAX_PULSE_LENGTH*PULSE_DIV)) { + return 0; + } + } + + return -1; +} + +static void createMessage(int systemcode, int unitcode, int state) { + logilink_switch->message = json_mkobject(); + json_append_member(logilink_switch->message, "systemcode", json_mknumber(systemcode, 0)); + json_append_member(logilink_switch->message, "unitcode", json_mknumber(unitcode, 0)); + if(state == 0) { + json_append_member(logilink_switch->message, "state", json_mkstring("on")); + } else { + json_append_member(logilink_switch->message, "state", json_mkstring("off")); + } +} + +static void parseCode(void) { + int i = 0, x = 0, binary[RAW_LENGTH/2]; + int systemcode = 0, state = 0, unitcode = 0; + + if(logilink_switch->rawlen>RAW_LENGTH) { + logprintf(LOG_ERR, "logilink_switch: parsecode - invalid parameter passed %d", logilink_switch->rawlen); + return; + } + + for(x=0;xrawlen-1;x+=2) { + if(logilink_switch->raw[x] > (int)((double)AVG_PULSE_LENGTH*((double)PULSE_MULTIPLIER/2))) { + binary[i++] = 1; + } else { + binary[i++] = 0; + } + } + systemcode = binToDecRev(binary, 0, 19); + state = binary[20]; + unitcode = binToDecRev(binary, 21, 23); + + createMessage(systemcode, unitcode, state); +} + +static void createLow(int s, int e) { + int i = 0; + + for(i=s;i<=e;i+=2) { + logilink_switch->raw[i]=(AVG_PULSE_LENGTH); + logilink_switch->raw[i+1]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + } +} + +static void createHigh(int s, int e) { + int i = 0; + + for(i=s;i<=e;i+=2) { + logilink_switch->raw[i]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + logilink_switch->raw[i+1]=(AVG_PULSE_LENGTH); + } +} + +static void clearCode(void) { + createLow(0, logilink_switch->rawlen-2); +} + +static void createSystemCode(int systemcode) { + int binary[255]; + int length=0; + int i = 0, x = 38; + + length = decToBin(systemcode, binary); + for(i=length;i>=0;i--) { + if(binary[i] == 1) { + createHigh(x, x+1); + } + + x -= 2; + } +} + +static void createUnitCode(int unitcode) { + switch(unitcode) { + case 7: + createHigh(42, 47); // Button 1 + break; + case 3: + createLow(42, 43); // Button 2 + createHigh(44, 47); + break; + case 5: + createHigh(42, 43); // Button 3 + createLow(44, 45); + createHigh(46, 47); + break; + case 6: + createHigh(42, 45); // Button 4 + createLow(46, 47); + break; + case 0: + createLow(42, 47); // Button ALL OFF + break; + default: + break; + } +} + +static void createState(int state) { + if(state == 1) { + createLow(40, 41); + } else { + createHigh(40, 41); + } +} + +static void createFooter(void) { + logilink_switch->raw[48]=(AVG_PULSE_LENGTH); + logilink_switch->raw[49]=(PULSE_DIV*AVG_PULSE_LENGTH); +} + +static int createCode(struct JsonNode *code) { + int systemcode = -1; + int unitcode = -1; + int state = -1; + double itmp = 0; + + if(json_find_number(code, "systemcode", &itmp) == 0) + systemcode = (int)round(itmp); + if(json_find_number(code, "unitcode", &itmp) == 0) + unitcode = (int)round(itmp); + if(json_find_number(code, "off", &itmp) == 0) + state=1; + else if(json_find_number(code, "on", &itmp) == 0) + state=0; + + if(systemcode == -1 || unitcode == -1 || state == -1) { + logprintf(LOG_ERR, "logilink_switch: insufficient number of arguments"); + return EXIT_FAILURE; + } else if(systemcode > 2097151 || systemcode < 0) { + logprintf(LOG_ERR, "logilink_switch: invalid systemcode range"); + return EXIT_FAILURE; + } else if(unitcode > 7 || unitcode < 0) { + logprintf(LOG_ERR, "logilink_switch: invalid unitcode range"); + return EXIT_FAILURE; + } else { + createMessage(systemcode, unitcode, state); + clearCode(); + createSystemCode(systemcode); + createUnitCode(unitcode); + createState(state); + createFooter(); + logilink_switch->rawlen = RAW_LENGTH; + } + return EXIT_SUCCESS; +} + +static void printHelp(void) { + printf("\t -s --systemcode=systemcode\tcontrol a device with this systemcode\n"); + printf("\t -u --unitcode=unitcode\t\tcontrol a device with this unitcode\n"); + printf("\t -t --on\t\t\tsend an on signal\n"); + printf("\t -f --off\t\t\tsend an off signal\n"); +} + +#if !defined(MODULE) && !defined(_WIN32) +__attribute__((weak)) +#endif + +void logilinkSwitchInit(void) { + protocol_register(&logilink_switch); + protocol_set_id(logilink_switch, "logilink_switch"); + protocol_device_add(logilink_switch, "logilink_switch", "Logilink Switches"); + logilink_switch->devtype = SWITCH; + logilink_switch->hwtype = RF433; + logilink_switch->minrawlen = RAW_LENGTH; + logilink_switch->maxrawlen = RAW_LENGTH; + logilink_switch->maxgaplen = MAX_PULSE_LENGTH*PULSE_DIV; + logilink_switch->mingaplen = MIN_PULSE_LENGTH*PULSE_DIV; + + options_add(&logilink_switch->options, 's', "systemcode", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, NULL); + options_add(&logilink_switch->options, 'u', "unitcode", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^[0-7]$"); + options_add(&logilink_switch->options, 't', "on", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&logilink_switch->options, 'f', "off", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + + options_add(&logilink_switch->options, 0, "readonly", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + options_add(&logilink_switch->options, 0, "confirm", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + + logilink_switch->parseCode=&parseCode; + logilink_switch->createCode=&createCode; + logilink_switch->printHelp=&printHelp; + logilink_switch->validate=&validate; +} + +#if defined(MODULE) && !defined(_WIN32) +void compatibility(struct module_t *module) { + module->name = "logilink_sitch"; + module->version = "1.1"; + module->reqversion = "6.0"; + module->reqcommit = "84"; +} + +void init(void) { + logilinkSwitchInit(); +} +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/logilink_switch.h b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/logilink_switch.h new file mode 100755 index 0000000..373fef4 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/logilink_switch.h @@ -0,0 +1,27 @@ +/* + Copyright (C) 2015 CurlyMo, Meloen and Scarala + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#ifndef _PROTOCOL_LOGILINK_SWITCH_H_ +#define _PROTOCOL_LOGILINK_SWITCH_H_ + +#include "../protocol.h" + +struct protocol_t *logilink_switch; +void logilinkSwitchInit(void); + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/mumbi.c b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/mumbi.c new file mode 100755 index 0000000..67c6973 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/mumbi.c @@ -0,0 +1,234 @@ +/* + Copyright (C) 2014 CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#include +#include +#include +#include + +#include "../../core/pilight.h" +#include "../../core/common.h" +#include "../../core/dso.h" +#include "../../core/log.h" +#include "../protocol.h" +#include "../../core/binary.h" +#include "../../core/gc.h" +#include "mumbi.h" + +#define PULSE_MULTIPLIER 3 +#define MIN_PULSE_LENGTH 307 +#define MAX_PULSE_LENGTH 317 +#define AVG_PULSE_LENGTH 312 +#define RAW_LENGTH 50 + +static int validate(void) { + if(mumbi->rawlen == RAW_LENGTH) { + if(mumbi->raw[mumbi->rawlen-1] >= (MIN_PULSE_LENGTH*PULSE_DIV) && + mumbi->raw[mumbi->rawlen-1] <= (MAX_PULSE_LENGTH*PULSE_DIV)) { + return 0; + } + } + + return -1; +} + +static void createMessage(int systemcode, int unitcode, int state) { + mumbi->message = json_mkobject(); + json_append_member(mumbi->message, "systemcode", json_mknumber(systemcode, 0)); + json_append_member(mumbi->message, "unitcode", json_mknumber(unitcode, 0)); + if(state == 1) { + json_append_member(mumbi->message, "state", json_mkstring("on")); + } else { + json_append_member(mumbi->message, "state", json_mkstring("off")); + } +} + +static void parseCode(void) { + int binary[RAW_LENGTH/4], x = 0, i = 0; + + if(mumbi->rawlen>RAW_LENGTH) { + logprintf(LOG_ERR, "mumbi: parsecode - invalid parameter passed %d", mumbi->rawlen); + return; + } + + for(x=0;xrawlen-2;x+=4) { + if(mumbi->raw[x+3] > (int)((double)AVG_PULSE_LENGTH*((double)PULSE_MULTIPLIER/2))) { + binary[i++] = 1; + } else { + binary[i++] = 0; + } + } + + int systemcode = binToDec(binary, 0, 4); + int unitcode = binToDec(binary, 5, 9); + int state = binary[11]; + if(unitcode > 0) { + createMessage(systemcode, unitcode, state); + } +} + +static void createLow(int s, int e) { + int i; + + for(i=s;i<=e;i+=4) { + mumbi->raw[i]=(AVG_PULSE_LENGTH); + mumbi->raw[i+1]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + mumbi->raw[i+2]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + mumbi->raw[i+3]=(AVG_PULSE_LENGTH); + } +} + +static void createHigh(int s, int e) { + int i; + + for(i=s;i<=e;i+=4) { + mumbi->raw[i]=(AVG_PULSE_LENGTH); + mumbi->raw[i+1]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + mumbi->raw[i+2]=(AVG_PULSE_LENGTH); + mumbi->raw[i+3]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + } +} +static void clearCode(void) { + createLow(0,47); +} + +static void createSystemCode(int systemcode) { + int binary[255]; + int length = 0; + int i=0, x=0; + + length = decToBinRev(systemcode, binary); + for(i=0;i<=length;i++) { + if(binary[i]==1) { + x=i*4; + createHigh(x, x+3); + } + } +} + +static void createUnitCode(int unitcode) { + int binary[255]; + int length = 0; + int i=0, x=0; + + length = decToBinRev(unitcode, binary); + for(i=0;i<=length;i++) { + if(binary[i]==1) { + x=i*4; + createHigh(20+x, 20+x+3); + } + } +} + +static void createState(int state) { + if(state == 0) { + createHigh(44, 47); + } else { + createHigh(40, 43); + } +} + +static void createFooter(void) { + mumbi->raw[48]=(AVG_PULSE_LENGTH); + mumbi->raw[49]=(PULSE_DIV*AVG_PULSE_LENGTH); +} + +static int createCode(struct JsonNode *code) { + int systemcode = -1; + int unitcode = -1; + int state = -1; + double itmp = 0; + + if(json_find_number(code, "systemcode", &itmp) == 0) + systemcode = (int)round(itmp); + if(json_find_number(code, "unitcode", &itmp) == 0) + unitcode = (int)round(itmp); + if(json_find_number(code, "off", &itmp) == 0) + state=0; + else if(json_find_number(code, "on", &itmp) == 0) + state=1; + + if(systemcode == -1 || unitcode == -1 || state == -1) { + logprintf(LOG_ERR, "mumbi: insufficient number of arguments"); + return EXIT_FAILURE; + } else if(systemcode > 31 || systemcode < 0) { + logprintf(LOG_ERR, "mumbi: invalid systemcode range"); + return EXIT_FAILURE; + } else if(unitcode > 31 || unitcode < 0) { + logprintf(LOG_ERR, "mumbi: invalid unitcode range"); + return EXIT_FAILURE; + } else { + createMessage(systemcode, unitcode, state); + clearCode(); + createSystemCode(systemcode); + createUnitCode(unitcode); + createState(state); + createFooter(); + mumbi->rawlen = RAW_LENGTH; + } + return EXIT_SUCCESS; +} + +static void printHelp(void) { + printf("\t -s --systemcode=systemcode\tcontrol a device with this systemcode\n"); + printf("\t -u --unitcode=unitcode\t\tcontrol a device with this unitcode\n"); + printf("\t -t --on\t\t\tsend an on signal\n"); + printf("\t -f --off\t\t\tsend an off signal\n"); +} + +#if !defined(MODULE) && !defined(_WIN32) +__attribute__((weak)) +#endif +void mumbiInit(void) { + + protocol_register(&mumbi); + protocol_set_id(mumbi, "mumbi"); + protocol_device_add(mumbi, "mumbi", "Mumbi Switches"); + mumbi->devtype = SWITCH; + mumbi->hwtype = RF433; + mumbi->minrawlen = RAW_LENGTH; + mumbi->maxrawlen = RAW_LENGTH; + mumbi->maxgaplen = MAX_PULSE_LENGTH*PULSE_DIV; + mumbi->mingaplen = MIN_PULSE_LENGTH*PULSE_DIV; + + options_add(&mumbi->options, 's', "systemcode", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^(3[012]?|[012][0-9]|[0-9]{1})$"); + options_add(&mumbi->options, 'u', "unitcode", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^(3[012]?|[012][0-9]|[0-9]{1})$"); + options_add(&mumbi->options, 't', "on", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&mumbi->options, 'f', "off", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + + options_add(&mumbi->options, 0, "readonly", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + options_add(&mumbi->options, 0, "confirm", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + + mumbi->parseCode=&parseCode; + mumbi->createCode=&createCode; + mumbi->printHelp=&printHelp; + mumbi->validate=&validate; +} + +#if defined(MODULE) && !defined(_WIN32) +void compatibility(struct module_t *module) { + module->name = "mumbi"; + module->version = "2.4"; + module->reqversion = "6.0"; + module->reqcommit = "84"; +} + +void init(void) { + mumbiInit(); +} +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/mumbi.h b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/mumbi.h new file mode 100755 index 0000000..5ca74e2 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/mumbi.h @@ -0,0 +1,27 @@ +/* + Copyright (C) 2014 CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#ifndef _PROTOCOL_MUMBI_H_ +#define _PROTOCOL_MUMBI_H_ + +#include "../protocol.h" + +struct protocol_t *mumbi; +void mumbiInit(void); + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/ninjablocks_weather.c b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/ninjablocks_weather.c new file mode 100755 index 0000000..b4bda11 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/ninjablocks_weather.c @@ -0,0 +1,246 @@ +/* + Copyright (C) 2014 CurlyMo & wo_rasp + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#include +#include +#include +#include + +#include "../../core/pilight.h" +#include "../../core/common.h" +#include "../../core/dso.h" +#include "../../core/log.h" +#include "../protocol.h" +#include "../../core/binary.h" +#include "../../core/gc.h" +#include "ninjablocks_weather.h" + +#define PULSE_MULTIPLIER 2 +#define MIN_PULSE_LENGTH 2115 +#define MAX_PULSE_LENGTH 2125 +#define AVG_PULSE_LENGTH 2120 +#define MIN_RAW_LENGTH 41 +#define MAX_RAW_LENGTH 70 +#define RAW_LENGTH 50 + +#define PULSE_NINJA_WEATHER_SHORT 1000 +#define PULSE_NINJA_WEATHER_LONG 2000 +#define PULSE_NINJA_WEATHER_FOOTER AVG_PULSE_LENGTH // 72080/PULSE_DIV +#define PULSE_NINJA_WEATHER_LOWER 750 // SHORT*0,75 +#define PULSE_NINJA_WEATHER_UPPER 1250 // SHORT * 1,25 + +typedef struct settings_t { + double id; + double unit; + double temp; + double humi; + struct settings_t *next; +} settings_t; + +static struct settings_t *settings = NULL; + +static int validate(void) { + if(ninjablocks_weather->rawlen >= MIN_RAW_LENGTH && ninjablocks_weather->rawlen <= MAX_RAW_LENGTH) { + if(ninjablocks_weather->raw[ninjablocks_weather->rawlen-1] >= (MIN_PULSE_LENGTH*PULSE_DIV) && + ninjablocks_weather->raw[ninjablocks_weather->rawlen-1] <= (MAX_PULSE_LENGTH*PULSE_DIV)) { + return 0; + } + } + + return -1; +} + +static void createMessage(int id, int unit, double temperature, double humidity) { + ninjablocks_weather->message = json_mkobject(); + json_append_member(ninjablocks_weather->message, "id", json_mknumber(id, 0)); + json_append_member(ninjablocks_weather->message, "unit", json_mknumber(unit, 0)); + json_append_member(ninjablocks_weather->message, "temperature", json_mknumber(temperature/100, 2)); + json_append_member(ninjablocks_weather->message, "humidity", json_mknumber(humidity, 0)); +} + +static void parseCode(void) { + int x = 0, pRaw = 0, binary[MAX_RAW_LENGTH/2]; + int iParity = 1, iParityData = -1; // init for even parity + int iHeaderSync = 12; // 1100 + int iDataSync = 6; // 110 + double temp_offset = 0.0; + double humi_offset = 0.0; + + if(ninjablocks_weather->rawlen>MAX_RAW_LENGTH) { + logprintf(LOG_ERR, "ninjablocks_weather: parsecode - invalid parameter passed %d", ninjablocks_weather->rawlen); + return; + } + + // Decode Biphase Mark Coded Differential Manchester (BMCDM) pulse stream into binary + for(x=0; x<=(MAX_RAW_LENGTH/2); x++) { + if(ninjablocks_weather->raw[pRaw] > PULSE_NINJA_WEATHER_LOWER && + ninjablocks_weather->raw[pRaw] < PULSE_NINJA_WEATHER_UPPER) { + binary[x] = 1; + iParityData = iParity; + iParity = -iParity; + pRaw++; + } else { + binary[x] = 0; + } + pRaw++; + } + if(iParityData < 0) { + iParityData = 0; + } + + // Binary record: 0-3 sync0, 4-7 unit, 8-9 id, 10-12 sync1, 13-19 humidity, 20-34 temperature, 35 even par, 36 footer + int headerSync = binToDecRev(binary, 0,3); + int unit = binToDecRev(binary, 4,7); + int id = binToDecRev(binary, 8,9); + int dataSync = binToDecRev(binary, 10,12); + double humidity = binToDecRev(binary, 13,19); // % + double temperature = binToDecRev(binary, 20,34); + // ((temp * (100 / 128)) - 5000) * 10 °C, 2 digits + temperature = ((int)((double)(temperature * 0.78125)) - 5000); + + struct settings_t *tmp = settings; + while(tmp) { + if(fabs(tmp->id-id) < EPSILON && fabs(tmp->unit-unit) < EPSILON) { + humi_offset = tmp->humi; + temp_offset = tmp->temp; + break; + } + tmp = tmp->next; + } + + temperature += temp_offset; + humidity += humi_offset; + + if(iParityData == 0 && (iHeaderSync == headerSync || dataSync == iDataSync)) { + createMessage(id, unit, temperature, humidity); + } +} + +static int checkValues(struct JsonNode *jvalues) { + struct JsonNode *jid = NULL; + + if((jid = json_find_member(jvalues, "id"))) { + struct settings_t *snode = NULL; + struct JsonNode *jchild = NULL; + struct JsonNode *jchild1 = NULL; + double unit = -1, id = -1; + int match = 0; + + jchild = json_first_child(jid); + while(jchild) { + jchild1 = json_first_child(jchild); + while(jchild1) { + if(strcmp(jchild1->key, "unit") == 0) { + unit = jchild1->number_; + } + if(strcmp(jchild1->key, "id") == 0) { + id = jchild1->number_; + } + jchild1 = jchild1->next; + } + jchild = jchild->next; + } + + struct settings_t *tmp = settings; + while(tmp) { + if(fabs(tmp->id-id) < EPSILON && fabs(tmp->unit-unit) < EPSILON) { + match = 1; + break; + } + tmp = tmp->next; + } + + if(match == 0) { + if((snode = MALLOC(sizeof(struct settings_t))) == NULL) { + fprintf(stderr, "out of memory\n"); + exit(EXIT_FAILURE); + } + snode->id = id; + snode->unit = unit; + snode->temp = 0; + snode->humi = 0; + + json_find_number(jvalues, "temperature-offset", &snode->temp); + json_find_number(jvalues, "humidity-offset", &snode->humi); + + snode->next = settings; + settings = snode; + } + } + return 0; +} + +static void gc(void) { + struct settings_t *tmp = NULL; + while(settings) { + tmp = settings; + settings = settings->next; + FREE(tmp); + } + if(settings != NULL) { + FREE(settings); + } +} + +#if !defined(MODULE) && !defined(_WIN32) +__attribute__((weak)) +#endif +void ninjablocksWeatherInit(void) { + + protocol_register(&ninjablocks_weather); + protocol_set_id(ninjablocks_weather, "ninjablocks_weather"); + protocol_device_add(ninjablocks_weather, "ninjablocks_weather", "Ninjablocks Weather Sensors"); + ninjablocks_weather->devtype = WEATHER; + ninjablocks_weather->hwtype = RF433; + ninjablocks_weather->minrawlen = MIN_RAW_LENGTH; + ninjablocks_weather->maxrawlen = MAX_RAW_LENGTH; + ninjablocks_weather->mingaplen = MIN_PULSE_LENGTH*PULSE_DIV; + ninjablocks_weather->maxgaplen = MAX_PULSE_LENGTH*PULSE_DIV; + + // sync-id[4]; Homecode[4], Channel Code[2], Sync[3], Humidity[7], Temperature[15], Footer [1] + options_add(&ninjablocks_weather->options, 'u', "unit", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^([0-9]|1[0-5])$"); + options_add(&ninjablocks_weather->options, 'i', "id", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^([0-3])$"); + options_add(&ninjablocks_weather->options, 't', "temperature", OPTION_HAS_VALUE, DEVICES_VALUE, JSON_NUMBER, NULL, "^[0-9]{1,5}$"); + options_add(&ninjablocks_weather->options, 'h', "humidity", OPTION_HAS_VALUE, DEVICES_VALUE, JSON_NUMBER, NULL, "^[0-9]{1,5}$"); + + // options_add(&ninjablocks_weather->options, 0, "decimals", OPTION_HAS_VALUE, DEVICES_SETTING, JSON_NUMBER, (void *)2, "[0-9]"); + options_add(&ninjablocks_weather->options, 0, "temperature-decimals", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)2, "[0-9]"); + options_add(&ninjablocks_weather->options, 0, "humidity-decimals", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)2, "[0-9]"); + options_add(&ninjablocks_weather->options, 0, "readonly", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + options_add(&ninjablocks_weather->options, 0, "show-humidity", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)1, "^[10]{1}$"); + options_add(&ninjablocks_weather->options, 0, "show-temperature", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)1, "^[10]{1}$"); + + ninjablocks_weather->parseCode=&parseCode; + ninjablocks_weather->checkValues=&checkValues; + ninjablocks_weather->validate=&validate; + ninjablocks_weather->gc=&gc; +} + +#if defined(MODULE) && !defined(_WIN32) +void compatibility(struct module_t *module) { + module->name = "ninjablocks_weather"; + module->version = "1.2"; + module->reqversion = "7.0"; + module->reqcommit = "84"; +} + +void init(void) { + ninjablocksWeatherInit(); +} +#endif + diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/ninjablocks_weather.h b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/ninjablocks_weather.h new file mode 100755 index 0000000..0ccc116 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/ninjablocks_weather.h @@ -0,0 +1,28 @@ +/* + Copyright (C) 2014 CurlyMo & wo-rasp + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#ifndef _PROTOCOL_NINJABLOCKS_WEATHER_H_ +#define _PROTOCOL_NINJABLOCKS_WEATHER_H_ + +#include "../protocol.h" + +struct protocol_t *ninjablocks_weather; +void ninjablocksWeatherInit(void); + +#endif + diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/pollin.c b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/pollin.c new file mode 100755 index 0000000..6cc305e --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/pollin.c @@ -0,0 +1,230 @@ +/* + Copyright (C) 2014 CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#include +#include +#include +#include + +#include "../../core/pilight.h" +#include "../../core/common.h" +#include "../../core/dso.h" +#include "../../core/log.h" +#include "../protocol.h" +#include "../../core/binary.h" +#include "../../core/gc.h" +#include "pollin.h" + +#define PULSE_MULTIPLIER 3 +#define MIN_PULSE_LENGTH 296 +#define MAX_PULSE_LENGTH 306 +#define AVG_PULSE_LENGTH 301 +#define RAW_LENGTH 50 + +static int validate(void) { + if(pollin->rawlen == RAW_LENGTH) { + if(pollin->raw[pollin->rawlen-1] >= (MIN_PULSE_LENGTH*PULSE_DIV) && + pollin->raw[pollin->rawlen-1] <= (MAX_PULSE_LENGTH*PULSE_DIV)) { + return 0; + } + } + + return -1; +} + +static void createMessage(int systemcode, int unitcode, int state) { + pollin->message = json_mkobject(); + json_append_member(pollin->message, "systemcode", json_mknumber(systemcode, 0)); + json_append_member(pollin->message, "unitcode", json_mknumber(unitcode, 0)); + if(state == 0) { + json_append_member(pollin->message, "state", json_mkstring("on")); + } else { + json_append_member(pollin->message, "state", json_mkstring("off")); + } +} + +static void parseCode(void) { + int binary[RAW_LENGTH/4], x = 0, i = 0; + + if(pollin->rawlen>RAW_LENGTH) { + logprintf(LOG_ERR, "pollin: parsecode - invalid parameter passed %d", pollin->rawlen); + return; + } + + for(x=0;xrawlen-2;x+=4) { + if(pollin->raw[x+3] > (int)((double)AVG_PULSE_LENGTH*((double)PULSE_MULTIPLIER/2))) { + binary[i++] = 1; + } else { + binary[i++] = 0; + } + } + + int systemcode = binToDec(binary, 0, 4); + int unitcode = binToDec(binary, 5, 9); + int state = binary[11]; + createMessage(systemcode, unitcode, state); +} + +static void createLow(int s, int e) { + int i; + + for(i=s;i<=e;i+=4) { + pollin->raw[i]=(AVG_PULSE_LENGTH); + pollin->raw[i+1]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + pollin->raw[i+2]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + pollin->raw[i+3]=(AVG_PULSE_LENGTH); + } +} + +static void createHigh(int s, int e) { + int i; + + for(i=s;i<=e;i+=4) { + pollin->raw[i]=(AVG_PULSE_LENGTH); + pollin->raw[i+1]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + pollin->raw[i+2]=(AVG_PULSE_LENGTH); + pollin->raw[i+3]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH); + } +} +static void clearCode(void) { + createLow(0,47); +} + +static void createSystemCode(int systemcode) { + int binary[255]; + int length = 0; + int i=0, x=0; + + length = decToBinRev(systemcode, binary); + for(i=0;i<=length;i++) { + if(binary[i]==1) { + x=i*4; + createHigh(x, x+3); + } + } +} + +static void createUnitCode(int unitcode) { + int binary[255]; + int length = 0; + int i=0, x=0; + + length = decToBinRev(unitcode, binary); + for(i=0;i<=length;i++) { + if(binary[i]==1) { + x=i*4; + createHigh(20+x, 20+x+3); + } + } +} + +static void createState(int state) { + if(state == 1) { + createHigh(44, 47); + } +} + +static void createFooter(void) { + pollin->raw[48]=(AVG_PULSE_LENGTH); + pollin->raw[49]=(PULSE_DIV*AVG_PULSE_LENGTH); +} + +static int createCode(struct JsonNode *code) { + int systemcode = -1; + int unitcode = -1; + int state = -1; + double itmp = -1; + + if(json_find_number(code, "systemcode", &itmp) == 0) + systemcode = (int)round(itmp); + if(json_find_number(code, "unitcode", &itmp) == 0) + unitcode = (int)round(itmp); + if(json_find_number(code, "off", &itmp) == 0) + state=1; + else if(json_find_number(code, "on", &itmp) == 0) + state=0; + + if(systemcode == -1 || unitcode == -1 || state == -1) { + logprintf(LOG_ERR, "pollin: insufficient number of arguments"); + return EXIT_FAILURE; + } else if(systemcode > 31 || systemcode < 0) { + logprintf(LOG_ERR, "pollin: invalid systemcode range"); + return EXIT_FAILURE; + } else if(unitcode > 31 || unitcode < 0) { + logprintf(LOG_ERR, "pollin: invalid unitcode range"); + return EXIT_FAILURE; + } else { + createMessage(systemcode, unitcode, state); + clearCode(); + createSystemCode(systemcode); + createUnitCode(unitcode); + createState(state); + createFooter(); + pollin->rawlen = RAW_LENGTH; + } + return EXIT_SUCCESS; +} + +static void printHelp(void) { + printf("\t -s --systemcode=systemcode\tcontrol a device with this systemcode\n"); + printf("\t -u --unitcode=unitcode\t\tcontrol a device with this unitcode\n"); + printf("\t -t --on\t\t\tsend an on signal\n"); + printf("\t -f --off\t\t\tsend an off signal\n"); +} + +#if !defined(MODULE) && !defined(_WIN32) +__attribute__((weak)) +#endif +void pollinInit(void) { + + protocol_register(&pollin); + protocol_set_id(pollin, "pollin"); + protocol_device_add(pollin, "pollin", "Pollin Switches"); + pollin->devtype = SWITCH; + pollin->hwtype = RF433; + pollin->minrawlen = RAW_LENGTH; + pollin->maxrawlen = RAW_LENGTH; + pollin->maxgaplen = MAX_PULSE_LENGTH*PULSE_DIV; + pollin->mingaplen = MIN_PULSE_LENGTH*PULSE_DIV; + + options_add(&pollin->options, 's', "systemcode", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^(3[012]?|[012][0-9]|[0-9]{1})$"); + options_add(&pollin->options, 'u', "unitcode", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^(3[012]?|[012][0-9]|[0-9]{1})$"); + options_add(&pollin->options, 't', "on", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + options_add(&pollin->options, 'f', "off", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL); + + options_add(&pollin->options, 0, "readonly", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + options_add(&pollin->options, 0, "confirm", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$"); + + pollin->parseCode=&parseCode; + pollin->createCode=&createCode; + pollin->printHelp=&printHelp; + pollin->validate=&validate; +} + +#if defined(MODULE) && !defined(_WIN32) +void compatibility(struct module_t *module) { + module->name = "pollin"; + module->version = "2.4"; + module->reqversion = "6.0"; + module->reqcommit = "84"; +} + +void init(void) { + pollinInit(); +} +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/pollin.h b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/pollin.h new file mode 100755 index 0000000..2707d45 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/pollin.h @@ -0,0 +1,27 @@ +/* + Copyright (C) 2014 CurlyMo + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ + +#ifndef _PROTOCOL_POLLIN_H_ +#define _PROTOCOL_POLLIN_H_ + +#include "../protocol.h" + +struct protocol_t *pollin; +void pollinInit(void); + +#endif diff --git a/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/quigg_gt1000.c b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/quigg_gt1000.c new file mode 100755 index 0000000..9bced65 --- /dev/null +++ b/lib/ESPiLight/src/pilight/libs/pilight/protocols/433.92/quigg_gt1000.c @@ -0,0 +1,430 @@ +/* + Copyright (C) 2015 CurlyMo & wo_rasp & RvW + + This file is part of pilight. + + pilight 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. + + pilight 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 pilight. If not, see +*/ +/* Protocol quigg_gt1000 is an implementation of a protocol for the Quigg GT-FSI-08 switches + with GT-1000 remote. It is believed that they are compatible with the Lidl and Brennenstuhl outlets + RvW, jan 2015 + This implementation mimics the GT-1000 remote control with extensions + These switches listen to the next command string of 24 bit: + |-|0-----------4|5---------20|21------24| + |S| group-id[4] | random[16] |unit-id[4]| + |-|-------------|------------|----------| + where: + S=Start, a short high pulse of 350 us followed by a low of 2350 us + group-id[4]= 4 logic bits for the group-id code, range 0..15 + unit-id[4]= 4 bits for the unit code for switches 1..4. Two different coding schemes are used + random[16]= a 16 bit sequence where no bits seems to have a specific function. Each action + for each switch uses 4 codes (40 in total for one group-id) that are transmitted in a rolling sequence by the RC + Although codes are reused within a single group, for the sake of code simplicity and readability + this implementation will not use this fact. So we will implement a table with 16*40 = 640 "random" codes + + A logic bit is encoded by the position of the high to low transition in the 1540 us bit window. + A logic 1 therefore is encoded by a mark(high) of 1100 us followed by a space(low) of 440 us. + A logic 0 is encoded by a mark of 330 us followed by a space of 1210 us + + The total message consists of 24 bit + start pulse, and has therefore a duration 39660 us + There is no space between the repetition of a message, so the next message immediately follows the previous message + + To program a switch the RC uses a second type of start pulse with a mark(high) of 3000us and a space(low) of 7300us + In order to be able to program switches from pilight, this implementation mimics this behavior by + transmitting two normal code sequences followed by a program sequence with this long start pulse. + This implies that the complete message in fact consists of 75 bits and a short footer. +*/ + +#include +#include +#include +#include + +#include "../../core/pilight.h" +#include "../../core/common.h" +#include "../../core/dso.h" +#include "../../core/log.h" +#include "../protocol.h" +#include "../../core/binary.h" +#include "../../core/gc.h" +#include "quigg_gt1000.h" + +/* define the basic protocol Mark-Space pulse width and other values*/ +#define START_MARK 350 +#define START_SPACE 2340 +#define PROG_MARK 3000 +#define PROG_SPACE 7300 +#define SHORT_MARK 330 +#define LONG_SPACE 1210 +#define LONG_MARK 1100 +#define SHORT_SPACE 440 +#define FOOTER_MARK 200 + +#define LEARN_REPEATS 40 +#define NORMAL_REPEATS 10 +#define PULSE_MULTIPLIER 2 +#define MIN_PULSE_LENGTH 320 +#define MAX_PULSE_LENGTH 340 +#define AVG_PULSE_LENGTH 330 +#define RAW_LENGTH 151 +#define BIN_LENGTH 24 +/* +// Support Rx +static int validate(void) { + if(quigg_gt1000->rawlen == RAW_LENGTH) { + if(quigg_gt1000->raw[0] >= (int)(START_MARK*0.9) && + quigg_gt1000->raw[0] <= (int)(START_MARK*1.1) && + quigg_gt1000->raw[1] >= (int)(START_SPACE*0.9) && + quigg_gt1000->raw[1] <= (int)(START_SPACE*1.1)) { + return 0; + } + } + return -1; +} +*/ +int codetab[16][40] = { + /* the next table contains the random codes (bits 4..19) of the 4 code sequences for + * each off-on action for each switch 0..3 in each group-id 0..15. + * For each group-id a separate array is created where de codes are stored in the next order: + * off-codes 0..3 for switch 0, on-codes 0..3 for switch 0, + * off-codes 0..3 for switch 1, on-codes 0..3 for switch 1, + * off-codes 0..3 for switch 2, on-codes 0..3 for switch 2, + * off-codes 0..3 for switch 3, on-codes 0..3 for switch 3, + * off-codes 0..3 for all, on-codes 0..3 for all + * If a code is unknown 0 is substituted, f.e. for group-id 0 no codes are known, so all 40 values are 0 + */ + /* code 0 */ + { 0xBB42,0x4301,0x00F9,0x8EBA, 0xE4C3,0x1924,0x6CDB,0x9D57, + 0x00F9,0x4301,0x00F9,0x8EBA, 0x9D57,0xE4C3,0x6CDB,0x1924, + 0x8EBA,0x00F9,0x4301,0xBB42, 0x1924,0xE4C3,0x6CDB,0x9D57, + 0xE4C3,0x1924,0x9D57,0x6CDB, 0x8EBA,0xBB42,0x4301,0x00F9, + 0xE4C3,0x1924,0x9D57,0x6CDB, 0x4301,0x00F9,0x8EBA,0xBB42 }, + /* code 1 */ + { 0x616A,0xCF71,0x7AE2,0xB639, 0xF59B,0xE923,0x14C4,0x32A7, + 0x616A,0xB639,0xCF71,0x7AE2, 0x14C4,0x32A7,0xF59B,0xE923, + 0xCF71,0x616A,0x7AE2,0xB639, 0xE923,0xF59B,0x14C4,0x32A7, + 0x14C4,0xE923,0xF59B,0x32A7, 0x7AE2,0x616A,0xCF71,0xB639, + 0x14C4,0xE923,0xF59B,0x32A7, 0x7AE2,0x616A,0xB639,0xCF71 }, + /* code 2 */ + { 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 }, + /* code 3 */ + { 0x32D7,0x7D4B,0x9482,0xC818, 0x067A,0x2901,0xB1B6,0xEA35, + 0x5B5C,0x6364,0x85F9,0xFEED, 0x1FA0,0x4CCF,0xA793,0xD02E, + 0x067A,0x2901,0xB1B6,0xEA35, 0x32D7,0x7D4B,0x9482,0xC818, + 0x1FA0,0x4CCF,0xA793,0xD02E, 0x5B5C,0x6364,0x85F9,0xFEED, + 0x067A,0x2901,0xB1B6,0xEA35, 0x32D7,0x7D4B,0x9482,0xC818 }, + /* code 4 */ + { 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 }, + /* code 5 */ + { 0x23C2,0x71A1,0x88DA,0xDC19, 0x1FBB,0x39E4,0xA743,0xFE77, + 0x23C2,0x71A1,0x88DA,0xDC19, 0x1FBB,0x39E4,0xA743,0xFE77, + 0x23C2,0x71A1,0x88DA,0xDC19, 0x1FBB,0x39E4,0xA743,0xFE77, + 0x1FBB,0x39E4,0xA743,0xFE77, 0x23C2,0x71A1,0x88DA,0xDC19, + 0x1FBB,0x39E4,0xA743,0xFE77, 0x23C2,0x71A1,0x88DA,0xDC19 }, + /* code 6 */ + { 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 }, + /* code 7 */ + { 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 }, + /* code 8 */ + { 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 }, + /* code 9 */ + { 0x1B1A,0x27D9,0x75C1,0xC0A2, 0x02FB,0x3D03,0x6EE7,0xB974, + 0x1B1A,0x27D9,0x75C1,0xC0A2, 0x02FB,0x3D03,0x6EE7,0xB974, + 0x1B1A,0x27D9,0x75C1,0xC0A2, 0x02FB,0x3D03,0x6EE7,0xB974, + 0x02FB,0x3D03,0x6EE7,0xB974, 0x1B1A,0x27D9,0x75C1,0xC0A2, + 0x02FB,0x3D03,0x6EE7,0xB974, 0x1B1A,0x27D9,0x75C1,0xC0A2 }, + /* code 10 */ + { 0x9E98,0xC2AB,0xE057,0xF4F2, 0x460A,0x6971,0x77E6,0xBB25, + 0x0CBD,0x1589,0x51C4,0x8FDC, 0x2313,0x386F,0xAD3E,0xDA40, + 0x460A,0x6971,0x77E6,0xBB25, 0x9E98,0xC2AB,0xE057,0xF4F2, + 0x2313,0x386F,0xAD3E,0xDA40, 0x0CBD,0x1589,0x51C4,0x8FDC, + 0x460A,0x6971,0x77E6,0xBB25, 0x9E98,0xC2AB,0xE057,0xF4F2 }, + /* code 11 */ + { 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 }, + /* code 12 */ + { 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 }, + /* code 13 */ + { 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 }, + /* code 14 */ + { 0xE692,0x98F8,0x577B,0xABB7, 0x0AD6,0xBDA1,0xC465,0x6F4A, + 0xF234,0x89EC,0x2519,0x715D, 0x1383,0xDC2F,0x40CE,0x3E00, + 0x0AD6,0xBDA1,0xC465,0x6F4A, 0xE692,0x98F8,0x577B,0xABB7, + 0x1383,0xDC2F,0x40CE,0x3E00, 0xF234,0x89EC,0x2519,0x715D, + 0x0AD6,0xBDA1,0xC465,0x6F4A, 0xE692,0x98F8,0x577B,0xABB7 }, + /* code 15 */ + { 0x1067,0x3D02,0x9B1B,0, 0x2636,0x6C81,0xE5B5,0xF1FA, + 0x432D,0x54CC,0x8A79,0xC7D4, 0x7290,0xA85F,0xDFEE,0, + 0x2636,0x6C81,0xE5B5,0xF1FA, 0x1067,0x3D02,0x9B1B,0, + 0x7290,0xA85F,0xDFEE,0, 0x432D,0x54CC,0x8A79,0xC7D4, + 0x2636,0x6C81,0xE5B5,0xF1FA, 0x1067,0x3D02,0x9B1B,0 } +}; + +int unittab[2][5] = { + /* This table defines the coding of the unit-id or switch-id. Two coding schemes are used + * I call them first and second generation switches. They encode the unit-id in a different way. + */ + /* first gen group-id = 1, 5, 9, 12 */ + { 0x0, 0x4, 0xC, 0x2, 0xA }, /* these are 1st gen switch-codes for switch 0..3 and all */ + /* second gen all other */ + { 0xC, 0x5, 0xE, 0x7, 0x2 } /* 2nd gen switch-codes */ +}; + +int gentab[16] = + /* This table defines the switch generation for each group-id + * A 0 at location x means group-id x belongs to first generation, and a 1 means second generation + */ + /* for now only group-id 1,5,9 and 12 are first generation */ + {0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1}; + +#define NRSUPERMASK 0x07 +#define NRSUPERCODES (NRSUPERMASK+1) /* we will use 8 on and 8 off supercodes */ +/* make sure the number of Supercodes is always a power of 2, i.e. 2,4,8,16 ... */ +int supercodes[2*NRSUPERCODES] = { + /* since with supercodes group-id's are irrelevant, the codes consist of the first 20 bits of the code sequence */ + /* OFF codes */ + 0x0F000,0x1F006,0x4F00E,0x5F016,0x8F01E,0x9F026,0xCF02E,0xDF02F, + /* ON codes */ + 0x0F005,0x1F008,0x4F015,0x5F018,0x8F025,0x9F028,0xCF02C,0xDF03C }; + +char bincode[BIN_LENGTH+1]; + +static void createMessage(int id, int unit, int state, int seq, int learn) { + int i = 0; + + for(i=0;imessage = json_mkobject(); + json_append_member(quigg_gt1000->message, "id", json_mknumber(id, 0)); + json_append_member(quigg_gt1000->message, "unit", json_mknumber(unit, 0)); + json_append_member(quigg_gt1000->message, "seq", json_mknumber(seq, 0)); + if(state == 1) { + json_append_member(quigg_gt1000->message, "state", json_mkstring("on")); + } else { + json_append_member(quigg_gt1000->message, "state", json_mkstring("off")); + } + json_append_member(quigg_gt1000->message, "code", json_mkstring(bincode)); +} + +static int fillLow(int idx) { + /* fill in the mark-space code for a logic Low = short pulse*/ + quigg_gt1000->raw[idx++] = SHORT_MARK; + quigg_gt1000->raw[idx++] = LONG_SPACE; + return idx; +} + +static int fillHigh(int idx) { + /* fill in the mark-space code for a logic High = long pulse*/ + quigg_gt1000->raw[idx++] = LONG_MARK; + quigg_gt1000->raw[idx++] = SHORT_SPACE; + return idx; +} + +static void numtoBin(int idx, int num, int len) { + /* fill in bits idx to idx+len-1 for number, msb first */ + for(len--;len>=0;len--) { + bincode[idx+len] = (num & 1); /* value is lsb num */ + num = num>>1; /* shift number 1 place to right, so 2th bit becomes lsb */ + } +} + +static int fillRawCode(void) { + /* convert binary code in bincode[] to raw Mark-Space combis for this protocol */ + /* the complete RawCode consist of