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/oic/res";
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 protocol (IPv4, IPv6, etc)
41 //to be used for sending unicast messages. Default set to IPv4.
42 static OCConnectivityType OC_CONNTYPE = OC_IPV4;
43 static const char * MULTICAST_RESOURCE_DISCOVERY_QUERY = "/oic/res";
45 static int IPV4_ADDR_SIZE = 16;
46 void StripNewLineChar(char* str);
50 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
51 void handleSigInt(int signum)
59 static void PrintUsage()
61 OC_LOG(INFO, TAG, "Usage : occlient -c <0|1> -u <0|1> -t <1|2|3>");
62 OC_LOG(INFO, TAG, "-c <0|1> : IPv4/IPv6 (IPv6 not currently supported)");
63 OC_LOG(INFO, TAG, "-u <0|1> : Perform multicast/unicast discovery of resources");
64 OC_LOG(INFO, TAG, "-t 1 : Discover Resources");
65 OC_LOG(INFO, TAG, "-t 2 : Discover Resources and Initiate Nonconfirmable Get Request");
66 OC_LOG(INFO, TAG, "-t 3 : Discover Resources and Initiate Confirmable Get Request");
69 OCStackResult InvokeOCDoResource(std::ostringstream &query,
70 OCMethod method, OCQualityOfService qos,
71 OCClientResponseHandler cb, OCHeaderOption * options, uint8_t numOptions)
74 OCCallbackData cbData;
77 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
80 ret = OCDoResource(NULL, method, query.str().c_str(), 0,
81 NULL, OC_CONNTYPE, qos, &cbData, options, numOptions);
83 if (ret != OC_STACK_OK)
85 OC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, method);
91 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
93 if(clientResponse == NULL)
95 OC_LOG(INFO, TAG, "The clientResponse is NULL");
96 return OC_STACK_DELETE_TRANSACTION;
98 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
100 OC_LOG(INFO, TAG, "Callback Context for GET query recvd successfully");
103 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
104 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
105 OC_LOG_V(INFO, TAG, "JSON = %s =============> Get Response",
106 clientResponse->resJSONPayload);
108 if(clientResponse->rcvdVendorSpecificHeaderOptions &&
109 clientResponse->numRcvdVendorSpecificHeaderOptions)
111 OC_LOG (INFO, TAG, "Received vendor specific options");
113 OCHeaderOption * rcvdOptions = clientResponse->rcvdVendorSpecificHeaderOptions;
114 for( i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
116 if(((OCHeaderOption)rcvdOptions[i]).protocolID == OC_COAP_ID)
118 OC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with",
119 ((OCHeaderOption)rcvdOptions[i]).optionID );
121 OC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData,
122 MAX_HEADER_OPTION_DATA_LENGTH);
126 return OC_STACK_DELETE_TRANSACTION;
129 // This is a function called back when a device is discovered
130 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
131 OCClientResponse * clientResponse)
133 uint8_t remoteIpAddr[4];
134 uint16_t remotePortNu;
136 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
138 OC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully");
143 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
145 OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr,
146 remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3);
147 OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu);
150 "Device =============> Discovered %s @ %d.%d.%d.%d:%d",
151 clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1],
152 remoteIpAddr[2], remoteIpAddr[3], remotePortNu);
154 parseClientResponse(clientResponse);
158 case TEST_NON_CON_OP:
159 InitGetRequest(OC_LOW_QOS);
162 InitGetRequest(OC_HIGH_QOS);
170 return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION ;
174 int InitGetRequest(OCQualityOfService qos)
176 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
177 std::ostringstream query;
178 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
180 return (InvokeOCDoResource(query, OC_REST_GET, (qos == OC_HIGH_QOS)?
181 OC_HIGH_QOS:OC_LOW_QOS, getReqCB, NULL, 0));
187 OCCallbackData cbData;
188 /* Start a discovery query*/
189 char szQueryUri[64] = { 0 };
190 if (UNICAST_DISCOVERY)
192 char ipv4addr[IPV4_ADDR_SIZE];
193 printf("Enter IPv4 address of the Server hosting resource (Ex: 192.168.0.15)\n");
194 if (fgets(ipv4addr, IPV4_ADDR_SIZE, stdin))
196 //Strip newline char from ipv4addr
197 StripNewLineChar(ipv4addr);
198 snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_DISCOVERY_QUERY, ipv4addr);
202 OC_LOG(ERROR, TAG, "!! Bad input for IPV4 address. !!");
203 return OC_STACK_INVALID_PARAM;
208 strcpy(szQueryUri, MULTICAST_RESOURCE_DISCOVERY_QUERY);
210 cbData.cb = discoveryReqCB;
211 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
213 if(UNICAST_DISCOVERY)
215 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, OC_CONNTYPE,
216 OC_LOW_QOS, &cbData, NULL, 0);
220 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, OC_ALL,
221 OC_LOW_QOS, &cbData, NULL, 0);
223 if (ret != OC_STACK_OK)
225 OC_LOG(ERROR, TAG, "OCStack resource error");
230 int main(int argc, char* argv[])
234 while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
239 UNICAST_DISCOVERY = atoi(optarg);
242 TEST_CASE = atoi(optarg);
245 // TODO: re-enable IPv4/IPv6 command line selection when IPv6 is supported
246 // OC_CONNTYPE = OCConnectivityType(atoi(optarg));
247 OC_CONNTYPE = OC_IPV4;
255 if ((UNICAST_DISCOVERY != 0 && UNICAST_DISCOVERY != 1) ||
256 (TEST_CASE < TEST_DISCOVER_REQ || TEST_CASE >= MAX_TESTS) )
262 /* Initialize OCStack*/
263 if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK)
265 OC_LOG(ERROR, TAG, "OCStack init error");
271 // Break from loop with Ctrl+C
272 OC_LOG(INFO, TAG, "Entering occlient main loop...");
273 signal(SIGINT, handleSigInt);
276 if (OCProcess() != OC_STACK_OK)
278 OC_LOG(ERROR, TAG, "OCStack process error");
284 OC_LOG(INFO, TAG, "Exiting occlient main loop...");
286 if (OCStop() != OC_STACK_OK)
288 OC_LOG(ERROR, TAG, "OCStack stop error");
294 std::string getIPAddrTBServer(OCClientResponse * clientResponse)
296 if(!clientResponse) return "";
297 if(!clientResponse->addr) return "";
298 uint8_t a, b, c, d = 0;
299 if(0 != OCDevAddrToIPv4Addr(clientResponse->addr, &a, &b, &c, &d) ) return "";
301 char ipaddr[16] = {'\0'};
302 // ostringstream not working correctly here, hence snprintf
303 snprintf(ipaddr, sizeof(ipaddr), "%d.%d.%d.%d", a,b,c,d);
304 return std::string (ipaddr);
307 std::string getPortTBServer(OCClientResponse * clientResponse)
309 if(!clientResponse) return "";
310 if(!clientResponse->addr) return "";
312 if(0 != OCDevAddrToPort(clientResponse->addr, &p) ) return "";
313 std::ostringstream ss;
318 std::string getQueryStrForGetPut(OCClientResponse * clientResponse)
323 void parseClientResponse(OCClientResponse * clientResponse)
325 coapServerIP = getIPAddrTBServer(clientResponse);
326 coapServerPort = getPortTBServer(clientResponse);
327 coapServerResource = getQueryStrForGetPut(clientResponse);