1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH 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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
30 #include "occlientslow.h"
32 static int UNICAST_DISCOVERY = 0;
33 static int TEST_CASE = 0;
34 static const char * UNICAST_DISCOVERY_QUERY = "coap://%s:6298/oc/core";
35 static std::string putPayload = "{\"state\":\"off\",\"power\":10}";
36 static std::string coapServerIP = "255.255.255.255";
37 static std::string coapServerPort = "5683";
38 static std::string coapServerResource = "/a/led";
40 //The following variable determines the interface (wifi, ethernet etc.)
41 //to be used for sending unicast messages. Default set to WIFI.
42 static OCConnectivityType OC_CONNTYPE = OC_WIFI;
43 static const char * MULTICAST_RESOURCE_DISCOVERY_QUERY = "/oc/core";
44 static int IPV4_ADDR_SIZE = 16;
45 void StripNewLineChar(char* str);
49 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
50 void handleSigInt(int signum)
58 static void PrintUsage()
60 OC_LOG(INFO, TAG, "Usage : occlient -u <0|1> -t <1|2|3> -c <0|1>");
61 OC_LOG(INFO, TAG, "-c <0|1> : Send unicast messages over Ethernet or WIFI");
62 OC_LOG(INFO, TAG, "-u <0|1> : Perform multicast/unicast discovery of resources");
63 OC_LOG(INFO, TAG, "-t 1 : Discover Resources");
64 OC_LOG(INFO, TAG, "-t 2 : Discover Resources and Initiate Nonconfirmable Get Request");
65 OC_LOG(INFO, TAG, "-t 3 : Discover Resources and Initiate Confirmable Get Request");
68 OCStackResult InvokeOCDoResource(std::ostringstream &query,
69 OCMethod method, OCQualityOfService qos,
70 OCClientResponseHandler cb, OCHeaderOption * options, uint8_t numOptions)
73 OCCallbackData cbData;
76 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
79 ret = OCDoResource(NULL, method, query.str().c_str(), 0,
80 NULL, OC_CONNTYPE, qos, &cbData, options, numOptions);
82 if (ret != OC_STACK_OK)
84 OC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, method);
90 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
92 if(clientResponse == NULL)
94 OC_LOG(INFO, TAG, "The clientResponse is NULL");
95 return OC_STACK_DELETE_TRANSACTION;
97 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
99 OC_LOG(INFO, TAG, "Callback Context for GET query recvd successfully");
102 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
103 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
104 OC_LOG_V(INFO, TAG, "JSON = %s =============> Get Response",
105 clientResponse->resJSONPayload);
107 if(clientResponse->rcvdVendorSpecificHeaderOptions &&
108 clientResponse->numRcvdVendorSpecificHeaderOptions)
110 OC_LOG (INFO, TAG, "Received vendor specific options");
112 OCHeaderOption * rcvdOptions = clientResponse->rcvdVendorSpecificHeaderOptions;
113 for( i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
115 if(((OCHeaderOption)rcvdOptions[i]).protocolID == OC_COAP_ID)
117 OC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with",
118 ((OCHeaderOption)rcvdOptions[i]).optionID );
119 OC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData,
120 ((OCHeaderOption)rcvdOptions[i]).optionLength);
124 return OC_STACK_DELETE_TRANSACTION;
127 // This is a function called back when a device is discovered
128 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
129 OCClientResponse * clientResponse)
131 uint8_t remoteIpAddr[4];
132 uint16_t remotePortNu;
134 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
136 OC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully");
141 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
143 OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr,
144 remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3);
145 OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu);
148 "Device =============> Discovered %s @ %d.%d.%d.%d:%d",
149 clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1],
150 remoteIpAddr[2], remoteIpAddr[3], remotePortNu);
152 parseClientResponse(clientResponse);
156 case TEST_NON_CON_OP:
157 InitGetRequest(OC_LOW_QOS);
160 InitGetRequest(OC_HIGH_QOS);
168 return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION ;
172 int InitGetRequest(OCQualityOfService qos)
174 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
175 std::ostringstream query;
176 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
178 return (InvokeOCDoResource(query, OC_REST_GET, (qos == OC_HIGH_QOS)?
179 OC_HIGH_QOS:OC_LOW_QOS, getReqCB, NULL, 0));
185 OCCallbackData cbData;
186 /* Start a discovery query*/
187 char szQueryUri[64] = { 0 };
188 if (UNICAST_DISCOVERY)
190 char ipv4addr[IPV4_ADDR_SIZE];
191 printf("Enter IPv4 address of the Server hosting resource (Ex: 192.168.0.15)\n");
192 if (fgets(ipv4addr, IPV4_ADDR_SIZE, stdin))
194 //Strip newline char from ipv4addr
195 StripNewLineChar(ipv4addr);
196 snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_DISCOVERY_QUERY, ipv4addr);
200 OC_LOG(ERROR, TAG, "!! Bad input for IPV4 address. !!");
201 return OC_STACK_INVALID_PARAM;
206 strcpy(szQueryUri, MULTICAST_RESOURCE_DISCOVERY_QUERY);
208 cbData.cb = discoveryReqCB;
209 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
211 if(UNICAST_DISCOVERY)
213 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, OC_CONNTYPE,
214 OC_LOW_QOS, &cbData, NULL, 0);
218 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, OC_ALL,
219 OC_LOW_QOS, &cbData, NULL, 0);
221 if (ret != OC_STACK_OK)
223 OC_LOG(ERROR, TAG, "OCStack resource error");
228 int main(int argc, char* argv[])
232 while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
237 UNICAST_DISCOVERY = atoi(optarg);
240 TEST_CASE = atoi(optarg);
243 OC_CONNTYPE = OCConnectivityType(atoi(optarg));
251 if ((UNICAST_DISCOVERY != 0 && UNICAST_DISCOVERY != 1) ||
252 (TEST_CASE < TEST_DISCOVER_REQ || TEST_CASE >= MAX_TESTS) )
258 /* Initialize OCStack*/
259 if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK)
261 OC_LOG(ERROR, TAG, "OCStack init error");
267 // Break from loop with Ctrl+C
268 OC_LOG(INFO, TAG, "Entering occlient main loop...");
269 signal(SIGINT, handleSigInt);
272 if (OCProcess() != OC_STACK_OK)
274 OC_LOG(ERROR, TAG, "OCStack process error");
280 OC_LOG(INFO, TAG, "Exiting occlient main loop...");
282 if (OCStop() != OC_STACK_OK)
284 OC_LOG(ERROR, TAG, "OCStack stop error");
290 std::string getIPAddrTBServer(OCClientResponse * clientResponse)
292 if(!clientResponse) return "";
293 if(!clientResponse->addr) return "";
294 uint8_t a, b, c, d = 0;
295 if(0 != OCDevAddrToIPv4Addr(clientResponse->addr, &a, &b, &c, &d) ) return "";
297 char ipaddr[16] = {'\0'};
298 // ostringstream not working correctly here, hence snprintf
299 snprintf(ipaddr, sizeof(ipaddr), "%d.%d.%d.%d", a,b,c,d);
300 return std::string (ipaddr);
303 std::string getPortTBServer(OCClientResponse * clientResponse)
305 if(!clientResponse) return "";
306 if(!clientResponse->addr) return "";
308 if(0 != OCDevAddrToPort(clientResponse->addr, &p) ) return "";
309 std::ostringstream ss;
314 std::string getQueryStrForGetPut(OCClientResponse * clientResponse)
319 void parseClientResponse(OCClientResponse * clientResponse)
321 coapServerIP = getIPAddrTBServer(clientResponse);
322 coapServerPort = getPortTBServer(clientResponse);
323 coapServerResource = getQueryStrForGetPut(clientResponse);