On Sat, 12 Jan 2013, Scott Talbert wrote:

Also, what do you think about moving the pre/post web function calls into update_firmware/config()?

In case you were having trouble visualizing :), the attached is what I was thinking. I think it makes the API just a little bit cleaner - all of the functionality related to updating the config or firmware can be done in a single call.

I also rewrote some of the get_stages stuff to be a bit more dynamic, rather than have a bunch of arrays that are hard coded.

Scott
Move website connectivity and set_time() functionality inside of
update_configuration and update_firmware.  Update API to add no_web parameter.
Rewrite get_stages functionality to be more dynamically generated rather than
lots of hard coded arrays.

Signed-off-by: Scott Talbert <s...@techie.net>

Index: concordance/concordance.c
===================================================================
RCS file: /cvsroot/concordance/concordance/concordance/concordance.c,v
retrieving revision 1.41.2.23
diff -u -p -r1.41.2.23 concordance.c
--- concordance/concordance.c   12 Jan 2013 23:50:32 -0000      1.41.2.23
+++ concordance/concordance.c   17 Jan 2013 04:27:32 -0000
@@ -792,33 +792,11 @@ int upload_config(struct options_t *opti
 {
        int err;
 
-       /*
-        * Tell the website we're going to start. This, it seems creates a
-        * session object on their side, because if you miss the pre-config
-        * communication, you get a no-session error.
-        */
-       if (!(*options).binary && !(*options).noweb) {
-               if ((err = post_preconfig(cb, cb_arg)))
-                       return err;
-       }
-
-       if ((err = update_configuration(cb, cb_arg,
-                       (*options).noreset))) {
-               return err;
-       }
-
-       /* Set the time, after a reboot */
-       if ((err = set_time(cb, NULL))) {
+       if ((err = update_configuration(cb, cb_arg, (*options).noreset,
+                       (*options).binary || (*options).noweb))) {
                return err;
        }
 
-       /* Tell the website we're done */
-       if (!(*options).binary && !(*options).noweb) {
-               if ((err = post_postconfig(cb, cb_arg))) {
-                       return err;
-               }
-       }
-
        return 0;
 }
 
@@ -867,14 +845,10 @@ int upload_firmware(struct options_t *op
        }
 
        if ((err = update_firmware(cb, cb_arg, (*options).noreset,
-                       (*options).direct)))
+                       (*options).direct,
+                       (*options).binary || (*options).noweb)))
                return err;
 
-       if (!(*options).binary && !(*options).noweb) {
-               if ((err = post_postfirmware(cb, cb_arg)))
-                       return err;
-       }
-
        return 0;
 }
 
Index: libconcord/libconcord.cpp
===================================================================
RCS file: /cvsroot/concordance/concordance/libconcord/libconcord.cpp,v
retrieving revision 1.42.2.24
diff -u -p -r1.42.2.24 libconcord.cpp
--- libconcord/libconcord.cpp   12 Jan 2013 23:54:55 -0000      1.42.2.24
+++ libconcord/libconcord.cpp   17 Jan 2013 04:27:33 -0000
@@ -34,6 +34,7 @@
 #include <zzip/lib.h>
 #include <list>
 #include <unistd.h>
+#include <vector>
 #include "libconcord.h"
 #include "lc_internal.h"
 #include "remote.h"
@@ -601,18 +602,8 @@ static const uint32_t update_configurati
        LC_CB_STAGE_ERASE_FLASH,
        LC_CB_STAGE_WRITE_CONFIG,
        LC_CB_STAGE_VERIFY_CONFIG,
-       LC_CB_STAGE_RESET,
 };
-static const int update_configuration_hid_num_stages = 6;
-
-static const uint32_t update_configuration_hid_noreset_stages[]={
-       LC_CB_STAGE_INITIALIZE_UPDATE,
-       LC_CB_STAGE_INVALIDATE_FLASH,
-       LC_CB_STAGE_ERASE_FLASH,
-       LC_CB_STAGE_WRITE_CONFIG,
-       LC_CB_STAGE_VERIFY_CONFIG,
-};
-static const int update_configuration_hid_noreset_num_stages = 5;
+static const int update_configuration_hid_num_stages = 5;
 
 static const uint32_t update_configuration_zwave_stages[]={
        LC_CB_STAGE_INITIALIZE_UPDATE,
@@ -621,33 +612,81 @@ static const uint32_t update_configurati
 };
 static const int update_configuration_zwave_num_stages = 3;
 
