forked from WildDogTeam/wilddog-client-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwilddog_api.c
596 lines (523 loc) · 17.2 KB
/
wilddog_api.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*
* Copyright (C) 2014-2016 Wilddog Technologies. All Rights Reserved.
*
* FileName: wilddog_api.c
*
* Description: Wilddog API functions.
*
* History:
* Version Author Date Description
*
* 0.4.0 Jimmy.Pan 2015-05-15 Create file.
*
*/
#ifndef WILDDOG_PORT_TYPE_ESP
#include <stdio.h>
#endif
#include <stdlib.h>
#include "wilddog.h"
#include "wilddog_ct.h"
#include "wilddog_common.h"
/*
* Function: wilddog_increaseTime
* Description: Optional, user defined time increase. Because SDK need a
* time to ageing request packets, if you have a requirement
* in accuracy, you should call this func, in a timer or other
* methods.
* Input: ms: time want to increase, in mili second
* Output: N/A
* Return: N/A
*/
void wilddog_increaseTime(u32 ms)
{
_wilddog_setTimeIncrease(ms);
return;
}
/*
* Function: wilddog_trySync
* Description: When called, try to sync interal time and receive data from
* internet.
* Input: N/A
* Output: N/A
* Return: N/A
*/
void wilddog_trySync(void)
{
_wilddog_ct_ioctl(WILDDOG_APICMD_SYNC, NULL, 0);
}
/*
* Function: wilddog_initWithUrl
* Description: Init a wilddog client. A client is the path in the HOST tree.
* Input: url: A url such as coaps://<appid>.wilddogio.com/<path>
* Output: N/A
* Return: Id of the client.
*/
Wilddog_T wilddog_initWithUrl(Wilddog_Str_T *url)
{
wilddog_assert(url, 0);
return (Wilddog_T )_wilddog_ct_ioctl(WILDDOG_APICMD_CREATEREF, url,0);
}
/*
* Function: wilddog_destroy
* Description: Destory a wilddog client.
* Input: p_wilddog: a pointer which point to the client id.
* Output: N/A
* Return: 0 means succeed, negative number means failed.
*/
Wilddog_Return_T wilddog_destroy(Wilddog_T *p_wilddog)
{
wilddog_assert(p_wilddog, WILDDOG_ERR_NULL);
return (Wilddog_Return_T)_wilddog_ct_ioctl(WILDDOG_APICMD_DESTROYREF, \
p_wilddog,0);
}
/*
* Function: wilddog_auth
* Description: Set the auth data to a host(such as aaa.wilddogio.com).
* Input: p_host: a pointer to host .
* p_auth: the auth data
* len: the auth data length
* onAuth: the callback function called when the server returns
* a response or send fail.
* args: the arg defined by user, if you do not need, can be NULL.
* Output: N/A
* Return: 0 means succeed, negative number means failed.
* Others: Input url such as appId.wilddogio.com
*/
Wilddog_Return_T wilddog_auth
(
Wilddog_Str_T * p_host,
u8 *p_auth,
int len,
onAuthFunc onAuth,
void* args
)
{
Wilddog_Arg_SetAuth_T arg;
wilddog_assert(p_host, WILDDOG_ERR_NULL);
wilddog_assert(p_auth, WILDDOG_ERR_NULL);
arg.p_host = p_host;
arg.p_auth = p_auth;
arg.d_len = len;
arg.onAuth = onAuth;
arg.arg = args;
return (Wilddog_Return_T)_wilddog_ct_ioctl(WILDDOG_APICMD_SETAUTH, &arg,0);
}
/*
* Function: wilddog_unauth
* Description: Unauth to a host(such as aaa.wilddogio.com).
* Input: p_host: a pointer to host .
* onAuth: the callback function called when the server returns
* a response or send fail.
* args: the arg defined by user, if you do not need, can be NULL.
* Output: N/A
* Return: 0 means succeed, negative number means failed.
* Others: Input url such as appId.wilddogio.com
*/
Wilddog_Return_T wilddog_unauth
(
Wilddog_Str_T * p_host,
onAuthFunc onAuth,
void* args
)
{
Wilddog_Arg_SetAuth_T arg;
wilddog_assert(p_host, WILDDOG_ERR_NULL);
arg.p_host = p_host;
arg.p_auth = NULL;
arg.d_len = 0;
arg.onAuth = onAuth;
arg.arg = args;
return (Wilddog_Return_T)_wilddog_ct_ioctl(WILDDOG_APICMD_SETAUTH, &arg,0);
}
/*
* Function: wilddog_getValue
* Description: Get the data of the client from server.
* Input: wilddog: the id of wilddog client.
* callback: the callback function called when the server returns
* a response or send fail.
* args: the arg defined by user, if you do not need, can be NULL.
* Output: N/A
* Return: 0 means succeed, negative number means failed.
*/
Wilddog_Return_T wilddog_getValue
(
Wilddog_T wilddog,
onQueryFunc callback,
void* arg
)
{
Wilddog_Arg_Query_T args;
wilddog_assert(wilddog, WILDDOG_ERR_NULL);
args.p_ref = wilddog;
args.p_callback = (Wilddog_Func_T)callback;
args.arg = arg;
return (Wilddog_Return_T)_wilddog_ct_ioctl(WILDDOG_APICMD_QUERY, &args,0);
}
/*
* Function: wilddog_setValue
* Description: Post the data of the client to server.
* Input: wilddog: Id of the client.
* p_node: a point to node(Wilddog_Node_T structure), you can
* create a node tree by call node APIs.
* callback: the callback function called when the server returns
* a response or send fail.
* args: the arg defined by user, if you do not need, can be NULL.
* Output: N/A
* Return: 0 means succeed, negative number means failed.
*/
Wilddog_Return_T wilddog_setValue
(
Wilddog_T wilddog,
Wilddog_Node_T *p_node,
onSetFunc callback,
void* arg
)
{
Wilddog_Arg_Set_T args;
wilddog_assert(wilddog, WILDDOG_ERR_NULL);
args.p_ref = wilddog;
args.p_node = p_node;
args.p_callback = (Wilddog_Func_T)callback;
args.arg = arg;
return (Wilddog_Return_T)_wilddog_ct_ioctl(WILDDOG_APICMD_SET, &args,0);
}
/*
* Function: wilddog_push
* Description: Push the data of the client to server.
* Input: wilddog: Id of the client.
* p_node: a point to node(Wilddog_Node_T structure), you can
* create a node tree by call node APIs, can free after
* this function.
* callback: the callback function called when the server returns
* a response or send fail.
* args: the arg defined by user, if you do not need, can be NULL.
* Output: N/A
* Return: 0 means succeed, negative number means failed.
*/
Wilddog_Return_T wilddog_push
(
Wilddog_T wilddog,
Wilddog_Node_T *p_node,
onPushFunc callback,
void* arg
)
{
Wilddog_Arg_Push_T args;
wilddog_assert(wilddog, WILDDOG_ERR_NULL);
args.p_ref = wilddog;
args.p_node = p_node;
args.p_callback = (Wilddog_Func_T)callback;
args.arg = arg;
return (Wilddog_Return_T)_wilddog_ct_ioctl(WILDDOG_APICMD_PUSH, &args,0);
}
/*
* Function: wilddog_removeValue
* Description: Remove the data of the client from server.
* Input: wilddog: Id of the client.
* callback: the callback function called when the server returns
* a response or send fail.
* args: the arg defined by user, if you do not need, can be NULL.
* Output: N/A
* Return: 0 means succeed, negative number means failed.
*/
Wilddog_Return_T wilddog_removeValue
(
Wilddog_T wilddog,
onRemoveFunc callback,
void* arg
)
{
Wilddog_Arg_Remove_T args;
wilddog_assert(wilddog, WILDDOG_ERR_NULL);
args.p_ref = wilddog;
args.p_callback = (Wilddog_Func_T)callback;
args.arg = arg;
return (Wilddog_Return_T)_wilddog_ct_ioctl(WILDDOG_APICMD_REMOVE, &args,0);
}
/*
* Function: wilddog_addObserver
* Description: Subscibe the client's data change, if data changed, server
* will notify the client.
* Input: wilddog: Id of the client.
* event: Event type, see the struct.
* onDataChange: The callback function called when the server
* sends a data change packet.
* dataChangeArg: The arg defined by user, if you do not need,
* can be NULL.
* Output: N/A
* Return: 0 means succeed, negative number means failed.
*/
Wilddog_Return_T wilddog_addObserver
(
Wilddog_T wilddog,
Wilddog_EventType_T event,
onEventFunc onDataChange,
void* dataChangeArg
)
{
Wilddog_Arg_On_T args;
wilddog_assert(wilddog, WILDDOG_ERR_NULL);
args.p_ref = wilddog;
args.d_event = event;
args.p_onData = (Wilddog_Func_T)onDataChange;
args.p_dataArg = dataChangeArg;
return (Wilddog_Return_T)_wilddog_ct_ioctl(WILDDOG_APICMD_ON, &args,0);
}
/*
* Function: wilddog_removeObserver
* Description: Unsubscibe the client's data change.
* Input: wilddog: Id of the client.
* event: Event type, see the struct.
* Output: N/A
* Return: 0 means succeed, negative number means failed.
*/
Wilddog_Return_T wilddog_removeObserver
(
Wilddog_T wilddog,
Wilddog_EventType_T event
)
{
Wilddog_Arg_Off_T args;
wilddog_assert(wilddog, WILDDOG_ERR_NULL);
args.p_ref = wilddog;
args.d_event = event;
return (Wilddog_Return_T)_wilddog_ct_ioctl(WILDDOG_APICMD_OFF, &args,0);
}
/*
* Function: wilddog_getParent
* Description: Get the client's parent.
* Input: p_wilddog: a pointer to the client
* Output: N/A
* Return: A pointer point to the client's parent, if the client is root ,
* return NULL.
*/
Wilddog_T wilddog_getParent(Wilddog_T wilddog)
{
Wilddog_Arg_GetRef_T args;
wilddog_assert(wilddog, 0);
args.p_ref = wilddog;
args.d_cmd = WILDDOG_REFCHG_PARENT;
args.p_str = NULL;
return (Wilddog_T )_wilddog_ct_ioctl(WILDDOG_APICMD_GETREF, &args,0);
}
/*
* Function: wilddog_getRoot
* Description: Get the client's Root.
* Input: wilddog: Id of the client.
* Output: N/A
* Return: an id of your client's root.
*/
Wilddog_T wilddog_getRoot(Wilddog_T wilddog)
{
Wilddog_Arg_GetRef_T args;
wilddog_assert(wilddog, 0);
args.p_ref = wilddog;
args.d_cmd = WILDDOG_REFCHG_ROOT;
args.p_str = NULL;
return (Wilddog_T )_wilddog_ct_ioctl(WILDDOG_APICMD_GETREF, &args,0);
}
/*
* Function: wilddog_getChild
* Description: Get the client's child.
* Input: wilddog: Id of the client.
* Output: N/A
* Return: an id of your client's child.
* Others: The sdk do not check wether the child is really in the server
* or not, only create it.
*/
Wilddog_T wilddog_getChild(Wilddog_T wilddog, Wilddog_Str_T * childName)
{
Wilddog_Arg_GetRef_T args;
wilddog_assert(wilddog, 0);
args.p_ref = wilddog;
args.d_cmd = WILDDOG_REFCHG_CHILD;
args.p_str = childName;
return (Wilddog_T )_wilddog_ct_ioctl(WILDDOG_APICMD_GETREF, &args, 0);
}
/*
* Function: wilddog_getKey
* Description: Get the client's key(the node's name).
* Input: wilddog: Id of the client.
* Output: N/A
* Return: a pointer point to a name string(should be freed by user).
* Others: N/A
*/
Wilddog_Str_T *wilddog_getKey(Wilddog_T wilddog)
{
wilddog_assert(wilddog, NULL);
return (Wilddog_Str_T *)_wilddog_ct_ioctl(WILDDOG_APICMD_GETKEY, \
(void*)wilddog, 0);
}
/*
* Function: wilddog_getHost
* Description: Get the client's host.
* Input: wilddog: Id of the client.
* Output: N/A
* Return: a pointer point to a host string(should be freed by user),
* like "aaa.wilddogio.com" .
* Others: N/A
*/
Wilddog_Str_T *wilddog_getHost(Wilddog_T wilddog)
{
wilddog_assert(wilddog, NULL);
return (Wilddog_Str_T *)_wilddog_ct_ioctl(WILDDOG_APICMD_GETHOST, \
(void*)wilddog, 0);
}
/*
* Function: wilddog_getPath
* Description: Get the client's path.
* Input: wilddog: Id of the client.
* Output: N/A
* Return: a pointer point to a path string(should be freed by user),
* like "/a/b/c" .
* Others: N/A
*/
Wilddog_Str_T *wilddog_getPath(Wilddog_T wilddog)
{
wilddog_assert(wilddog, NULL);
return (Wilddog_Str_T *)_wilddog_ct_ioctl(WILDDOG_APICMD_GETPATH, \
(void*)wilddog, 0);
}
/*
* Function: wilddog_onDisconnectSetValue
* Description: Set the device's disconnect action to cloud, when the device is
* offline, the value will be set to the cloud.
* Input: wilddog: the client id.
* p_node: a point to node(Wilddog_Node_T structure), you can
* create a node tree by call node APIs, can free after
* this function.
* callback: the callback function called when the server returns
* or send fail.
* args: the arg defined by user, if you do not need, can be NULL.
* Output: N/A
* Return: 0 means success , others fail.
* Others: N/A
*/
Wilddog_Return_T wilddog_onDisconnectSetValue
(
Wilddog_T wilddog,
Wilddog_Node_T *p_node,
onDisConnectFunc callback,
void* arg
)
{
Wilddog_Arg_Set_T args;
wilddog_assert(wilddog, WILDDOG_ERR_NULL);
args.p_ref = wilddog;
args.p_node = p_node;
args.p_callback = (Wilddog_Func_T)callback;
args.arg = arg;
return (Wilddog_Return_T)_wilddog_ct_ioctl(WILDDOG_APICMD_DISCONN_SET, \
&args,0);
}
/*
* Function: wilddog_onDisconnectPush
* Description: Set the device's disconnect action to cloud, when the device is
* offline, the value will be push to the cloud.
* Input: wilddog: the client id.
* p_node: a point to node(Wilddog_Node_T structure), you can
* create a node tree by call node APIs, can free after
* this function.
* callback: the callback function called when the server returns
* or send fail.
* args: the arg defined by user, if you do not need, can be NULL.
* Output: N/A
* Return: 0 means success , others fail.
* Others: N/A
*/
Wilddog_Return_T wilddog_onDisconnectPush
(
Wilddog_T wilddog,
Wilddog_Node_T *p_node,
onDisConnectFunc callback,
void* arg
)
{
Wilddog_Arg_Push_T args;
wilddog_assert(wilddog, WILDDOG_ERR_NULL);
args.p_ref = wilddog;
args.p_node = p_node;
args.p_callback = (Wilddog_Func_T)callback;
args.arg = arg;
return (Wilddog_Return_T)_wilddog_ct_ioctl(WILDDOG_APICMD_DISCONN_PUSH, \
&args,0);
}
/*
* Function: wilddog_onDisconnectRemoveValue
* Description: Set the device's disconnect action to cloud, when the device is
* offline, the value will be push to the cloud.
* Input: wilddog: Id of the client.
* callback: the callback function called when the server returns
* or send fail.
* args: the arg defined by user, if you do not need, can be NULL.
* Output: N/A
* Return: 0 means success , others fail.
* Others: N/A
*/
Wilddog_Return_T wilddog_onDisconnectRemoveValue
(
Wilddog_T wilddog,
onDisConnectFunc callback,
void* arg
)
{
Wilddog_Arg_Remove_T args;
wilddog_assert(wilddog, WILDDOG_ERR_NULL);
args.p_ref = wilddog;
args.p_callback = (Wilddog_Func_T)callback;
args.arg = arg;
return (Wilddog_Return_T)_wilddog_ct_ioctl(WILDDOG_APICMD_DISCONN_RMV, \
&args,0);
}
/*
* Function: wilddog_cancelDisconnectOperations
* Description: Cancel the wilddog client's disconnect actions.
* Input: wilddog: Id of the client.
* callback: the callback function called when the server returns
* or send fail.
* args: the arg defined by user, if you do not need, can be NULL.
* Output: N/A
* Return: 0 means success , others fail.
* Others: N/A
*/
Wilddog_Return_T wilddog_cancelDisconnectOperations
(
Wilddog_T wilddog,
onDisConnectFunc callback,
void* arg
)
{
Wilddog_Arg_Remove_T args;
wilddog_assert(wilddog, WILDDOG_ERR_NULL);
args.p_ref = wilddog;
args.p_callback = (Wilddog_Func_T)callback;
args.arg = arg;
return (Wilddog_Return_T)_wilddog_ct_ioctl(WILDDOG_APICMD_DISCONN_CANCEL, \
&args,0);
}
/*
* Function: wilddog_goOffline
* Description: let the device offline.
* Input: N/A
* Output: N/A
* Return: 0 means success , others fail.
* Others: N/A
*/
Wilddog_Return_T wilddog_goOffline(void)
{
wilddog_debug_level(WD_DEBUG_LOG, "Go offline has been called!");
return (Wilddog_Return_T)_wilddog_ct_ioctl(WILDDOG_APICMD_GOOFFLINE, NULL,0);
}
/*
* Function: wilddog_goOnline
* Description: let the device online.
* Input: N/A
* Output: N/A
* Return: 0 means success , others fail.
* Others: N/A
*/
Wilddog_Return_T wilddog_goOnline(void)
{
wilddog_debug_level(WD_DEBUG_LOG, "Go online has been called!");
return (Wilddog_Return_T)_wilddog_ct_ioctl(WILDDOG_APICMD_GOONLINE, NULL,0);
}