yocto_daisychain.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_daisychain.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindDaisyChain(), the high-level API for DaisyChain 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_daisychain.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__ "daisychain"
50 
51 YDaisyChain::YDaisyChain(const string& func): YFunction(func)
52 //--- (YDaisyChain initialization)
53  ,_daisyState(DAISYSTATE_INVALID)
54  ,_childCount(CHILDCOUNT_INVALID)
55  ,_requiredChildCount(REQUIREDCHILDCOUNT_INVALID)
56  ,_valueCallbackDaisyChain(NULL)
57 //--- (end of YDaisyChain initialization)
58 {
59  _className="DaisyChain";
60 }
61 
63 {
64 //--- (YDaisyChain cleanup)
65 //--- (end of YDaisyChain cleanup)
66 }
67 //--- (YDaisyChain implementation)
68 // static attributes
69 
71 {
72  if(json_val->has("daisyState")) {
73  _daisyState = (Y_DAISYSTATE_enum)json_val->getInt("daisyState");
74  }
75  if(json_val->has("childCount")) {
76  _childCount = json_val->getInt("childCount");
77  }
78  if(json_val->has("requiredChildCount")) {
79  _requiredChildCount = json_val->getInt("requiredChildCount");
80  }
81  return YFunction::_parseAttr(json_val);
82 }
83 
84 
95 {
98  try {
101  {
104  }
105  }
106  }
107  res = _daisyState;
108  } catch (std::exception) {
110  throw;
111  }
113  return res;
114 }
115 
124 {
125  int res = 0;
127  try {
130  {
133  }
134  }
135  }
136  res = _childCount;
137  } catch (std::exception) {
139  throw;
140  }
142  return res;
143 }
144 
153 {
154  int res = 0;
156  try {
159  {
162  }
163  }
164  }
165  res = _requiredChildCount;
166  } catch (std::exception) {
168  throw;
169  }
171  return res;
172 }
173 
187 {
188  string rest_val;
189  int res;
191  try {
192  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
193  res = _setAttr("requiredChildCount", rest_val);
194  } catch (std::exception) {
196  throw;
197  }
199  return res;
200 }
201 
230 {
231  YDaisyChain* obj = NULL;
232  int taken = 0;
233  if (YAPI::_apiInitialized) {
235  taken = 1;
236  }try {
237  obj = (YDaisyChain*) YFunction::_FindFromCache("DaisyChain", func);
238  if (obj == NULL) {
239  obj = new YDaisyChain(func);
240  YFunction::_AddToCache("DaisyChain", func, obj);
241  }
242  } catch (std::exception) {
244  throw;
245  }
247  return obj;
248 }
249 
262 {
263  string val;
264  if (callback != NULL) {
266  } else {
268  }
269  _valueCallbackDaisyChain = callback;
270  // Immediately invoke value callback with current value
271  if (callback != NULL && this->isOnline()) {
272  val = _advertisedValue;
273  if (!(val == "")) {
274  this->_invokeValueCallback(val);
275  }
276  }
277  return 0;
278 }
279 
281 {
282  if (_valueCallbackDaisyChain != NULL) {
283  _valueCallbackDaisyChain(this, value);
284  } else {
286  }
287  return 0;
288 }
289 
291 {
292  string hwid;
293 
294  if(YISERR(_nextFunction(hwid)) || hwid=="") {
295  return NULL;
296  }
297  return YDaisyChain::FindDaisyChain(hwid);
298 }
299 
301 {
302  vector<YFUN_DESCR> v_fundescr;
303  YDEV_DESCR ydevice;
304  string serial, funcId, funcName, funcVal, errmsg;
305 
306  if(YISERR(YapiWrapper::getFunctionsByClass("DaisyChain", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
307  v_fundescr.size() == 0 ||
308  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
309  return NULL;
310  }
311  return YDaisyChain::FindDaisyChain(serial+"."+funcId);
312 }
313 
314 //--- (end of YDaisyChain implementation)
315 
316 //--- (YDaisyChain functions)
317 //--- (end of YDaisyChain functions)
Y_DAISYSTATE_enum _daisyState
static const int REQUIREDCHILDCOUNT_INVALID
string _advertisedValue
Definition: yocto_api.h:1569
static const Y_DAISYSTATE_enum DAISYSTATE_INVALID
YDaisyChain * nextDaisyChain(void)
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
static YDaisyChain * FindDaisyChain(string func)
static const int CHILDCOUNT_INVALID
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
virtual int registerValueCallback(YDaisyChainValueCallback callback)
int set_requiredChildCount(int newval)
static int DefaultCacheValidity
Definition: yocto_api.h:468
static YDaisyChain * FirstDaisyChain(void)
u64 _cacheExpiration
Definition: yocto_api.h:1571
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
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_DAISYSTATE_enum get_daisyState(void)
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
void(* YDaisyChainValueCallback)(YDaisyChain *func, const string &functionValue)
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
YDaisyChain(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
YDaisyChainValueCallback _valueCallbackDaisyChain
int get_childCount(void)
string _className
Definition: yocto_api.h:1558
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 _parseAttr(YJSONObject *json_val)
bool has(const string &key)
Definition: yocto_api.cpp:735
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
int get_requiredChildCount(void)
Y_DAISYSTATE_enum
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