+@code{.cpp}\r
+ if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)\r
+ std::cout << endl << "Observe is used." << endl << endl;\r
+ else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)\r
+ std::cout << endl << "ObserveAll is used." << endl << endl;\r
+\r
+ QueryParamsMap test;\r
+\r
+ curResource->observe(OBSERVE_TYPE_TO_USE, test, &onObserve);\r
+\r
+// callback\r
+void onObserve(const OCRepresentation& rep, const int& eCode, const int& sequenceNumber)\r
+{\r
+ if(eCode == SUCCESS_RESPONSE)\r
+ {\r
+ AttributeMap attributeMap = rep.getAttributeMap();\r
+\r
+ std::cout << "OBSERVE RESULT:"<<std::endl;\r
+ std::cout << "\tSequenceNumber: "<< sequenceNumber << endl;\r
+ for(auto it = attributeMap.begin(); it != attributeMap.end(); ++it)\r
+ {\r
+ std::cout << "\tAttribute name: "<< it->first << " value: ";\r
+ for(auto valueItr = it->second.begin(); valueItr != it->second.end(); ++valueItr)\r
+ {\r
+ std::cout <<"\t"<< *valueItr << " ";\r
+ }\r
+\r
+ std::cout << std::endl;\r
+ }\r
+\r
+ if(observe_count() > 30)\r
+ {\r
+ std::cout<<"Cancelling Observe..."<<std::endl;\r
+ OCStackResult result = curResource->cancelObserve();\r
+\r
+ std::cout << "Cancel result: "<< result <<std::endl;\r
+ sleep(10);\r
+ std::cout << "DONE"<<std::endl;\r
+ std::exit(0);\r
+ }\r
+ }\r
+ else\r
+ {\r
+ std::cout << "onObserve Response error: " << eCode << std::endl;\r
+ std::exit(-1);\r
+ }\r
+}\r
+\r
+\r
+@endcode\r
+\r
+\r
+\r
+@section Observe_Server_CPP Observing resource state [Observe] in C++ [Server]\r
+@code{.cpp}\r
+// Handling observe in server's entity handler \r
+if(requestFlag == RequestHandlerFlag::ObserverFlag)\r
+ {\r
+ pthread_t threadId;\r
+\r
+ cout << "\t\trequestFlag : Observer\n";\r
+ gObservation = 1;\r
+\r
+ static int startedThread = 0;\r
+\r
+ // Observation happens on a different thread in ChangeLightRepresentation function.\r
+ // If we have not created the thread already, we will create one here.\r
+ if(!startedThread)\r
+ {\r
+ pthread_create (&threadId, NULL, ChangeLightRepresentation, (void *)NULL);\r
+ startedThread = 1;\r
+ }\r
+\r
+// ChangeLightRepresentaion is an observation function,\r
+// which notifies any changes to the resource to stack\r
+// via notifyObservers\r
+void * ChangeLightRepresentation (void *param)\r
+{\r
+ // This function continuously monitors for the changes\r
+ while (1)\r
+ {\r
+ sleep (5);\r
+\r
+ if (gObservation)\r
+ {\r
+ // If under observation if there are any changes to the light resource\r
+ // we call notifyObservors\r
+ //\r
+ // For demostration we are changing the power value and notifying.\r
+ myLightResource.m_power += 10;\r
+\r
+ cout << "\nPower updated to : " << myLightResource.m_power << endl;\r
+ cout << "Notifying observers with resource handle: " << myLightResource.getHandle() << endl;\r
+\r
+ OCStackResult result = OCPlatform::notifyObservers(myLightResource.getHandle());\r
+\r
+ if(OC_STACK_NO_OBSERVERS == result)\r
+ {\r
+ cout << "No More observers, stopping notifications" << endl;\r
+ gObservation = 0;\r
+ }\r
+ }\r
+ }\r
+\r
+ return NULL;\r
+}\r
+\r
+\r
+@endcode\r
+\r
+\r
+\r
+\r
+@section Oberve_OTA_Response Over the air request\r
+\r
+The following observation request is basically a GET request with the observation option set.\r
+\r
+<table cellspacing="0" cellpadding="0" border=1> \r
+<tr>\r
+<td valign="top" ><p>Fields</p></td>\r
+<td valign="top" ><p>Value</p></td>\r
+<td valign="top" ><p>Notes</p></td>\r
+</tr>\r
+\r
+<tr>\r
+<td valign="top" ><p>Address</p></td>\r
+<td valign="top" ><p>192.168.1.11:5683</p></td>\r
+<td valign="top" ><p>Unicast packet</p></td>\r
+</tr>\r
+\r
+<tr>\r
+<td valign="top" ><p>Header</p></td>\r
+<td valign="top" ><p>CON, GET, MID=0x7d44, TOK=0x3f</p></td>\r
+<td valign="top" ><p>Confirmation requested</p></td>\r
+</tr>\r
+\r
+<tr>\r
+<td valign="top" ><p>Observe</p></td>\r
+<td valign="top" ><p>Register (0)</p></td>\r
+<td valign="top" ><p>This indicates registration</p></td>\r
+</tr>\r
+\r
+<tr>\r
+<td valign="top" ><p>URI-Path</p></td>\r
+<td valign="top" ><p>Light</p></td>\r
+<td valign="top" ><p>"/light/1 "</p></td>\r
+</tr>\r
+\r
+<tr>\r
+<td valign="top" ><p>URI-Path</p></td>\r
+<td valign="top" ><p>1</p></td>\r
+<td valign="top" ><p><br />\r
+</p></td>\r
+</tr>\r
+\r
+<tr>\r
+<td valign="top" ><p>Accept</p></td>\r
+<td valign="top" ><p>application/json</p></td>\r
+<td valign="top" ><p>Requesting result in JSON</p></td>\r
+</tr>\r
+\r
+</table>\r
+\r
+\r
+\r
+@section Observe_OTA_Response Over the air response(s)\r
+\r
+A successful observe request would be similar to the following:\r
+\r
+<table cellspacing="0" cellpadding="0" border=1> \r
+<tr>\r
+<td valign="top" ><p>Field</p></td>\r
+<td valign="top" ><p>Value</p></td>\r
+<td valign="top" ><p>Explanation</p></td>\r
+</tr>\r
+\r
+<tr>\r
+<td valign="top" ><p>Address</p></td>\r
+<td valign="top" ><p>192.168.1.1:5683</p></td>\r
+<td valign="top" ><p>Client Address</p></td>\r
+</tr>\r
+\r
+<tr>\r
+<td valign="top" ><p>Header</p></td>\r
+<td valign="top" ><p>ACK, CONTENT, MID=0x7d44, TOK=0x3f</p></td>\r
+<td valign="top" ><p>Success w/content</p></td>\r
+</tr>\r
+\r
+<tr>\r
+<td valign="top" ><p>Observe</p></td>\r
+<td valign="top" ><p>12</p></td>\r
+<td valign="top" ><p>Sequence number for ordering</p></td>\r
+</tr>\r
+\r
+<tr>\r
+<td valign="top" ><p>Max-Age</p></td>\r
+<td valign="top" ><p>30</p></td>\r
+<td valign="top" ><p>Indicates that the value is fresh for 30 seconds.</p>\r
+\r
+<p>It also indicates that the server should send an</p>\r
+\r
+<p>update within this time period.</p></td>\r
+</tr>\r
+\r
+<tr>\r
+<td valign="top" ><p>Content</p>\r
+\r
+<p>Type</p></td>\r
+<td valign="top" ><p>application/json</p></td>\r
+<td valign="top" ><p><br />\r
+</p></td>\r
+</tr>\r
+\r
+<tr>\r
+<td valign="top" ><p>Payload</p></td>\r
+<td valign="top" ><p>{</p>\r
+\r
+<p>"power" : 0,</p>\r
+\r
+<p>"level" : 10</p>\r
+\r
+<p>}</p></td>\r
+<td valign="top" ><p><br />\r
+</p></td>\r
+</tr>\r
+\r
+</table>\r
+\r
+<b>Subsequent Notifications from 192.168.1.1 </b>\r
+\r
+If the light resource is being observed and the light transitions from an off state to an on state, a notification is sent to the client from the server. The following is an example of such a notification:\r
+\r
+\r
+<table cellspacing="0" cellpadding="0" border=1> \r
+<tr>\r
+<td valign="top" ><p>Field</p></td>\r
+<td valign="top" ><p>Value</p></td>\r
+<td valign="top" ><p>Explanation</p></td>\r
+</tr>\r
+\r
+<tr>\r
+<td valign="top" ><p>Address</p></td>\r
+<td valign="top" ><p>192.168.1.1:5683</p></td>\r
+<td valign="top" ><p>Client Address</p></td>\r
+</tr>\r
+\r
+<tr>\r
+<td valign="top" ><p>Header</p></td>\r
+<td valign="top" ><p>CON, CONTENT, MID=0x7D45, TOK=0x3f</p></td>\r
+<td valign="top" ><p>Content, Can be confirmable or non-confirmable</p></td>\r
+</tr>\r
+\r
+<tr>\r
+<td valign="top" ><p>Observe</p></td>\r
+<td valign="top" ><p>15</p></td>\r
+<td valign="top" ><p>Monotonically increasing until overflow</p></td>\r
+</tr>\r
+\r
+<tr>\r
+<td valign="top" ><p>Max-Age</p></td>\r
+<td valign="top" ><p>30</p></td>\r
+<td valign="top" ><p>Indicates that the value is fresh for 30 seconds.</p>\r
+\r
+<p>It also indicates that the server should send an</p>\r
+\r
+<p>update within this time period.</p></td>\r
+</tr>\r
+\r
+<tr>\r
+<td valign="top" ><p>Payload</p></td>\r
+<td valign="top" ><p>{</p>\r
+\r
+<p>"power" : 1,</p>\r
+\r
+<p>"level" : 10</p>\r
+\r
+<p>}</p></td>\r
+<td valign="top" ><p><br />\r
+</p></td>\r
+</tr>\r
+\r
+</table>\r
+\r
+Since the above notification was marked confirmable, the client should acknowledge the notification with a packet such as the following:\r
+\r
+<table cellspacing="0" cellpadding="0" border=1> \r
+\r
+<tr>\r
+<td valign="top" ><p>Field</p></td>\r
+<td valign="top" ><p>Value</p></td>\r
+<td valign="top" ><p>Explanation</p></td>\r
+</tr>\r
+\r
+<tr>\r
+<td valign="top" ><p>Address</p></td>\r
+<td valign="top" ><p>192.168.1.11:5683</p></td>\r
+<td valign="top" ><p>Unicast packet</p></td>\r
+</tr>\r
+\r
+<tr>\r
+<td valign="top" ><p>Header</p></td>\r
+<td valign="top" ><p>ACK, MID=0x7D45, TOK=0x3f</p></td>\r
+<td valign="top" ><p>Success</p></td>\r
+</tr>\r
+\r
+</table>\r
+\r