1 //*****************************************************************
3 // Copyright 2015 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 //****************************************************************
21 #include "caraadapter.h"
28 #include "caadapterutils.h"
30 #include "uarraylist.h"
32 #include "oic_malloc.h"
33 #include "oic_string.h"
34 #include "caremotehandler.h"
38 #include "caprotocolmessage.h"
39 #include "xmpp_helper.h"
40 #include "xmpp_utils.h"
42 #include "xmpp_utils.h"
48 #define SET_BUT_NOT_USED(x) (void) x
50 * Logging tag for module name.
52 #define RA_ADAPTER_TAG "RA_ADAP_IBB"
55 * Network Packet Received Callback to CA.
57 static CANetworkPacketReceivedCallback g_networkPacketCallback = NULL;
60 * Network Changed Callback to CA.
62 static CANetworkChangeCallback g_networkChangeCallback = NULL;
65 * Holds XMPP data information.
67 #define RA_MAX_HOSTNAME_LENGTH 256
68 #define RA_MAX_PASSWORD_LENGTH 64
73 char hostName[RA_MAX_HOSTNAME_LENGTH];
74 char password[RA_MAX_PASSWORD_LENGTH];
75 char jid[CA_RAJABBERID_SIZE];
76 CANetworkStatus_t connectionStatus;
77 CAJidBoundCallback jidBoundCallback;
80 static ca_mutex g_raadapterMutex = NULL;
82 static CARAXmppData_t g_xmppData = {.xmpp = NULL, .port = 5222, .hostName = {0},
83 .password = {0}, .jid = {0}, .connectionStatus = CA_INTERFACE_DOWN,
84 .jidBoundCallback = NULL};
86 static void CARANotifyNetworkChange(const char *address, CANetworkStatus_t status);
88 void CARANotifyNetworkChange(const char *address, CANetworkStatus_t status)
90 OIC_LOG(DEBUG, RA_ADAPTER_TAG, "CARANotifyNetworkChange IN");
92 g_xmppData.connectionStatus = status;
94 CAEndpoint_t *localEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
95 CA_ADAPTER_REMOTE_ACCESS,
99 OIC_LOG(ERROR, RA_ADAPTER_TAG, "localEndpoint creation failed!");
102 CANetworkChangeCallback networkChangeCallback = g_networkChangeCallback;
103 if (networkChangeCallback)
105 networkChangeCallback(localEndpoint, status);
109 OIC_LOG(ERROR, RA_ADAPTER_TAG, "g_networkChangeCallback is NULL");
112 CAFreeEndpoint(localEndpoint);
114 OIC_LOG(DEBUG, RA_ADAPTER_TAG, "CARANotifyNetworkChange OUT");
117 #define MAX_IBB_SESSION_ID_LENGTH 32
119 #define OBSERVE_REGISTER 0
120 #define OBSERVE_DEREGISTER 1
123 static ilist_t * g_observerList = NULL;
125 typedef struct _obs_item_t
127 char sessid[MAX_IBB_SESSION_ID_LENGTH + 1];
131 static bool CARAFindSessID(obs_item_t *item, char *key)
133 if (item == NULL || key == NULL)
137 if (strcmp(item->sessid, key) == 0)
147 static bool CARAPDUIsRequest(uint32_t x)
149 return (x == CA_GET || x == CA_POST || x == CA_PUT || x == CA_DELETE);
152 static void CARAUpdateObsList(int option, char *sid)
154 if (option == OBSERVE_REGISTER)
156 obs_item_t *item = (obs_item_t *) OICMalloc(sizeof(*item));
157 OICStrcpy(item->sessid, sizeof(item->sessid), sid);
158 item->option = OBSERVE_REGISTER;
159 ilist_add(g_observerList, item);
161 else if (option == OBSERVE_DEREGISTER)
163 obs_item_t *item = ilist_finditem_func(g_observerList, (find_fp) CARAFindSessID, sid);
166 item->option = OBSERVE_DEREGISTER;
171 static int CARAGetReqObsOption(coap_pdu_t *pdu, const CAEndpoint_t *endPoint)
173 uint32_t obsopt = -1;
175 CARequestInfo_t *reqInfo = (CARequestInfo_t *) OICMalloc(sizeof(*reqInfo));
176 VERIFY_NON_NULL_RET(reqInfo, RA_ADAPTER_TAG, "Memory alloc of CARequestInfo_t failed!", -1);
178 CAResult_t result = CAGetRequestInfoFromPDU(pdu, endPoint, reqInfo);
179 if (CA_STATUS_OK != result)
182 OIC_LOG(ERROR, RA_ADAPTER_TAG, "Get Request Info failed!");
185 if (!CARAPDUIsRequest(reqInfo->method))
188 OIC_LOG(DEBUG, RA_ADAPTER_TAG, "It is not a request data.");
192 uint8_t numOpt = reqInfo->info.numOptions;
193 CAHeaderOption_t *options = reqInfo->info.options;
194 for (uint8_t i = 0; i < numOpt; i++)
196 if(options[i].protocolID == CA_COAP_ID &&
197 options[i].optionID == COAP_OPTION_OBSERVE)
199 obsopt = options[i].optionData[0];
207 static int CARAErrorCB(xmpp_ibb_session_t *sess, xmpperror_t *xerr)
209 OIC_LOG_V(ERROR, RA_ADAPTER_TAG, "%s(): code(%d) tyep'%s' mesg'%s'",
210 __FUNCTION__, xerr->code, xerr->type, xerr->mesg);
211 SET_BUT_NOT_USED(sess);
212 SET_BUT_NOT_USED(xerr);
216 static int CARAOpenCB(xmpp_ibb_session_t *sess, char *type)
218 OIC_LOG_V(DEBUG, RA_ADAPTER_TAG, "%s(): set type '%s'", __FUNCTION__, type);
219 SET_BUT_NOT_USED(sess);
220 SET_BUT_NOT_USED(type);
224 static int CARACloseCB(xmpp_ibb_session_t *sess, char *type)
226 OIC_LOG_V(DEBUG, RA_ADAPTER_TAG, "%s(): set type '%s'", __FUNCTION__, type);
227 char *sid = xmpp_ibb_get_sid(sess);
228 obs_item_t *item = ilist_finditem_func(g_observerList, (find_fp) CARAFindSessID, sid);
231 ilist_remove(g_observerList, item);
234 SET_BUT_NOT_USED(type);
238 static char *CARAGetSIDFromPDU(coap_pdu_t *pdu)
240 static char s_sid[MAX_IBB_SESSION_ID_LENGTH + 1] = {0};
242 VERIFY_NON_NULL_RET(pdu, RA_ADAPTER_TAG, "Invalid parameter!", NULL);
244 if (pdu->hdr->coap_hdr_udp_t.token_length * 2 > MAX_IBB_SESSION_ID_LENGTH)
246 OIC_LOG(ERROR, RA_ADAPTER_TAG, "Token length more than expected!");
251 for (int i = 0; i < pdu->hdr->coap_hdr_udp_t.token_length; i++)
253 snprintf(hex, 3, "%02x", pdu->hdr->coap_hdr_udp_t.token[i]);
254 OICStrcat(s_sid, sizeof(s_sid), hex);
260 static int CARARecvCB(xmpp_ibb_session_t *sess, xmppdata_t *xdata)
264 /* xdata == NULL, send ack result */
268 char *msg = xdata->data;
269 char *from = xmpp_ibb_get_remote_jid(sess);
270 if (g_networkPacketCallback)
272 VERIFY_NON_NULL_RET(from, RA_ADAPTER_TAG, "from sender is NULL", -1);
273 VERIFY_NON_NULL_RET(msg, RA_ADAPTER_TAG, "message is NULL", -1);
275 OIC_LOG_V (DEBUG, RA_ADAPTER_TAG, "Message received from %s", from);
277 CAEndpoint_t *endPoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
278 CA_ADAPTER_REMOTE_ACCESS, from, 0);
281 OIC_LOG(ERROR, RA_ADAPTER_TAG, "EndPoint creation failed!");
284 uint32_t code = CA_NOT_FOUND;
285 coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU(xdata->data, xdata->size, &code,
287 char *sid = CARAGetSIDFromPDU(pdu);
288 int obsopt = CARAGetReqObsOption(pdu, endPoint);
289 coap_delete_pdu(pdu);
291 if (CARAPDUIsRequest(code))
293 OIC_LOG(DEBUG, RA_ADAPTER_TAG, "this is a request data");
294 if (obsopt == OBSERVE_DEREGISTER || obsopt == OBSERVE_REGISTER)
296 CARAUpdateObsList(obsopt, sid);
301 OIC_LOG(DEBUG, RA_ADAPTER_TAG, "this is a response data");
302 obs_item_t *item = ilist_finditem_func(g_observerList, (find_fp) CARAFindSessID, sid);
305 if (item->option == OBSERVE_DEREGISTER)
307 xmpp_ibb_close(sess);
308 ilist_remove(g_observerList, item);
314 xmpp_ibb_close(sess);
319 xmpp_ibb_userdata_alloc(sess, &buf, xdata->size);
322 OIC_LOG(ERROR, RA_ADAPTER_TAG, "Memory alloc of message failed!");
323 CAFreeEndpoint(endPoint);
326 memcpy(buf, xdata->data, xdata->size);
327 CASecureEndpoint_t sep =
328 {.endpoint = {.adapter = CA_ADAPTER_IP, .flags = CA_DEFAULT_FLAGS}};
329 memcpy(&sep.endpoint, endPoint, sizeof(sep.endpoint));
330 g_networkPacketCallback(&sep, buf, xdata->size);
332 CAFreeEndpoint (endPoint);
336 OIC_LOG(ERROR, RA_ADAPTER_TAG, "No callback for RA received message found");
341 static int CARAConnHandler(xmpp_t *xmpp, xmppconn_info_t *conninfo, void *udata)
343 if (conninfo->connevent != 0)
345 OIC_LOG_V(ERROR, RA_ADAPTER_TAG, " status(%d) error(%d) errorType(%d) errorText '%s'\n",
346 conninfo->connevent, conninfo->error, conninfo->errortype,
347 conninfo->errortext);
348 CARANotifyNetworkChange(g_xmppData.jid, CA_INTERFACE_DOWN);
351 OIC_LOG_V(DEBUG, RA_ADAPTER_TAG, "Bound JID: '%s'", xmpphelper_get_bound_jid(xmpp));
352 if (g_xmppData.jidBoundCallback != NULL)
354 g_xmppData.jidBoundCallback((char *) xmpphelper_get_bound_jid(xmpp));
356 CARANotifyNetworkChange(xmpphelper_get_bound_jid(xmpp), CA_INTERFACE_UP);
357 VERIFY_NON_NULL_RET(udata, RA_ADAPTER_TAG, "Invalid parameter!", 0);
361 CAResult_t CAInitializeRA(CARegisterConnectivityCallback registerCallback,
362 CANetworkPacketReceivedCallback networkPacketCallback,
363 CANetworkChangeCallback netCallback, ca_thread_pool_t handle)
365 OIC_LOG(DEBUG, RA_ADAPTER_TAG, "CAInitializeRA IN");
366 if (!registerCallback || !networkPacketCallback || !netCallback || !handle)
368 OIC_LOG(ERROR, RA_ADAPTER_TAG, "Invalid parameter!");
369 return CA_STATUS_INVALID_PARAM;
372 g_networkChangeCallback = netCallback;
373 g_networkPacketCallback = networkPacketCallback;
375 CAConnectivityHandler_t raHandler = {
376 .startAdapter = CAStartRA,
377 .startListenServer = CAStartRAListeningServer,
378 .startDiscoveryServer = CAStartRADiscoveryServer,
379 .sendData = CASendRAUnicastData,
380 .sendDataToAll = CASendRAMulticastData,
381 .GetnetInfo = CAGetRAInterfaceInformation,
382 .readData = CAReadRAData,
383 .stopAdapter = CAStopRA,
384 .terminate = CATerminateRA};
385 registerCallback(raHandler, CA_ADAPTER_REMOTE_ACCESS);
387 xmpp_log_t *log = xmpp_get_default_logger(XMPP_LEVEL_ERROR);
389 xmpp_log_t *log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG);
391 g_xmppData.xmpp = xmpphelper_new(CARAConnHandler, NULL, log, NULL);
392 xmpphelper_force_tls(g_xmppData.xmpp);
393 g_observerList = ilist_new();
398 CAResult_t CASetRAInfo(const CARAInfo_t *caraInfo)
402 OIC_LOG(ERROR, RA_ADAPTER_TAG, "Invalid parameter!");
403 return CA_STATUS_INVALID_PARAM;
405 if (caraInfo->hostName != NULL)
407 OICStrcpy(g_xmppData.hostName, sizeof(g_xmppData.hostName), caraInfo->hostName);
411 OIC_LOG(ERROR, RA_ADAPTER_TAG, "Invalid parameter!");
412 return CA_STATUS_INVALID_PARAM;
414 if (caraInfo->userName != NULL && strlen(caraInfo->userName) != 0)
416 OICStrcpy(g_xmppData.jid, sizeof(g_xmppData.jid), caraInfo->userName);
420 OIC_LOG(ERROR, RA_ADAPTER_TAG, "Invalid parameter!");
421 return CA_STATUS_INVALID_PARAM;
423 if (caraInfo->xmppDomain != NULL && strlen(caraInfo->xmppDomain) != 0)
425 OICStrcat(g_xmppData.jid, sizeof(g_xmppData.jid), "@");
426 OICStrcat(g_xmppData.jid, sizeof(g_xmppData.jid), caraInfo->xmppDomain);
427 if (caraInfo->resource != NULL && strlen(caraInfo->resource) != 0)
429 OICStrcat(g_xmppData.jid, sizeof(g_xmppData.jid), "/");
430 OICStrcat(g_xmppData.jid, sizeof(g_xmppData.jid), caraInfo->resource);
433 if (caraInfo->password != NULL)
435 OICStrcpy(g_xmppData.password, sizeof(g_xmppData.password), caraInfo->password);
437 g_xmppData.port = caraInfo->port;
438 g_xmppData.jidBoundCallback = caraInfo->jidBoundCallback;
446 ilist_destroy(g_observerList);
447 xmpphelper_join(g_xmppData.xmpp);
448 xmpphelper_release(g_xmppData.xmpp);
449 g_xmppData.xmpp = NULL;
452 CAResult_t CAStartRA()
454 OIC_LOG(DEBUG, RA_ADAPTER_TAG, PCF("Starting RA adapter"));
456 if (!g_xmppData.xmpp)
458 OIC_LOG (ERROR, RA_ADAPTER_TAG, "CAStartRA(): g_xmppData.xmpp == NULL");
459 return CA_STATUS_FAILED;
462 g_raadapterMutex = ca_mutex_new ();
463 if (!g_raadapterMutex)
465 OIC_LOG (ERROR, RA_ADAPTER_TAG, PCF("Memory allocation for mutex failed."));
466 return CA_MEMORY_ALLOC_FAILED;
469 ca_mutex_lock (g_raadapterMutex);
471 xmpphelper_connect(g_xmppData.xmpp, g_xmppData.hostName, g_xmppData.port,
472 g_xmppData.jid, g_xmppData.password);
473 xmpp_ibb_reg_funcs_t regfuncs;
474 regfuncs.open_cb = CARAOpenCB;
475 regfuncs.close_cb = CARACloseCB;
476 regfuncs.recv_cb = CARARecvCB;
477 regfuncs.error_cb = CARAErrorCB;
478 xmpp_ibb_register(xmpphelper_get_conn(g_xmppData.xmpp), ®funcs);
480 xmpphelper_run(g_xmppData.xmpp);
482 ca_mutex_unlock (g_raadapterMutex);
484 OIC_LOG(DEBUG, RA_ADAPTER_TAG, "RA adapter started succesfully");
488 CAResult_t CAStopRA()
490 OIC_LOG(DEBUG, RA_ADAPTER_TAG, PCF("Stopping RA adapter"));
492 xmpphelper_stop(g_xmppData.xmpp);
493 xmpp_ibb_unregister(xmpphelper_get_conn(g_xmppData.xmpp));
494 if (!g_raadapterMutex)
496 ca_mutex_free (g_raadapterMutex);
497 g_raadapterMutex = NULL;
499 OIC_LOG(DEBUG, RA_ADAPTER_TAG, PCF("Stopped RA adapter successfully"));
503 int32_t CASendRAUnicastData(const CAEndpoint_t *remoteEndpoint, const void *data,
506 if (!remoteEndpoint || !data)
508 OIC_LOG(ERROR, RA_ADAPTER_TAG, "Invalid parameter!");
514 OIC_LOG(ERROR, RA_ADAPTER_TAG, "Data length is 0!");
517 OIC_LOG_V(DEBUG, RA_ADAPTER_TAG, "Sending unicast data to %s", remoteEndpoint->addr);
519 uint32_t code = CA_NOT_FOUND;
520 coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU(data, dataLength, &code, remoteEndpoint);
521 char *sid = CARAGetSIDFromPDU(pdu);
522 int obsopt = CARAGetReqObsOption(pdu, remoteEndpoint);
523 coap_delete_pdu(pdu);
525 ca_mutex_lock (g_raadapterMutex);
526 if (CA_INTERFACE_UP != g_xmppData.connectionStatus)
528 OIC_LOG(ERROR, RA_ADAPTER_TAG, "Unable to send XMPP message, RA not connected");
529 ca_mutex_unlock (g_raadapterMutex);
533 xmpp_ibb_session_t *sess = xmpp_ibb_get_session_by_sid(sid);
536 sess = xmpp_ibb_open(xmpphelper_get_conn(g_xmppData.xmpp), (char * const) remoteEndpoint->addr, sid);
539 OIC_LOG(ERROR, RA_ADAPTER_TAG, "IBB session establish failed!");
540 ca_mutex_unlock (g_raadapterMutex);
544 if (CARAPDUIsRequest(code))
546 if (obsopt == OBSERVE_REGISTER || obsopt == OBSERVE_DEREGISTER)
548 CARAUpdateObsList(obsopt, sid);
551 xmppdata_t xdata = {.data = (char *) data, .size = dataLength};
552 int rc = xmpp_ibb_send_data(sess, &xdata);
553 ca_mutex_unlock (g_raadapterMutex);
556 OIC_LOG(ERROR, RA_ADAPTER_TAG, "IBB send data failed!");
560 OIC_LOG_V(INFO, RA_ADAPTER_TAG, "Successfully dispatched bytes[%d] to addr[%s]",
561 dataLength, remoteEndpoint->addr);
566 CAResult_t CAGetRAInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
568 VERIFY_NON_NULL(info, RA_ADAPTER_TAG, "info is NULL");
569 VERIFY_NON_NULL(size, RA_ADAPTER_TAG, "size is NULL");
573 int32_t CASendRAMulticastData(const CAEndpoint_t *endpoint,
574 const void *data, uint32_t dataLength)
576 OIC_LOG(INFO, RA_ADAPTER_TAG, "RA adapter does not support sending multicast data");
577 SET_BUT_NOT_USED(endpoint);
578 SET_BUT_NOT_USED(data);
579 SET_BUT_NOT_USED(dataLength);
583 CAResult_t CAStartRAListeningServer()
585 OIC_LOG(INFO, RA_ADAPTER_TAG, "RA adapter does not support listening for multicast data");
586 return CA_NOT_SUPPORTED;
589 CAResult_t CAStartRADiscoveryServer()
591 OIC_LOG(INFO, RA_ADAPTER_TAG, "RA adapter does not support discovery of multicast servers");
592 return CA_NOT_SUPPORTED;
595 CAResult_t CAReadRAData()
597 OIC_LOG(INFO, RA_ADAPTER_TAG, "Read data is not implemented for the RA adapter");
598 return CA_NOT_SUPPORTED;
601 #else /* #ifdef RA_ADAPTER_IBB */
604 * Logging tag for module name.
606 #define RA_ADAPTER_TAG "RA_ADAP"
609 * Network Packet Received Callback to CA.
611 static CANetworkPacketReceivedCallback g_networkPacketCallback = NULL;
614 * Network Changed Callback to CA.
616 static CANetworkChangeCallback g_networkChangeCallback = NULL;
619 * Holds XMPP data information.
623 xmpp_context_t context;
624 xmpp_handle_t handle;
625 xmpp_connection_callback_t connection_callback;
626 xmpp_connection_handle_t connection_handle;
627 xmpp_message_context_t message_context;
628 xmpp_message_callback_t message_callback;
629 CANetworkStatus_t connection_status;
631 xmpp_identity_t g_identity;
632 char jabberID[CA_RAJABBERID_SIZE];
635 static ca_mutex g_raadapterMutex = NULL;
637 static CARAXmppData_t g_xmppData = {};
639 static void CARANotifyNetworkChange(const char *address, CANetworkStatus_t status);
641 static void CARAXmppConnectedCB(void * const param, xmpp_error_code_t result,
642 const char *const bound_jid,
643 xmpp_connection_handle_t connection);
645 static void CARAXmppDisonnectedCB(void * const param, xmpp_error_code_t result,
646 xmpp_connection_handle_t connection);
648 static void CARAXmppMessageSentCB(void * const param, xmpp_error_code_t result,
649 const void *const recipient, const void *const msg, size_t messageOctets);
651 static void CARAXmppMessageReceivedCB(void * const param, xmpp_error_code_t result,
652 const void *const sender, const void *const msg, size_t messageOctets);
654 void CARANotifyNetworkChange(const char *address, CANetworkStatus_t status)
656 OIC_LOG(DEBUG, RA_ADAPTER_TAG, "CARANotifyNetworkChange IN");
658 CAEndpoint_t *localEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
659 CA_ADAPTER_REMOTE_ACCESS,
663 OIC_LOG(ERROR, RA_ADAPTER_TAG, "localEndpoint creation failed!");
666 CANetworkChangeCallback networkChangeCallback = g_networkChangeCallback;
667 if (networkChangeCallback)
669 networkChangeCallback(localEndpoint, status);
673 OIC_LOG(ERROR, RA_ADAPTER_TAG, "g_networkChangeCallback is NULL");
676 CAFreeEndpoint(localEndpoint);
678 OIC_LOG(DEBUG, RA_ADAPTER_TAG, "CARANotifyNetworkChange OUT");
681 void CARAXmppConnectedCB(void * const param, xmpp_error_code_t result,
682 const char *const bound_jid,
683 xmpp_connection_handle_t connection)
685 OIC_LOG(DEBUG, RA_ADAPTER_TAG, "CARAXmppConnectedCB IN");
686 CANetworkStatus_t connection_status;
687 if (XMPP_ERR_OK == result)
689 printf("\n\n\t\t===>your jid: %s\n\n", bound_jid);
691 ca_mutex_lock (g_raadapterMutex);
692 OICStrcpy (g_xmppData.jabberID, CA_RAJABBERID_SIZE, bound_jid);
694 g_xmppData.connection_status = CA_INTERFACE_UP;
695 connection_status = CA_INTERFACE_UP;
696 g_xmppData.connection_handle = connection;
697 g_xmppData.message_callback.on_received = CARAXmppMessageReceivedCB;
698 g_xmppData.message_callback.on_sent = CARAXmppMessageSentCB;
699 g_xmppData.message_context = xmpp_message_context_create(g_xmppData.connection_handle,
700 g_xmppData.message_callback);
704 g_xmppData.connection_status = CA_INTERFACE_DOWN;
705 connection_status = CA_INTERFACE_DOWN;
706 OIC_LOG_V(ERROR, RA_ADAPTER_TAG, "XMPP connected callback status: %d", result);
709 ca_mutex_unlock (g_raadapterMutex);
710 // Notify network change to CA
711 CARANotifyNetworkChange(bound_jid, connection_status);
713 OIC_LOG(DEBUG, RA_ADAPTER_TAG, "CARAXmppConnectedCB OUT");
716 void CARAXmppDisonnectedCB(void * const param, xmpp_error_code_t result,
717 xmpp_connection_handle_t connection)
719 OIC_LOG(DEBUG, RA_ADAPTER_TAG, "CARAXmppDisonnectedCB IN");
720 char jabberID[CA_RAJABBERID_SIZE];
721 ca_mutex_lock (g_raadapterMutex);
723 g_xmppData.connection_status = CA_INTERFACE_DOWN;
724 xmpp_message_context_destroy(g_xmppData.message_context);
725 OICStrcpy (jabberID, CA_RAJABBERID_SIZE, g_xmppData.jabberID);
727 ca_mutex_unlock (g_raadapterMutex);
729 // Notify network change to CA
730 CARANotifyNetworkChange(jabberID, CA_INTERFACE_DOWN);
732 OIC_LOG(DEBUG, RA_ADAPTER_TAG, "CARAXmppDisonnectedCB OUT");
735 void CARAXmppMessageSentCB(void * const param, xmpp_error_code_t result,
736 const void *const recipient, const void *const msg, size_t messageOctets)
738 OIC_LOG_V(DEBUG, RA_ADAPTER_TAG, "Sending message to %s has result %d",
742 void CARAXmppMessageReceivedCB(void * const param, xmpp_error_code_t result,
743 const void *const sender, const void *const msg, size_t messageOctets)
745 if (g_networkPacketCallback)
747 VERIFY_NON_NULL_VOID(sender, RA_ADAPTER_TAG, "sender is NULL");
748 VERIFY_NON_NULL_VOID(msg, RA_ADAPTER_TAG, "message is NULL");
750 OIC_LOG_V (ERROR, RA_ADAPTER_TAG, "Message received from %s", sender);
751 OIC_LOG_V (ERROR, RA_ADAPTER_TAG, "Message reception result %d", result);
753 CAEndpoint_t *endPoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
754 CA_ADAPTER_REMOTE_ACCESS, sender, 0);
757 OIC_LOG(ERROR, RA_ADAPTER_TAG, "EndPoint creation failed!");
761 void *buf = OICMalloc(messageOctets);
764 OIC_LOG(ERROR, RA_ADAPTER_TAG, "Memory alloc of message failed!");
765 CAFreeEndpoint(endPoint);
768 memcpy(buf, msg, messageOctets);
769 CANetworkPacketReceivedCallback networkPacketCallback = g_networkPacketCallback;
770 if (networkPacketCallback)
772 g_networkPacketCallback(endPoint, buf, messageOctets);
775 CAFreeEndpoint (endPoint);
779 OIC_LOG_V (ERROR, RA_ADAPTER_TAG, "No callback for RA received message found");
783 CAResult_t CAInitializeRA(CARegisterConnectivityCallback registerCallback,
784 CANetworkPacketReceivedCallback networkPacketCallback,
785 CANetworkChangeCallback netCallback, ca_thread_pool_t handle)
787 OIC_LOG(DEBUG, RA_ADAPTER_TAG, "CAInitializeRA IN");
788 if (!registerCallback || !networkPacketCallback || !netCallback || !handle)
790 return CA_STATUS_INVALID_PARAM;
793 g_networkChangeCallback = netCallback;
794 g_networkPacketCallback = networkPacketCallback;
796 CAConnectivityHandler_t raHandler = {};
797 raHandler.startAdapter = CAStartRA;
798 raHandler.startListenServer = CAStartRAListeningServer;
799 raHandler.startDiscoveryServer = CAStartRADiscoveryServer;
800 raHandler.sendData = CASendRAUnicastData;
801 raHandler.sendDataToAll = CASendRAMulticastData;
802 raHandler.GetnetInfo = CAGetRAInterfaceInformation;
803 raHandler.readData = CAReadRAData;
804 raHandler.stopAdapter = CAStopRA;
805 raHandler.terminate = CATerminateRA;
806 registerCallback(raHandler, CA_ADAPTER_REMOTE_ACCESS);
811 CAResult_t CASetRAInfo(const CARAInfo_t *caraInfo)
815 return CA_STATUS_INVALID_PARAM;
817 xmpp_identity_init(&g_xmppData.g_identity, caraInfo->username, caraInfo->password,
818 caraInfo->user_jid, XMPP_TRY_IN_BAND_REGISTER);
819 xmpp_host_init(&g_xmppData.g_host, caraInfo->hostname, caraInfo->port,
820 caraInfo->xmpp_domain, XMPP_PROTOCOL_XMPP);
829 CAResult_t CAStartRA()
831 if (g_xmppData.handle.abstract_handle)
833 OIC_LOG(WARNING, RA_ADAPTER_TAG, "RA adapter already started");
837 OIC_LOG(DEBUG, RA_ADAPTER_TAG, PCF("Starting RA adapter"));
839 g_raadapterMutex = ca_mutex_new ();
840 if (!g_raadapterMutex)
842 OIC_LOG (ERROR, RA_ADAPTER_TAG, PCF("Memory allocation for mutex failed."));
843 return CA_MEMORY_ALLOC_FAILED;
846 ca_mutex_lock (g_raadapterMutex);
848 xmpp_context_init(&g_xmppData.context);
849 g_xmppData.handle = xmpp_startup(&g_xmppData.context);
851 // Wire up connection callbacks and call API to connect to XMPP server
852 g_xmppData.connection_callback.on_connected = CARAXmppConnectedCB;
853 g_xmppData.connection_callback.on_disconnected = CARAXmppDisonnectedCB;
855 xmpp_error_code_t ret = xmpp_connect(g_xmppData.handle, &g_xmppData.g_host,
856 &g_xmppData.g_identity, g_xmppData.connection_callback);
858 // Destroy host and identity structures as they are only
859 // required to establish initial connection
860 xmpp_identity_destroy(&g_xmppData.g_identity);
861 xmpp_host_destroy(&g_xmppData.g_host);
863 ca_mutex_unlock (g_raadapterMutex);
865 if (XMPP_ERR_OK != ret)
867 OIC_LOG_V(ERROR, RA_ADAPTER_TAG, "Failed to init XMPP connection status: %d",
869 return CA_STATUS_FAILED;
872 OIC_LOG(DEBUG, RA_ADAPTER_TAG, "RA adapter started succesfully");
876 CAResult_t CAStopRA()
878 OIC_LOG(DEBUG, RA_ADAPTER_TAG, PCF("Stopping RA adapter"));
880 xmpp_error_code_t ret = xmpp_close(g_xmppData.connection_handle);
881 if (XMPP_ERR_OK != ret)
883 OIC_LOG_V(ERROR, RA_ADAPTER_TAG, "Failed to close XMPP connection, status: %d",
885 return CA_STATUS_FAILED;
888 xmpp_shutdown_xmpp(g_xmppData.handle);
889 xmpp_context_destroy(&g_xmppData.context);
890 ca_mutex_free (g_raadapterMutex);
891 g_raadapterMutex = NULL;
893 OIC_LOG(DEBUG, RA_ADAPTER_TAG, PCF("Stopped RA adapter successfully"));
897 int32_t CASendRAUnicastData(const CAEndpoint_t *remoteEndpoint, const void *data,
900 if (!remoteEndpoint || !data)
902 OIC_LOG(ERROR, RA_ADAPTER_TAG, "Invalid parameter!");
908 OIC_LOG(ERROR, RA_ADAPTER_TAG, "Data length is 0!");
912 OIC_LOG_V(ERROR, RA_ADAPTER_TAG, "Sending unicast data to %s", remoteEndpoint->addr);
913 ca_mutex_lock (g_raadapterMutex);
915 if (CA_INTERFACE_UP != g_xmppData.connection_status)
917 OIC_LOG(ERROR, RA_ADAPTER_TAG, "Unable to send XMPP message, RA not connected");
918 ca_mutex_unlock (g_raadapterMutex);
922 xmpp_error_code_t res = xmpp_send_message(g_xmppData.message_context,
923 remoteEndpoint->addr, data, dataLength,
924 XMPP_MESSAGE_TRANSMIT_DEFAULT);
925 if (XMPP_ERR_OK != res)
927 OIC_LOG_V(ERROR, RA_ADAPTER_TAG, "Unable to send XMPP message, status: %d", res);
928 ca_mutex_unlock (g_raadapterMutex);
931 ca_mutex_unlock (g_raadapterMutex);
933 OIC_LOG_V(INFO, RA_ADAPTER_TAG, "Successfully dispatched bytes[%d] to addr[%s]",
934 dataLength, remoteEndpoint->addr);
939 CAResult_t CAGetRAInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
941 VERIFY_NON_NULL(info, RA_ADAPTER_TAG, "info is NULL");
942 VERIFY_NON_NULL(size, RA_ADAPTER_TAG, "size is NULL");
944 ca_mutex_lock (g_raadapterMutex);
946 if (CA_INTERFACE_UP != g_xmppData.connection_status)
948 OIC_LOG(ERROR, RA_ADAPTER_TAG, "Failed to get interface info, RA not Connected");
949 ca_mutex_unlock (g_raadapterMutex);
950 return CA_ADAPTER_NOT_ENABLED;
953 ca_mutex_unlock (g_raadapterMutex);
955 CAEndpoint_t *localEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
956 CA_ADAPTER_REMOTE_ACCESS,
957 g_xmppData.jabberID, 0);
960 *info = localEndpoint;
965 int32_t CASendRAMulticastData(const CAEndpoint_t *endpoint,
966 const void *data, uint32_t dataLength)
968 OIC_LOG(INFO, RA_ADAPTER_TAG, "RA adapter does not support sending multicast data");
972 CAResult_t CAStartRAListeningServer()
974 OIC_LOG(INFO, RA_ADAPTER_TAG, "RA adapter does not support listening for multicast data");
975 return CA_NOT_SUPPORTED;
978 CAResult_t CAStartRADiscoveryServer()
980 OIC_LOG(INFO, RA_ADAPTER_TAG, "RA adapter does not support discovery of multicast servers");
981 return CA_NOT_SUPPORTED;
984 CAResult_t CAReadRAData()
986 OIC_LOG(INFO, RA_ADAPTER_TAG, "Read data is not implemented for the RA adapter");
987 return CA_NOT_SUPPORTED;