Fixing the segfault reported by Doug. It is caused because we are attempting to doubl...
[platform/upstream/iotivity.git] / csdk / stack / samples / SimpleClientServer / ocserver.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <signal.h>
27 #include <pthread.h>
28 #include <ocstack.h>
29 #include <logger.h>
30
31 const char *getResult(OCStackResult result);
32
33 #define TAG PCF("ocserver")
34
35 int gQuitFlag = 0;
36 int gLEDUnderObservation = 0;
37 void createLEDResource();
38 typedef struct LEDRESOURCE{
39     OCResourceHandle handle;
40     bool state;
41     int power;
42 } LEDResource;
43
44 static LEDResource LED;
45
46 // TODO: hard coded for now, change after Sprint4
47 static unsigned char responsePayloadGet[] = "{\"oc\": {\"payload\": {\"state\" : \"on\",\"power\" : \"10\"}}}";
48 static unsigned char responsePayloadPut[] = "{\"oc\": {\"payload\": {\"state\" : \"off\",\"power\" : \"0\"}}}";
49 static uint16_t OC_WELL_KNOWN_PORT = 5683;
50
51 OCStackResult OCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest ) {
52     const char* typeOfMessage;
53
54     switch (flag) {
55         case OC_INIT_FLAG:
56             typeOfMessage = "OC_INIT_FLAG";
57             break;
58         case OC_REQUEST_FLAG:
59             typeOfMessage = "OC_REQUEST_FLAG";
60             break;
61         case OC_OBSERVE_FLAG:
62             typeOfMessage = "OC_OBSERVE_FLAG";
63             break;
64         default:
65             typeOfMessage = "UNKNOWN";
66     }
67     OC_LOG_V(INFO, TAG, "Receiving message type: %s", typeOfMessage);
68     if(entityHandlerRequest && flag == OC_REQUEST_FLAG){ //[CL]
69     if(OC_REST_GET == entityHandlerRequest->method)
70             //entityHandlerRequest->resJSONPayload = reinterpret_cast<unsigned char*>(const_cast<unsigned char*> (responsePayloadGet.c_str()));
71             entityHandlerRequest->resJSONPayload = responsePayloadGet;
72         if(OC_REST_PUT == entityHandlerRequest->method) {
73             //std::cout << std::string(reinterpret_cast<const char*>(entityHandlerRequest->reqJSONPayload)) << std::endl;
74             OC_LOG_V(INFO, TAG, "PUT JSON payload from client: %s", entityHandlerRequest->reqJSONPayload);
75             //entityHandlerRequest->resJSONPayload = reinterpret_cast<unsigned char*>(const_cast<char*> (responsePayloadPut.c_str()));
76             entityHandlerRequest->resJSONPayload = responsePayloadPut;
77             //responsePayloadGet = responsePayloadPut; // just a bad hack!
78             }
79
80     } else if (entityHandlerRequest && flag == OC_OBSERVE_FLAG) {
81         gLEDUnderObservation = 1;
82     }
83
84     //OC_LOG_V(INFO, TAG, "/nReceiving message type:/n/t %s. /n/nWith request:/n/t %s", typeOfMessage, request);
85
86     return OC_STACK_OK;
87 }
88
89 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
90 void handleSigInt(int signum) {
91     if (signum == SIGINT) {
92         gQuitFlag = 1;
93     }
94 }
95
96 void *ChangeLEDRepresentation (void *param)
97 {
98     (void)param;
99     OCStackResult result = OC_STACK_ERROR;
100
101     while (1)
102     {
103         sleep(10);
104         LED.power += 5;
105         if (gLEDUnderObservation)
106         {
107             OC_LOG_V(INFO, TAG, " =====> Notifying stack of new power level %d\n", LED.power);
108             result = OCNotifyObservers (LED.handle);
109             if (OC_STACK_NO_OBSERVERS == result)
110             {
111                 gLEDUnderObservation = 0;
112             }
113         }
114     }
115     return NULL;
116 }
117
118 int main() {
119     OC_LOG(DEBUG, TAG, "OCServer is starting...");
120     uint8_t addr[20] = {0};
121     uint8_t* paddr = NULL;
122     uint16_t port = OC_WELL_KNOWN_PORT;
123     uint8_t ifname[] = "eth0";
124     pthread_t threadId;
125
126     /*Get Ip address on defined interface and initialize coap on it with random port number
127      * this port number will be used as a source port in all coap communications*/
128     if ( OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr,
129                                sizeof(addr)) == ERR_SUCCESS)
130     {
131         OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
132         paddr = addr;
133     }
134
135     if (OCInit((char *) paddr, port, OC_SERVER) != OC_STACK_OK) {
136         OC_LOG(ERROR, TAG, "OCStack init error");
137         return 0;
138     }
139
140     /*
141      * Declare and create the example resource: LED
142      */
143     createLEDResource();
144
145     /*
146      * Create a thread for changing the representation of the LED
147      */
148     pthread_create (&threadId, NULL, ChangeLEDRepresentation, (void *)NULL);
149
150     // Break from loop with Ctrl-C
151     OC_LOG(INFO, TAG, "Entering ocserver main loop...");
152     signal(SIGINT, handleSigInt);
153     while (!gQuitFlag) {
154         if (OCProcess() != OC_STACK_OK) {
155             OC_LOG(ERROR, TAG, "OCStack process error");
156             return 0;
157         }
158         sleep(3);
159     }
160
161     OC_LOG(INFO, TAG, "Exiting ocserver main loop...");
162
163     if (OCStop() != OC_STACK_OK) {
164         OC_LOG(ERROR, TAG, "OCStack process error");
165     }
166
167     return 0;
168 }
169 void createLEDResource() {
170     LED.state = false;
171     OCStackResult res = OCCreateResource(&LED.handle,
172             "core.led",
173             "core.rw",
174             "/a/led",
175             OCEntityHandlerCb,
176             OC_DISCOVERABLE|OC_OBSERVABLE);
177     OC_LOG_V(INFO, TAG, "Created LED resource with result: %s", getResult(res));
178 }
179