#include <arpa/inet.h>
#endif
-
//-----------------------------------------------------------------------------
// Typedefs
//-----------------------------------------------------------------------------
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
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
}
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..."));
}
}
- 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;
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<numConnTypes; i++)
+ {
+ // Ignore CA_NOT_SUPPORTED error. The CA Layer may have not compiled in the interface.
+ if(caResult == CA_STATUS_OK || caResult == CA_NOT_SUPPORTED)
+ {
+ caResult = CASelectNetwork(connTypes[i]);
+ if(caResult == CA_STATUS_OK)
+ {
+ retResult = CA_STATUS_OK;
+ }
+ }
+ }
+
+ if(retResult != CA_STATUS_OK)
+ {
+ return caResult; // Returns error of appropriate transport that failed fatally.
+ }
+
+ return retResult;
+}
+
/**
* Takes a @ref CAResult_t and converts it to a similar or equivalent @ref OCStackResult value.
*