1 //******************************************************************
3 // Copyright 2015 Samsung Electronics 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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
26 #include "ocpayload.h"
27 #include "provisioninghandler.h"
32 #include "cathreadpool.h"
34 #include "oic_malloc.h"
37 * @var g_provisioningMutex
38 * @brief Mutex to synchronize access to g_caDtlsContext.
40 static ca_mutex g_provisioningMutex = NULL;
41 static ca_cond g_provisioningCond = NULL;
42 bool g_provisioningCondFlag = false;
44 static EnrolleeNWProvInfo_t* netProvInfo;
48 * @brief Callback for providing provisioning status callback to application
50 static OCProvisioningStatusCB cbData = NULL;
51 static ca_thread_pool_t g_threadPoolHandle = NULL;
53 void ErrorCallback(ProvStatus status)
55 ProvisioningInfo *provInfo = GetCallbackObjectOnError(status);
59 OCStackResult InitProvisioningHandler()
61 OCStackResult ret = OC_STACK_ERROR;
62 /* Initialize OCStack*/
63 if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK)
65 OIC_LOG(ERROR, TAG, "OCStack init error");
69 g_provisioningMutex = ca_mutex_new();
71 OIC_LOG(DEBUG, TAG, "ca_thread_pool_init initializing");
73 if (CA_STATUS_OK != ca_thread_pool_init(2, &g_threadPoolHandle))
75 OIC_LOG(DEBUG, TAG, "thread_pool_init failed");
76 return OC_STACK_ERROR;
79 g_provisioningCond = ca_cond_new();
80 if (NULL == g_provisioningCond)
82 OIC_LOG(DEBUG, TAG, "Failed to create condition");
83 ca_mutex_free(g_provisioningMutex);
84 ca_thread_pool_free(g_threadPoolHandle);
85 return OC_STACK_ERROR;
88 char *string = "listeningFunc invoked in a thread";
89 if (CA_STATUS_OK != ca_thread_pool_add_task(g_threadPoolHandle, listeningFunc, (void *) string))
91 OIC_LOG(DEBUG, TAG, "thread_pool_add_task failed");
92 ca_thread_pool_free(g_threadPoolHandle);
93 ca_mutex_unlock(g_provisioningMutex);
94 ca_mutex_free(g_provisioningMutex);
95 ca_cond_free(g_provisioningCond);
96 return OC_STACK_ERROR;
101 OCStackResult TerminateProvisioningHandler()
103 OCStackResult ret = OC_STACK_ERROR;
104 if (OCStop() != OC_STACK_OK)
106 OIC_LOG(ERROR, TAG, "OCStack stop error");
109 ca_mutex_lock(g_provisioningMutex);
110 g_provisioningCondFlag = true;
111 //ca_cond_signal(g_provisioningCond);
112 ca_mutex_unlock(g_provisioningMutex);
114 ca_mutex_free(g_provisioningMutex);
115 g_provisioningMutex = NULL;
117 ca_thread_pool_free(g_threadPoolHandle);
118 g_threadPoolHandle = NULL;
124 void listeningFunc(void *data)
126 while (!g_provisioningCondFlag)
128 OCStackResult result;
130 ca_mutex_lock(g_provisioningMutex);
131 result = OCProcess();
132 ca_mutex_unlock(g_provisioningMutex);
134 if (result != OC_STACK_OK)
136 OIC_LOG(ERROR, TAG, "OCStack stop error");
139 // To minimize CPU utilization we may wish to do this with sleep
144 OCStackApplicationResult ProvisionEnrolleeResponse(void* ctx, OCDoHandle handle,
145 OCClientResponse * clientResponse)
148 ProvisioningInfo *provInfo;
150 if (!ValidateEnrolleResponse(clientResponse))
152 ErrorCallback( DEVICE_NOT_PROVISIONED);
153 return OC_STACK_DELETE_TRANSACTION;
159 OCRepPayload* input = (OCRepPayload*) (clientResponse->payload);
165 if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_PS, &ps))
170 OIC_LOG_V(DEBUG, TAG, "PS is proper");
176 OIC_LOG_V(DEBUG, TAG, "PS is NOT proper");
182 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_TNN, &tnn))
184 if (!strcmp(tnn, netProvInfo->netAddressInfo.WIFI.ssid))
186 OIC_LOG_V(DEBUG, TAG, "SSID is proper");
192 OIC_LOG_V(DEBUG, TAG, "SSID is NOT proper");
197 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CD, &cd))
199 if (!strcmp(cd, netProvInfo->netAddressInfo.WIFI.pwd))
201 OIC_LOG_V(DEBUG, TAG, "Password is proper");
207 OIC_LOG_V(DEBUG, TAG, "Password is NOT proper");
212 LogProvisioningResponse(input->values);
218 SuccessCallback(clientResponse);
220 return OC_STACK_KEEP_TRANSACTION;
225 ErrorCallback( DEVICE_NOT_PROVISIONED);
227 return OC_STACK_DELETE_TRANSACTION;
232 OCStackResult ProvisionEnrollee(OCQualityOfService qos, const char* query, const char* resUri,
233 OCDevAddr *destination)
235 OIC_LOG_V(INFO, TAG, "\n\nExecuting ProvisionEnrollee%s", __func__);
237 OCRepPayload* payload = OCRepPayloadCreate();
239 OCRepPayloadSetUri(payload, resUri);
240 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_TNN, netProvInfo->netAddressInfo.WIFI.ssid);
241 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CD, netProvInfo->netAddressInfo.WIFI.pwd);
243 OIC_LOG_V(DEBUG, TAG, "OCPayload ready for ProvisionEnrollee");
245 OCStackResult ret = InvokeOCDoResource(query, OC_REST_PUT, destination, OC_HIGH_QOS,
246 ProvisionEnrolleeResponse, payload, NULL, 0);
251 OCStackApplicationResult GetProvisioningStatusResponse(void* ctx, OCDoHandle handle,
252 OCClientResponse * clientResponse)
255 ProvisioningInfo *provInfo;
257 if (!ValidateEnrolleResponse(clientResponse))
259 ErrorCallback( DEVICE_NOT_PROVISIONED);
261 return OC_STACK_DELETE_TRANSACTION;
264 OCRepPayload* input = (OCRepPayload*) (clientResponse->payload);
266 char query[OIC_STRING_MAX_VALUE] =
268 char resURI[MAX_URI_LENGTH] =
271 OIC_LOG_V(DEBUG, TAG, "resUri = %s", input->uri);
273 strncpy(resURI, input->uri, sizeof(resURI));
275 snprintf(query, sizeof(query), UNICAST_PROV_STATUS_QUERY, clientResponse->addr->addr, IP_PORT,
278 //OCPayloadLogRep(DEBUG,TAG,input);
280 if (ProvisionEnrollee(OC_HIGH_QOS, query, OC_RSRVD_ES_URI_PROV, clientResponse->addr)
284 "GetProvisioningStatusResponse received NULL clientResponse.Invoking Provisioing Status Callback");
286 ErrorCallback( DEVICE_NOT_PROVISIONED);
288 return OC_STACK_DELETE_TRANSACTION;
291 return OC_STACK_KEEP_TRANSACTION;
295 OCStackResult InvokeOCDoResource(const char* query, OCMethod method, const OCDevAddr *dest,
296 OCQualityOfService qos, OCClientResponseHandler cb, OCRepPayload* payload,
297 OCHeaderOption * options, uint8_t numOptions)
300 OCCallbackData cbData;
303 cbData.context = (void*) DEFAULT_CONTEXT_VALUE;
306 ret = OCDoResource(NULL, method, query, dest, (OCPayload*) payload, OC_CONNTYPE, qos, &cbData,
307 options, numOptions);
309 if (ret != OC_STACK_OK)
311 OIC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, method);
317 OCStackResult GetProvisioningStatus(OCQualityOfService qos, const char* query,
318 const OCDevAddr *destination)
320 OCStackResult ret = OC_STACK_ERROR;
321 OCHeaderOption options[MAX_HEADER_OPTIONS];
323 OIC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
326 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
328 { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
329 memset(options, 0, sizeof(OCHeaderOption) * MAX_HEADER_OPTIONS);
330 options[0].protocolID = OC_COAP_ID;
331 options[0].optionID = 2048;
332 memcpy(options[0].optionData, option0, sizeof(option0));
333 options[0].optionLength = 10;
334 options[1].protocolID = OC_COAP_ID;
335 options[1].optionID = 3000;
336 memcpy(options[1].optionData, option1, sizeof(option1));
337 options[1].optionLength = 10;
339 ret = InvokeOCDoResource(query, OC_REST_GET, destination, OC_HIGH_QOS,
340 GetProvisioningStatusResponse, NULL, options, 2);
344 OCStackResult StartProvisioningProcess(const EnrolleeNWProvInfo_t *netInfo,
345 OCProvisioningStatusCB provisioningStatusCallback)
348 OCStackResult result = OC_STACK_ERROR;
350 if (!ValidateEasySetupParams(netInfo, provisioningStatusCallback))
355 //Only basis test is done for below API
356 if (!SetProgress(provisioningStatusCallback))
358 // Device provisioning session is running already.
359 OIC_LOG(INFO, TAG, PCF("Device provisioning session is running already"));
363 if (!ConfigEnrolleeObject(netInfo))
368 if (CA_STATUS_OK != ca_thread_pool_add_task(g_threadPoolHandle, FindProvisioningResource,
378 ErrorCallback( DEVICE_NOT_PROVISIONED);
380 return OC_STACK_ERROR;
385 void StopProvisioningProcess()
387 //Only basis test is done for below API
391 // This is a function called back when a device is discovered
392 OCStackApplicationResult FindProvisioningResourceResponse(void* ctx, OCDoHandle handle,
393 OCClientResponse * clientResponse)
396 OIC_LOG(INFO, TAG, PCF("Entering FindProvisioningResourceResponse"));
398 if (!ValidateFinddResourceResponse(clientResponse))
400 ErrorCallback( DEVICE_NOT_PROVISIONED);
401 return OC_STACK_DELETE_TRANSACTION;
404 OCStackApplicationResult response = OC_STACK_DELETE_TRANSACTION;
406 ProvisioningInfo *provInfo;
407 char szQueryUri[64] =
410 OCDiscoveryPayload* discoveryPayload = (OCDiscoveryPayload*) (clientResponse->payload);
412 // Need to conform if below check is required or not. As Null check of clientResponse->payload is already performed above
413 if (!discoveryPayload)
415 OIC_LOG_V(DEBUG, TAG, "Failed To parse");
416 ErrorCallback( DEVICE_NOT_PROVISIONED);
417 return OC_STACK_DELETE_TRANSACTION;
420 OIC_LOG_V(DEBUG, TAG, "resUri = %s", discoveryPayload->resources->uri);
422 snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_PROV_STATUS_QUERY,
423 clientResponse->devAddr.addr, IP_PORT, discoveryPayload->resources->uri);
425 OIC_LOG_V(DEBUG, TAG, "query before GetProvisioningStatus call = %s", szQueryUri);
427 if (GetProvisioningStatus(OC_HIGH_QOS, szQueryUri, &clientResponse->devAddr) != OC_STACK_OK)
429 ErrorCallback( DEVICE_NOT_PROVISIONED);
430 return OC_STACK_DELETE_TRANSACTION;
433 return OC_STACK_KEEP_TRANSACTION;
437 void FindProvisioningResource(void *data)
439 OCStackResult ret = OC_STACK_ERROR;
441 /* Start a discovery query*/
442 char szQueryUri[64] =
445 snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_PROVISIONING_QUERY,
446 netProvInfo->netAddressInfo.WIFI.ipAddress, IP_PORT);
448 OIC_LOG_V(DEBUG, TAG, "szQueryUri = %s", szQueryUri);
450 OCCallbackData ocCBData;
452 ocCBData.cb = FindProvisioningResourceResponse;
453 ocCBData.context = (void*) DEFAULT_CONTEXT_VALUE;
456 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, NULL, NULL, OC_CONNTYPE, OC_LOW_QOS,
459 if (ret != OC_STACK_OK)
461 ErrorCallback( DEVICE_NOT_PROVISIONED);
466 OCStackApplicationResult SubscribeProvPresenceCallback(void* ctx, OCDoHandle handle,
467 OCClientResponse* clientResponse)
469 OIC_LOG(INFO, TAG, PCF("Entering SubscribeProvPresenceCallback"));
471 OCStackApplicationResult response = OC_STACK_DELETE_TRANSACTION;
473 if (clientResponse->result != OC_STACK_OK)
475 OIC_LOG(ERROR, TAG, "OCStack stop error");
481 OIC_LOG(INFO, TAG, PCF("Client Response exists"));
483 if (clientResponse->payload && clientResponse->payload->type != PAYLOAD_TYPE_REPRESENTATION)
485 OIC_LOG_V(DEBUG, TAG, "Incoming payload not a representation");
489 OCRepPayload* discoveryPayload = (OCRepPayload*) (clientResponse->payload);
490 if (!discoveryPayload)
492 OIC_LOG_V(DEBUG, TAG, "invalid payload");
496 char sourceIPAddr[OIC_STRING_MAX_VALUE] =
498 snprintf(sourceIPAddr, sizeof(sourceIPAddr), "%s", clientResponse->addr->addr);
500 OIC_LOG_V(DEBUG, TAG, "Discovered %s @ %s", discoveryPayload->uri, sourceIPAddr);
502 /* Start a discovery query*/
503 char szQueryUri[64] =
506 snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_PROVISIONING_QUERY, sourceIPAddr, IP_PORT);
508 /*if (FindProvisioningResource(qos, szQueryUri) != OC_STACK_OK) {
509 OIC_LOG(ERROR, TAG, "FindProvisioningResource failed");
510 return OC_STACK_KEEP_TRANSACTION;
515 // clientResponse is invalid
516 OIC_LOG(ERROR, TAG, PCF("Client Response is NULL!"));
518 return OC_STACK_KEEP_TRANSACTION;
521 OCStackResult SubscribeProvPresence(OCQualityOfService qos, const char* requestURI)
523 OCStackResult ret = OC_STACK_ERROR;
525 OCCallbackData cbData;
527 cbData.cb = &SubscribeProvPresenceCallback;
528 cbData.context = (void*) DEFAULT_CONTEXT_VALUE;
531 ret = OCDoResource(NULL, OC_REST_PRESENCE, requestURI, 0, 0, OC_CONNTYPE, OC_LOW_QOS, &cbData,
534 if (ret != OC_STACK_OK)
536 OIC_LOG(ERROR, TAG, "OCStack resource error");
542 OCStackResult FindNetworkResource()
544 OCStackResult ret = OC_STACK_ERROR;
545 if (OCStop() != OC_STACK_OK)
547 OIC_LOG(ERROR, TAG, "OCStack stop error");
553 ProvisioningInfo* PrepareProvisioingStatusCB(OCClientResponse * clientResponse,
554 ProvStatus provStatus)
557 ProvisioningInfo *provInfo = (ProvisioningInfo *) OICCalloc(1, sizeof(ProvisioningInfo));
559 if (provInfo == NULL)
561 OIC_LOG_V(ERROR, TAG, "Failed to allocate memory");
565 OCDevAddr *devAddr = (OCDevAddr *) OICCalloc(1, sizeof(OCDevAddr));
569 OIC_LOG_V(ERROR, TAG, "Failed to allocate memory");
573 strncpy(devAddr->addr, clientResponse->addr->addr, sizeof(devAddr->addr));
574 devAddr->port = clientResponse->addr->port;
576 provInfo->provDeviceInfo.addr = devAddr;
578 provInfo->provStatus = provStatus;
583 bool ValidateEasySetupParams(const EnrolleeNWProvInfo_t *netInfo,
584 OCProvisioningStatusCB provisioningStatusCallback)
587 if (netInfo == NULL || netInfo->netAddressInfo.WIFI.ipAddress == NULL)
589 OIC_LOG(ERROR, TAG, "Request URI is NULL");
593 if (provisioningStatusCallback == NULL)
595 OIC_LOG(ERROR, TAG, "ProvisioningStatusCallback is NULL");
606 // It means already Easy Setup provisioning session is going on.
609 OIC_LOG(ERROR, TAG, "Easy setup session is already in progress");
616 bool SetProgress(OCProvisioningStatusCB provisioningStatusCallback)
618 ca_mutex_lock(g_provisioningMutex);
623 cbData = provisioningStatusCallback;
625 ca_mutex_unlock(g_provisioningMutex);
632 ca_mutex_lock(g_provisioningMutex);
636 ca_mutex_unlock(g_provisioningMutex);
639 ProvisioningInfo* CreateCallBackObject()
642 ProvisioningInfo *provInfo = (ProvisioningInfo *) OICCalloc(1, sizeof(ProvisioningInfo));
644 if (provInfo == NULL)
646 OIC_LOG_V(ERROR, TAG, "Failed to allocate memory");
650 OCDevAddr *devAddr = (OCDevAddr *) OICCalloc(1, sizeof(OCDevAddr));
654 OIC_LOG_V(ERROR, TAG, "Failed to allocate memory");
658 provInfo->provDeviceInfo.addr = devAddr;
664 ProvisioningInfo* GetCallbackObjectOnError(ProvStatus status)
667 ProvisioningInfo *provInfo = CreateCallBackObject();
668 strncpy(provInfo->provDeviceInfo.addr->addr, netProvInfo->netAddressInfo.WIFI.ipAddress,
669 sizeof(provInfo->provDeviceInfo.addr->addr));
670 provInfo->provDeviceInfo.addr->port = IP_PORT;
671 provInfo->provStatus = status;
675 ProvisioningInfo* GetCallbackObjectOnSuccess(OCClientResponse * clientResponse,
676 ProvStatus provStatus)
678 ProvisioningInfo *provInfo = CreateCallBackObject();
679 strncpy(provInfo->provDeviceInfo.addr->addr, clientResponse->addr->addr,
680 sizeof(provInfo->provDeviceInfo.addr->addr));
681 provInfo->provDeviceInfo.addr->port = clientResponse->addr->port;
682 provInfo->provStatus = provStatus;
686 bool ValidateFinddResourceResponse(OCClientResponse * clientResponse)
689 if (!(clientResponse) || !(clientResponse->payload))
692 OIC_LOG_V(INFO, TAG, "ProvisionEnrolleeResponse received Null clientResponse");
700 bool ValidateEnrolleResponse(OCClientResponse * clientResponse)
703 if (!(clientResponse) || !(clientResponse->payload))
706 OIC_LOG_V(INFO, TAG, "ProvisionEnrolleeResponse received Null clientResponse");
712 if (clientResponse->payload->type != PAYLOAD_TYPE_REPRESENTATION)
715 OIC_LOG_V(DEBUG, TAG, "Incoming payload not a representation");
720 // If flow reachese here means no error condition hit.
726 void SuccessCallback(OCClientResponse * clientResponse)
728 ProvisioningInfo *provInfo = GetCallbackObjectOnSuccess(clientResponse, DEVICE_PROVISIONED);
735 OIC_LOG(DEBUG, TAG, "thread_pool_add_task of FindProvisioningResource failed");
736 ca_thread_pool_free(g_threadPoolHandle);
737 ca_mutex_unlock(g_provisioningMutex);
738 ca_mutex_free(g_provisioningMutex);
739 ca_cond_free(g_provisioningCond);
745 bool ConfigEnrolleeObject(const EnrolleeNWProvInfo_t *netInfo)
748 //Copy Network Provisioning Information
749 netProvInfo = (EnrolleeNWProvInfo_t *) OICCalloc(1, sizeof(EnrolleeNWProvInfo_t));
751 if (netProvInfo == NULL)
753 OIC_LOG(ERROR, TAG, "Invalid input..");
757 memcpy(netProvInfo, netInfo, sizeof(EnrolleeNWProvInfo_t));
759 OIC_LOG_V(DEBUG, TAG, "Network Provisioning Info. SSID = %s",
760 netProvInfo->netAddressInfo.WIFI.ssid);
762 OIC_LOG_V(DEBUG, TAG, "Network Provisioning Info. PWD = %s",
763 netProvInfo->netAddressInfo.WIFI.pwd);
769 void LogProvisioningResponse(OCRepPayloadValue* val)
774 case OCREP_PROP_NULL:
775 OIC_LOG_V(DEBUG, TAG, "\t\t%s: NULL", val->name);
778 OIC_LOG_V(DEBUG, TAG, "\t\t%s(int):%lld", val->name, val->i);
780 case OCREP_PROP_DOUBLE:
781 OIC_LOG_V(DEBUG, TAG, "\t\t%s(double):%f", val->name, val->d);
783 case OCREP_PROP_BOOL:
784 OIC_LOG_V(DEBUG, TAG, "\t\t%s(bool):%s", val->name, val->b ? "true" : "false");
786 case OCREP_PROP_STRING:
787 OIC_LOG_V(DEBUG, TAG, "\t\t%s(string):%s", val->name, val->str);
789 case OCREP_PROP_OBJECT:
790 // Note: Only prints the URI (if available), to print further, you'll
791 // need to dig into the object better!
792 OIC_LOG_V(DEBUG, TAG, "\t\t%s(OCRep):%s", val->name, val->obj->uri);
794 case OCREP_PROP_ARRAY:
795 switch (val->arr.type)
798 OIC_LOG_V(DEBUG, TAG, "\t\t%s(int array):%lld x %lld x %lld", val->name,
799 val->arr.dimensions[0], val->arr.dimensions[1], val->arr.dimensions[2]);
801 case OCREP_PROP_DOUBLE:
802 OIC_LOG_V(DEBUG, TAG, "\t\t%s(double array):%lld x %lld x %lld", val->name,
803 val->arr.dimensions[0], val->arr.dimensions[1], val->arr.dimensions[2]);
805 case OCREP_PROP_BOOL:
806 OIC_LOG_V(DEBUG, TAG, "\t\t%s(bool array):%lld x %lld x %lld", val->name,
807 val->arr.dimensions[0], val->arr.dimensions[1], val->arr.dimensions[2]);
809 case OCREP_PROP_STRING:
810 OIC_LOG_V(DEBUG, TAG, "\t\t%s(string array):%lld x %lld x %lld", val->name,
811 val->arr.dimensions[0], val->arr.dimensions[1], val->arr.dimensions[2]);
813 case OCREP_PROP_OBJECT:
814 OIC_LOG_V(DEBUG, TAG, "\t\t%s(OCRep array):%lld x %lld x %lld", val->name,
815 val->arr.dimensions[0], val->arr.dimensions[1], val->arr.dimensions[2]);
818 //OIC_LOG_V(ERROR, TAG, "\t\t%s <-- Unknown/unsupported array type!",
824 /*OC_LOG_V(ERROR, TAG
825 , "\t\t%s <-- Unknown type!", val->name);*/