From 429151c9bd9f1327a228d00d24bb14023dbe3499 Mon Sep 17 00:00:00 2001 From: Joseph Morrow Date: Mon, 23 Feb 2015 16:07:13 -0500 Subject: [PATCH] Added ability to init with only transports that are compiled-in. Added algorithm to try to initialize all transports. If a transport is not currently supported (ie. not compile-in to CA Library) then we ignore the error. This fixes Jira Ticket IOT-255 -> "CA Call to CASelectNetwork() fails with CA_NOT_SUPPORTED when two network types are specified." Change-Id: I599a4a70fa3f64aa6215997bfbb0e0252437e830 Signed-off-by: Joseph Morrow Reviewed-on: https://gerrit.iotivity.org/gerrit/326 Tested-by: jenkins-iotivity Reviewed-by: Sachin Agrawal Reviewed-by: Sudarshan Prasad --- resource/csdk/stack/src/ocstack.c | 80 ++++++++++++++++++++++++++++++--------- 1 file changed, 63 insertions(+), 17 deletions(-) diff --git a/resource/csdk/stack/src/ocstack.c b/resource/csdk/stack/src/ocstack.c index 7ce2c54..95ff875 100644 --- a/resource/csdk/stack/src/ocstack.c +++ b/resource/csdk/stack/src/ocstack.c @@ -52,7 +52,6 @@ #include #endif - //----------------------------------------------------------------------------- // Typedefs //----------------------------------------------------------------------------- @@ -1299,6 +1298,7 @@ static OCStackResult verifyUriQueryLength(const char * inputUri, uint16_t uriLen); static uint8_t OCIsPacketTransferRequired(const char *request, const char *response, uint16_t size); OCStackResult getResourceType(const char * query, unsigned char** resourceType); +static CAResult_t OCSelectNetwork(); //----------------------------------------------------------------------------- // Public APIs @@ -1321,6 +1321,7 @@ OCStackResult getResourceType(const char * query, unsigned char** resourceType); OCStackResult OCInit(const char *ipAddr, uint16_t port, OCMode mode) { OCStackResult result = OC_STACK_ERROR; + CAResult_t caResult = CA_STATUS_OK; OC_LOG(INFO, TAG, PCF("Entering OCInit")); // Validate mode @@ -1336,16 +1337,19 @@ OCStackResult OCInit(const char *ipAddr, uint16_t port, OCMode mode) } OCSeedRandom(); - CAInitialize(); - //It is ok to select network to CA_WIFI for now -#ifdef WITH_ARDUINO - CAResult_t caResult = CASelectNetwork(CA_ETHERNET); -#else - CAResult_t caResult = CASelectNetwork(CA_WIFI|CA_ETHERNET); -#endif + caResult = CAInitialize(); + if(caResult == CA_STATUS_OK) + { + caResult = OCSelectNetwork(); + } + else + { + OC_LOG(ERROR, TAG, PCF("Failed to initialize the CA Layer.")); + return CAResultToOCResult(caResult); + } + if(caResult == CA_STATUS_OK) { - OC_LOG(INFO, TAG, PCF("CASelectNetwork to WIFI")); CARegisterHandler(HandleCARequests, HandleCAResponses); { OC_LOG(INFO, TAG, PCF("CARegisterHandler...")); @@ -1376,14 +1380,13 @@ OCStackResult OCInit(const char *ipAddr, uint16_t port, OCMode mode) } } - if (caResult == CA_STATUS_OK) - { - result = OC_STACK_OK; - } - else - { - result = OC_STACK_ERROR; - } + + result = CAResultToOCResult(caResult); + } + else + { + OC_LOG(ERROR, TAG, PCF("Failed to initialize any transports.")); + return CAResultToOCResult(caResult); } myStackMode = mode; @@ -3730,6 +3733,49 @@ int32_t OCDevAddrToPort(OCDevAddr *ipAddr, uint16_t *port) return OC_STACK_OK; } +/* + * Attempts to initialize every network interface that the CA Layer might have compiled in. + * + * Note: At least one interface must succeed to initialize. If all calls to @ref CASelectNetwork + * return something other than @ref CA_STATUS_OK, then this function fails. + * + * @return + * CA_STATUS_OK - Success + * CA_NOT_SUPPORTED or CA_STATUS_FAILED - None of the transports successfully initialized. + */ +CAResult_t OCSelectNetwork() +{ + CAResult_t retResult = CA_STATUS_FAILED; + CAResult_t caResult = CA_STATUS_OK; + + CAConnectivityType_t connTypes[] = { + CA_ETHERNET, + CA_WIFI, + CA_EDR, + CA_LE}; + int numConnTypes = sizeof(connTypes)/sizeof(connTypes[0]); + + for(int i = 0; i