-
-
-const char * getIPAddr(const OCClientResponse * clientResponse)
-{
- uint8_t a, b, c, d;
- if(!clientResponse || 0 != OCDevAddrToIPv4Addr(clientResponse->addr, &a, &b, &c, &d))
- {
- return "";
- }
-
- char * ipaddr = NULL;
- if((ipaddr = (char *) OICCalloc(1, MAX_IP_ADDR_ST_SZ)))
- {
- snprintf(ipaddr, MAX_IP_ADDR_ST_SZ, "%d.%d.%d.%d", a,b,c,d);
- }
- else
- {
- OC_LOG(ERROR, TAG, "Memory not allocated to ipaddr");
- }
- return ipaddr;
-}
-
-const char * getPort(const OCClientResponse * clientResponse)
-{
- uint16_t p = 0;
- if(!clientResponse || 0 != OCDevAddrToPort(clientResponse->addr, &p) )
- {
- return "";
- }
-
- char * port = NULL;
- if((port = (char *) OICCalloc(1, MAX_PORT_ST_SZ)))
- {
- snprintf(port, MAX_PORT_ST_SZ, "%d", p);
- }
- else
- {
- OC_LOG(ERROR, TAG, "Memory not allocated to port");
- }
- return port;
-}
-
-int parseJSON(const char * resJSONPayload, char ** sid_c,
- char *** uri_c, int * totalRes)
-{
- cJSON * root = NULL;
- cJSON * oc = NULL;
-
- root = cJSON_Parse((char *)(resJSONPayload));
-
- if (!root)
- {
- OC_LOG(ERROR, TAG, "JSON Parsing Error");
- return OC_STACK_INVALID_JSON;
- }
-
- oc = cJSON_GetObjectItem(root,"oic");
- if (!oc)
- {
- OC_LOG(ERROR, TAG, "Invalid JSON : Missing oc object");
- return OC_STACK_INVALID_JSON;
- }
-
- * totalRes = cJSON_GetArraySize(oc);
-
- if(oc->type == cJSON_Array)
- {
- cJSON * resource = cJSON_GetArrayItem(oc, 0);
-
- if(!resource)
- {
- return OC_STACK_INVALID_JSON;
- }
-
- if (cJSON_GetObjectItem(resource, "sid"))
- {
- char * sid = cJSON_GetObjectItem(resource, "sid")->valuestring;
- if((* sid_c = (char *)OICCalloc(1, strlen (sid) + 1)))
- {
- memcpy(* sid_c, sid, strlen(sid) + 1);
- }
- else
- {
- OC_LOG(ERROR, TAG, "Memory not allocated to sid");
- return OC_STACK_NO_MEMORY;
- }
- }
- else
- {
- OC_LOG(ERROR, TAG, "Invalid JSON : Missing sid object");
- return OC_STACK_INVALID_JSON;
- }
-
- if(!(* uri_c = (char ** )OICMalloc ((* totalRes) * sizeof(char **))))
- {
- OC_LOG(ERROR, TAG, "Memory not allocated to sid_c array");
- return OC_STACK_NO_MEMORY;
- }
-
- int i = 0;
-
- while(true)
- {
- if (cJSON_GetObjectItem(resource, "href"))
- {
- char *uri= cJSON_GetObjectItem(resource, "href")->valuestring;
- if(((*uri_c)[i] = (char *)OICCalloc(1, strlen (uri) + 1)))
- {
- memcpy((*uri_c)[i], uri, strlen(uri) + 1);
- }
- else
- {
- OC_LOG(ERROR, TAG, "Memory not allocated to uri");
- return OC_STACK_NO_MEMORY;
- }
- i++;
- if(i >= (* totalRes))
- break;
- resource = cJSON_GetArrayItem(oc, i);
- }
- else
- {
- OC_LOG(ERROR, TAG, "Invalid JSON : Missing uri object");
- return OC_STACK_INVALID_JSON;
- }
- }
- }
- else
- {
- return OC_STACK_INVALID_JSON;
- OC_LOG(ERROR, TAG, "Invalid JSON : oc object type is not an array");
- }
- return OC_STACK_OK;
-}
-