yocto_wireless.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_wireless.cpp 28753 2017-10-03 11:23:38Z seb $
4  *
5  * Implements yFindWireless(), the high-level API for Wireless 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_wireless.h"
43 #include "yapi/yjson.h"
44 #include "yapi/yapi.h"
45 #include <string.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #define __FILE_ID__ "wireless"
49 
50 
51 
52 YWlanRecord::YWlanRecord(const string& json):
53 //--- (generated code: YWlanRecord initialization)
54  _channel(0)
55  ,_rssi(0)
56 //--- (end of generated code: YWlanRecord initialization)
57 {
59 
60  // Parse JSON data
61  j.src = json.c_str();
62  j.end = j.src + strlen(j.src);
63  j.st = YJSON_START;
65  return ;
66  }
67  while(yJsonParse(&j) == YJSON_PARSE_AVAIL && j.st == YJSON_PARSE_MEMBNAME) {
68  if (!strcmp(j.token, "ssid")) {
69  if (yJsonParse(&j) != YJSON_PARSE_AVAIL) {
70  return ;
71  }
72  _ssid = (string)j.token;
74  _ssid +=(string)j.token;
75  }
76  }else if (!strcmp(j.token, "sec")) {
77  if (yJsonParse(&j) != YJSON_PARSE_AVAIL) {
78  return ;
79  }
80  _sec = (string)j.token;
82  _sec +=(string)j.token;
83  }
84  } else if(!strcmp(j.token, "channel")) {
85  if (yJsonParse(&j) != YJSON_PARSE_AVAIL) {
86  return;
87  }
88  _channel = atoi(j.token);;
89  } else if(!strcmp(j.token, "rssi")) {
90  if (yJsonParse(&j) != YJSON_PARSE_AVAIL) {
91  return;
92  }
93  _rssi = atoi(j.token);;
94  } else {
95  yJsonSkip(&j, 1);
96  }
97  }
98 }
99 
100 //--- (generated code: YWlanRecord implementation)
101 // static attributes
102 
103 
105 {
106  return _ssid;
107 }
108 
110 {
111  return _channel;
112 }
113 
115 {
116  return _sec;
117 }
118 
120 {
121  return _rssi;
122 }
123 //--- (end of generated code: YWlanRecord implementation)
124 
125 
126 YWireless::YWireless(const string& func): YFunction(func)
127 //--- (generated code: YWireless initialization)
128  ,_linkQuality(LINKQUALITY_INVALID)
129  ,_ssid(SSID_INVALID)
130  ,_channel(CHANNEL_INVALID)
131  ,_security(SECURITY_INVALID)
132  ,_message(MESSAGE_INVALID)
133  ,_wlanConfig(WLANCONFIG_INVALID)
134  ,_wlanState(WLANSTATE_INVALID)
135  ,_valueCallbackWireless(NULL)
136 //--- (end of generated code: YWireless initialization)
137 {
138  _className = "Wireless";
139 }
140 
142 {
143 //--- (generated code: YWireless cleanup)
144 //--- (end of generated code: YWireless cleanup)
145 }
146 //--- (generated code: YWireless implementation)
147 // static attributes
151 
153 {
154  if(json_val->has("linkQuality")) {
155  _linkQuality = json_val->getInt("linkQuality");
156  }
157  if(json_val->has("ssid")) {
158  _ssid = json_val->getString("ssid");
159  }
160  if(json_val->has("channel")) {
161  _channel = json_val->getInt("channel");
162  }
163  if(json_val->has("security")) {
164  _security = (Y_SECURITY_enum)json_val->getInt("security");
165  }
166  if(json_val->has("message")) {
167  _message = json_val->getString("message");
168  }
169  if(json_val->has("wlanConfig")) {
170  _wlanConfig = json_val->getString("wlanConfig");
171  }
172  if(json_val->has("wlanState")) {
173  _wlanState = (Y_WLANSTATE_enum)json_val->getInt("wlanState");
174  }
175  return YFunction::_parseAttr(json_val);
176 }
177 
178 
187 {
188  int res = 0;
190  try {
193  {
196  }
197  }
198  }
199  res = _linkQuality;
200  } catch (std::exception) {
202  throw;
203  }
205  return res;
206 }
207 
216 {
217  string res;
219  try {
222  {
225  }
226  }
227  }
228  res = _ssid;
229  } catch (std::exception) {
231  throw;
232  }
234  return res;
235 }
236 
246 {
247  int res = 0;
249  try {
252  {
255  }
256  }
257  }
258  res = _channel;
259  } catch (std::exception) {
261  throw;
262  }
264  return res;
265 }
266 
276 {
277  Y_SECURITY_enum res;
279  try {
282  {
285  }
286  }
287  }
288  res = _security;
289  } catch (std::exception) {
291  throw;
292  }
294  return res;
295 }
296 
305 {
306  string res;
308  try {
311  {
314  }
315  }
316  }
317  res = _message;
318  } catch (std::exception) {
320  throw;
321  }
323  return res;
324 }
325 
327 {
328  string res;
330  try {
333  {
336  }
337  }
338  }
339  res = _wlanConfig;
340  } catch (std::exception) {
342  throw;
343  }
345  return res;
346 }
347 
348 int YWireless::set_wlanConfig(const string& newval)
349 {
350  string rest_val;
351  int res;
353  try {
354  rest_val = newval;
355  res = _setAttr("wlanConfig", rest_val);
356  } catch (std::exception) {
358  throw;
359  }
361  return res;
362 }
363 
386 {
387  Y_WLANSTATE_enum res;
389  try {
392  {
395  }
396  }
397  }
398  res = _wlanState;
399  } catch (std::exception) {
401  throw;
402  }
404  return res;
405 }
406 
435 {
436  YWireless* obj = NULL;
437  int taken = 0;
438  if (YAPI::_apiInitialized) {
440  taken = 1;
441  }try {
442  obj = (YWireless*) YFunction::_FindFromCache("Wireless", func);
443  if (obj == NULL) {
444  obj = new YWireless(func);
445  YFunction::_AddToCache("Wireless", func, obj);
446  }
447  } catch (std::exception) {
449  throw;
450  }
452  return obj;
453 }
454 
467 {
468  string val;
469  if (callback != NULL) {
471  } else {
473  }
474  _valueCallbackWireless = callback;
475  // Immediately invoke value callback with current value
476  if (callback != NULL && this->isOnline()) {
477  val = _advertisedValue;
478  if (!(val == "")) {
479  this->_invokeValueCallback(val);
480  }
481  }
482  return 0;
483 }
484 
486 {
487  if (_valueCallbackWireless != NULL) {
488  _valueCallbackWireless(this, value);
489  } else {
491  }
492  return 0;
493 }
494 
506 {
507  string config;
508  config = this->get_wlanConfig();
509  // a full scan is triggered when a config is applied
510  return this->set_wlanConfig(config);
511 }
512 
525 int YWireless::joinNetwork(string ssid,string securityKey)
526 {
527  return this->set_wlanConfig(YapiWrapper::ysprintf("INFRA:%s\\%s", ssid.c_str(),securityKey.c_str()));
528 }
529 
550 int YWireless::adhocNetwork(string ssid,string securityKey)
551 {
552  return this->set_wlanConfig(YapiWrapper::ysprintf("ADHOC:%s\\%s", ssid.c_str(),securityKey.c_str()));
553 }
554 
573 int YWireless::softAPNetwork(string ssid,string securityKey)
574 {
575  return this->set_wlanConfig(YapiWrapper::ysprintf("SOFTAP:%s\\%s", ssid.c_str(),securityKey.c_str()));
576 }
577 
589 vector<YWlanRecord> YWireless::get_detectedWlans(void)
590 {
591  string json;
592  vector<string> wlanlist;
593  vector<YWlanRecord> res;
594 
595  json = this->_download("wlan.json?by=name");
596  wlanlist = this->_json_get_array(json);
597  res.clear();
598  for (unsigned ii = 0; ii < wlanlist.size(); ii++) {
599  res.push_back(YWlanRecord(wlanlist[ii]));
600  }
601  return res;
602 }
603 
605 {
606  string hwid;
607 
608  if(YISERR(_nextFunction(hwid)) || hwid=="") {
609  return NULL;
610  }
611  return YWireless::FindWireless(hwid);
612 }
613 
615 {
616  vector<YFUN_DESCR> v_fundescr;
617  YDEV_DESCR ydevice;
618  string serial, funcId, funcName, funcVal, errmsg;
619 
620  if(YISERR(YapiWrapper::getFunctionsByClass("Wireless", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
621  v_fundescr.size() == 0 ||
622  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
623  return NULL;
624  }
625  return YWireless::FindWireless(serial+"."+funcId);
626 }
627 
628 //--- (end of generated code: YWireless implementation)
629 
630 //--- (generated code: YWireless functions)
631 //--- (end of generated code: YWireless functions)
Y_SECURITY_enum _security
string _advertisedValue
Definition: yocto_api.h:1569
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
string ssid(void)
static const int LINKQUALITY_INVALID
virtual string get_ssid(void)
static const string SSID_INVALID
YWirelessValueCallback _valueCallbackWireless
static const string MESSAGE_INVALID
Y_SECURITY_enum get_security(void)
char token[62]
Definition: yjson.h:88
virtual int registerValueCallback(YWirelessValueCallback callback)
static int DefaultCacheValidity
Definition: yocto_api.h:468
string getString(const string &key)
Definition: yocto_api.cpp:773
int get_linkQuality(void)
string get_message(void)
Y_SECURITY_enum
Y_WLANSTATE_enum _wlanState
u64 _cacheExpiration
Definition: yocto_api.h:1571
yJsonState next
Definition: yjson.h:85
yJsonRetCode yJsonParse(yJsonStateMachine *j)
Definition: yjson.c:83
_FAR const char * src
Definition: yjson.h:82
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
virtual string get_security(void)
static const string WLANCONFIG_INVALID
string _download(const string &url)
Definition: yocto_api.cpp:3053
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 _invokeValueCallback(string value)
void yJsonSkip(yJsonStateMachine *j, int nitems)
Definition: yjson.c:367
YWireless(const string &func)
virtual int adhocNetwork(string ssid, string securityKey)
static const int CHANNEL_INVALID
#define YAPI_INVALID_STRING
Definition: yocto_api.h:69
string _wlanConfig
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:2367
_FAR const char * end
Definition: yjson.h:83
virtual vector< YWlanRecord > get_detectedWlans(void)
YAPI_FUNCTION YFUN_DESCR
Definition: yocto_api.h:230
virtual int get_channel(void)
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 set_wlanConfig(const string &newval)
yJsonState st
Definition: yjson.h:84
vector< string > _json_get_array(const string &json)
Definition: yocto_api.cpp:2811
static const Y_SECURITY_enum SECURITY_INVALID
static string ysprintf(const char *fmt,...)
Definition: yocto_api.cpp:4997
static const Y_WLANSTATE_enum WLANSTATE_INVALID
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
YWireless * nextWireless(void)
string get_ssid(void)
static YWireless * FirstWireless(void)
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 joinNetwork(string ssid, string securityKey)
virtual int startWlanScan(void)
Y_WLANSTATE_enum
string _className
Definition: yocto_api.h:1558
virtual int get_linkQuality(void)
int get_channel(void)
static bool _apiInitialized
Definition: yocto_api.h:475
YRETCODE _nextFunction(string &hwId)
Definition: yocto_api.cpp:2709
virtual int _parseAttr(YJSONObject *json_val)
static void _UpdateValueCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3509
#define YISERR(retcode)
Definition: ydef.h:394
virtual int softAPNetwork(string ssid, string securityKey)
Y_WLANSTATE_enum get_wlanState(void)
YWlanRecord(const string &json)
bool has(const string &key)
Definition: yocto_api.cpp:735
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
void(* YWirelessValueCallback)(YWireless *func, const string &functionValue)
static YWireless * FindWireless(string func)
string get_wlanConfig(void)
virtual int _invokeValueCallback(string value)
Definition: yocto_api.cpp:2563
string _message
string _ssid


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