1 //******************************************************************
3 // Copyright 2014 Intel Corporation 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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
31 const char *getResult(OCStackResult result);
33 #define TAG PCF("ocserver")
36 int gLEDUnderObservation = 0;
37 void createLEDResource();
38 typedef struct LEDRESOURCE{
39 OCResourceHandle handle;
44 static LEDResource LED;
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;
51 OCStackResult OCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest ) {
52 const char* typeOfMessage;
56 typeOfMessage = "OC_INIT_FLAG";
59 typeOfMessage = "OC_REQUEST_FLAG";
62 typeOfMessage = "OC_OBSERVE_FLAG";
65 typeOfMessage = "UNKNOWN";
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!
80 } else if (entityHandlerRequest && flag == OC_OBSERVE_FLAG) {
81 gLEDUnderObservation = 1;
84 //OC_LOG_V(INFO, TAG, "/nReceiving message type:/n/t %s. /n/nWith request:/n/t %s", typeOfMessage, request);
89 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
90 void handleSigInt(int signum) {
91 if (signum == SIGINT) {
96 void *ChangeLEDRepresentation (void *param)
99 OCStackResult result = OC_STACK_ERROR;
105 if (gLEDUnderObservation)
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)
111 gLEDUnderObservation = 0;
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";
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)
131 OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
135 if (OCInit((char *) paddr, port, OC_SERVER) != OC_STACK_OK) {
136 OC_LOG(ERROR, TAG, "OCStack init error");
141 * Declare and create the example resource: LED
146 * Create a thread for changing the representation of the LED
148 pthread_create (&threadId, NULL, ChangeLEDRepresentation, (void *)NULL);
150 // Break from loop with Ctrl-C
151 OC_LOG(INFO, TAG, "Entering ocserver main loop...");
152 signal(SIGINT, handleSigInt);
154 if (OCProcess() != OC_STACK_OK) {
155 OC_LOG(ERROR, TAG, "OCStack process error");
161 OC_LOG(INFO, TAG, "Exiting ocserver main loop...");
163 if (OCStop() != OC_STACK_OK) {
164 OC_LOG(ERROR, TAG, "OCStack process error");
169 void createLEDResource() {
171 OCStackResult res = OCCreateResource(&LED.handle,
176 OC_DISCOVERABLE|OC_OBSERVABLE);
177 OC_LOG_V(INFO, TAG, "Created LED resource with result: %s", getResult(res));