-static const uint32_t update_configuration_usbnet_stages[]={
-       LC_CB_STAGE_INITIALIZE_UPDATE,
-       LC_CB_STAGE_WRITE_CONFIG,
-       LC_CB_STAGE_FINALIZE_UPDATE,
-       LC_CB_STAGE_RESET,
-};
-static const int update_configuration_usbnet_num_stages = 4;
-
 static const uint32_t update_firmware_hid_stages[]={
        LC_CB_STAGE_INITIALIZE_UPDATE,
        LC_CB_STAGE_INVALIDATE_FLASH,
        LC_CB_STAGE_ERASE_FLASH,
        LC_CB_STAGE_WRITE_FIRMWARE,
        LC_CB_STAGE_FINALIZE_UPDATE,
-       LC_CB_STAGE_RESET,
-       LC_CB_STAGE_SET_TIME,
 };
-static const int update_firmware_hid_num_stages = 7;
+static const int update_firmware_hid_num_stages = 5;
 
 static const uint32_t update_firmware_hid_direct_stages[]={
        LC_CB_STAGE_INVALIDATE_FLASH,
        LC_CB_STAGE_ERASE_FLASH,
        LC_CB_STAGE_WRITE_FIRMWARE,
-       LC_CB_STAGE_RESET,
-       LC_CB_STAGE_SET_TIME,
 };
