📄 ms_upnp_event.c
字号:
/* * * Copyright (c) Sigma Designs, Inc. 2005. All rights reserved. * */#define ALLOW_OS_CODE 1#include "rmdef/rmdef.h"#include "../include/ms_upnp.h"#include <stdio.h>#include "ms_cardea_types.h"#include "ms_udp.h"#include "wmdrmndcoreapi.h"#if 0#define MSEVENTDBG ENABLE#else#define MSEVENTDBG DISABLE#endif/* Maximum number of proximity detection attempt */#define MAX_PROX_ATTEMPT 50/** The callback to the UPnP stack we use to verify we have been validated by a MS server */extern validation_check_op_t ms_validation_check_op;/** * Try to perform proximity detection for the current client, with the given * device as server * @param ms_upnp - device to test proximity with * @return RM_OK on success, RM_ERROR else */static RMstatus test_ms_proximity(ms_upnp_extension *ms_upnp){ RMuint8 *message; RMuint32 message_size; RMuint8 *answer; RMuint32 answer_size; RMstatus status; RMascii *address; ms_udp_socket_t *ms_socket; RMuint32 attempt = 0; if (ms_upnp == NULL){ RMDBGLOG((ENABLE,"Null device in test_ms_proximity\n")); return RM_ERROR; } if ((ms_upnp->flags & MS_ISVALIDATED) == 0){ RMDBGLOG((ENABLE,"Device %s not registered, cannot process proximity detection.\n")); return RM_ERROR; } /* Get proximity address */ status = get_proximity_address(ms_upnp->drm_context, &address); if (RMFAILED(status)){ RMDBGLOG((ENABLE,"Error getting proximity address\n")); return RM_ERROR; } /* Open the socket */ ms_socket = open_ms_udp_socket(address); if (ms_socket == NULL){ RMDBGLOG((ENABLE,"Error opening MS socket\n")); goto error; } else { RMDBGLOG((ENABLE,"Opened MS socket\n")); } /* Update our state with this device */ ms_upnp->flags |= MS_WAITPROXYMITY; while (attempt < MAX_PROX_ATTEMPT) { attempt++; answer = NULL; answer_size = 0; do { status = process_proximity(ms_upnp->drm_context, answer, answer_size, &message, &message_size); if (status != RM_PENDING){ /* Error and we need to restart or success and * we need to stop*/ break; } else { /* Send a new proximity message */ /* TODO this breaks the select loop, add it inside the ILibChain */ /* Try to send and receive, polling for 50 ms before retrying, and during 120 s */ status = ms_udp_send_receive(ms_socket, message, message_size, &answer, &answer_size, 50, 120); if (RMFAILED(status)) break; } } while (RMSUCCEEDED(status)); if (RMSUCCEEDED(status)) /* Success, proximity is over */ break; } if (!(attempt < MAX_PROX_ATTEMPT)){ RMDBGLOG((ENABLE,"Too many attempt, proximity test failed\n")); goto error; } RMDBGLOG((ENABLE,"Proximity detection OK\n")); close_ms_udp_socket(ms_socket); /* We are not waiting anything anymore */ ms_upnp->flags &= ~MS_WAITPROXYMITY; /* Promixity OK */ ms_upnp->flags |= MS_PROXYMITYOK; return RM_OK;error: if (ms_socket) close_ms_udp_socket(ms_socket); /* We are not waiting anything anymore */ ms_upnp->flags &= ~MS_WAITPROXYMITY; return RM_ERROR;} /** * Callback for the UPnP stack when a validation event is received * @param ms_upnp - opaque ms_upnp handle * @param error - 0 on success * @param result - validation result */void cardea_validation_event_handler( void *ms_upnp_handle, int error, int result){ RMuint32 is_validated; ms_upnp_extension *ms_upnp = (ms_upnp_extension *) ms_upnp_handle; if (error != 0){ RMDBGLOG((ENABLE,"Error in is_validated on %s : %d\n", ms_upnp->name, error)); } else { if (ms_upnp){ is_validated = (ms_upnp->flags & MS_ISVALIDATED) ? 1 : 0; if ((unsigned)result != is_validated){ /* Our status changed */ if (result){ ms_upnp->flags = ms_upnp->flags | MS_ISAUTHORIZED; fprintf(stderr,"We have been validated by %s\n",ms_upnp->name); ms_upnp->flags |= MS_ISVALIDATED; ms_upnp->flags &= ~MS_WAITREGISTRATION; } else { ms_upnp->flags = ms_upnp->flags & (~MS_ISAUTHORIZED); fprintf(stderr,"We have been unvalidated by %s\n",ms_upnp->name); ms_upnp->flags &= ~MS_ISVALIDATED; } } if (result == 0 || (ms_upnp->flags & MS_FORCEREGISTER) ){ /* Not registered to this server, check if * registration is already in progress */ if ((ms_upnp->flags & MS_WAITREGISTRATION) == 0){ RMint32 register_status; fprintf(stderr,"Try to register with %s\n", ms_upnp->name); register_status = register_ms_client(ms_upnp); if (register_status < 0) fprintf(stderr,"Error registering at %s : %ld\n", ms_upnp->name, register_status); else fprintf(stderr,"Registration at %s started.\n", ms_upnp->name); } else { RMDBGLOG((ENABLE,"Registration pending with %s\n", ms_upnp->name)); } } } else { fprintf(stderr,"Error, received a registrar callback for an unknown MS device\n"); } }}/** * Callback for the UPnP stack when am authorization event is received * @param ms_upnp - opaque ms_upnp handle * @param error - 0 on success * @param result - authorization result */void cardea_authorization_event_handler( void *ms_upnp_handle, int error, int result){ RMuint32 is_authorized; ms_upnp_extension *ms_upnp = (ms_upnp_extension *)ms_upnp_handle; if (error != 0){ RMDBGLOG((ENABLE,"Error in is_authorized on %s : %d\n", ms_upnp->name, error)); } else { if (ms_upnp){ is_authorized = ms_device_status(ms_upnp) & MS_ISAUTHORIZED; if ((unsigned)result != is_authorized){ /* Our status changed */ if (result){ ms_upnp->flags = ms_upnp->flags | MS_ISAUTHORIZED; fprintf(stderr,"We have been granted authorization by %s\n",ms_upnp->name); /* Check if we need to validate again */ if (ms_validation_check_op) ms_validation_check_op(ms_upnp->upnp_stack_data); else RMDBGLOG((ENABLE,"No valid validation check op\n")); } else { ms_upnp->flags = ms_upnp->flags & (~MS_ISAUTHORIZED); fprintf(stderr,"We have been denied authorization by %s\n",ms_upnp->name); } } } else { fprintf(stderr,"Error, received a registrar callback for an unknown MS device\n"); } }}/** * Callback for the UPnP stack when a registration response is received * @param ms_upnp - opaque ms_upnp handle * @param error - 0 on success * @param message - registration response message * @param message_size */void cardea_registration_event_handler( void *ms_upnp_handle, int error, unsigned char *message, int message_size){ RMstatus status; ms_upnp_extension *ms_upnp = (ms_upnp_extension *)ms_upnp_handle; RMDBGLOG((ENABLE,"Received registration answer\n")); if (error != 0) { fprintf(stderr,"Error in registration : %d on %s\n", error, ms_upnp->name); } else { if (!ms_upnp || !message ){ RMDBGLOG((ENABLE,"Error in registration event handler, invalid parameters\n")); return; } RMDBGLOG((ENABLE,"Registration successful : %d, process the response ...\n", error)); status = process_registration_response(ms_upnp->drm_context, message, message_size); if (RMSUCCEEDED(status)){ RMDBGLOG((ENABLE, "Registration successful on %s, testing proximity ... \n", ms_upnp->name)); ms_upnp->flags |= MS_ISVALIDATED; status = test_ms_proximity(ms_upnp); if (RMFAILED(status)){ fprintf(stderr,"Proximity detection failed on %s\n", ms_upnp->name); } else { fprintf(stderr,"Promixity detection ok on %s\n", ms_upnp->name); } } else { fprintf(stderr, "Registration failed on on %s\n", ms_upnp->name); } } /* Not waiting registration anymore */ ms_upnp->flags &= ~MS_WAITREGISTRATION; /* If regisatretion was forced, don't do it again */ ms_upnp->flags &= ~MS_FORCEREGISTER;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -