AJSVC_ServiceStatus AJNS_Producer_MessageProcessor(AJ_BusAttachment* busAttachment, AJ_Message* msg, AJ_Status* msgStatus) { AJSVC_ServiceStatus serviceStatus = AJSVC_SERVICE_STATUS_NOT_HANDLED; switch (msg->msgId) { case EMERGENCY_NOTIFICATION_GET_PROPERTY: case WARNING_NOTIFICATION_GET_PROPERTY: case INFO_NOTIFICATION_GET_PROPERTY: case NOTIFICATION_PRODUCER_GET_PROPERTY: *msgStatus = AJ_BusPropGet(msg, AJNS_Producer_PropGetHandler, NULL); serviceStatus = AJSVC_SERVICE_STATUS_HANDLED; break; case EMERGENCY_NOTIFICATION_SET_PROPERTY: case WARNING_NOTIFICATION_SET_PROPERTY: case INFO_NOTIFICATION_SET_PROPERTY: case NOTIFICATION_PRODUCER_SET_PROPERTY: *msgStatus = AJ_BusPropSet(msg, AJNS_Producer_PropSetHandler, NULL); serviceStatus = AJSVC_SERVICE_STATUS_HANDLED; break; case NOTIFICATION_PRODUCER_DISMISS: *msgStatus = AJNS_Producer_DismissRequestHandler(busAttachment, msg); serviceStatus = AJSVC_SERVICE_STATUS_HANDLED; break; default: break; } return serviceStatus; }
AJSVC_ServiceStatus EventsAndActionsMessageProcessor(AJ_BusAttachment* busAttachment, AJ_Message* msg, AJ_Status* msgStatus) { AJ_Message reply; AJSVC_ServiceStatus serviceStatus = AJSVC_SERVICE_STATUS_HANDLED; switch (msg->msgId) { case EVENTSANDACTIONS_GET_PROP: *msgStatus = AJ_BusPropGet(msg, EventsAndActionsPropGetHandler, NULL); break; case EVENTSANDACTIONS_SET_PROP: *msgStatus = AJ_BusPropSet(msg, EventsAndActionsPropSetHandler, NULL); break; case ACTIONS_SETMODETOAUTO: case ACTIONS_SETMODETOCOOL: case ACTIONS_SETMODETOHEAT: case ACTIONS_SETMODETOFAN: case ACTIONS_SETMODETOOFF: AJ_MarshalReplyMsg(msg, &reply); setCurrentMode((msg->msgId & 0xFF) - (ACTIONS_SETMODETOAUTO & 0xFF)); *msgStatus = AJ_DeliverMsg(&reply); break; default: serviceStatus = AJSVC_SERVICE_STATUS_NOT_HANDLED; break; } return serviceStatus; }
AJSVC_ServiceStatus AJCFG_MessageProcessor(AJ_BusAttachment* bus, AJ_Message* msg, AJ_Status* msgStatus) { AJSVC_ServiceStatus serviceStatus = AJSVC_SERVICE_STATUS_HANDLED; switch (msg->msgId) { case CONFIG_GET_PROP: *msgStatus = AJ_BusPropGet(msg, AJCFG_PropGetHandler, NULL); break; case CONFIG_SET_PROP: *msgStatus = AJ_BusPropSet(msg, AJCFG_PropSetHandler, NULL); break; case CONFIG_FACTORY_RESET: *msgStatus = AJCFG_FactoryResetHandler(msg); break; case CONFIG_RESTART: *msgStatus = AJCFG_RestartHandler(msg); break; case CONFIG_GET_CONFIG_CONFIGURATIONS: *msgStatus = AJCFG_GetConfigurationsHandler(msg); break; case CONFIG_RESET_CONFIGURATIONS: *msgStatus = AJCFG_ResetConfigurationsHandler(msg); break; case CONFIG_UPDATE_CONFIGURATIONS: *msgStatus = AJCFG_UpdateConfigurationsHandler(msg); break; case CONFIG_SET_PASSCODE: *msgStatus = AJCFG_SetPasscodeHandler(msg); break; default: serviceStatus = AJSVC_SERVICE_STATUS_NOT_HANDLED; break; } return serviceStatus; }
AJSVC_ServiceStatus AJOBS_MessageProcessor(AJ_BusAttachment* busAttachment, AJ_Message* msg, AJ_Status* msgStatus) { AJSVC_ServiceStatus serviceStatus = AJSVC_SERVICE_STATUS_HANDLED; if (*msgStatus == AJ_OK) { switch (msg->msgId) { case OBS_GET_PROP: *msgStatus = AJ_BusPropGet(msg, AJOBS_PropGetHandler, NULL); break; case OBS_SET_PROP: *msgStatus = AJ_BusPropSet(msg, AJOBS_PropSetHandler, NULL); break; case OBS_CONFIGURE_WIFI: *msgStatus = AJOBS_ConfigureWiFiHandler(msg); break; case OBS_CONNECT: *msgStatus = AJOBS_ConnectWiFiHandler(msg); break; case OBS_OFFBOARD: *msgStatus = AJOBS_OffboardWiFiHandler(msg); break; case OBS_GET_SCAN_INFO: *msgStatus = AJOBS_GetScanInfoHandler(msg); break; default: serviceStatus = AJSVC_SERVICE_STATUS_NOT_HANDLED; break; } } else { serviceStatus = AJSVC_SERVICE_STATUS_NOT_HANDLED; } return serviceStatus; }
AJ_Status AJ_RunAllJoynService(AJ_BusAttachment* bus, AllJoynConfiguration* config) { uint8_t connected = FALSE; AJ_Status status = AJ_OK; while (TRUE) { AJ_Time start = { 0, 0 }; AJ_Message msg; uint32_t now; // get the next timeout //Timer* timer = GetNextTimeout(); uint32_t next; // wait forever uint32_t timeout = (uint32_t) -1; if (!connected) { status = AJ_StartService2( bus, config->daemonName, config->connect_timeout, config->connected, config->session_port, config->service_name, config->flags, config->opts); if (status != AJ_OK) { continue; } AJ_InfoPrintf(("StartService returned AJ_OK\n")); AJ_InfoPrintf(("Connected to Daemon:%s\n", AJ_GetUniqueName(bus))); connected = TRUE; /* Register a callback for providing bus authentication password */ AJ_BusSetPasswordCallback(bus, config->password_callback); /* Configure timeout for the link to the daemon bus */ AJ_SetBusLinkTimeout(bus, config->link_timeout); if (config->connection_handler != NULL) { (config->connection_handler)(connected); } } // absolute time in milliseconds now = AJ_GetElapsedTime(&start, FALSE); next = RunExpiredTimers(now); if (next != (uint32_t) -1) { // if no timers running, wait forever timeout = next; } status = AJ_UnmarshalMsg(bus, &msg, min(500, timeout)); if (AJ_ERR_TIMEOUT == status && AJ_ERR_LINK_TIMEOUT == AJ_BusLinkStateProc(bus)) { status = AJ_ERR_READ; } if (status == AJ_ERR_TIMEOUT) { // go back around and handle the expired timers continue; } if (status == AJ_OK) { uint8_t handled = FALSE; const MessageHandlerEntry* message_entry = config->message_handlers; const PropHandlerEntry* prop_entry = config->prop_handlers; // check the user's handlers first. ANY message that AllJoyn can handle is override-able. while (handled != TRUE && message_entry->msgid != 0) { if (message_entry->msgid == msg.msgId) { if (msg.hdr->msgType == AJ_MSG_METHOD_CALL) { // build a method reply AJ_Message reply; status = AJ_MarshalReplyMsg(&msg, &reply); if (status == AJ_OK) { status = (message_entry->handler)(&msg, &reply); } if (status == AJ_OK) { status = AJ_DeliverMsg(&reply); } } else { // call the handler! status = (message_entry->handler)(&msg, NULL); } handled = TRUE; } ++message_entry; } // we need to check whether this is a property getter or setter. // these are stored in an array because multiple getters and setters can exist if running more than one bus object while (handled != TRUE && prop_entry->msgid != 0) { if (prop_entry->msgid == msg.msgId) { // extract the method from the ID; GetProperty or SetProperty uint32_t method = prop_entry->msgid & 0x000000FF; if (method == AJ_PROP_GET) { status = AJ_BusPropGet(&msg, prop_entry->callback, prop_entry->context); } else if (method == AJ_PROP_SET) { status = AJ_BusPropSet(&msg, prop_entry->callback, prop_entry->context); } else { // this should never happen!!! AJ_ASSERT(!"Invalid property method"); } handled = TRUE; } ++prop_entry; } // handler not found! if (handled == FALSE) { if (msg.msgId == AJ_METHOD_ACCEPT_SESSION) { uint8_t accepted = (config->acceptor)(&msg); status = AJ_BusReplyAcceptSession(&msg, accepted); } else { status = AJ_BusHandleBusMessage(&msg); } } // Any received packets indicates the link is active, so call to reinforce the bus link state AJ_NotifyLinkActive(); } /* * Unarshaled messages must be closed to free resources */ AJ_CloseMsg(&msg); if (status == AJ_ERR_READ) { AJ_InfoPrintf(("AllJoyn disconnect\n")); AJ_InfoPrintf(("Disconnected from Daemon:%s\n", AJ_GetUniqueName(bus))); AJ_Disconnect(bus); connected = FALSE; if (config->connection_handler != NULL) { (config->connection_handler)(connected); } /* * Sleep a little while before trying to reconnect */ AJ_Sleep(10 * 1000); } } // this will never actually return! return AJ_OK; }
int AJ_Main(void) { AJ_Status status = AJ_OK; uint8_t connected = FALSE; /* One time initialization before calling any other AllJoyn APIs. */ AJ_Initialize(); /* This is for debug purposes and is optional. */ AJ_PrintXML(AppObjects); AJ_RegisterObjects(AppObjects, NULL); while (TRUE) { AJ_Message msg; if (!connected) { status = AJ_StartService(&busAttachment, NULL, CONNECT_TIMEOUT, FALSE, ServicePort, ServiceName, AJ_NAME_REQ_DO_NOT_QUEUE, NULL); if (status != AJ_OK) { continue; } AJ_InfoPrintf(("StartService returned %d\n", status)); connected = TRUE; } status = AJ_UnmarshalMsg(&busAttachment, &msg, UNMARSHAL_TIMEOUT); if (AJ_ERR_TIMEOUT == status) { continue; } if (AJ_OK == status) { switch (msg.msgId) { case AJ_METHOD_ACCEPT_SESSION: { uint16_t port; char* joiner; uint32_t sessionId; AJ_UnmarshalArgs(&msg, "qus", &port, &sessionId, &joiner); status = AJ_BusReplyAcceptSession(&msg, TRUE); AJ_InfoPrintf(("Accepted session. Port=%u, session_id=%u joiner='%s'.\n", port, sessionId, joiner)); } break; case BASIC_SIGNAL_SERVICE_GET_NAME: status = AJ_BusPropGet(&msg, GetName, NULL); break; case BASIC_SIGNAL_SERVICE_SET_NAME: status = AJ_BusPropSet(&msg, SetName, NULL); if (AJ_OK == status) { status = SendSignal(); AJ_InfoPrintf(("SendSignal reports status 0x%04x.\n", status)); } break; case AJ_SIGNAL_SESSION_LOST_WITH_REASON: { uint32_t id, reason; AJ_UnmarshalArgs(&msg, "uu", &id, &reason); AJ_AlwaysPrintf(("Session lost. ID = %u, reason = %u", id, reason)); } break; default: /* Pass to the built-in handlers. */ status = AJ_BusHandleBusMessage(&msg); break; } } /* Messages MUST be discarded to free resources. */ AJ_CloseMsg(&msg); if ((status == AJ_ERR_READ) || (status == AJ_ERR_WRITE)) { AJ_AlwaysPrintf(("AllJoyn disconnect.\n")); AJ_Disconnect(&busAttachment); connected = FALSE; /* Sleep a little while before trying to reconnect. */ AJ_Sleep(SLEEP_TIME); } } AJ_AlwaysPrintf(("Basic service exiting with status 0x%04x.\n", status)); return status; }
AJSVC_ServiceStatus GeneratedMessageProcessor(AJ_BusAttachment* bus, AJ_Message* msg, AJ_Status* msgStatus) { AJSVC_ServiceStatus AJSVC_ServiceStatus = AJSVC_SERVICE_STATUS_HANDLED; switch (msg->msgId) { GET_WIDGET_VALUE_CASES *msgStatus = AJ_BusPropGet(msg, AJCPS_GetWidgetProperty, NULL); break; GET_WIDGET_ALL_VALUE_CASES *msgStatus = AJCPS_GetAllWidgetProperties(msg, msg->msgId); break; GET_ROOT_VALUE_CASES *msgStatus = AJ_BusPropGet(msg, AJCPS_GetRootProperty, NULL); break; GET_ROOT_ALL_VALUE_CASES *msgStatus = AJCPS_GetAllRootProperties(msg, msg->msgId); break; SET_VALUE_CASES { SetValueContext context; context.sender = msg->sender; context.numSignals = 0; *msgStatus = AJ_BusPropSet(msg, SetValueProperty, &context); if (*msgStatus == AJ_OK && context.numSignals != 0) { uint16_t indx; for (indx = 0; indx < context.numSignals; indx++) { *msgStatus = AJCPS_SendPropertyChangedSignal(bus, context.signals[indx], AJCPS_GetCurrentSessionId()); } } } break; ACTION_CASES { ExecuteActionContext context; context.numSignals = 0; *msgStatus = ExecuteAction(msg, msg->msgId, &context); if (*msgStatus == AJ_OK && context.numSignals != 0) { uint16_t indx; for (indx = 0; indx < context.numSignals; indx++) { if (context.signals[indx].signalType == SIGNAL_TYPE_DATA_CHANGED) { *msgStatus = AJCPS_SendPropertyChangedSignal(bus, context.signals[indx].signalId, AJCPS_GetCurrentSessionId()); } else if (context.signals[indx].signalType == SIGNAL_TYPE_DISMISS) { *msgStatus = AJCPS_SendDismissSignal(bus, context.signals[indx].signalId, AJCPS_GetCurrentSessionId()); } } } } break; GET_URL_CASES *msgStatus = AJCPS_SendRootUrl(msg, msg->msgId); break; case AJ_SIGNAL_SESSION_LOST: break; default: AJSVC_ServiceStatus = AJSVC_SERVICE_STATUS_NOT_HANDLED; break; } return AJSVC_ServiceStatus; }
int AJ_Main() { AJ_Status status = AJ_OK; AJ_BusAttachment bus; uint8_t connected = FALSE; uint32_t sessionId = 0; /* * One time initialization before calling any other AllJoyn APIs */ AJ_Initialize(); AJ_PrintXML(AppObjects); AJ_RegisterObjects(AppObjects, NULL); SetBusAuthPwdCallback(MyBusAuthPwdCB); while (TRUE) { AJ_Message msg; if (!connected) { status = AJ_StartService(&bus, NULL, CONNECT_TIMEOUT, FALSE, ServicePort, ServiceName, AJ_NAME_REQ_DO_NOT_QUEUE, NULL); if (status != AJ_OK) { continue; } AJ_InfoPrintf(("StartService returned AJ_OK\n")); AJ_InfoPrintf(("Connected to Daemon:%s\n", AJ_GetUniqueName(&bus))); connected = TRUE; #ifdef SECURE_OBJECT status = AJ_SetObjectFlags("/org/alljoyn/alljoyn_test", AJ_OBJ_FLAG_SECURE, 0); if (status != AJ_OK) { AJ_ErrPrintf(("Error calling AJ_SetObjectFlags.. [%s] \n", AJ_StatusText(status))); return -1; } #endif #if defined(SECURE_INTERFACE) || defined(SECURE_OBJECT) /* Register a callback for providing bus authentication password */ AJ_BusSetPasswordCallback(&bus, PasswordCallback); AJ_BusEnableSecurity(&bus, suites, numsuites); AJ_BusSetAuthListenerCallback(&bus, AuthListenerCallback); #endif /* Configure timeout for the link to the daemon bus */ AJ_SetBusLinkTimeout(&bus, 60); // 60 seconds } status = AJ_UnmarshalMsg(&bus, &msg, UNMARSHAL_TIMEOUT); if (AJ_ERR_TIMEOUT == status && AJ_ERR_LINK_TIMEOUT == AJ_BusLinkStateProc(&bus)) { status = AJ_ERR_READ; } if (status != AJ_OK) { if (status == AJ_ERR_TIMEOUT) { AppDoWork(); continue; } } if (status == AJ_OK) { switch (msg.msgId) { case AJ_REPLY_ID(AJ_METHOD_ADD_MATCH): if (msg.hdr->msgType == AJ_MSG_ERROR) { AJ_InfoPrintf(("Failed to add match\n")); status = AJ_ERR_FAILURE; } else { status = AJ_OK; } break; case AJ_METHOD_ACCEPT_SESSION: { uint16_t port; char* joiner; AJ_UnmarshalArgs(&msg, "qus", &port, &sessionId, &joiner); if (port == ServicePort) { status = AJ_BusReplyAcceptSession(&msg, TRUE); AJ_InfoPrintf(("Accepted session session_id=%u joiner=%s\n", sessionId, joiner)); } else { status = AJ_BusReplyAcceptSession(&msg, FALSE); AJ_InfoPrintf(("Accepted rejected session_id=%u joiner=%s\n", sessionId, joiner)); } } break; case APP_MY_PING: status = AppHandlePing(&msg); break; case APP_GET_PROP: status = AJ_BusPropGet(&msg, PropGetHandler, NULL); break; case APP_SET_PROP: status = AJ_BusPropSet(&msg, PropSetHandler, NULL); if (status == AJ_OK) { AJ_InfoPrintf(("Property successfully set to %d.\n", propVal)); } else { AJ_InfoPrintf(("Property set attempt unsuccessful. Status = 0x%04x.\n", status)); } break; case AJ_SIGNAL_SESSION_LOST_WITH_REASON: { uint32_t id, reason; AJ_UnmarshalArgs(&msg, "uu", &id, &reason); AJ_InfoPrintf(("Session lost. ID = %u, reason = %u", id, reason)); if (CancelAdvertiseName) { status = AJ_BusAdvertiseName(&bus, ServiceName, AJ_TRANSPORT_ANY, AJ_BUS_START_ADVERTISING, 0); } status = AJ_ERR_SESSION_LOST; } break; case AJ_SIGNAL_SESSION_JOINED: if (CancelAdvertiseName) { status = AJ_BusAdvertiseName(&bus, ServiceName, AJ_TRANSPORT_ANY, AJ_BUS_STOP_ADVERTISING, 0); } break; case AJ_REPLY_ID(AJ_METHOD_CANCEL_ADVERTISE): case AJ_REPLY_ID(AJ_METHOD_ADVERTISE_NAME): if (msg.hdr->msgType == AJ_MSG_ERROR) { status = AJ_ERR_FAILURE; } break; case APP_MY_SIGNAL: AJ_InfoPrintf(("Received my_signal\n")); status = AJ_OK; if (ReflectSignal) { AJ_Message out; AJ_MarshalSignal(&bus, &out, APP_MY_SIGNAL, msg.destination, msg.sessionId, 0, 0); AJ_MarshalArgs(&out, "a{ys}", 0, NULL); AJ_DeliverMsg(&out); AJ_CloseMsg(&out); } break; default: /* * Pass to the built-in bus message handlers */ status = AJ_BusHandleBusMessage(&msg); break; } // Any received packets indicates the link is active, so call to reinforce the bus link state AJ_NotifyLinkActive(); } /* * Unarshaled messages must be closed to free resources */ AJ_CloseMsg(&msg); if ((status == AJ_ERR_READ) || (status == AJ_ERR_LINK_DEAD)) { AJ_InfoPrintf(("AllJoyn disconnect\n")); AJ_InfoPrintf(("Disconnected from Daemon:%s\n", AJ_GetUniqueName(&bus))); AJ_Disconnect(&bus); connected = FALSE; /* * Sleep a little while before trying to reconnect */ AJ_Sleep(10 * 1000); } } AJ_WarnPrintf(("svclite EXIT %d\n", status)); return status; }