Patch 2 : Fixed identified review comments.
Modified ocserver to listen on '5683' so that occlient
can send unicast discovery requests to it.
Patch 3 : 'Really' taking care of tabs this time.
Change-Id: I50c0d47133a469d20ba17e26f2df540e97d099bc
* Copyright (C) 2010--2014 Olaf Bergmann <bergmann@tzi.org>
*
* This file is part of the CoAP library libcoap. Please see
- * README for terms of use.
+ * README for terms of use.
*/
#include "config.h"
#elif HAVE_SYS_UNISTD_H
#include <sys/unistd.h>
#endif
-#ifdef HAVE_SYS_TYPES_H
+#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#ifdef COAP_DEFAULT_WKC_HASHKEY
/** Checks if @p Key is equal to the pre-defined hash key for.well-known/core. */
-#define is_wkc(Key) \
+#define is_wkc(Key) \
(memcmp((Key), COAP_DEFAULT_WKC_HASHKEY, sizeof(coap_key_t)) == 0)
#else
/* Implements a singleton to store a hash key for the .wellknown/core
#if defined(WITH_LWIP) || defined(WITH_CONTIKI)
/* FIXME: with lwip, we can do better */
coap_hash((const unsigned char *)&peer->port, sizeof(peer->port), h);
- coap_hash((const unsigned char *)&peer->addr, sizeof(peer->addr), h);
+ coap_hash((const unsigned char *)&peer->addr, sizeof(peer->addr), h);
#endif /* WITH_LWIP || WITH_CONTIKI */
coap_hash((const unsigned char * )&pdu->hdr->id, sizeof(unsigned short), h);
return COAP_INVALID_TID;
}
-/**
+/**
* Checks if @p opt fits into the message that ends with @p maxpos.
* This function returns @c 1 on success, or @c 0 if the option @p opt
* would exceed @p maxpos.
coap_address_init(&src);
#if defined(WITH_POSIX) || defined(WITH_ARDUINO)
- //bytes_read = recvfrom(ctx->sockfd, buf, sizeof(buf), 0,
- // &src.addr.sa, &src.size);
bytes_read = OCRecvFrom( sockfd, (uint8_t*)buf, sizeof(buf), 0,
- (OCDevAddr*)&src);
+ (OCDevAddr*)&src);
+
#endif /* WITH_POSIX || WITH_ARDUINO */
#ifdef WITH_CONTIKI
if(uip_newdata()) {
}
#endif
- return 0;
+ return bytes_read;
error:
/* FIXME: send back RST? */
coap_delete_node(node);
- return -1;
+ return bytes_read;
error_early:
#ifdef WITH_LWIP
/* even if there was an error, clean up */
pbuf_free(ctx->pending_package);
ctx->pending_package = NULL;
#endif
- return -1;
+ return bytes_read;
}
int coap_remove_from_queue(coap_queue_t **queue, coap_tid_t id,
return resp;
}
-#define WANT_WKC(Pdu,Key) \
+#define WANT_WKC(Pdu,Key) \
(((Pdu)->hdr->code == COAP_REQUEST_GET) && is_wkc(Key))
/************************************************************************************************
*/
int OCProcessCoAP() {
OC_LOG(INFO, TAG, PCF("Entering OCProcessCoAP"));
-
- coap_read(gCoAPCtx, gCoAPCtx->sockfd);
+ int read = 0;
+ read = coap_read(gCoAPCtx, gCoAPCtx->sockfd);
+ if(read > 0)
+ {
+ OC_LOG(INFO, TAG, "This is a Unicast<============");
+ }
if (-1 != gCoAPCtx->sockfd_wellknown) {
- coap_read(gCoAPCtx, gCoAPCtx->sockfd_wellknown);
+ read = coap_read(gCoAPCtx, gCoAPCtx->sockfd_wellknown);
+ if(read > 0)
+ {
+ OC_LOG(INFO, TAG, "This is a Multicast<===========");
+ }
}
coap_dispatch(gCoAPCtx);
return 0;
sin = (struct sockaddr_in *)(ipmcastaddr->addr);
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
- sa.sin_addr.s_addr = INADDR_ANY;
+ sa.sin_addr.s_addr = sin->sin_addr.s_addr;
sa.sin_port = sin->sin_port;
if ((ret = bind(sfd, (struct sockaddr*)&sa, sizeof(sa))) < 0) {
static int UNICAST_DISCOVERY = 0;
static int TEST_CASE = 0;
+static const char * TEST_APP_UNICAST_DISCOVERY_QUERY = "coap://0.0.0.0:5683/oc/core";
static std::string putPayload = "{\"state\":\"off\",\"power\":\"0\"}";
// The handle for the observe registration
int gQuitFlag = 0;
/* SIGINT handler: set gQuitFlag to 1 for graceful termination */
void handleSigInt(int signum) {
- if (signum == SIGINT) {
- gQuitFlag = 1;
- }
+ if (signum == SIGINT) {
+ gQuitFlag = 1;
+ }
}
// Forward Declaration
}
OCStackApplicationResult putReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) {
- if(clientResponse) {}
- if(ctx == (void*)CTX_VAL) {
- OC_LOG_V(INFO, TAG, "Callback Context for PUT query recvd successfully");
- OC_LOG_V(INFO, TAG, "JSON = %s =============> Discovered", clientResponse->resJSONPayload);
- }
+ if(clientResponse) {}
+ if(ctx == (void*)CTX_VAL) {
+ OC_LOG_V(INFO, TAG, "Callback Context for PUT query recvd successfully");
+ OC_LOG_V(INFO, TAG, "JSON = %s =============> Discovered", clientResponse->resJSONPayload);
+ }
- return OC_STACK_KEEP_TRANSACTION;
+ return OC_STACK_KEEP_TRANSACTION;
}
OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) {
}
}
}
- }
- return OC_STACK_KEEP_TRANSACTION;
+ }
+ return OC_STACK_KEEP_TRANSACTION;
}
{
InitGetRequestToUnavailableResource(clientResponse);
}
- return OC_STACK_KEEP_TRANSACTION;
+ return OC_STACK_KEEP_TRANSACTION;
}
int InitGetRequestToUnavailableResource(OCClientResponse * clientResponse)
{
OCStackResult ret;
- OCCallbackData cbData;
+ OCCallbackData cbData;
OCDoHandle handle;
std::ostringstream getQuery;
getQuery << "coap://" << getIPAddrTBServer(clientResponse) << ":" << getPortTBServer(clientResponse) << "/SomeUnknownResource";
ret = OCDoResource(&handle, OC_REST_GET, getQuery.str().c_str(), 0, 0, OC_NON_CONFIRMABLE, &cbData);
if (ret != OC_STACK_OK)
{
- OC_LOG(ERROR, TAG, "OCStack resource error");
+ OC_LOG(ERROR, TAG, "OCStack resource error");
}
return ret;
}
{
OC_LOG(ERROR, TAG, "OCStack resource error");
}
- else
+ else
{
gObserveDoHandle = handle;
}
int InitGetRequest(OCClientResponse * clientResponse)
{
OCStackResult ret;
- OCCallbackData cbData;
+ OCCallbackData cbData;
OCDoHandle handle;
//* Make a GET query*/
std::ostringstream getQuery;
ret = OCDoResource(&handle, OC_REST_GET, getQuery.str().c_str(), 0, 0, OC_NON_CONFIRMABLE, &cbData);
if (ret != OC_STACK_OK)
{
- OC_LOG(ERROR, TAG, "OCStack resource error");
+ OC_LOG(ERROR, TAG, "OCStack resource error");
}
return ret;
}
-#define TEST_APP_UNICAST_DISCOVERY_QUERY PCF("coap://0.0.0.0:5683/oc/core")
int InitDiscovery()
{
OCStackResult ret;
- OCCallbackData cbData;
+ OCCallbackData cbData;
OCDoHandle handle;
- /* Start a discovery query*/
- char szQueryUri[64] = { 0 };
+ /* Start a discovery query*/
+ char szQueryUri[64] = { 0 };
if (UNICAST_DISCOVERY)
{
strcpy(szQueryUri, TEST_APP_UNICAST_DISCOVERY_QUERY);
{
strcpy(szQueryUri, OC_WELL_KNOWN_QUERY);
}
- cbData.cb = discoveryReqCB;
- cbData.context = (void*)CTX_VAL;
- ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, OC_NON_CONFIRMABLE, &cbData);
+ cbData.cb = discoveryReqCB;
+ cbData.context = (void*)CTX_VAL;
+ ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, OC_NON_CONFIRMABLE, &cbData);
if (ret != OC_STACK_OK)
{
- OC_LOG(ERROR, TAG, "OCStack resource error");
+ OC_LOG(ERROR, TAG, "OCStack resource error");
}
return ret;
}
int main(int argc, char* argv[]) {
- uint8_t addr[20] = {0};
- uint8_t* paddr = NULL;
- uint16_t port = USE_RANDOM_PORT;
- uint8_t ifname[] = "eth0";
+ uint8_t addr[20] = {0};
+ uint8_t* paddr = NULL;
+ uint16_t port = USE_RANDOM_PORT;
+ uint8_t ifname[] = "eth0";
if (argc == 3)
{
}
- /*Get Ip address on defined interface and initialize coap on it with random port number
- * this port number will be used as a source port in all coap communications*/
+ /*Get Ip address on defined interface and initialize coap on it with random port number
+ * this port number will be used as a source port in all coap communications*/
if ( OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr,
sizeof(addr)) == ERR_SUCCESS)
{
- OC_LOG_V(INFO, TAG, "Starting occlient on address %s",addr);
+ OC_LOG_V(INFO, TAG, "Starting occlient on address %s",addr);
paddr = addr;
}
- /* Initialize OCStack*/
- if (OCInit((char *) paddr, port, OC_CLIENT) != OC_STACK_OK) {
- OC_LOG(ERROR, TAG, "OCStack init error");
- return 0;
- }
+ /* Initialize OCStack*/
+ if (OCInit((char *) paddr, port, OC_CLIENT) != OC_STACK_OK) {
+ OC_LOG(ERROR, TAG, "OCStack init error");
+ return 0;
+ }
InitDiscovery();
- // Break from loop with Ctrl+C
- OC_LOG(INFO, TAG, "Entering occlient main loop...");
- signal(SIGINT, handleSigInt);
- while (!gQuitFlag) {
+ // Break from loop with Ctrl+C
+ OC_LOG(INFO, TAG, "Entering occlient main loop...");
+ signal(SIGINT, handleSigInt);
+ while (!gQuitFlag) {
- if (OCProcess() != OC_STACK_OK) {
- OC_LOG(ERROR, TAG, "OCStack process error");
- return 0;
- }
+ if (OCProcess() != OC_STACK_OK) {
+ OC_LOG(ERROR, TAG, "OCStack process error");
+ return 0;
+ }
- sleep(3);
- }
- OC_LOG(INFO, TAG, "Exiting occlient main loop...");
+ sleep(3);
+ }
+ OC_LOG(INFO, TAG, "Exiting occlient main loop...");
- if (OCStop() != OC_STACK_OK) {
- OC_LOG(ERROR, TAG, "OCStack stop error");
- }
+ if (OCStop() != OC_STACK_OK) {
+ OC_LOG(ERROR, TAG, "OCStack stop error");
+ }
- return 0;
+ return 0;
}
char *getResult(OCStackResult result) {
- char *resString = new char[40];
- memset(resString, 0, 40);
+ char *resString = new char[40];
+ memset(resString, 0, 40);
strcpy(resString, "Result: ");
switch (result) {
case OC_STACK_OK:
std::string getIPAddrTBServer(OCClientResponse * clientResponse) {
- if(!clientResponse) return "";
- if(!clientResponse->addr) return "";
- uint8_t a, b, c, d = 0;
- if(0 != OCDevAddrToIPv4Addr(clientResponse->addr, &a, &b, &c, &d) ) return "";
-
- char ipaddr[16] = {'\0'};
- snprintf(ipaddr, sizeof(ipaddr), "%d.%d.%d.%d", a,b,c,d); // ostringstream not working correctly here, hence snprintf
- //printf("IP address string of the TB server = %s\n", *out_ipaddr);
- return std::string (ipaddr);
+ if(!clientResponse) return "";
+ if(!clientResponse->addr) return "";
+ uint8_t a, b, c, d = 0;
+ if(0 != OCDevAddrToIPv4Addr(clientResponse->addr, &a, &b, &c, &d) ) return "";
+
+ char ipaddr[16] = {'\0'};
+ snprintf(ipaddr, sizeof(ipaddr), "%d.%d.%d.%d", a,b,c,d); // ostringstream not working correctly here, hence snprintf
+ //printf("IP address string of the TB server = %s\n", *out_ipaddr);
+ return std::string (ipaddr);
}
std::string getPortTBServer(OCClientResponse * clientResponse){
- if(!clientResponse) return "";
- if(!clientResponse->addr) return "";
- uint16_t p = 0;
- if(0 != OCDevAddrToPort(clientResponse->addr, &p) ) return "";
- std::ostringstream ss;
- ss << p;
- return ss.str();
+ if(!clientResponse) return "";
+ if(!clientResponse->addr) return "";
+ uint16_t p = 0;
+ if(0 != OCDevAddrToPort(clientResponse->addr, &p) ) return "";
+ std::ostringstream ss;
+ ss << p;
+ return ss.str();
}
std::string getQueryStrForGetPut(unsigned const char * responsePayload){
- // JSON = {"oc":{"payload":[{"href":"/a/led","rt":["core.led"],"if":["core.rw"],"obs":1}]}}
+ // JSON = {"oc":{"payload":[{"href":"/a/led","rt":["core.led"],"if":["core.rw"],"obs":1}]}}
- //std::string jsonPayload(responsePayload, responsePayload + sizeof responsePayload / sizeof responsePayload[0]);
- std::string jsonPayload(reinterpret_cast<char*>(const_cast<unsigned char*>(responsePayload)));
- //std::cout << jsonPayload << std::endl;
+ //std::string jsonPayload(responsePayload, responsePayload + sizeof responsePayload / sizeof responsePayload[0]);
+ std::string jsonPayload(reinterpret_cast<char*>(const_cast<unsigned char*>(responsePayload)));
+ //std::cout << jsonPayload << std::endl;
- return "/a/led";
+ return "/a/led";
}
int gLEDUnderObservation = 0;
void createLEDResource();
typedef struct LEDRESOURCE{
- OCResourceHandle handle;
- bool state;
+ OCResourceHandle handle;
+ bool state;
int power;
} LEDResource;
// TODO: hard coded for now, change after Sprint4
static unsigned char responsePayloadGet[] = "{\"oc\": {\"payload\": {\"state\" : \"on\",\"power\" : \"10\"}}}";
static unsigned char responsePayloadPut[] = "{\"oc\": {\"payload\": {\"state\" : \"off\",\"power\" : \"0\"}}}";
+static uint16_t OC_WELL_KNOWN_PORT = 5683;
OCStackResult OCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest ) {
- const char* typeOfMessage;
-
- switch (flag) {
- case OC_INIT_FLAG:
- typeOfMessage = "OC_INIT_FLAG";
- break;
- case OC_REQUEST_FLAG:
- typeOfMessage = "OC_REQUEST_FLAG";
- break;
- case OC_OBSERVE_FLAG:
- typeOfMessage = "OC_OBSERVE_FLAG";
- break;
- default:
- typeOfMessage = "UNKNOWN";
- }
- OC_LOG_V(INFO, TAG, "Receiving message type: %s", typeOfMessage);
- if(entityHandlerRequest && flag == OC_REQUEST_FLAG){ //[CL]
- if(OC_REST_GET == entityHandlerRequest->method)
- //entityHandlerRequest->resJSONPayload = reinterpret_cast<unsigned char*>(const_cast<unsigned char*> (responsePayloadGet.c_str()));
- entityHandlerRequest->resJSONPayload = responsePayloadGet;
- if(OC_REST_PUT == entityHandlerRequest->method) {
- //std::cout << std::string(reinterpret_cast<const char*>(entityHandlerRequest->reqJSONPayload)) << std::endl;
- OC_LOG_V(INFO, TAG, "PUT JSON payload from client: %s", entityHandlerRequest->reqJSONPayload);
- //entityHandlerRequest->resJSONPayload = reinterpret_cast<unsigned char*>(const_cast<char*> (responsePayloadPut.c_str()));
- entityHandlerRequest->resJSONPayload = responsePayloadPut;
- //responsePayloadGet = responsePayloadPut; // just a bad hack!
- }
-
- } else if (entityHandlerRequest && flag == OC_OBSERVE_FLAG) {
+ const char* typeOfMessage;
+
+ switch (flag) {
+ case OC_INIT_FLAG:
+ typeOfMessage = "OC_INIT_FLAG";
+ break;
+ case OC_REQUEST_FLAG:
+ typeOfMessage = "OC_REQUEST_FLAG";
+ break;
+ case OC_OBSERVE_FLAG:
+ typeOfMessage = "OC_OBSERVE_FLAG";
+ break;
+ default:
+ typeOfMessage = "UNKNOWN";
+ }
+ OC_LOG_V(INFO, TAG, "Receiving message type: %s", typeOfMessage);
+ if(entityHandlerRequest && flag == OC_REQUEST_FLAG){ //[CL]
+ if(OC_REST_GET == entityHandlerRequest->method)
+ //entityHandlerRequest->resJSONPayload = reinterpret_cast<unsigned char*>(const_cast<unsigned char*> (responsePayloadGet.c_str()));
+ entityHandlerRequest->resJSONPayload = responsePayloadGet;
+ if(OC_REST_PUT == entityHandlerRequest->method) {
+ //std::cout << std::string(reinterpret_cast<const char*>(entityHandlerRequest->reqJSONPayload)) << std::endl;
+ OC_LOG_V(INFO, TAG, "PUT JSON payload from client: %s", entityHandlerRequest->reqJSONPayload);
+ //entityHandlerRequest->resJSONPayload = reinterpret_cast<unsigned char*>(const_cast<char*> (responsePayloadPut.c_str()));
+ entityHandlerRequest->resJSONPayload = responsePayloadPut;
+ //responsePayloadGet = responsePayloadPut; // just a bad hack!
+ }
+
+ } else if (entityHandlerRequest && flag == OC_OBSERVE_FLAG) {
gLEDUnderObservation = 1;
}
- //OC_LOG_V(INFO, TAG, "/nReceiving message type:/n/t %s. /n/nWith request:/n/t %s", typeOfMessage, request);
+ //OC_LOG_V(INFO, TAG, "/nReceiving message type:/n/t %s. /n/nWith request:/n/t %s", typeOfMessage, request);
- return OC_STACK_OK;
+ return OC_STACK_OK;
}
/* SIGINT handler: set gQuitFlag to 1 for graceful termination */
void handleSigInt(int signum) {
- if (signum == SIGINT) {
- gQuitFlag = 1;
- }
+ if (signum == SIGINT) {
+ gQuitFlag = 1;
+ }
}
void *ChangeLEDRepresentation (void *param)
LED.power += 5;
if (gLEDUnderObservation)
{
- OC_LOG_V(INFO, TAG, " =====> Notifying stack of new power level %d\n", LED.power);
+ OC_LOG_V(INFO, TAG, " =====> Notifying stack of new power level %d\n", LED.power);
result = OCNotifyObservers (LED.handle);
if (OC_STACK_NO_OBSERVERS == result)
{
}
int main() {
- OC_LOG(DEBUG, TAG, "OCServer is starting...");
- uint8_t addr[20] = {0};
- uint8_t* paddr = NULL;
- uint16_t port = USE_RANDOM_PORT;
- uint8_t ifname[] = "eth0";
+ OC_LOG(DEBUG, TAG, "OCServer is starting...");
+ uint8_t addr[20] = {0};
+ uint8_t* paddr = NULL;
+ uint16_t port = OC_WELL_KNOWN_PORT;
+ uint8_t ifname[] = "eth0";
pthread_t threadId;
- /*Get Ip address on defined interface and initialize coap on it with random port number
- * this port number will be used as a source port in all coap communications*/
+ /*Get Ip address on defined interface and initialize coap on it with random port number
+ * this port number will be used as a source port in all coap communications*/
if ( OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr,
sizeof(addr)) == ERR_SUCCESS)
{
paddr = addr;
}
- if (OCInit((char *) paddr, port, OC_SERVER) != OC_STACK_OK) {
- OC_LOG(ERROR, TAG, "OCStack init error");
- return 0;
- }
+ if (OCInit((char *) paddr, port, OC_SERVER) != OC_STACK_OK) {
+ OC_LOG(ERROR, TAG, "OCStack init error");
+ return 0;
+ }
- /*
- * Declare and create the example resource: LED
- */
- createLEDResource();
+ /*
+ * Declare and create the example resource: LED
+ */
+ createLEDResource();
/*
* Create a thread for changing the representation of the LED
*/
pthread_create (&threadId, NULL, ChangeLEDRepresentation, (void *)NULL);
- // Break from loop with Ctrl-C
- OC_LOG(INFO, TAG, "Entering ocserver main loop...");
- signal(SIGINT, handleSigInt);
- while (!gQuitFlag) {
- if (OCProcess() != OC_STACK_OK) {
- OC_LOG(ERROR, TAG, "OCStack process error");
- return 0;
- }
- sleep(3);
- }
+ // Break from loop with Ctrl-C
+ OC_LOG(INFO, TAG, "Entering ocserver main loop...");
+ signal(SIGINT, handleSigInt);
+ while (!gQuitFlag) {
+ if (OCProcess() != OC_STACK_OK) {
+ OC_LOG(ERROR, TAG, "OCStack process error");
+ return 0;
+ }
+ sleep(3);
+ }
- OC_LOG(INFO, TAG, "Exiting ocserver main loop...");
+ OC_LOG(INFO, TAG, "Exiting ocserver main loop...");
- if (OCStop() != OC_STACK_OK) {
- OC_LOG(ERROR, TAG, "OCStack process error");
- }
+ if (OCStop() != OC_STACK_OK) {
+ OC_LOG(ERROR, TAG, "OCStack process error");
+ }
- return 0;
+ return 0;
}
void createLEDResource() {
- LED.state = false;
- OCStackResult res = OCCreateResource(&LED.handle,
- "core.led",
- "core.rw",
- "/a/led",
- OCEntityHandlerCb,
- OC_DISCOVERABLE|OC_OBSERVABLE);
- OC_LOG_V(INFO, TAG, "Created LED resource with result: %s", getResult(res));
+ LED.state = false;
+ OCStackResult res = OCCreateResource(&LED.handle,
+ "core.led",
+ "core.rw",
+ "/a/led",
+ OCEntityHandlerCb,
+ OC_DISCOVERABLE|OC_OBSERVABLE);
+ OC_LOG_V(INFO, TAG, "Created LED resource with result: %s", getResult(res));
}
char *getResult(OCStackResult result) {
char *resString = new char[40];
- memset(resString, 0, 40);
+ memset(resString, 0, 40);
strcpy(resString, "Result: ");
switch (result) {
case OC_STACK_OK: