changeset 408:28915b460db7

wip, optional args, bug fix toggle w/ transition
author Louis Opter <kalessin@kalessin.fr>
date Tue, 29 Dec 2015 18:40:32 +0100
parents 554a26aaa1df
children 6bc379a8f256
files add_power_transition.patch optional_jsonrpc_args.patch series
diffstat 3 files changed, 152 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/add_power_transition.patch	Tue Dec 29 14:43:27 2015 +0100
+++ b/add_power_transition.patch	Tue Dec 29 18:40:32 2015 +0100
@@ -1,5 +1,5 @@
 # HG changeset patch
-# Parent  bfa59be534ab65b5caff5b25d70a1d9fa961bfbb
+# Parent  87ea0f02cb6990de202d043b7ff05c9dbca83296
 Add a transition argument to the power functions
 
 diff --git a/CMakeLists.txt b/CMakeLists.txt
@@ -533,35 +533,6 @@
              false
          ),
          LGTD_JSONRPC_NODE(
-@@ -1103,23 +1169,23 @@
- {
-     static const struct lgtd_jsonrpc_method methods[] = {
-         LGTD_JSONRPC_METHOD(
--            "power_on", 1, // t
-+            "power_on", 2, // t, [transition]
-             lgtd_jsonrpc_check_and_call_power_on
-         ),
-         LGTD_JSONRPC_METHOD(
--            "power_off", 1, // t
-+            "power_off", 2, // t, [transition]
-             lgtd_jsonrpc_check_and_call_power_off
-         ),
-         LGTD_JSONRPC_METHOD(
--            "power_toggle", 1, // t
-+            "power_toggle", 2, // t, [transition]
-             lgtd_jsonrpc_check_and_call_power_toggle
-         ),
-         LGTD_JSONRPC_METHOD(
--            "set_light_from_hsbk", 6, // t, h, s, b, k, t
-+            "set_light_from_hsbk", 6, // t, h, s, b, k, [transition]
-             lgtd_jsonrpc_check_and_call_set_light_from_hsbk
-         ),
-         LGTD_JSONRPC_METHOD(
--            // t, waveform, h, s, b, k, period, cycles, skew_ratio, transient
-+            // t, waveform, h, s, b, k, period, cycles, skew_ratio, [transient]
-             "set_waveform", 10,
-             lgtd_jsonrpc_check_and_call_set_waveform
-         ),
 diff --git a/core/lightsd.c b/core/lightsd.c
 --- a/core/lightsd.c
 +++ b/core/lightsd.c
@@ -682,7 +653,7 @@
  
      struct lgtd_router_device_list *devices = NULL;
      devices = lgtd_router_targets_to_devices(targets);
-@@ -85,33 +131,58 @@
+@@ -85,33 +131,67 @@
          return;
      }
  
@@ -709,8 +680,11 @@
 +                ok = false;
 +                break;
 +            }
-+            lgtd_ieee8023mactoa(
-+                bulb->addr, target->target, LGTD_LIFX_ADDR_STRLEN
++            snprintf(
++                target->target, LGTD_LIFX_ADDR_STRLEN,
++                "%02x%02x%02x%02x%02x%02x",
++                bulb->addr[0], bulb->addr[1], bulb->addr[2], bulb->addr[3],
++                bulb->addr[4], bulb->addr[5]
 +            );
 +            SLIST_INSERT_HEAD(
 +                bulb->state.power ? &targets_to_turn_off : &targets_to_turn_on,
@@ -727,14 +701,20 @@
  
 -    SEND_RESULT(client, true);
 +    if (transition) {
-+        ok = lgtd_effect_power_transition(
-+            &targets_to_turn_on, LGTD_EFFECT_POWER_TRANSITION_ON, transition
-+        );
-+        lgtd_proto_target_list_clear(&targets_to_turn_on);
-+        ok = ok && lgtd_effect_power_transition(
-+            &targets_to_turn_off, LGTD_EFFECT_POWER_TRANSITION_ON, transition
-+        );
-+        lgtd_proto_target_list_clear(&targets_to_turn_off);
++        if (!SLIST_EMPTY(&targets_to_turn_on)) {
++            ok = ok && lgtd_effect_power_transition(
++                &targets_to_turn_on, LGTD_EFFECT_POWER_TRANSITION_ON, transition
++            );
++            lgtd_proto_target_list_clear(&targets_to_turn_on);
++        }
++        if (!SLIST_EMPTY(&targets_to_turn_off)) {
++            ok = ok && lgtd_effect_power_transition(
++                &targets_to_turn_off,
++                LGTD_EFFECT_POWER_TRANSITION_OFF,
++                transition
++            );
++            lgtd_proto_target_list_clear(&targets_to_turn_off);
++        }
 +    }
 +
 +    SEND_RESULT(client, ok);
@@ -1218,7 +1198,7 @@
 +// if you power off a bulb and set its color right away you'll see the bulb
 +// flicker, so let's delay the restore for a bit once the transition is done:
 +enum {
-+    LGTD_EFFECT_POWER_TRANSITION_OFF_RESTORE_LIGHT_COLOR_DELAY_MSECS = 300
++    LGTD_EFFECT_POWER_TRANSITION_OFF_RESTORE_LIGHT_COLOR_DELAY_MSECS = 250
 +};
 +
 +struct lgtd_effect_power_transition_target {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/optional_jsonrpc_args.patch	Tue Dec 29 18:40:32 2015 +0100
@@ -0,0 +1,129 @@
+# HG changeset patch
+# Parent  bfa59be534ab65b5caff5b25d70a1d9fa961bfbb
+Correctly support optional arguments in the JSON-RPC API
+
+diff --git a/core/jsonrpc.c b/core/jsonrpc.c
+--- a/core/jsonrpc.c
++++ b/core/jsonrpc.c
+@@ -459,7 +459,7 @@
+         }
+     }
+ 
+-    return si == schema_size;
++    return !objsize || (si < schema_size && schema[si].optional);
+ }
+ 
+ static bool
+@@ -716,7 +716,7 @@
+             offsetof(struct lgtd_jsonrpc_set_light_from_hsbk_args, t),
+             -1,
+             lgtd_jsonrpc_type_integer,
+-            false
++            true
+         ),
+     };
+ 
+@@ -854,7 +854,7 @@
+             offsetof(struct lgtd_jsonrpc_set_waveform_args, transient),
+             -1,
+             lgtd_jsonrpc_type_bool,
+-            false
++            true
+         ),
+     };
+ 
+@@ -1102,43 +1102,24 @@
+                           int *batch_sent)
+ {
+     static const struct lgtd_jsonrpc_method methods[] = {
++        LGTD_JSONRPC_METHOD("power_on", lgtd_jsonrpc_check_and_call_power_on),
++        LGTD_JSONRPC_METHOD("power_off", lgtd_jsonrpc_check_and_call_power_off),
+         LGTD_JSONRPC_METHOD(
+-            "power_on", 1, // t
+-            lgtd_jsonrpc_check_and_call_power_on
++            "power_toggle", lgtd_jsonrpc_check_and_call_power_toggle
+         ),
+         LGTD_JSONRPC_METHOD(
+-            "power_off", 1, // t
+-            lgtd_jsonrpc_check_and_call_power_off
+-        ),
+-        LGTD_JSONRPC_METHOD(
+-            "power_toggle", 1, // t
+-            lgtd_jsonrpc_check_and_call_power_toggle
+-        ),
+-        LGTD_JSONRPC_METHOD(
+-            "set_light_from_hsbk", 6, // t, h, s, b, k, t
++            "set_light_from_hsbk",
+             lgtd_jsonrpc_check_and_call_set_light_from_hsbk
+         ),
+         LGTD_JSONRPC_METHOD(
+-            // t, waveform, h, s, b, k, period, cycles, skew_ratio, transient
+-            "set_waveform", 10,
+-            lgtd_jsonrpc_check_and_call_set_waveform
++            "set_waveform", lgtd_jsonrpc_check_and_call_set_waveform
+         ),
+         LGTD_JSONRPC_METHOD(
+-            "get_light_state", 1, // t
+-            lgtd_jsonrpc_check_and_call_get_light_state
++            "get_light_state", lgtd_jsonrpc_check_and_call_get_light_state
+         ),
+-        LGTD_JSONRPC_METHOD(
+-            "tag", 2, // t, tag
+-            lgtd_jsonrpc_check_and_call_tag
+-        ),
+-        LGTD_JSONRPC_METHOD(
+-            "untag", 2, // t, tag
+-            lgtd_jsonrpc_check_and_call_untag
+-        ),
+-        LGTD_JSONRPC_METHOD(
+-            "set_label", 2, // t, label
+-            lgtd_jsonrpc_check_and_call_set_label
+-        )
++        LGTD_JSONRPC_METHOD("tag", lgtd_jsonrpc_check_and_call_tag),
++        LGTD_JSONRPC_METHOD("untag", lgtd_jsonrpc_check_and_call_untag),
++        LGTD_JSONRPC_METHOD("set_label", lgtd_jsonrpc_check_and_call_set_label)
+     };
+ 
+     if (batch_sent) {
+@@ -1172,15 +1153,11 @@
+             continue;
+         }
+         int diff = memcmp(
+-            methods[i].name, &client->json[request.method->start], methods[i].namelen
++            methods[i].name,
++            &client->json[request.method->start],
++            methods[i].namelen
+         );
+         if (!diff) {
+-            int params_count = request.params ? request.params->size : 0;
+-            if (params_count != methods[i].params_count) {
+-                error_code = LGTD_JSONRPC_INVALID_PARAMS;
+-                error_msg = "Invalid number of parameters";
+-                goto error;
+-            }
+             struct bufferevent *client_io = NULL; // keep compilers happy...
+             if (!request.id) {
+                 // Ugly hack to behave correctly on jsonrpc notifications, it's
+diff --git a/core/jsonrpc.h b/core/jsonrpc.h
+--- a/core/jsonrpc.h
++++ b/core/jsonrpc.h
+@@ -63,15 +63,13 @@
+ struct lgtd_jsonrpc_method {
+     const char  *name;
+     int         namelen;
+-    int         params_count;
+     void        (*method)(struct lgtd_client *);
+ };
+ 
+-#define LGTD_JSONRPC_METHOD(name_, params_count_, method_) {    \
+-    .name = (name_),                                            \
+-    .namelen = sizeof((name_)) -1,                              \
+-    .params_count = (params_count_),                            \
+-    .method = (method_)                                         \
++#define LGTD_JSONRPC_METHOD(name_, method_) {   \
++    .name = (name_),                            \
++    .namelen = sizeof((name_)) -1,              \
++    .method = (method_)                         \
+ }
+ 
+ enum lgtd_jsonrpc_error_code {
--- a/series	Tue Dec 29 14:43:27 2015 +0100
+++ b/series	Tue Dec 29 18:40:32 2015 +0100
@@ -1,3 +1,4 @@
+optional_jsonrpc_args.patch
 add_power_transition.patch
 network_discovery.patch
 make_gateway_write_callbacks_low_priority.patch #+future