aboutsummaryrefslogtreecommitdiff
path: root/src/zjs_ocf_common.h
blob: 99e0c7f863a97812442ef3eff6c588472c59d35d (plain)
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
// Copyright (c) 2016, Intel Corporation.

#include "jerry-api.h"

#include "zjs_common.h"
#include "zjs_ocf_client.h"
#include "zjs_util.h"

#include "oc_api.h"
#include <stdio.h>
#include <signal.h>
#include <pthread.h>
//#include "port/oc_signal_main_loop.h"
#include "port/oc_clock.h"

struct props_handle {
    jerry_value_t props_array;
    uint32_t size;
    char** names_array;
};

#define TYPE_IS_NUMBER 0
#define TYPE_IS_INT    1
#define TYPE_IS_UINT   2

#define OCF_MAX_DEVICE_ID_LEN 36
#define OCF_MAX_RES_TYPE_LEN  16
#define OCF_MAX_RES_PATH_LEN  64
#define OCF_MAX_URI_LEN       64
#define OCF_MAX_PROP_NAME_LEN 16

// Helper to get the string from a jerry_value_t
#define ZJS_GET_STRING(jval, name, maxlen)       \
    jerry_size_t name##_size = maxlen; \
    char name[name##_size]; \
    zjs_copy_jstring(jval, name, &name##_size);

#define REJECT(promise, err_name, err_msg, handler) \
    handler = new_ocf_handler(NULL); \
    zjs_make_promise(promise, post_ocf_promise, handler); \
    jerry_value_t error = make_ocf_error(err_name, err_msg, NULL); \
    zjs_reject_promise(promise, &error, 1); \
    jerry_release_value(error);

/*
 * Test if value at index is a double or integer
 *
 * @param val           JerryScript value to test
 *
 * @return              0=number, 1=int, 2=uint
 */
int zjs_ocf_is_int(jerry_value_t val);

/*
 * Encode all properties in props_object into a CborEncoder for an
 * iotivity-constrained callback.
 *
 * @param props_object  JerryScript object containing properties to encode
 * @param encoder       CborEncoder to encode the properties into
 * @param root          Flag if this is the root object or a sub-object
 *
 * @return              Handle to free the properties later
 */
void* zjs_ocf_props_setup(jerry_value_t props_object,
                          CborEncoder* encoder,
                          bool root);

/*
 * Free properties after a call to zjs_ocf_props_setup()
 *
 * @param h             Handler returned from zjs_ocf_props_setup()
 */
void zjs_ocf_free_props(void* h);

/**
 * Routine to call into iotivity-constrained
 *
 * @return              iotivity-constrained's polling functions return value
 */
uint8_t main_poll_routine(void* handle);

/*
 * Start Iotivity-constrained.
 */
int zjs_ocf_start();

/*
 * Object returned from require('ocf')
 */
jerry_value_t zjs_ocf_init();