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 -c <0|1> -u <0|1> -t <1|2|3>");
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 );
120 OC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData,
121 MAX_HEADER_OPTION_DATA_LENGTH);
125 return OC_STACK_DELETE_TRANSACTION;
128 // This is a function called back when a device is discovered
129 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
130 OCClientResponse * clientResponse)
132 uint8_t remoteIpAddr[4];
133 uint16_t remotePortNu;
135 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
137 OC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully");
142 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
144 OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr,
145 remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3);
146 OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu);
149 "Device =============> Discovered %s @ %d.%d.%d.%d:%d",
150 clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1],
151 remoteIpAddr[2], remoteIpAddr[3], remotePortNu);
153 parseClientResponse(clientResponse);
157 case TEST_NON_CON_OP:
158 InitGetRequest(OC_LOW_QOS);
161 InitGetRequest(OC_HIGH_QOS);
169 return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION ;
173 int InitGetRequest(OCQualityOfService qos)
175 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
176 std::ostringstream query;
177 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
179 return (InvokeOCDoResource(query, OC_REST_GET, (qos == OC_HIGH_QOS)?
180 OC_HIGH_QOS:OC_LOW_QOS, getReqCB, NULL, 0));
186 OCCallbackData cbData;
187 /* Start a discovery query*/
188 char szQueryUri[64] = { 0 };
189 if (UNICAST_DISCOVERY)
191 char ipv4addr[IPV4_ADDR_SIZE];
192 printf("Enter IPv4 address of the Server hosting resource (Ex: 192.168.0.15)\n");
193 if (fgets(ipv4addr, IPV4_ADDR_SIZE, stdin))
195 //Strip newline char from ipv4addr
196 StripNewLineChar(ipv4addr);
197 snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_DISCOVERY_QUERY, ipv4addr);
201 OC_LOG(ERROR, TAG, "!! Bad input for IPV4 address. !!");
202 return OC_STACK_INVALID_PARAM;
207 strcpy(szQueryUri, MULTICAST_RESOURCE_DISCOVERY_QUERY);
209 cbData.cb = discoveryReqCB;
210 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
212 if(UNICAST_DISCOVERY)
214 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, OC_CONNTYPE,
215 OC_LOW_QOS, &cbData, NULL, 0);
219 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, OC_ALL,
220 OC_LOW_QOS, &cbData, NULL, 0);
222 if (ret != OC_STACK_OK)
224 OC_LOG(ERROR, TAG, "OCStack resource error");
229 int main(int argc, char* argv[])
233 while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
238 UNICAST_DISCOVERY = atoi(optarg);
241 TEST_CASE = atoi(optarg);
244 OC_CONNTYPE = OCConnectivityType(atoi(optarg));
252 if ((UNICAST_DISCOVERY != 0 && UNICAST_DISCOVERY != 1) ||
253 (TEST_CASE < TEST_DISCOVER_REQ || TEST_CASE >= MAX_TESTS) )
259 /* Initialize OCStack*/
260 if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK)
262 OC_LOG(ERROR, TAG, "OCStack init error");
268 // Break from loop with Ctrl+C
269 OC_LOG(INFO, TAG, "Entering occlient main loop...");
270 signal(SIGINT, handleSigInt);
273 if (OCProcess() != OC_STACK_OK)
275 OC_LOG(ERROR, TAG, "OCStack process error");
281 OC_LOG(INFO, TAG, "Exiting occlient main loop...");
283 if (OCStop() != OC_STACK_OK)
285 OC_LOG(ERROR, TAG, "OCStack stop error");
291 std::string getIPAddrTBServer(OCClientResponse * clientResponse)
293 if(!clientResponse) return "";
294 if(!clientResponse->addr) return "";
295 uint8_t a, b, c, d = 0;
296 if(0 != OCDevAddrToIPv4Addr(clientResponse->addr, &a, &b, &c, &d) ) return "";
298 char ipaddr[16] = {'\0'};
299 // ostringstream not working correctly here, hence snprintf
300 snprintf(ipaddr, sizeof(ipaddr), "%d.%d.%d.%d", a,b,c,d);
301 return std::string (ipaddr);
304 std::string getPortTBServer(OCClientResponse * clientResponse)
306 if(!clientResponse) return "";
307 if(!clientResponse->addr) return "";
309 if(0 != OCDevAddrToPort(clientResponse->addr, &p) ) return "";
310 std::ostringstream ss;
315 std::string getQueryStrForGetPut(OCClientResponse * clientResponse)
320 void parseClientResponse(OCClientResponse * clientResponse)
322 coapServerIP = getIPAddrTBServer(clientResponse);
323 coapServerPort = getPortTBServer(clientResponse);
324 coapServerResource = getQueryStrForGetPut(clientResponse);