yocto_wakeupmonitor.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_wakeupmonitor.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindWakeUpMonitor(), the high-level API for WakeUpMonitor 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_wakeupmonitor.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__ "wakeupmonitor"
50 
51 YWakeUpMonitor::YWakeUpMonitor(const string& func): YFunction(func)
52 //--- (YWakeUpMonitor initialization)
53  ,_powerDuration(POWERDURATION_INVALID)
54  ,_sleepCountdown(SLEEPCOUNTDOWN_INVALID)
55  ,_nextWakeUp(NEXTWAKEUP_INVALID)
56  ,_wakeUpReason(WAKEUPREASON_INVALID)
57  ,_wakeUpState(WAKEUPSTATE_INVALID)
58  ,_rtcTime(RTCTIME_INVALID)
59  ,_endOfTime(2145960000)
60  ,_valueCallbackWakeUpMonitor(NULL)
61 //--- (end of YWakeUpMonitor initialization)
62 {
63  _className="WakeUpMonitor";
64 }
65 
67 {
68 //--- (YWakeUpMonitor cleanup)
69 //--- (end of YWakeUpMonitor cleanup)
70 }
71 //--- (YWakeUpMonitor implementation)
72 // static attributes
73 
75 {
76  if(json_val->has("powerDuration")) {
77  _powerDuration = json_val->getInt("powerDuration");
78  }
79  if(json_val->has("sleepCountdown")) {
80  _sleepCountdown = json_val->getInt("sleepCountdown");
81  }
82  if(json_val->has("nextWakeUp")) {
83  _nextWakeUp = json_val->getLong("nextWakeUp");
84  }
85  if(json_val->has("wakeUpReason")) {
86  _wakeUpReason = (Y_WAKEUPREASON_enum)json_val->getInt("wakeUpReason");
87  }
88  if(json_val->has("wakeUpState")) {
89  _wakeUpState = (Y_WAKEUPSTATE_enum)json_val->getInt("wakeUpState");
90  }
91  if(json_val->has("rtcTime")) {
92  _rtcTime = json_val->getLong("rtcTime");
93  }
94  return YFunction::_parseAttr(json_val);
95 }
96 
97 
106 {
107  int res = 0;
109  try {
112  {
115  }
116  }
117  }
118  res = _powerDuration;
119  } catch (std::exception) {
121  throw;
122  }
124  return res;
125 }
126 
138 {
139  string rest_val;
140  int res;
142  try {
143  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
144  res = _setAttr("powerDuration", rest_val);
145  } catch (std::exception) {
147  throw;
148  }
150  return res;
151 }
152 
161 {
162  int res = 0;
164  try {
167  {
170  }
171  }
172  }
173  res = _sleepCountdown;
174  } catch (std::exception) {
176  throw;
177  }
179  return res;
180 }
181 
192 {
193  string rest_val;
194  int res;
196  try {
197  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
198  res = _setAttr("sleepCountdown", rest_val);
199  } catch (std::exception) {
201  throw;
202  }
204  return res;
205 }
206 
215 {
216  s64 res = 0;
218  try {
221  {
224  }
225  }
226  }
227  res = _nextWakeUp;
228  } catch (std::exception) {
230  throw;
231  }
233  return res;
234 }
235 
246 {
247  string rest_val;
248  int res;
250  try {
251  char buf[32]; sprintf(buf, "%u", (u32)newval); rest_val = string(buf);
252  res = _setAttr("nextWakeUp", rest_val);
253  } catch (std::exception) {
255  throw;
256  }
258  return res;
259 }
260 
271 {
274  try {
277  {
280  }
281  }
282  }
283  res = _wakeUpReason;
284  } catch (std::exception) {
286  throw;
287  }
289  return res;
290 }
291 
300 {
301  Y_WAKEUPSTATE_enum res;
303  try {
306  {
309  }
310  }
311  }
312  res = _wakeUpState;
313  } catch (std::exception) {
315  throw;
316  }
318  return res;
319 }
320 
322 {
323  string rest_val;
324  int res;
326  try {
327  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
328  res = _setAttr("wakeUpState", rest_val);
329  } catch (std::exception) {
331  throw;
332  }
334  return res;
335 }
336 
338 {
339  s64 res = 0;
341  try {
344  {
347  }
348  }
349  }
350  res = _rtcTime;
351  } catch (std::exception) {
353  throw;
354  }
356  return res;
357 }
358 
387 {
388  YWakeUpMonitor* obj = NULL;
389  int taken = 0;
390  if (YAPI::_apiInitialized) {
392  taken = 1;
393  }try {
394  obj = (YWakeUpMonitor*) YFunction::_FindFromCache("WakeUpMonitor", func);
395  if (obj == NULL) {
396  obj = new YWakeUpMonitor(func);
397  YFunction::_AddToCache("WakeUpMonitor", func, obj);
398  }
399  } catch (std::exception) {
401  throw;
402  }
404  return obj;
405 }
406 
419 {
420  string val;
421  if (callback != NULL) {
423  } else {
425  }
426  _valueCallbackWakeUpMonitor = callback;
427  // Immediately invoke value callback with current value
428  if (callback != NULL && this->isOnline()) {
429  val = _advertisedValue;
430  if (!(val == "")) {
431  this->_invokeValueCallback(val);
432  }
433  }
434  return 0;
435 }
436 
438 {
439  if (_valueCallbackWakeUpMonitor != NULL) {
440  _valueCallbackWakeUpMonitor(this, value);
441  } else {
443  }
444  return 0;
445 }
446 
451 {
452  return this->set_wakeUpState(Y_WAKEUPSTATE_AWAKE);
453 }
454 
465 int YWakeUpMonitor::sleep(int secBeforeSleep)
466 {
467  int currTime = 0;
468  currTime = (int)(this->get_rtcTime());
469  if (!(currTime != 0)) {
470  _throw(YAPI_RTC_NOT_READY,"RTC time not set");
471  return YAPI_RTC_NOT_READY;
472  }
473  this->set_nextWakeUp(_endOfTime);
474  this->set_sleepCountdown(secBeforeSleep);
475  return YAPI_SUCCESS;
476 }
477 
490 int YWakeUpMonitor::sleepFor(int secUntilWakeUp,int secBeforeSleep)
491 {
492  int currTime = 0;
493  currTime = (int)(this->get_rtcTime());
494  if (!(currTime != 0)) {
495  _throw(YAPI_RTC_NOT_READY,"RTC time not set");
496  return YAPI_RTC_NOT_READY;
497  }
498  this->set_nextWakeUp(currTime+secUntilWakeUp);
499  this->set_sleepCountdown(secBeforeSleep);
500  return YAPI_SUCCESS;
501 }
502 
515 int YWakeUpMonitor::sleepUntil(int wakeUpTime,int secBeforeSleep)
516 {
517  int currTime = 0;
518  currTime = (int)(this->get_rtcTime());
519  if (!(currTime != 0)) {
520  _throw(YAPI_RTC_NOT_READY,"RTC time not set");
521  return YAPI_RTC_NOT_READY;
522  }
523  this->set_nextWakeUp(wakeUpTime);
524  this->set_sleepCountdown(secBeforeSleep);
525  return YAPI_SUCCESS;
526 }
527 
535 {
536  this->set_sleepCountdown(0);
537  this->set_nextWakeUp(0);
538  return YAPI_SUCCESS;
539 }
540 
542 {
543  string hwid;
544 
545  if(YISERR(_nextFunction(hwid)) || hwid=="") {
546  return NULL;
547  }
549 }
550 
552 {
553  vector<YFUN_DESCR> v_fundescr;
554  YDEV_DESCR ydevice;
555  string serial, funcId, funcName, funcVal, errmsg;
556 
557  if(YISERR(YapiWrapper::getFunctionsByClass("WakeUpMonitor", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
558  v_fundescr.size() == 0 ||
559  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
560  return NULL;
561  }
562  return YWakeUpMonitor::FindWakeUpMonitor(serial+"."+funcId);
563 }
564 
565 //--- (end of YWakeUpMonitor implementation)
566 
567 //--- (YWakeUpMonitor functions)
568 //--- (end of YWakeUpMonitor functions)
string _advertisedValue
Definition: yocto_api.h:1569
Y_WAKEUPSTATE_enum _wakeUpState
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
static YWakeUpMonitor * FindWakeUpMonitor(string func)
static const int SLEEPCOUNTDOWN_INVALID
int set_sleepCountdown(int newval)
virtual int resetSleepCountDown(void)
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
static int DefaultCacheValidity
Definition: yocto_api.h:468
u64 _cacheExpiration
Definition: yocto_api.h:1571
void _throw(YRETCODE errType, string errMsg)
Definition: yocto_api.cpp:2656
static const Y_WAKEUPSTATE_enum WAKEUPSTATE_INVALID
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
virtual int sleepUntil(int wakeUpTime, int secBeforeSleep)
static const int POWERDURATION_INVALID
static YRETCODE getFunctionInfo(YFUN_DESCR fundesc, YDEV_DESCR &devdescr, string &serial, string &funcId, string &funcName, string &funcVal, string &errmsg)
Definition: yocto_api.cpp:4930
Y_WAKEUPSTATE_enum
YWakeUpMonitor * nextWakeUpMonitor(void)
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:2367
Y_WAKEUPSTATE_enum get_wakeUpState(void)
YAPI_FUNCTION YFUN_DESCR
Definition: yocto_api.h:230
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
virtual int wakeUp(void)
Y_WAKEUPREASON_enum
virtual int _invokeValueCallback(string value)
YAPI_DEVICE YDEV_DESCR
Definition: yocto_api.h:229
static YFunction * _FindFromCache(const string &classname, const string &func)
Definition: yocto_api.cpp:2338
YWakeUpMonitor(const string &func)
Y_WAKEUPREASON_enum _wakeUpReason
virtual int _parseAttr(YJSONObject *json_val)
void yEnterCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:647
static yCRITICAL_SECTION _global_cs
Definition: yocto_api.h:476
bool isOnline(void)
Definition: yocto_api.cpp:3291
YWakeUpMonitorValueCallback _valueCallbackWakeUpMonitor
int set_nextWakeUp(s64 newval)
static int getFunctionsByClass(const string &class_str, YFUN_DESCR prevfundesc, vector< YFUN_DESCR > &buffer, int maxsize, string &errmsg)
Definition: yocto_api.cpp:4852
static YWakeUpMonitor * FirstWakeUpMonitor(void)
string _className
Definition: yocto_api.h:1558
static const s64 NEXTWAKEUP_INVALID
s64 getLong(const string &key)
Definition: yocto_api.cpp:790
Y_WAKEUPREASON_enum get_wakeUpReason(void)
static const Y_WAKEUPREASON_enum WAKEUPREASON_INVALID
static const s64 RTCTIME_INVALID
virtual int registerValueCallback(YWakeUpMonitorValueCallback callback)
virtual int sleep(int secBeforeSleep)
static bool _apiInitialized
Definition: yocto_api.h:475
YRETCODE _nextFunction(string &hwId)
Definition: yocto_api.cpp:2709
virtual int sleepFor(int secUntilWakeUp, int secBeforeSleep)
int set_wakeUpState(Y_WAKEUPSTATE_enum newval)
static void _UpdateValueCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3509
#define YISERR(retcode)
Definition: ydef.h:394
void(* YWakeUpMonitorValueCallback)(YWakeUpMonitor *func, const string &functionValue)
bool has(const string &key)
Definition: yocto_api.cpp:735
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
int set_powerDuration(int newval)
virtual int _invokeValueCallback(string value)
Definition: yocto_api.cpp:2563


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