-static const int update_firmware_hid_direct_num_stages = 5;
+static const int update_firmware_hid_direct_num_stages = 3;
+
+std::vector<uint32_t> _get_update_config_stages(int noreset, int noweb)
+{
+       std::vector<uint32_t> stages;
+       uint32_t *base_stages;
+       int num_base_stages;
+
+       if (is_z_remote()) {
+               base_stages = (uint32_t*)update_configuration_zwave_stages;
+               num_base_stages = update_configuration_zwave_num_stages;
+       } else {
+               base_stages = (uint32_t*)update_configuration_hid_stages;
+               num_base_stages = update_configuration_hid_num_stages;
+       }
+
+       if (!noweb)
+               stages.push_back(LC_CB_STAGE_HTTP);
+
+       for (int i = 0; i < num_base_stages; i++)
+               stages.push_back(base_stages[i]);
+
+       if (!noreset && !(is_z_remote() && !is_usbnet_remote()))
+               stages.push_back(LC_CB_STAGE_RESET);
+
+       stages.push_back(LC_CB_STAGE_SET_TIME);
+
+       if (!noweb)
+               stages.push_back(LC_CB_STAGE_HTTP);
+
+       return stages;
+}
+
+std::vector<uint32_t> _get_update_firmware_stages(int noreset, int direct,
+       int noweb)
+{
+       std::vector<uint32_t> stages;
+       uint32_t *base_stages;
+       int num_base_stages;
+
+       if (direct) {
+               base_stages = (uint32_t*)update_firmware_hid_direct_stages;
+               num_base_stages = update_firmware_hid_direct_num_stages;
+       } else {
+               base_stages = (uint32_t*)update_firmware_hid_stages;
+               num_base_stages = update_firmware_hid_num_stages;
+       }
+
+       for (int i = 0; i < num_base_stages; i++)
+               stages.push_back(base_stages[i]);
+
+       if (!noreset && !(is_z_remote() && !is_usbnet_remote()))
+               stages.push_back(LC_CB_STAGE_RESET);
+
+       stages.push_back(LC_CB_STAGE_SET_TIME);
+
+       if (!noweb)
+               stages.push_back(LC_CB_STAGE_HTTP);
+
+       return stages;
+}
 
 /*
  * GENERAL REMOTE STUFF
@@ -893,13 +932,14 @@ int get_time()
        return 0;
 }
 
-int _set_time(lc_callback cb, void *cb_arg, uint32_t cb_stage)
+int _set_time(lc_callback cb, void *cb_arg)
 {
        const time_t t = time(NULL);
        struct tm *lt = localtime(&t);
 
        if (cb)
-               cb(cb_stage, 0, 1, 2, LC_CB_COUNTER_TYPE_STEPS, cb_arg, NULL);
+               cb(LC_CB_STAGE_SET_TIME, 0, 1, 2, LC_CB_COUNTER_TYPE_STEPS,
+                       cb_arg, NULL);
 
        rtime.second = lt->tm_sec;
        rtime.minute = lt->tm_min;
@@ -916,7 +956,8 @@ int _set_time(lc_callback cb, void *cb_a
                return err;
        }
        if (cb)
-               cb(cb_stage, 1, 2, 2, LC_CB_COUNTER_TYPE_STEPS, cb_arg, NULL);
+               cb(LC_CB_STAGE_SET_TIME, 1, 2, 2, LC_CB_COUNTER_TYPE_STEPS,
+                       cb_arg, NULL);
 
        return 0;
 }
@@ -924,7 +965,7 @@ int _set_time(lc_callback cb, void *cb_a
 int set_time(lc_callback cb, void *cb_arg)
 {
        _report_stages(cb, cb_arg, 1, NULL);
-       return _set_time(cb, cb_arg, LC_CB_STAGE_SET_TIME);
+       return _set_time(cb, cb_arg);
 }
 
 
@@ -1147,28 +1188,27 @@ int _update_configuration_hid(lc_callbac
        return 0;
 }
 
-int update_configuration(lc_callback cb, void *cb_arg, int noreset)
+int update_configuration(lc_callback cb, void *cb_arg, int noreset, int noweb)
 {
        int err;
+
+       std::vector<uint32_t> stages = _get_update_config_stages(noreset,
+               noweb);
+       _report_stages(cb, cb_arg, stages.size(), &stages[0]);
+
+       /*
+        * Tell the website we're going to start. This, it seems creates a
+        * session object on their side, because if you miss the pre-config
+        * communication, you get a no-session error.
+        */
+       if (!noweb) {
+               if ((err = post_preconfig(cb, cb_arg)))
+                       return err;
+       }
+
        if (is_z_remote()) {
-               if (is_usbnet_remote())
-                       _report_stages(cb, cb_arg,
-                               update_configuration_usbnet_num_stages,
-                               update_configuration_usbnet_stages);
-               else
-                       _report_stages(cb, cb_arg,
-                               update_configuration_zwave_num_stages,
-                               update_configuration_zwave_stages);
                err = _update_configuration_zwave(cb, cb_arg);
        } else {
-               if (noreset)
-                       _report_stages(cb, cb_arg,
-                               update_configuration_hid_noreset_num_stages,
-                               update_configuration_hid_noreset_stages);
-               else
-                       _report_stages(cb, cb_arg,
-                               update_configuration_hid_num_stages,
-                               update_configuration_hid_stages);
                err = _update_configuration_hid(cb, cb_arg);
        }
 
@@ -1176,12 +1216,20 @@ int update_configuration(lc_callback cb,
                return err;
 
        // usbnet seems to need a reset; not sure why zwave-hid's do not
-       if (noreset || (is_z_remote() && !is_usbnet_remote()))
-               return 0;
+       if (!noreset && !(is_z_remote() && !is_usbnet_remote()))
+               if ((err = reset_remote(cb, cb_arg)))
+                       return err;
 
-       if ((err = reset_remote(cb, cb_arg)))
+       if ((err = _set_time(cb, cb_arg)))
                return err;
 
+       /* Tell the website we're done */
+       if (!noweb) {
+               if ((err = post_postconfig(cb, cb_arg))) {
+                       return err;
+               }
+       }
+
        return 0;
 }
 
@@ -1382,7 +1430,8 @@ int write_firmware_to_file(uint8_t *in, 
        return 0;
 }
 
