yocto_bluetoothlink.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_bluetoothlink.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindBluetoothLink(), the high-level API for BluetoothLink 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_bluetoothlink.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__ "bluetoothlink"
50 
51 YBluetoothLink::YBluetoothLink(const string& func): YFunction(func)
52 //--- (YBluetoothLink initialization)
53  ,_ownAddress(OWNADDRESS_INVALID)
54  ,_pairingPin(PAIRINGPIN_INVALID)
55  ,_remoteAddress(REMOTEADDRESS_INVALID)
56  ,_remoteName(REMOTENAME_INVALID)
57  ,_mute(MUTE_INVALID)
58  ,_preAmplifier(PREAMPLIFIER_INVALID)
59  ,_volume(VOLUME_INVALID)
60  ,_linkState(LINKSTATE_INVALID)
61  ,_linkQuality(LINKQUALITY_INVALID)
62  ,_command(COMMAND_INVALID)
63  ,_valueCallbackBluetoothLink(NULL)
64 //--- (end of YBluetoothLink initialization)
65 {
66  _className="BluetoothLink";
67 }
68 
70 {
71 //--- (YBluetoothLink cleanup)
72 //--- (end of YBluetoothLink cleanup)
73 }
74 //--- (YBluetoothLink implementation)
75 // static attributes
81 
83 {
84  if(json_val->has("ownAddress")) {
85  _ownAddress = json_val->getString("ownAddress");
86  }
87  if(json_val->has("pairingPin")) {
88  _pairingPin = json_val->getString("pairingPin");
89  }
90  if(json_val->has("remoteAddress")) {
91  _remoteAddress = json_val->getString("remoteAddress");
92  }
93  if(json_val->has("remoteName")) {
94  _remoteName = json_val->getString("remoteName");
95  }
96  if(json_val->has("mute")) {
97  _mute = (Y_MUTE_enum)json_val->getInt("mute");
98  }
99  if(json_val->has("preAmplifier")) {
100  _preAmplifier = json_val->getInt("preAmplifier");
101  }
102  if(json_val->has("volume")) {
103  _volume = json_val->getInt("volume");
104  }
105  if(json_val->has("linkState")) {
106  _linkState = (Y_LINKSTATE_enum)json_val->getInt("linkState");
107  }
108  if(json_val->has("linkQuality")) {
109  _linkQuality = json_val->getInt("linkQuality");
110  }
111  if(json_val->has("command")) {
112  _command = json_val->getString("command");
113  }
114  return YFunction::_parseAttr(json_val);
115 }
116 
117 
127 {
128  string res;
130  try {
133  {
136  }
137  }
138  }
139  res = _ownAddress;
140  } catch (std::exception) {
142  throw;
143  }
145  return res;
146 }
147 
160 {
161  string res;
163  try {
166  {
169  }
170  }
171  }
172  res = _pairingPin;
173  } catch (std::exception) {
175  throw;
176  }
178  return res;
179 }
180 
192 int YBluetoothLink::set_pairingPin(const string& newval)
193 {
194  string rest_val;
195  int res;
197  try {
198  rest_val = newval;
199  res = _setAttr("pairingPin", rest_val);
200  } catch (std::exception) {
202  throw;
203  }
205  return res;
206 }
207 
216 {
217  string res;
219  try {
222  {
225  }
226  }
227  }
228  res = _remoteAddress;
229  } catch (std::exception) {
231  throw;
232  }
234  return res;
235 }
236 
246 int YBluetoothLink::set_remoteAddress(const string& newval)
247 {
248  string rest_val;
249  int res;
251  try {
252  rest_val = newval;
253  res = _setAttr("remoteAddress", rest_val);
254  } catch (std::exception) {
256  throw;
257  }
259  return res;
260 }
261 
270 {
271  string res;
273  try {
276  {
279  }
280  }
281  }
282  res = _remoteName;
283  } catch (std::exception) {
285  throw;
286  }
288  return res;
289 }
290 
299 {
300  Y_MUTE_enum res;
302  try {
305  {
308  }
309  }
310  }
311  res = _mute;
312  } catch (std::exception) {
314  throw;
315  }
317  return res;
318 }
319 
331 {
332  string rest_val;
333  int res;
335  try {
336  rest_val = (newval>0 ? "1" : "0");
337  res = _setAttr("mute", rest_val);
338  } catch (std::exception) {
340  throw;
341  }
343  return res;
344 }
345 
354 {
355  int res = 0;
357  try {
360  {
363  }
364  }
365  }
366  res = _preAmplifier;
367  } catch (std::exception) {
369  throw;
370  }
372  return res;
373 }
374 
385 {
386  string rest_val;
387  int res;
389  try {
390  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
391  res = _setAttr("preAmplifier", rest_val);
392  } catch (std::exception) {
394  throw;
395  }
397  return res;
398 }
399 
408 {
409  int res = 0;
411  try {
414  {
417  }
418  }
419  }
420  res = _volume;
421  } catch (std::exception) {
423  throw;
424  }
426  return res;
427 }
428 
439 {
440  string rest_val;
441  int res;
443  try {
444  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
445  res = _setAttr("volume", rest_val);
446  } catch (std::exception) {
448  throw;
449  }
451  return res;
452 }
453 
463 {
464  Y_LINKSTATE_enum res;
466  try {
469  {
472  }
473  }
474  }
475  res = _linkState;
476  } catch (std::exception) {
478  throw;
479  }
481  return res;
482 }
483 
493 {
494  int res = 0;
496  try {
499  {
502  }
503  }
504  }
505  res = _linkQuality;
506  } catch (std::exception) {
508  throw;
509  }
511  return res;
512 }
513 
515 {
516  string res;
518  try {
521  {
524  }
525  }
526  }
527  res = _command;
528  } catch (std::exception) {
530  throw;
531  }
533  return res;
534 }
535 
536 int YBluetoothLink::set_command(const string& newval)
537 {
538  string rest_val;
539  int res;
541  try {
542  rest_val = newval;
543  res = _setAttr("command", rest_val);
544  } catch (std::exception) {
546  throw;
547  }
549  return res;
550 }
551 
580 {
581  YBluetoothLink* obj = NULL;
582  int taken = 0;
583  if (YAPI::_apiInitialized) {
585  taken = 1;
586  }try {
587  obj = (YBluetoothLink*) YFunction::_FindFromCache("BluetoothLink", func);
588  if (obj == NULL) {
589  obj = new YBluetoothLink(func);
590  YFunction::_AddToCache("BluetoothLink", func, obj);
591  }
592  } catch (std::exception) {
594  throw;
595  }
597  return obj;
598 }
599 
612 {
613  string val;
614  if (callback != NULL) {
616  } else {
618  }
619  _valueCallbackBluetoothLink = callback;
620  // Immediately invoke value callback with current value
621  if (callback != NULL && this->isOnline()) {
622  val = _advertisedValue;
623  if (!(val == "")) {
624  this->_invokeValueCallback(val);
625  }
626  }
627  return 0;
628 }
629 
631 {
632  if (_valueCallbackBluetoothLink != NULL) {
633  _valueCallbackBluetoothLink(this, value);
634  } else {
636  }
637  return 0;
638 }
639 
648 {
649  return this->set_command("C");
650 }
651 
660 {
661  return this->set_command("D");
662 }
663 
665 {
666  string hwid;
667 
668  if(YISERR(_nextFunction(hwid)) || hwid=="") {
669  return NULL;
670  }
672 }
673 
675 {
676  vector<YFUN_DESCR> v_fundescr;
677  YDEV_DESCR ydevice;
678  string serial, funcId, funcName, funcVal, errmsg;
679 
680  if(YISERR(YapiWrapper::getFunctionsByClass("BluetoothLink", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
681  v_fundescr.size() == 0 ||
682  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
683  return NULL;
684  }
685  return YBluetoothLink::FindBluetoothLink(serial+"."+funcId);
686 }
687 
688 //--- (end of YBluetoothLink implementation)
689 
690 //--- (YBluetoothLink functions)
691 //--- (end of YBluetoothLink functions)
string _advertisedValue
Definition: yocto_api.h:1569
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
Y_MUTE_enum
Definition: yocto_audioin.h:57
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
static int DefaultCacheValidity
Definition: yocto_api.h:468
string getString(const string &key)
Definition: yocto_api.cpp:773
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
#define YAPI_INVALID_STRING
Definition: yocto_api.h:69
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
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
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
bool has(const string &key)
Definition: yocto_api.cpp:735
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
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