yocto_watchdog.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_watchdog.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindWatchdog(), the high-level API for Watchdog functions
6  *
7  * - - - - - - - - - License information: - - - - - - - - -
8  *
9  * Copyright (C) 2011 and beyond by Yoctopuce Sarl, Switzerland.
10  *
11  * Yoctopuce Sarl (hereafter Licensor) grants to you a perpetual
12  * non-exclusive license to use, modify, copy and integrate this
13  * file into your software for the sole purpose of interfacing
14  * with Yoctopuce products.
15  *
16  * You may reproduce and distribute copies of this file in
17  * source or object form, as long as the sole purpose of this
18  * code is to interface with Yoctopuce products. You must retain
19  * this notice in the distributed source file.
20  *
21  * You should refer to Yoctopuce General Terms and Conditions
22  * for additional information regarding your rights and
23  * obligations.
24  *
25  * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED 'AS IS' WITHOUT
26  * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
27  * WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS
28  * FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO
29  * EVENT SHALL LICENSOR BE LIABLE FOR ANY INCIDENTAL, SPECIAL,
30  * INDIRECT OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA,
31  * COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR
32  * SERVICES, ANY CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT
33  * LIMITED TO ANY DEFENSE THEREOF), ANY CLAIMS FOR INDEMNITY OR
34  * CONTRIBUTION, OR OTHER SIMILAR COSTS, WHETHER ASSERTED ON THE
35  * BASIS OF CONTRACT, TORT (INCLUDING NEGLIGENCE), BREACH OF
36  * WARRANTY, OR OTHERWISE.
37  *
38  *********************************************************************/
39 
40 
41 #define _CRT_SECURE_NO_DEPRECATE //do not use windows secure crt
42 #include "yocto_watchdog.h"
43 #include "yapi/yjson.h"
44 #include "yapi/yapi.h"
45 #include <string.h>
46 #include <stdio.h>
47 #include <math.h>
48 #include <stdlib.h>
49 #define __FILE_ID__ "watchdog"
50 
51 YWatchdog::YWatchdog(const string& func): YFunction(func)
52 //--- (YWatchdog initialization)
53  ,_state(STATE_INVALID)
54  ,_stateAtPowerOn(STATEATPOWERON_INVALID)
55  ,_maxTimeOnStateA(MAXTIMEONSTATEA_INVALID)
56  ,_maxTimeOnStateB(MAXTIMEONSTATEB_INVALID)
57  ,_output(OUTPUT_INVALID)
58  ,_pulseTimer(PULSETIMER_INVALID)
59  ,_delayedPulseTimer(DELAYEDPULSETIMER_INVALID)
60  ,_countdown(COUNTDOWN_INVALID)
61  ,_autoStart(AUTOSTART_INVALID)
62  ,_running(RUNNING_INVALID)
63  ,_triggerDelay(TRIGGERDELAY_INVALID)
64  ,_triggerDuration(TRIGGERDURATION_INVALID)
65  ,_valueCallbackWatchdog(NULL)
66 //--- (end of YWatchdog initialization)
67 {
68  _className="Watchdog";
69 }
70 
72 {
73 //--- (YWatchdog cleanup)
74 //--- (end of YWatchdog cleanup)
75 }
76 //--- (YWatchdog implementation)
77 // static attributes
79 
81 {
82  if(json_val->has("state")) {
83  _state = (Y_STATE_enum)json_val->getInt("state");
84  }
85  if(json_val->has("stateAtPowerOn")) {
86  _stateAtPowerOn = (Y_STATEATPOWERON_enum)json_val->getInt("stateAtPowerOn");
87  }
88  if(json_val->has("maxTimeOnStateA")) {
89  _maxTimeOnStateA = json_val->getLong("maxTimeOnStateA");
90  }
91  if(json_val->has("maxTimeOnStateB")) {
92  _maxTimeOnStateB = json_val->getLong("maxTimeOnStateB");
93  }
94  if(json_val->has("output")) {
95  _output = (Y_OUTPUT_enum)json_val->getInt("output");
96  }
97  if(json_val->has("pulseTimer")) {
98  _pulseTimer = json_val->getLong("pulseTimer");
99  }
100  if(json_val->has("delayedPulseTimer")) {
101  YJSONObject* subjson = json_val->getYJSONObject("delayedPulseTimer");
102  if (subjson->has("moving")) {
103  _delayedPulseTimer.moving = subjson->getInt("moving");
104  }
105  if (subjson->has("target")) {
106  _delayedPulseTimer.target = subjson->getInt("target");
107  }
108  if (subjson->has("ms")) {
109  _delayedPulseTimer.ms = subjson->getInt("ms");
110  }
111  }
112  if(json_val->has("countdown")) {
113  _countdown = json_val->getLong("countdown");
114  }
115  if(json_val->has("autoStart")) {
116  _autoStart = (Y_AUTOSTART_enum)json_val->getInt("autoStart");
117  }
118  if(json_val->has("running")) {
119  _running = (Y_RUNNING_enum)json_val->getInt("running");
120  }
121  if(json_val->has("triggerDelay")) {
122  _triggerDelay = json_val->getLong("triggerDelay");
123  }
124  if(json_val->has("triggerDuration")) {
125  _triggerDuration = json_val->getLong("triggerDuration");
126  }
127  return YFunction::_parseAttr(json_val);
128 }
129 
130 
140 {
141  Y_STATE_enum res;
143  try {
146  {
149  }
150  }
151  }
152  res = _state;
153  } catch (std::exception) {
155  throw;
156  }
158  return res;
159 }
160 
172 {
173  string rest_val;
174  int res;
176  try {
177  rest_val = (newval>0 ? "1" : "0");
178  res = _setAttr("state", rest_val);
179  } catch (std::exception) {
181  throw;
182  }
184  return res;
185 }
186 
198 {
201  try {
204  {
207  }
208  }
209  }
210  res = _stateAtPowerOn;
211  } catch (std::exception) {
213  throw;
214  }
216  return res;
217 }
218 
231 {
232  string rest_val;
233  int res;
235  try {
236  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
237  res = _setAttr("stateAtPowerOn", rest_val);
238  } catch (std::exception) {
240  throw;
241  }
243  return res;
244 }
245 
255 {
256  s64 res = 0;
258  try {
261  {
264  }
265  }
266  }
267  res = _maxTimeOnStateA;
268  } catch (std::exception) {
270  throw;
271  }
273  return res;
274 }
275 
287 {
288  string rest_val;
289  int res;
291  try {
292  char buf[32]; sprintf(buf, "%u", (u32)newval); rest_val = string(buf);
293  res = _setAttr("maxTimeOnStateA", rest_val);
294  } catch (std::exception) {
296  throw;
297  }
299  return res;
300 }
301 
311 {
312  s64 res = 0;
314  try {
317  {
320  }
321  }
322  }
323  res = _maxTimeOnStateB;
324  } catch (std::exception) {
326  throw;
327  }
329  return res;
330 }
331 
343 {
344  string rest_val;
345  int res;
347  try {
348  char buf[32]; sprintf(buf, "%u", (u32)newval); rest_val = string(buf);
349  res = _setAttr("maxTimeOnStateB", rest_val);
350  } catch (std::exception) {
352  throw;
353  }
355  return res;
356 }
357 
367 {
368  Y_OUTPUT_enum res;
370  try {
373  {
376  }
377  }
378  }
379  res = _output;
380  } catch (std::exception) {
382  throw;
383  }
385  return res;
386 }
387 
399 {
400  string rest_val;
401  int res;
403  try {
404  rest_val = (newval>0 ? "1" : "0");
405  res = _setAttr("output", rest_val);
406  } catch (std::exception) {
408  throw;
409  }
411  return res;
412 }
413 
425 {
426  s64 res = 0;
428  try {
431  {
434  }
435  }
436  }
437  res = _pulseTimer;
438  } catch (std::exception) {
440  throw;
441  }
443  return res;
444 }
445 
447 {
448  string rest_val;
449  int res;
451  try {
452  char buf[32]; sprintf(buf, "%u", (u32)newval); rest_val = string(buf);
453  res = _setAttr("pulseTimer", rest_val);
454  } catch (std::exception) {
456  throw;
457  }
459  return res;
460 }
461 
472 int YWatchdog::pulse(int ms_duration)
473 {
474  string rest_val;
475  char buf[32]; sprintf(buf, "%u", (u32)ms_duration); rest_val = string(buf);
476  return _setAttr("pulseTimer", rest_val);
477 }
478 
480 {
481  YDelayedPulse res;
483  try {
486  {
489  }
490  }
491  }
492  res = _delayedPulseTimer;
493  } catch (std::exception) {
495  throw;
496  }
498  return res;
499 }
500 
502 {
503  string rest_val;
504  int res;
506  try {
507  char buff[64]; sprintf(buff,"%d:%d",newval.target,newval.ms); rest_val = string(buff);
508  res = _setAttr("delayedPulseTimer", rest_val);
509  } catch (std::exception) {
511  throw;
512  }
514  return res;
515 }
516 
527 int YWatchdog::delayedPulse(int ms_delay,int ms_duration)
528 {
529  string rest_val;
530  char buff[64]; sprintf(buff,"%d:%d",ms_delay,ms_duration); rest_val = string(buff);
531  return _setAttr("delayedPulseTimer", rest_val);
532 }
533 
544 {
545  s64 res = 0;
547  try {
550  {
553  }
554  }
555  }
556  res = _countdown;
557  } catch (std::exception) {
559  throw;
560  }
562  return res;
563 }
564 
573 {
574  Y_AUTOSTART_enum res;
576  try {
579  {
582  }
583  }
584  }
585  res = _autoStart;
586  } catch (std::exception) {
588  throw;
589  }
591  return res;
592 }
593 
606 {
607  string rest_val;
608  int res;
610  try {
611  rest_val = (newval>0 ? "1" : "0");
612  res = _setAttr("autoStart", rest_val);
613  } catch (std::exception) {
615  throw;
616  }
618  return res;
619 }
620 
629 {
630  Y_RUNNING_enum res;
632  try {
635  {
638  }
639  }
640  }
641  res = _running;
642  } catch (std::exception) {
644  throw;
645  }
647  return res;
648 }
649 
660 {
661  string rest_val;
662  int res;
664  try {
665  rest_val = (newval>0 ? "1" : "0");
666  res = _setAttr("running", rest_val);
667  } catch (std::exception) {
669  throw;
670  }
672  return res;
673 }
674 
685 {
686  string rest_val;
687  rest_val = "1";
688  return _setAttr("running", rest_val);
689 }
690 
700 {
701  s64 res = 0;
703  try {
706  {
709  }
710  }
711  }
712  res = _triggerDelay;
713  } catch (std::exception) {
715  throw;
716  }
718  return res;
719 }
720 
732 {
733  string rest_val;
734  int res;
736  try {
737  char buf[32]; sprintf(buf, "%u", (u32)newval); rest_val = string(buf);
738  res = _setAttr("triggerDelay", rest_val);
739  } catch (std::exception) {
741  throw;
742  }
744  return res;
745 }
746 
755 {
756  s64 res = 0;
758  try {
761  {
764  }
765  }
766  }
767  res = _triggerDuration;
768  } catch (std::exception) {
770  throw;
771  }
773  return res;
774 }
775 
786 {
787  string rest_val;
788  int res;
790  try {
791  char buf[32]; sprintf(buf, "%u", (u32)newval); rest_val = string(buf);
792  res = _setAttr("triggerDuration", rest_val);
793  } catch (std::exception) {
795  throw;
796  }
798  return res;
799 }
800 
829 {
830  YWatchdog* obj = NULL;
831  int taken = 0;
832  if (YAPI::_apiInitialized) {
834  taken = 1;
835  }try {
836  obj = (YWatchdog*) YFunction::_FindFromCache("Watchdog", func);
837  if (obj == NULL) {
838  obj = new YWatchdog(func);
839  YFunction::_AddToCache("Watchdog", func, obj);
840  }
841  } catch (std::exception) {
843  throw;
844  }
846  return obj;
847 }
848 
861 {
862  string val;
863  if (callback != NULL) {
865  } else {
867  }
868  _valueCallbackWatchdog = callback;
869  // Immediately invoke value callback with current value
870  if (callback != NULL && this->isOnline()) {
871  val = _advertisedValue;
872  if (!(val == "")) {
873  this->_invokeValueCallback(val);
874  }
875  }
876  return 0;
877 }
878 
880 {
881  if (_valueCallbackWatchdog != NULL) {
882  _valueCallbackWatchdog(this, value);
883  } else {
885  }
886  return 0;
887 }
888 
890 {
891  string hwid;
892 
893  if(YISERR(_nextFunction(hwid)) || hwid=="") {
894  return NULL;
895  }
896  return YWatchdog::FindWatchdog(hwid);
897 }
898 
900 {
901  vector<YFUN_DESCR> v_fundescr;
902  YDEV_DESCR ydevice;
903  string serial, funcId, funcName, funcVal, errmsg;
904 
905  if(YISERR(YapiWrapper::getFunctionsByClass("Watchdog", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
906  v_fundescr.size() == 0 ||
907  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
908  return NULL;
909  }
910  return YWatchdog::FindWatchdog(serial+"."+funcId);
911 }
912 
913 //--- (end of YWatchdog implementation)
914 
915 //--- (YWatchdog functions)
916 //--- (end of YWatchdog functions)
int set_maxTimeOnStateB(s64 newval)
string _advertisedValue
Definition: yocto_api.h:1569
int set_output(Y_OUTPUT_enum newval)
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
static const s64 TRIGGERDELAY_INVALID
static YWatchdog * FindWatchdog(string func)
s64 get_triggerDelay(void)
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
virtual int _invokeValueCallback(string value)
Y_RUNNING_enum _running
s64 _triggerDuration
Y_OUTPUT_enum get_output(void)
s64 _maxTimeOnStateB
static YWatchdog * FirstWatchdog(void)
s64 get_countdown(void)
Y_AUTOSTART_enum _autoStart
virtual int _parseAttr(YJSONObject *json_val)
s64 _maxTimeOnStateA
static const Y_AUTOSTART_enum AUTOSTART_INVALID
static const s64 TRIGGERDURATION_INVALID
int set_pulseTimer(s64 newval)
YWatchdogValueCallback _valueCallbackWatchdog
static int DefaultCacheValidity
Definition: yocto_api.h:468
int set_state(Y_STATE_enum newval)
YJSONObject * getYJSONObject(const string &key)
Definition: yocto_api.cpp:744
u64 _cacheExpiration
Definition: yocto_api.h:1571
static const Y_STATE_enum STATE_INVALID
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
int resetWatchdog(void)
static YRETCODE getFunctionInfo(YFUN_DESCR fundesc, YDEV_DESCR &devdescr, string &serial, string &funcId, string &funcName, string &funcVal, string &errmsg)
Definition: yocto_api.cpp:4930
YDelayedPulse _delayedPulseTimer
static const Y_RUNNING_enum RUNNING_INVALID
s64 get_maxTimeOnStateB(void)
YDelayedPulse get_delayedPulseTimer(void)
static const s64 PULSETIMER_INVALID
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:2367
s64 get_pulseTimer(void)
int set_triggerDelay(s64 newval)
int set_stateAtPowerOn(Y_STATEATPOWERON_enum newval)
YAPI_FUNCTION YFUN_DESCR
Definition: yocto_api.h:230
static const Y_STATEATPOWERON_enum STATEATPOWERON_INVALID
int getInt(const string &key)
Definition: yocto_api.cpp:779
static u64 GetTickCount(void)
Definition: yocto_api.cpp:4755
static void _AddToCache(const string &classname, const string &func, YFunction *obj)
Definition: yocto_api.cpp:2345
static const Y_OUTPUT_enum OUTPUT_INVALID
YWatchdog * nextWatchdog(void)
int set_autoStart(Y_AUTOSTART_enum newval)
YAPI_DEVICE YDEV_DESCR
Definition: yocto_api.h:229
static YFunction * _FindFromCache(const string &classname, const string &func)
Definition: yocto_api.cpp:2338
static const s64 COUNTDOWN_INVALID
int set_running(Y_RUNNING_enum newval)
Y_OUTPUT_enum
Definition: yocto_relay.h:74
Y_STATEATPOWERON_enum _stateAtPowerOn
void yEnterCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:647
static yCRITICAL_SECTION _global_cs
Definition: yocto_api.h:476
Y_AUTOSTART_enum
Definition: yocto_api.h:175
Y_STATEATPOWERON_enum get_stateAtPowerOn(void)
bool isOnline(void)
Definition: yocto_api.cpp:3291
int set_delayedPulseTimer(YDelayedPulse newval)
Y_AUTOSTART_enum get_autoStart(void)
int set_maxTimeOnStateA(s64 newval)
virtual int registerValueCallback(YWatchdogValueCallback callback)
YWatchdog(const string &func)
static int getFunctionsByClass(const string &class_str, YFUN_DESCR prevfundesc, vector< YFUN_DESCR > &buffer, int maxsize, string &errmsg)
Definition: yocto_api.cpp:4852
Y_STATEATPOWERON_enum
Definition: yocto_relay.h:65
static const s64 MAXTIMEONSTATEB_INVALID
s64 get_maxTimeOnStateA(void)
string _className
Definition: yocto_api.h:1558
s64 getLong(const string &key)
Definition: yocto_api.cpp:790
Y_STATE_enum
Definition: yocto_relay.h:57
static const s64 MAXTIMEONSTATEA_INVALID
int delayedPulse(int ms_delay, int ms_duration)
static bool _apiInitialized
Definition: yocto_api.h:475
YRETCODE _nextFunction(string &hwId)
Definition: yocto_api.cpp:2709
static const YDelayedPulse DELAYEDPULSETIMER_INVALID
s64 get_triggerDuration(void)
static void _UpdateValueCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3509
#define YISERR(retcode)
Definition: ydef.h:394
int set_triggerDuration(s64 newval)
Y_RUNNING_enum get_running(void)
Y_OUTPUT_enum _output
int pulse(int ms_duration)
Y_STATE_enum _state
bool has(const string &key)
Definition: yocto_api.cpp:735
Y_RUNNING_enum
void(* YWatchdogValueCallback)(YWatchdog *func, const string &functionValue)
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
virtual int _invokeValueCallback(string value)
Definition: yocto_api.cpp:2563
Y_STATE_enum get_state(void)


yoctopuce_altimeter
Author(s): Anja Sheppard
autogenerated on Mon Jun 10 2019 15:49:11