-int update_firmware(lc_callback cb, void *cb_arg, int noreset, int direct)
+int update_firmware(lc_callback cb, void *cb_arg, int noreset, int direct,
+       int noweb)
 {
        int err;
 
@@ -1390,14 +1439,9 @@ int update_firmware(lc_callback cb, void
                return LC_ERROR_UNSUPP;
        }
 
-       if (direct)
-               _report_stages(cb, cb_arg,
-                       update_firmware_hid_direct_num_stages,
-                       update_firmware_hid_direct_stages);
-       else
-               _report_stages(cb, cb_arg,
-                       update_firmware_hid_num_stages,
-                       update_firmware_hid_stages);
+       vector<uint32_t> stages = _get_update_firmware_stages(noreset, direct,
+               noweb);
+       _report_stages(cb, cb_arg, stages.size(), &stages[0]);
 
        if (!direct) {
                if ((err = prep_firmware(cb, cb_arg)))
@@ -1418,14 +1462,18 @@ int update_firmware(lc_callback cb, void
                        return err;
        }
 
-       if (noreset)
-               return 0;
-
-       reset_remote(cb, cb_arg);
+       if (!noreset)
+               if ((err = reset_remote(cb, cb_arg)))
+                       return err;
 
-       if ((err = set_time(cb, cb_arg)))
+       if ((err = _set_time(cb, cb_arg)))
                return err;
 
+       if (!noweb) {
+               if ((err = post_postfirmware(cb, cb_arg)))
+                       return err;
+       }
+
        return 0;
 }
 
Index: libconcord/libconcord.h
===================================================================
RCS file: /cvsroot/concordance/concordance/libconcord/libconcord.h,v
retrieving revision 1.22.2.17
diff -u -p -r1.22.2.17 libconcord.h
--- libconcord/libconcord.h     12 Jan 2013 23:50:33 -0000      1.22.2.17
+++ libconcord/libconcord.h     17 Jan 2013 04:27:33 -0000
@@ -295,7 +295,7 @@ int invalidate_flash(lc_callback cb, voi
  * backing up remote configs. If you don't need to do that, you can skip down 
to
  * the next section.
  */
-int update_configuration(lc_callback cb, void *cb_arg, int noreset);
+int update_configuration(lc_callback cb, void *cb_arg, int noreset, int noweb);
 
 /*
  * Read the config from the remote and store it into the unit8_t array
@@ -385,7 +385,8 @@ int write_safemode_to_file(uint8_t *in, 
  * you. The other functions are provided mostly for backwards compatibility.
  * You can skip down to the next section.
  */
-int update_firmware(lc_callback cb, void *cb_arg, int noreset, int direct);
+int update_firmware(lc_callback cb, void *cb_arg, int noreset, int direct,
+       int noweb);
 
 /*
  * This function tells you if the config will be wiped out by a live
Index: libconcord/bindings/python/libconcord.py
===================================================================
RCS file: 
/cvsroot/concordance/concordance/libconcord/bindings/python/libconcord.py,v
retrieving revision 1.9.2.5
diff -u -p -r1.9.2.5 libconcord.py
--- libconcord/bindings/python/libconcord.py    12 Jan 2013 23:50:33 -0000      
1.9.2.5
+++ libconcord/bindings/python/libconcord.py    17 Jan 2013 04:27:33 -0000
@@ -656,13 +656,15 @@ invalidate_flash = _create_func(
     _in('cb_arg', py_object)
 )
 
-# int update_configuration(lc_callback cb, void *cb_arg, int noreset);
+# int update_configuration(lc_callback cb, void *cb_arg, int noreset,
+#     int noweb);
 update_configuration = _create_func(
     'update_configuration',
     _ret_lc_concord(),
     _in('cb', callback_type),
     _in('cb_arg', py_object),
-    _in('noreset', c_int)
+    _in('noreset', c_int),
+    _in('noweb', c_int)
 )
 
 # int read_config_from_remote(uint8_t **out, uint32_t *size,
@@ -755,14 +757,16 @@ write_safemode_to_file = _create_func(
     _in('file_name', c_char_p)
 )
 
-# int update_firmware(lc_callback cb, void *cb_arg, int noreset, int direct);
+# int update_firmware(lc_callback cb, void *cb_arg, int noreset, int direct,
+#     int noweb);
 update_firmware = _create_func(
     'update_firmware',
     _ret_lc_concord(),
     _in('cb', callback_type),
     _in('cb_arg', py_object),
     _in('noreset', c_int),
-    _in('direct', c_int)
+    _in('direct', c_int),
+    _in('noweb', c_int)
 )
 
 # int is_config_safe_after_fw();
------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. ON SALE this month only -- learn more at:
http://p.sf.net/sfu/learnmore_122712
_______________________________________________
concordance-devel mailing list
concordance-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/concordance-devel

Reply via email to