1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
30 #include "ocserverbasicops.h"
34 static LEDResource LED;
35 // This variable determines instance number of the LED resource.
36 // Used by POST method to create a new instance of LED resource.
37 static int gCurrLedInstance = 0;
38 #define SAMPLE_MAX_NUM_POST_INSTANCE 2
39 static LEDResource gLedInstance[SAMPLE_MAX_NUM_POST_INSTANCE];
41 char *gResourceUri= (char *)"/a/led";
43 static uint16_t OC_WELL_KNOWN_PORT = 5683;
45 //This function takes the request as an input and returns the response
47 char* constructJsonResponse (OCEntityHandlerRequest *ehRequest)
49 cJSON *json = cJSON_CreateObject();
52 LEDResource *currLEDResource = &LED;
54 if (ehRequest->resource == gLedInstance[0].handle)
56 currLEDResource = &gLedInstance[0];
57 gResourceUri = (char *) "a/led/0";
59 else if (ehRequest->resource == gLedInstance[1].handle)
61 currLEDResource = &gLedInstance[1];
62 gResourceUri = (char *) "a/led/1";
65 if(OC_REST_PUT == ehRequest->method)
67 cJSON *putJson = cJSON_Parse((char *)ehRequest->reqJSONPayload);
68 currLEDResource->state = ( !strcmp(cJSON_GetObjectItem(putJson,"state")->valuestring ,
70 currLEDResource->power = cJSON_GetObjectItem(putJson,"power")->valuedouble;
71 cJSON_Delete(putJson);
74 cJSON_AddStringToObject(json,"href",gResourceUri);
75 cJSON_AddItemToObject(json, "rep", format=cJSON_CreateObject());
76 cJSON_AddStringToObject(format, "state", (char *) (currLEDResource->state ? "on":"off"));
77 cJSON_AddNumberToObject(format, "power", currLEDResource->power);
79 jsonResponse = cJSON_Print(json);
84 void ProcessGetRequest (OCEntityHandlerRequest *ehRequest)
86 char *getResp = constructJsonResponse(ehRequest);
88 if (ehRequest->resJSONPayloadLen > strlen ((char *)getResp))
90 strncpy((char *)ehRequest->resJSONPayload, getResp,
91 strlen((char *)getResp));
95 OC_LOG_V (INFO, TAG, "Response buffer: %d bytes is too small",
96 ehRequest->resJSONPayloadLen);
102 void ProcessPutRequest (OCEntityHandlerRequest *ehRequest)
104 char *putResp = constructJsonResponse(ehRequest);
106 if (ehRequest->resJSONPayloadLen > strlen ((char *)putResp))
108 strncpy((char *)ehRequest->resJSONPayload, putResp,
109 strlen((char *)putResp));
113 OC_LOG_V (INFO, TAG, "Response buffer: %d bytes is too small",
114 ehRequest->resJSONPayloadLen);
120 void ProcessPostRequest (OCEntityHandlerRequest *ehRequest)
122 char *respPLPost_led = NULL;
127 * The entity handler determines how to process a POST request.
128 * Per the REST paradigm, POST can also be used to update representation of existing
129 * resource or create a new resource.
130 * In the sample below, if the POST is for /a/led then a new instance of the LED
131 * resource is created with default representation (if representation is included in
132 * POST payload it can be used as initial values) as long as the instance is
133 * lesser than max new instance count. Once max instance count is reached, POST on
134 * /a/led updated the representation of /a/led (just like PUT)
137 if (ehRequest->resource == LED.handle)
139 if (gCurrLedInstance < SAMPLE_MAX_NUM_POST_INSTANCE)
141 // Create new LED instance
142 char newLedUri[15] = "/a/led/";
143 sprintf (newLedUri + strlen(newLedUri), "%d", gCurrLedInstance);
145 json = cJSON_CreateObject();
147 cJSON_AddStringToObject(json,"href",gResourceUri);
148 cJSON_AddItemToObject(json, "rep", format=cJSON_CreateObject());
149 cJSON_AddStringToObject(format, "createduri", (char *) newLedUri);
151 if (0 == createLEDResource (newLedUri, &gLedInstance[gCurrLedInstance], false, 0))
153 OC_LOG (INFO, TAG, "Created new LED instance");
154 gLedInstance[gCurrLedInstance].state = 0;
155 gLedInstance[gCurrLedInstance].power = 0;
157 respPLPost_led = cJSON_Print(json);
164 respPLPost_led = constructJsonResponse(ehRequest);
169 for (int i = 0; i < SAMPLE_MAX_NUM_POST_INSTANCE; i++)
171 if (ehRequest->resource == gLedInstance[i].handle)
175 respPLPost_led = constructJsonResponse(ehRequest);
180 respPLPost_led = constructJsonResponse(ehRequest);
186 if (respPLPost_led != NULL && ehRequest->resJSONPayloadLen > strlen ((char *)respPLPost_led))
188 strncpy((char *)ehRequest->resJSONPayload, respPLPost_led,
189 strlen((char *)respPLPost_led));
193 OC_LOG_V (INFO, TAG, "Response buffer: %d bytes is too small",
194 ehRequest->resJSONPayloadLen);
197 free(respPLPost_led);
200 OCEntityHandlerResult
201 OCEntityHandlerCb (OCEntityHandlerFlag flag,
202 OCEntityHandlerRequest *entityHandlerRequest)
204 OC_LOG_V (INFO, TAG, "Inside entity handler - flags: 0x%x", flag);
205 if (flag & OC_INIT_FLAG)
207 OC_LOG (INFO, TAG, "Flag includes OC_INIT_FLAG");
209 if (flag & OC_REQUEST_FLAG)
211 OC_LOG (INFO, TAG, "Flag includes OC_REQUEST_FLAG");
212 if (entityHandlerRequest)
214 if (OC_REST_GET == entityHandlerRequest->method)
216 OC_LOG (INFO, TAG, "Received OC_REST_GET from client");
217 ProcessGetRequest (entityHandlerRequest);
219 else if (OC_REST_PUT == entityHandlerRequest->method)
221 OC_LOG (INFO, TAG, "Received OC_REST_PUT from client");
222 ProcessPutRequest (entityHandlerRequest);
224 else if (OC_REST_POST == entityHandlerRequest->method)
226 OC_LOG (INFO, TAG, "Received OC_REST_POST from client");
227 ProcessPostRequest (entityHandlerRequest);
231 OC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
232 entityHandlerRequest->method);
239 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
240 void handleSigInt(int signum)
242 if (signum == SIGINT)
248 int main(int argc, char* argv[])
250 uint8_t addr[20] = {0};
251 uint8_t* paddr = NULL;
252 uint16_t port = OC_WELL_KNOWN_PORT;
253 uint8_t ifname[] = "eth0";
257 OC_LOG(DEBUG, TAG, "OCServer is starting...");
258 /*Get Ip address on defined interface and initialize coap on it with random port number
259 * this port number will be used as a source port in all coap communications*/
260 if ( OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr,
261 sizeof(addr)) == ERR_SUCCESS)
263 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
267 if (OCInit((char *) paddr, port, OC_SERVER) != OC_STACK_OK)
269 OC_LOG(ERROR, TAG, "OCStack init error");
274 * Declare and create the example resource: LED
276 createLEDResource(gResourceUri, &LED, false, 0);
278 // Break from loop with Ctrl-C
279 OC_LOG(INFO, TAG, "Entering ocserver main loop...");
280 signal(SIGINT, handleSigInt);
283 if (OCProcess() != OC_STACK_OK)
285 OC_LOG(ERROR, TAG, "OCStack process error");
292 OC_LOG(INFO, TAG, "Exiting ocserver main loop...");
294 if (OCStop() != OC_STACK_OK)
296 OC_LOG(ERROR, TAG, "OCStack process error");
302 int createLEDResource (char *uri, LEDResource *ledResource, bool resourceState, int resourcePower)
306 OC_LOG(ERROR, TAG, "Resource URI cannot be NULL");
310 ledResource->state = resourceState;
311 ledResource->power= resourcePower;
312 OCStackResult res = OCCreateResource(&(ledResource->handle),
317 OC_DISCOVERABLE|OC_OBSERVABLE);
318 OC_LOG_V(INFO, TAG, "Created LED resource with result: %s", getResult(res));