yocto_oscontrol.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_oscontrol.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindOsControl(), the high-level API for OsControl 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_oscontrol.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__ "oscontrol"
50 
51 YOsControl::YOsControl(const string& func): YFunction(func)
52 //--- (YOsControl initialization)
53  ,_shutdownCountdown(SHUTDOWNCOUNTDOWN_INVALID)
54  ,_valueCallbackOsControl(NULL)
55 //--- (end of YOsControl initialization)
56 {
57  _className="OsControl";
58 }
59 
61 {
62 //--- (YOsControl cleanup)
63 //--- (end of YOsControl cleanup)
64 }
65 //--- (YOsControl implementation)
66 // static attributes
67 
69 {
70  if(json_val->has("shutdownCountdown")) {
71  _shutdownCountdown = json_val->getInt("shutdownCountdown");
72  }
73  return YFunction::_parseAttr(json_val);
74 }
75 
76 
87 {
88  int res = 0;
90  try {
93  {
96  }
97  }
98  }
99  res = _shutdownCountdown;
100  } catch (std::exception) {
102  throw;
103  }
105  return res;
106 }
107 
109 {
110  string rest_val;
111  int res;
113  try {
114  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
115  res = _setAttr("shutdownCountdown", rest_val);
116  } catch (std::exception) {
118  throw;
119  }
121  return res;
122 }
123 
152 {
153  YOsControl* obj = NULL;
154  int taken = 0;
155  if (YAPI::_apiInitialized) {
157  taken = 1;
158  }try {
159  obj = (YOsControl*) YFunction::_FindFromCache("OsControl", func);
160  if (obj == NULL) {
161  obj = new YOsControl(func);
162  YFunction::_AddToCache("OsControl", func, obj);
163  }
164  } catch (std::exception) {
166  throw;
167  }
169  return obj;
170 }
171 
184 {
185  string val;
186  if (callback != NULL) {
188  } else {
190  }
191  _valueCallbackOsControl = callback;
192  // Immediately invoke value callback with current value
193  if (callback != NULL && this->isOnline()) {
194  val = _advertisedValue;
195  if (!(val == "")) {
196  this->_invokeValueCallback(val);
197  }
198  }
199  return 0;
200 }
201 
203 {
204  if (_valueCallbackOsControl != NULL) {
205  _valueCallbackOsControl(this, value);
206  } else {
208  }
209  return 0;
210 }
211 
221 int YOsControl::shutdown(int secBeforeShutDown)
222 {
223  return this->set_shutdownCountdown(secBeforeShutDown);
224 }
225 
227 {
228  string hwid;
229 
230  if(YISERR(_nextFunction(hwid)) || hwid=="") {
231  return NULL;
232  }
233  return YOsControl::FindOsControl(hwid);
234 }
235 
237 {
238  vector<YFUN_DESCR> v_fundescr;
239  YDEV_DESCR ydevice;
240  string serial, funcId, funcName, funcVal, errmsg;
241 
242  if(YISERR(YapiWrapper::getFunctionsByClass("OsControl", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
243  v_fundescr.size() == 0 ||
244  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
245  return NULL;
246  }
247  return YOsControl::FindOsControl(serial+"."+funcId);
248 }
249 
250 //--- (end of YOsControl implementation)
251 
252 //--- (YOsControl functions)
253 //--- (end of YOsControl functions)
string _advertisedValue
Definition: yocto_api.h:1569
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
static YOsControl * FirstOsControl(void)
virtual int _parseAttr(YJSONObject *json_val)
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
int _shutdownCountdown
static int DefaultCacheValidity
Definition: yocto_api.h:468
YOsControl(const string &func)
u64 _cacheExpiration
Definition: yocto_api.h:1571
void(* YOsControlValueCallback)(YOsControl *func, const string &functionValue)
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
static const int SHUTDOWNCOUNTDOWN_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
static YOsControl * FindOsControl(string func)
virtual int shutdown(int secBeforeShutDown)
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:2367
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
YAPI_DEVICE YDEV_DESCR
Definition: yocto_api.h:229
static YFunction * _FindFromCache(const string &classname, const string &func)
Definition: yocto_api.cpp:2338
virtual int _invokeValueCallback(string value)
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
static int getFunctionsByClass(const string &class_str, YFUN_DESCR prevfundesc, vector< YFUN_DESCR > &buffer, int maxsize, string &errmsg)
Definition: yocto_api.cpp:4852
YOsControl * nextOsControl(void)
string _className
Definition: yocto_api.h:1558
int set_shutdownCountdown(int newval)
static bool _apiInitialized
Definition: yocto_api.h:475
YRETCODE _nextFunction(string &hwId)
Definition: yocto_api.cpp:2709
static void _UpdateValueCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3509
#define YISERR(retcode)
Definition: ydef.h:394
virtual int registerValueCallback(YOsControlValueCallback callback)
bool has(const string &key)
Definition: yocto_api.cpp:735
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
int get_shutdownCountdown(void)
virtual int _invokeValueCallback(string value)
Definition: yocto_api.cpp:2563
YOsControlValueCallback _valueCallbackOsControl


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