changeset 246:f660399f8544

wip, rework the timer interface so we can get the bulb from our timer callback, since it might have been closed
author Louis Opter <kalessin@kalessin.fr>
date Sat, 15 Aug 2015 13:12:43 -0700
parents 1e99c8fb2b88
children 66dce016437f
files add_start_stop_timer.patch implement_some_metadata_packet_types.patch
diffstat 2 files changed, 35 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/add_start_stop_timer.patch	Sat Aug 15 02:51:45 2015 -0700
+++ b/add_start_stop_timer.patch	Sat Aug 15 13:12:43 2015 -0700
@@ -34,7 +34,7 @@
      if (lgtd_lifx_timer_context.discovery_timeout_ev) {
          event_del(lgtd_lifx_timer_context.discovery_timeout_ev);
          event_free(lgtd_lifx_timer_context.discovery_timeout_ev);
-@@ -207,3 +215,54 @@
+@@ -207,3 +215,55 @@
      lgtd_lifx_timer_discovery_timeout_event_callback(-1, 0, NULL);
      lgtd_debug("starting discovery timer");
  }
@@ -53,8 +53,9 @@
 +
 +bool
 +lgtd_lifx_timer_start_timer(int ms,
-+                            void (*cb)(struct lgtd_lifx_timer *, void *),
-+                            void *ctx)
++                            void (*cb)(struct lgtd_lifx_timer *,
++                                       union lgtd_lifx_timer_ctx),
++                            union lgtd_lifx_timer_ctx ctx)
 +{
 +    assert(ms > 0);
 +    assert(cb);
@@ -92,7 +93,7 @@
 diff --git a/lifx/timer.h b/lifx/timer.h
 --- a/lifx/timer.h
 +++ b/lifx/timer.h
-@@ -17,13 +17,29 @@
+@@ -17,13 +17,36 @@
  
  #pragma once
  
@@ -111,10 +112,16 @@
 +
 +struct event;
 +
++union lgtd_lifx_timer_ctx {
++    uint64_t    as_uint;
++    void        *as_ptr;
++};
++
 +struct lgtd_lifx_timer {
 +    LIST_ENTRY(lgtd_lifx_timer) link;
-+    void                        (*callback)(struct lgtd_lifx_timer *, void *);
-+    void                        *ctx;
++    void                        (*callback)(struct lgtd_lifx_timer *,
++                                            union lgtd_lifx_timer_ctx);
++    union lgtd_lifx_timer_ctx   ctx;
 +    struct event                *event;
 +};
 +LIST_HEAD(lgtd_lifx_timer_list, lgtd_lifx_timer);
@@ -124,6 +131,7 @@
  void lgtd_lifx_timer_start_watchdog(void);
  void lgtd_lifx_timer_start_discovery(void);
 +bool lgtd_lifx_timer_start_timer(int, // ms, & the timer is activated right away
-+                                 void (*)(struct lgtd_lifx_timer *, void *),
-+                                 void *);
++                                 void (*)(struct lgtd_lifx_timer *,
++                                          union lgtd_lifx_timer_ctx),
++                                 union lgtd_lifx_timer_ctx);
 +void lgtd_lifx_timer_stop_timer(struct lgtd_lifx_timer *);
--- a/implement_some_metadata_packet_types.patch	Sat Aug 15 02:51:45 2015 -0700
+++ b/implement_some_metadata_packet_types.patch	Sat Aug 15 13:12:43 2015 -0700
@@ -271,7 +271,7 @@
 diff --git a/lifx/bulb.c b/lifx/bulb.c
 --- a/lifx/bulb.c
 +++ b/lifx/bulb.c
-@@ -33,12 +33,51 @@
+@@ -33,12 +33,57 @@
  #include "bulb.h"
  #include "gateway.h"
  #include "core/daemon.h"
@@ -290,13 +290,19 @@
 +const char * const lgtd_lifx_bulb_ip_names[] = { "mcu", "wifi" };
 +
 +static void
-+lgtd_lifx_bulb_fetch_hardware_info(struct lgtd_lifx_timer *timer, void *ctx)
++lgtd_lifx_bulb_fetch_hardware_info(struct lgtd_lifx_timer *timer,
++                                   union lgtd_lifx_timer_ctx ctx)
 +{
 +    assert(timer);
-+    assert(ctx);
++    assert(ctx.as_uint);
 +
-+    bool stop = true;
-+    struct lgtd_lifx_bulb *bulb = ctx;
++    // Get the bulb again, it might have been closed while we were waiting:
++    const uint8_t *bulb_addr = (const uint8_t *)&ctx.as_uint;
++    struct lgtd_lifx_bulb *bulb = lgtd_lifx_bulb_get(bulb_addr);
++    if (!bulb) {
++        lgtd_lifx_timer_stop_timer(timer);
++        return;
++    }
 +
 +#define RESEND_IF(test, pkt_type) do {                      \
 +    if ((test)) {                                           \
@@ -305,6 +311,7 @@
 +    }                                                       \
 +} while (0)
 +
++    bool stop = true;
 +    RESEND_IF(!bulb->product_info.vendor_id, LGTD_LIFX_GET_VERSION);
 +    RESEND_IF(
 +        !bulb->ips[LGTD_LIFX_BULB_MCU_IP].fw_info.version,
@@ -314,7 +321,6 @@
 +        !bulb->ips[LGTD_LIFX_BULB_WIFI_IP].fw_info.version,
 +        LGTD_LIFX_GET_WIFI_FIRMWARE_STATE
 +    );
-+
 +    if (stop) {
 +        lgtd_lifx_timer_stop_timer(timer);
 +    }
@@ -323,20 +329,22 @@
  struct lgtd_lifx_bulb *
  lgtd_lifx_bulb_get(const uint8_t *addr)
  {
-@@ -68,6 +107,12 @@
+@@ -68,6 +113,14 @@
  
      bulb->last_light_state_at = lgtd_time_monotonic_msecs();
  
++    union lgtd_lifx_timer_ctx ctx = { .as_uint = 0 };
++    memcpy(&ctx.as_uint, addr, LGTD_LIFX_ADDR_LENGTH);
 +    lgtd_lifx_timer_start_timer(
 +        LGTD_LIFX_BULB_FETCH_HARDWARE_INFO_TIMER_MSECS,
 +        lgtd_lifx_bulb_fetch_hardware_info,
-+        bulb
++        ctx
 +    );
 +
      return bulb;
  }
  
-@@ -123,6 +168,7 @@
+@@ -123,6 +176,7 @@
          LGTD_STATS_ADD_AND_UPDATE_PROCTITLE(
              bulbs_powered_on, state->power == LGTD_LIFX_POWER_ON ? 1 : -1
          );
@@ -344,7 +352,7 @@
      }
  
      lgtd_lifx_gateway_update_tag_refcounts(bulb->gw, bulb->state.tags, state->tags);
-@@ -140,6 +186,7 @@
+@@ -140,6 +194,7 @@
          LGTD_STATS_ADD_AND_UPDATE_PROCTITLE(
              bulbs_powered_on, power == LGTD_LIFX_POWER_ON ? 1 : -1
          );
@@ -352,7 +360,7 @@
      }
  
      bulb->state.power = power;
-@@ -154,3 +201,53 @@
+@@ -154,3 +209,53 @@
  
      bulb->state.tags = tags;
  }