Initial merge-commit of the OIC code. Should successfully do discovery for single...
[platform/upstream/iotivity.git] / csdk / stack / test / linux / ocserver.c
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
5
6
7 #include <stdio.h>
8 #include <string.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <signal.h>
12 #include <stdbool.h>
13 #include <ocstack.h>
14
15 #define TAG PCF("ocserver")
16
17 int gQuitFlag = 0;
18 void createLEDResource();
19
20 typedef struct LEDRESOURCE{
21     OCResourceHandle handle;
22     bool power;
23 } LEDResource;
24
25 static LEDResource LED;
26
27 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
28 void handleSigInt(int signum) {
29     if (signum == SIGINT) {
30         gQuitFlag = 1;
31     }
32 }
33
34 int main() {
35     uint8_t addr[20];
36     uint16_t port = USE_RANDOM_PORT;
37     uint8_t ifname[] = "eth0";
38
39     /*Get Ip address on defined interface and initialize coap on it with random port number
40      * this port number will be used as a source port in all coap communications*/
41     OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
42
43     OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
44     if (OCInit((char *) addr, port, OC_SERVER) != OC_STACK_OK) {
45         OC_LOG(ERROR, TAG, "OCStack init error");
46         return 0;
47     }
48
49     /*
50      * Declare and create the example resource: LED
51      */
52     createLEDResource();
53
54     // Break from loop with Ctrl-C
55     OC_LOG(INFO, TAG, "Entering ocserver main loop...");
56     signal(SIGINT, handleSigInt);
57     while (!gQuitFlag) {
58
59         if (OCProcess() != OC_STACK_OK) {
60             OC_LOG(ERROR, TAG, "OCStack process error");
61             return 0;
62         }
63
64         sleep(1);
65     }
66
67     OC_LOG(INFO, TAG, "Exiting ocserver main loop...");
68
69     if (OCStop() != OC_STACK_OK) {
70         OC_LOG(ERROR, TAG, "OCStack process error");
71     }
72
73     return 0;
74 }
75
76 void createLEDResource() {
77     LED.power = false;
78     OCStackResult res = OCCreateResource(&LED.handle,
79                     "core.led",
80                     "state:oc.bt.b;power:oc.bt.i",
81                     "core.rw",
82                     OC_REST_GET|OC_REST_PUT,
83                     "/a/led",
84                     0,
85                     OC_DISCOVERABLE|OC_OBSERVABLE);
86 }