static int TEST_CASE = 0;
static int IPV4_ADDR_SIZE = 16;
-static char UNICAST_DISCOVERY_QUERY[] = "coap://%s:6298/oc/core";
-static char MULTICAST_DISCOVERY_QUERY[] = "/oc/core";
+static char UNICAST_DISCOVERY_QUERY[] = "coap://%s:6298/oic/res";
+static char MULTICAST_DISCOVERY_QUERY[] = "/oic/res";
-static std::string putPayload = "{\"state\":\"off\",\"power\":10}";
+static std::string putPayload = "{\"oic\":[{\"rep\":{\"state\":\"off\",\"power\":10}}]}";
static std::string coapServerIP;
static std::string coapServerPort;
static std::string coapServerResource;
static OCConnectivityType ocConnType;
-//File containing Client's Identity and the PSK credentials
+//Secure Virtual Resource database for Iotivity Client application
+//It contains Client's Identity and the PSK credentials
//of other devices which the client trusts
-//This can be generated using 'gen_sec_bin' application
-static char CRED_FILE[] = "client_cred.bin";
+static char CRED_FILE[] = "oic_svr_db_client.json";
int gQuitFlag = 0;
OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
OCClientResponse * clientResponse)
{
- uint8_t remoteIpAddr[4];
- uint16_t remotePortNu;
-
OC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully");
if (clientResponse)
{
OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
-
- OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr,
- remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3);
- OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu);
-
OC_LOG_V(INFO, TAG,
- "Device =============> Discovered %s @ %d.%d.%d.%d:%d",
- clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1],
- remoteIpAddr[2], remoteIpAddr[3], remotePortNu);
+ "Device =============> Discovered %s @ %s:%d",
+ clientResponse->resJSONPayload, clientResponse->devAddr.addr, clientResponse->devAddr.port);
ocConnType = clientResponse->connType;
OC_HIGH_QOS:OC_LOW_QOS, getReqCB, NULL, 0));
}
-int InitDiscovery(OCQualityOfService qos)
+int InitDiscovery()
{
OCStackResult ret;
+ OCMethod method;
OCCallbackData cbData;
char szQueryUri[MAX_URI_LENGTH] = { 0 };
OCConnectivityType discoveryReqConnType;
OC_LOG(ERROR, TAG, "!! Bad input for IPV4 address. !!");
return OC_STACK_INVALID_PARAM;
}
- printf("Select Connectivity type on which discovery request needs to be send : ");
- printf("0:ETH, 1:WIFI\n");
- discoveryReqConnType = ((getchar() - '0') == 0) ? OC_ETHERNET : OC_WIFI;
+ discoveryReqConnType = CT_ADAPTER_IP;
+ method = OC_REST_GET;
}
else
{
//Send discovery request on Wifi and Ethernet interface
- discoveryReqConnType = OC_ALL;
+ discoveryReqConnType = CT_DEFAULT;
strcpy(szQueryUri, MULTICAST_DISCOVERY_QUERY);
+ method = OC_REST_DISCOVER;
}
cbData.cb = discoveryReqCB;
(UNICAST_DISCOVERY) ? "Unicast" : "Multicast",
szQueryUri);
- ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0,
- discoveryReqConnType, ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
+ ret = OCDoResource(NULL, method, szQueryUri, 0, 0,
+ discoveryReqConnType, OC_LOW_QOS,
&cbData, NULL, 0);
if (ret != OC_STACK_OK)
{
return ret;
}
+FILE* client_fopen(const char *path, const char *mode)
+{
+ (void)path;
+ return fopen(CRED_FILE, mode);
+}
+
int main(int argc, char* argv[])
{
int opt;
return -1;
}
+ // Initialize Persistent Storage for SVR database
+ OCPersistentStorage ps = {};
+ ps.open = client_fopen;
+ ps.read = fread;
+ ps.write = fwrite;
+ ps.close = fclose;
+ ps.unlink = unlink;
+ OCRegisterPersistentStorageHandler(&ps);
+
/* Initialize OCStack*/
- if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK)
+ if (OCInit(NULL, 0, OC_CLIENT_SERVER) != OC_STACK_OK)
{
OC_LOG(ERROR, TAG, "OCStack init error");
return 0;
}
- /*
- * Read DTLS PSK credentials from persistent storage and
- * set in the OC stack.
- */
- if (SetCredentials(CRED_FILE) != OC_STACK_OK)
- {
- OC_LOG(ERROR, TAG, "SetCredentials failed");
- return 0;
- }
-
- if(TEST_CASE == TEST_NON_CON_OP)
- {
- InitDiscovery(OC_LOW_QOS);
- }
- else if(TEST_CASE == TEST_CON_OP)
- {
- InitDiscovery(OC_HIGH_QOS);
- }
+ InitDiscovery();
timeout.tv_sec = 0;
timeout.tv_nsec = 100000000L;
return 0;
}
-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'};
- // ostringstream not working correctly here, hence snprintf
- snprintf(ipaddr, sizeof(ipaddr), "%d.%d.%d.%d", a,b,c,d);
- 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;
+ ss << clientResponse->devAddr.port;
return ss.str();
}
return -1;
}
- oc = cJSON_GetObjectItem(root,"oc");
+ oc = cJSON_GetObjectItem(root,"oic");
if (!oc)
{
return -1;
if (oc->type == cJSON_Array)
{
- if (cJSON_GetArraySize(oc) > 0)
+ int numRsrcs = cJSON_GetArraySize(oc);
+ for(int i = 0; i < numRsrcs; i++)
{
- cJSON * resource = cJSON_GetArrayItem(oc, 0);
+ cJSON * resource = cJSON_GetArrayItem(oc, i);
if (cJSON_GetObjectItem(resource, "href"))
{
coapServerResource.assign(cJSON_GetObjectItem(resource, "href")->valuestring);
cJSON * prop = cJSON_GetObjectItem(resource,"prop");
if (prop)
{
- // If this is a secure resource, the info about the port at which the
- // resource is hosted on server is embedded inside discovery JSON response
- if (cJSON_GetObjectItem(prop, "sec"))
+ cJSON * policy = cJSON_GetObjectItem(prop,"p");
+ if (policy)
{
- if ((cJSON_GetObjectItem(prop, "sec")->valueint) == 1)
+ // If this is a secure resource, the info about the port at which the
+ // resource is hosted on server is embedded inside discovery JSON response
+ if (cJSON_GetObjectItem(policy, "sec"))
{
- coapSecureResource = 1;
+ if ((cJSON_GetObjectItem(policy, "sec")->valueint) == 1)
+ {
+ coapSecureResource = 1;
+ }
}
- }
- OC_LOG_V(INFO, TAG, "Secure -- %s", coapSecureResource == 1 ? "YES" : "NO");
- if (cJSON_GetObjectItem(prop, "port"))
- {
- port = cJSON_GetObjectItem(prop, "port")->valueint;
- OC_LOG_V(INFO, TAG, "Hosting Server Port (embedded inside JSON) -- %u", port);
+ OC_LOG_V(INFO, TAG, "Secure -- %s", coapSecureResource == 1 ? "YES" : "NO");
+ if (cJSON_GetObjectItem(policy, "port"))
+ {
+ port = cJSON_GetObjectItem(policy, "port")->valueint;
+ OC_LOG_V(INFO, TAG, "Hosting Server Port (embedded inside JSON) -- %u", port);
- std::ostringstream ss;
- ss << port;
- coapServerPort = ss.str();
+ std::ostringstream ss;
+ ss << port;
+ coapServerPort = ss.str();
+ }
}
}
+
+ // If we discovered a secure resource, exit from here
+ if (coapSecureResource)
+ {
+ break;
+ }
}
}
cJSON_Delete(root);
- coapServerIP = getIPAddrTBServer(clientResponse);
+ coapServerIP = clientResponse->devAddr.addr;
if (port == -1)
{
coapServerPort = getPortTBServer(clientResponse);