yocto_hubport.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_hubport.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindHubPort(), the high-level API for HubPort 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_hubport.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__ "hubport"
50 
51 YHubPort::YHubPort(const string& func): YFunction(func)
52 //--- (YHubPort initialization)
53  ,_enabled(ENABLED_INVALID)
54  ,_portState(PORTSTATE_INVALID)
55  ,_baudRate(BAUDRATE_INVALID)
56  ,_valueCallbackHubPort(NULL)
57 //--- (end of YHubPort initialization)
58 {
59  _className="HubPort";
60 }
61 
63 {
64 //--- (YHubPort cleanup)
65 //--- (end of YHubPort cleanup)
66 }
67 //--- (YHubPort implementation)
68 // static attributes
69 
71 {
72  if(json_val->has("enabled")) {
73  _enabled = (Y_ENABLED_enum)json_val->getInt("enabled");
74  }
75  if(json_val->has("portState")) {
76  _portState = (Y_PORTSTATE_enum)json_val->getInt("portState");
77  }
78  if(json_val->has("baudRate")) {
79  _baudRate = json_val->getInt("baudRate");
80  }
81  return YFunction::_parseAttr(json_val);
82 }
83 
84 
94 {
95  Y_ENABLED_enum res;
97  try {
100  {
103  }
104  }
105  }
106  res = _enabled;
107  } catch (std::exception) {
109  throw;
110  }
112  return res;
113 }
114 
126 {
127  string rest_val;
128  int res;
130  try {
131  rest_val = (newval>0 ? "1" : "0");
132  res = _setAttr("enabled", rest_val);
133  } catch (std::exception) {
135  throw;
136  }
138  return res;
139 }
140 
150 {
151  Y_PORTSTATE_enum res;
153  try {
156  {
159  }
160  }
161  }
162  res = _portState;
163  } catch (std::exception) {
165  throw;
166  }
168  return res;
169 }
170 
181 {
182  int res = 0;
184  try {
187  {
190  }
191  }
192  }
193  res = _baudRate;
194  } catch (std::exception) {
196  throw;
197  }
199  return res;
200 }
201 
230 {
231  YHubPort* obj = NULL;
232  int taken = 0;
233  if (YAPI::_apiInitialized) {
235  taken = 1;
236  }try {
237  obj = (YHubPort*) YFunction::_FindFromCache("HubPort", func);
238  if (obj == NULL) {
239  obj = new YHubPort(func);
240  YFunction::_AddToCache("HubPort", 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  _valueCallbackHubPort = 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 (_valueCallbackHubPort != NULL) {
283  _valueCallbackHubPort(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 YHubPort::FindHubPort(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("HubPort", 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 YHubPort::FindHubPort(serial+"."+funcId);
312 }
313 
314 //--- (end of YHubPort implementation)
315 
316 //--- (YHubPort functions)
317 //--- (end of YHubPort functions)
string _advertisedValue
Definition: yocto_api.h:1569
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
Y_ENABLED_enum
Definition: yocto_current.h:59
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
Y_ENABLED_enum get_enabled(void)
static const Y_PORTSTATE_enum PORTSTATE_INVALID
static int DefaultCacheValidity
Definition: yocto_api.h:468
static const int BAUDRATE_INVALID
u64 _cacheExpiration
Definition: yocto_api.h:1571
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
YHubPortValueCallback _valueCallbackHubPort
Y_ENABLED_enum _enabled
Definition: yocto_hubport.h:97
static const Y_ENABLED_enum ENABLED_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
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:2367
void(* YHubPortValueCallback)(YHubPort *func, const string &functionValue)
Definition: yocto_hubport.h:54
static YHubPort * FindHubPort(string func)
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
int get_baudRate(void)
void yEnterCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:647
static yCRITICAL_SECTION _global_cs
Definition: yocto_api.h:476
virtual int registerValueCallback(YHubPortValueCallback callback)
bool isOnline(void)
Definition: yocto_api.cpp:3291
YHubPort * nextHubPort(void)
Y_PORTSTATE_enum
Definition: yocto_hubport.h:68
static int getFunctionsByClass(const string &class_str, YFUN_DESCR prevfundesc, vector< YFUN_DESCR > &buffer, int maxsize, string &errmsg)
Definition: yocto_api.cpp:4852
virtual int _invokeValueCallback(string value)
string _className
Definition: yocto_api.h:1558
Y_PORTSTATE_enum get_portState(void)
int _baudRate
Definition: yocto_hubport.h:99
YHubPort(const string &func)
static bool _apiInitialized
Definition: yocto_api.h:475
YRETCODE _nextFunction(string &hwId)
Definition: yocto_api.cpp:2709
int set_enabled(Y_ENABLED_enum newval)
static void _UpdateValueCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3509
#define YISERR(retcode)
Definition: ydef.h:394
static YHubPort * FirstHubPort(void)
bool has(const string &key)
Definition: yocto_api.cpp:735
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
virtual int _parseAttr(YJSONObject *json_val)
Y_PORTSTATE_enum _portState
Definition: yocto_hubport.h:98
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